Eneboo - Documentación para desarrolladores
|
00001 // -*- C++ -*- 00002 00009 #ifndef _MANAGEONESUPERBLOCK_H_ 00010 #define _MANAGEONESUPERBLOCK_H_ 00011 00018 namespace Hoard { 00019 00020 template <class SuperHeap> 00021 class ManageOneSuperblock : public SuperHeap { 00022 public: 00023 00024 typedef typename SuperHeap::SuperblockType SuperblockType; 00025 00027 inline void * malloc (size_t sz) { 00028 if (_current) { 00029 void * ptr = _current->malloc (sz); 00030 if (ptr) { 00031 assert (_current->getSize(ptr) >= sz); 00032 return ptr; 00033 } 00034 } 00035 return slowMallocPath (sz); 00036 } 00037 00039 inline void free (void * ptr) { 00040 SuperblockType * s = SuperHeap::getSuperblock (ptr); 00041 if (s == _current) { 00042 _current->free (ptr); 00043 } else { 00044 SuperHeap::free (ptr); 00045 } 00046 } 00047 00049 SuperblockType * get (void) { 00050 if (_current) { 00051 SuperblockType * s = _current; 00052 _current = NULL; 00053 return s; 00054 } else { 00055 // There's none cached, so just get one from the superheap. 00056 return SuperHeap::get(); 00057 } 00058 } 00059 00061 inline void put (SuperblockType * s) { 00062 if (!s || (s == _current) || (!s->isValidSuperblock())) { 00063 // Ignore if we already are holding this superblock, of if we 00064 // got a NULL pointer, or if it's invalid. 00065 return; 00066 } 00067 if (_current) { 00068 // We have one already -- push it out. 00069 SuperHeap::put (_current); 00070 } 00071 _current = s; 00072 } 00073 00074 private: 00075 00077 void * slowMallocPath (size_t sz) { 00078 void * ptr = NULL; 00079 while (!ptr) { 00080 if (_current) { 00081 ptr = _current->malloc (sz); 00082 if (ptr) { 00083 return ptr; 00084 } else { 00085 SuperHeap::put (_current); 00086 } 00087 } 00088 _current = SuperHeap::get(); 00089 if (!_current) { 00090 return NULL; 00091 } 00092 ptr = _current->malloc (sz); 00093 } 00094 return ptr; 00095 } 00096 00098 SuperblockType * _current; 00099 00100 }; 00101 00102 } 00103 00104 #endif