Eneboo - Documentación para desarrolladores
|
00001 /* -*- C++ -*- */ 00002 00003 #ifndef _BOUNDEDFREELISTHEAP_H_ 00004 #define _BOUNDEDFREELISTHEAP_H_ 00005 00006 // Beware -- this is for one "size class" only!! 00007 00008 template <int numObjects, class Super> 00009 class BoundedFreeListHeap : public Super { 00010 public: 00011 00012 BoundedFreeListHeap (void) 00013 : nObjects (0), 00014 myFreeList (NULL) 00015 {} 00016 00017 ~BoundedFreeListHeap (void) 00018 { 00019 clear(); 00020 } 00021 00022 inline void * malloc (size_t sz) { 00023 // Check the free list first. 00024 void * ptr = myFreeList; 00025 if (ptr == NULL) { 00026 ptr = Super::malloc (sz); 00027 } else { 00028 myFreeList = myFreeList->next; 00029 } 00030 return ptr; 00031 } 00032 00033 inline void free (void * ptr) { 00034 if (nObjects < numObjects) { 00035 // Add this object to the free list. 00036 ((freeObject *) ptr)->next = myFreeList; 00037 myFreeList = (freeObject *) ptr; 00038 nObjects++; 00039 } else { 00040 clear(); 00041 // Super::free (ptr); 00042 } 00043 } 00044 00045 inline void clear (void) { 00046 // Delete everything on the free list. 00047 void * ptr = myFreeList; 00048 while (ptr != NULL) { 00049 void * oldptr = ptr; 00050 ptr = (void *) ((freeObject *) ptr)->next; 00051 Super::free (oldptr); 00052 } 00053 myFreeList = NULL; 00054 nObjects = 0; 00055 } 00056 00057 private: 00058 00059 class freeObject { 00060 public: 00061 freeObject * next; 00062 }; 00063 00064 int nObjects; 00065 freeObject * myFreeList; 00066 }; 00067 00068 #endif