Eneboo - Documentación para desarrolladores
src/hoard/src/heaplayers/threadspecificheap.h
Ir a la documentación de este archivo.
00001 /* -*- C++ -*- */
00002 
00003 #ifndef _THREADHEAP_H_
00004 #define _THREADHEAP_H_
00005 
00006 #include <assert.h>
00007 #include <new.h>
00008 
00009 /*
00010 
00011   A ThreadHeap comprises NumHeaps "per-thread" heaps.
00012 
00013   To pick a per-thread heap, the current thread id is hashed (mod NumHeaps).
00014 
00015   malloc gets memory from its hashed per-thread heap.
00016   free returns memory to its hashed per-thread heap.
00017 
00018   (This allows the per-thread heap to determine the return
00019   policy -- 'pure private heaps', 'private heaps with ownership',
00020   etc.)
00021 
00022   NB: We assume that the thread heaps are 'locked' as needed.  */
00023 
00024 
00025 template <int NumHeaps, class PerThreadHeap>
00026 class ThreadHeap : public PerThreadHeap {
00027 public:
00028 
00029   ThreadHeap (void)
00030   {
00031   }
00032 
00033   inline void * malloc (size_t sz) {
00034     int tid = getThreadId() % NumHeaps;
00035     return getHeap(tid)->malloc (sz);
00036   }
00037 
00038   inline void free (void * ptr) {
00039     int tid = getThreadId() % NumHeaps;
00040     getHeap(tid)->free (ptr);
00041   }
00042 
00043 
00044 private:
00045 
00046   // Access the given heap within the buffer.
00047   PerThreadHeap * getHeap (int index) {
00048     assert (index >= 0);
00049     assert (index < NumHeaps);
00050         return &ptHeaps[index];
00051   }
00052 
00053   // Get a suitable thread id number.
00054   inline static volatile int getThreadId (void);
00055 
00056   PerThreadHeap ptHeaps[NumHeaps];
00057 
00058 };
00059 
00060 
00061 // A platform-dependent way to get a thread id.
00062 
00063 // Include the necessary platform-dependent crud.
00064 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32)
00065 #ifndef WIN32
00066 #define WIN32 1
00067 #endif
00068 #include <windows.h>
00069 #include <process.h>
00070 #endif
00071 
00072 template <int NumHeaps, class PerThreadHeap>
00073 inline volatile int
00074 ThreadHeap<NumHeaps, PerThreadHeap>::getThreadId (void) {
00075 #if WIN32
00076   return GetCurrentThreadId();
00077 #endif
00078 #if defined(__BEOS__)
00079   return find_thread(0);
00080 #endif
00081 #if defined(__linux)
00082   // Consecutive thread id's in Linux are 1024 apart;
00083   // dividing off the 1024 gives us an appropriate thread id.
00084   return (int) pthread_self() >> 10; // (>> 10 = / 1024)
00085 #endif
00086 #if defined(__SVR4)
00087   return (int) lwp_self();
00088 #endif
00089 #if defined(POSIX)
00090   return (int) pthread_self();
00091 #endif
00092 #if USE_SPROC
00093   // This hairiness has the same effect as calling getpid(),
00094   // but it's MUCH faster since it avoids making a system call
00095   // and just accesses the sproc-local data directly.
00096   int pid = (int) PRDA->sys_prda.prda_sys.t_pid;
00097   return pid;
00098 #endif
00099 }
00100   
00101 
00102 #endif
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'