Eneboo - Documentación para desarrolladores
|
00001 #ifndef _MULTIHEAP_H_ 00002 #define _MULTIHEAP_H_ 00003 00004 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) 00005 #ifndef WIN32 00006 #define WIN32 1 00007 #endif 00008 #include <windows.h> 00009 #include <process.h> 00010 #endif 00011 00012 template <int NumHeaps, class Super> 00013 class MultiHeap : public Super { 00014 public: 00015 00016 inline void * malloc (size_t sz) { 00017 // Hash the thread id. 00018 // We assume that it's impossible for two threads to collide. 00019 int tid = getThreadId() % NumHeaps; 00020 void * ptr = mySuper[tid].malloc (sz + sizeof(double)); 00021 *((int *) ptr) = tid; 00022 void * newPtr = (void *) ((double *) ptr + 1); 00023 return newPtr; 00024 } 00025 00026 inline void free (void * ptr) { 00027 // Return the object to its own heap. 00028 void * originalPtr = (void *) ((double *) ptr - 1); 00029 int heapIndex = *((int *) originalPtr); 00030 // FIX ME! A 'cache' would be nice here... 00031 // FIX ME! We need a lock here. 00032 mySuper[heapIndex].free (originalPtr); 00033 } 00034 00035 private: 00036 00037 Super mySuper[NumHeaps]; 00038 00039 int getThreadId (void) { 00040 #if WIN32 00041 return GetCurrentThreadId(); 00042 #endif 00043 } 00044 00045 }; 00046 00047 #endif