Eneboo - Documentación para desarrolladores
|
00001 // -*- C++ -*- 00002 00003 #ifndef _ADDHEADERHEAP_H_ 00004 #define _ADDHEADERHEAP_H_ 00005 00006 #include "sassert.h" 00007 #include "hldefines.h" 00008 00009 namespace Hoard { 00010 00015 template <class SuperblockType, 00016 size_t SuperblockSize, 00017 class SuperHeap> 00018 class AddHeaderHeap { 00019 private: 00020 00021 HL::sassert<(((int) SuperHeap::Alignment) % SuperblockSize == 0)> verifySize1; 00022 HL::sassert<(((int) SuperHeap::Alignment) >= SuperblockSize)> verifySize2; 00023 00024 SuperHeap theHeap; 00025 00026 public: 00027 00028 enum { Alignment = 0 }; 00029 00030 MALLOC_FUNCTION INLINE void * malloc (size_t sz) { 00031 // Allocate extra space for the header, 00032 // put it at the front of the object, 00033 // and return a pointer to just past it. 00034 const size_t headerSize = sizeof(typename SuperblockType::Header); 00035 void * ptr = theHeap.malloc (sz + headerSize); 00036 if (ptr == NULL) { 00037 return NULL; 00038 } 00039 typename SuperblockType::Header * p 00040 = new (ptr) typename SuperblockType::Header (sz, sz); 00041 assert ((size_t) (p + 1) == (size_t) ptr + headerSize); 00042 return reinterpret_cast<void *>(p + 1); 00043 } 00044 00045 INLINE static size_t getSize (void * ptr) { 00046 // Find the header (just before the pointer) and return the size 00047 // value stored there. 00048 typename SuperblockType::Header * p; 00049 p = reinterpret_cast<typename SuperblockType::Header *>(ptr); 00050 return (p - 1)->getSize (ptr); 00051 } 00052 00053 INLINE void free (void * ptr) { 00054 // Find the header (just before the pointer) and free the whole object. 00055 typename SuperblockType::Header * p; 00056 p = reinterpret_cast<typename SuperblockType::Header *>(ptr); 00057 theHeap.free (reinterpret_cast<void *>(p - 1)); 00058 } 00059 }; 00060 00061 } 00062 00063 #endif