00001 
00002 
00003 #ifndef _THREADHEAP_H_
00004 #define _THREADHEAP_H_
00005 
00006 #include <assert.h>
00007 #include <new>
00008 
00009 #if !defined(_WIN32)
00010 #include <pthread.h>
00011 #endif
00012 
00013 #if defined(__SVR4) // Solaris
00014 extern "C" unsigned int lwp_self(void);
00015 #endif
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 namespace HL {
00033 
00034 template <int NumHeaps, class PerThreadHeap>
00035 class ThreadHeap : public PerThreadHeap {
00036 public:
00037 
00038   inline void * malloc (size_t sz) {
00039     int tid = getThreadId() % NumHeaps;
00040     assert (tid >= 0);
00041     assert (tid < NumHeaps);
00042     return getHeap(tid)->malloc (sz);
00043   }
00044 
00045   inline void free (void * ptr) {
00046     int tid = getThreadId() % NumHeaps;
00047     assert (tid >= 0);
00048     assert (tid < NumHeaps);
00049     getHeap(tid)->free (ptr);
00050   }
00051 
00052   inline size_t getSize (void * ptr) {
00053     int tid = getThreadId() % NumHeaps;
00054     assert (tid >= 0);
00055     assert (tid < NumHeaps);
00056     return getHeap(tid)->getSize (ptr);
00057   }
00058 
00059     
00060 private:
00061 
00062   
00063   inline PerThreadHeap * getHeap (int index) {
00064     assert (index >= 0);
00065     assert (index < NumHeaps);
00066     return &ptHeaps[index];
00067   }
00068 
00069   
00070   inline static int getThreadId (void);
00071 
00072   PerThreadHeap ptHeaps[NumHeaps];
00073 
00074 };
00075 
00076 }
00077 
00078 
00079 
00080 
00081 
00082 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32)
00083 #ifndef WIN32
00084 #define WIN32 1
00085 #endif
00086 #include <windows.h>
00087 #include <process.h>
00088 #endif
00089 
00090 template <int NumHeaps, class PerThreadHeap>
00091 inline int
00092 HL::ThreadHeap<NumHeaps, PerThreadHeap>::getThreadId (void) {
00093 #if defined(WIN32)
00094   
00095   int tid = GetCurrentThreadId() >> 2;
00096   
00097 
00098   return tid;
00099 #endif
00100 #if defined(__BEOS__)
00101   return find_thread(0);
00102 #endif
00103 #if defined(__linux)
00104   
00105   
00106   return (int) pthread_self() >> 10; 
00107 #endif
00108 #if defined(__SVR4)
00109   
00110   return (int) lwp_self();
00111 #endif
00112 #if defined(__APPLE__)
00113   return (int) pthread_self();
00114 #endif
00115 #if defined(POSIX)
00116   return (int) pthread_self();
00117 #endif
00118 #if USE_SPROC
00119   
00120   
00121   
00122   int pid = (int) PRDA->sys_prda.prda_sys.t_pid;
00123   return pid;
00124 #endif
00125 }
00126   
00127 
00128 #endif