Eneboo - Documentación para desarrolladores
|
00001 #ifndef _THEAP_H_ 00002 #define _THEAP_H_ 00003 00004 // A general threshold-based heap. 00005 00006 // Threshold layer. 00007 // Once we have more than Threshold bytes on our freelist, 00008 // return memory to the superheap. 00009 00010 // The class argument FH should be some freelist heap that subclasses NullHeap. 00011 00012 template <class SuperHeap, class FH, int Threshold> 00013 class THeap : public SuperHeap { 00014 public: 00015 THeap (void) 00016 : total (0) 00017 {} 00018 00019 ~THeap (void) 00020 {} 00021 00022 inline void * malloc (size_t sz) { 00023 void * ptr; 00024 ptr = fHeap.malloc (sz); 00025 if (ptr == NULL) { 00026 // We have no memory on our freelist. 00027 // Get it from the superheap. 00028 ptr = SuperHeap::malloc (sz); 00029 } else { 00030 total -= size(ptr); 00031 // printf ("total = %d\n", total); 00032 } 00033 assert (size(ptr) >= sz); 00034 return ptr; 00035 } 00036 00037 inline void free (void * ptr) { 00038 if (total < Threshold) { 00039 // printf ("FREE TO FREELIST.\n"); 00040 total += size(ptr); 00041 fHeap.free (ptr); 00042 //printf ("total = %d\n", total); 00043 } else { 00044 // Dump the freelist heap. 00045 void * p = fHeap.malloc (1); 00046 while (p != NULL) { 00047 SuperHeap::free (p); 00048 p = fHeap.malloc (1); 00049 } 00050 SuperHeap::free (ptr); 00051 total = 0; 00052 } 00053 } 00054 00055 private: 00056 // Provide a free list that will return NULL if it is out of memory. 00057 FH fHeap; 00058 int total; 00059 }; 00060 00061 00062 #endif