Eneboo - Documentación para desarrolladores
|
00001 /* -*- C++ -*- */ 00002 00003 #ifndef _SIZETHREADHEAP_H_ 00004 #define _SIZETHREADHEAP_H_ 00005 00006 #include <assert.h> 00007 00008 template <class Super> 00009 class SizeThreadHeap : public Super { 00010 public: 00011 00012 inline void * malloc (size_t sz) { 00013 // Add room for a size field & a thread field. 00014 // Both of these must fit in a double. 00015 assert (sizeof(st) <= sizeof(double)); 00016 st * ptr = (st *) Super::malloc (sz + sizeof(double)); 00017 // Store the requested size. 00018 ptr->size = sz; 00019 assert (getOrigPtr(ptr + 1) == ptr); 00020 return (void *) (ptr + 1); 00021 } 00022 00023 inline void free (void * ptr) { 00024 void * origPtr = (void *) getOrigPtr(ptr); 00025 Super::free (origPtr); 00026 } 00027 00028 static inline volatile int getThreadId (void); 00029 00030 static inline size_t& size (void * ptr) { 00031 return getOrigPtr(ptr)->size; 00032 } 00033 00034 static inline int& thread (void * ptr) { 00035 return getOrigPtr(ptr)->tid; 00036 } 00037 00038 private: 00039 00040 typedef struct _st { 00041 size_t size; 00042 int tid; 00043 } st; 00044 00045 static inline st * getOrigPtr (void * ptr) { 00046 return (st *) ((double *) ptr - 1); 00047 } 00048 00049 }; 00050 00051 00052 00053 // A platform-dependent way to get a thread id. 00054 00055 // Include the necessary platform-dependent crud. 00056 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) 00057 #ifndef WIN32 00058 #define WIN32 1 00059 #endif 00060 #include <windows.h> 00061 #include <process.h> 00062 #endif 00063 00064 template <class SuperHeap> 00065 inline volatile int 00066 SizeThreadHeap<SuperHeap>::getThreadId (void) { 00067 #if defined(WIN32) 00068 return GetCurrentThreadId(); 00069 #endif 00070 #if defined(__BEOS__) 00071 return find_thread(0); 00072 #endif 00073 #if defined(__linux) 00074 // Consecutive thread id's in Linux are 1024 apart; 00075 // dividing off the 1024 gives us an appropriate thread id. 00076 return (int) pthread_self() >> 10; // (>> 10 = / 1024) 00077 #endif 00078 #if defined(__SVR4) 00079 return (int) lwp_self(); 00080 #endif 00081 #if defined(POSIX) // FIX ME?? 00082 return (int) pthread_self(); 00083 #endif 00084 #if USE_SPROC // FIX ME 00085 // This hairiness has the same effect as calling getpid(), 00086 // but it's MUCH faster since it avoids making a system call 00087 // and just accesses the sproc-local data directly. 00088 int pid = (int) PRDA->sys_prda.prda_sys.t_pid; 00089 return pid; 00090 #endif 00091 } 00092 00093 00094 00095 #endif