Eneboo - Documentación para desarrolladores
|
00001 /* -*- C++ -*- */ 00002 00003 #ifndef _FIFOFREELIST_H_ 00004 #define _FIFOFREELIST_H_ 00005 00006 #include <assert.h> 00007 00008 #ifdef NDEBUG 00009 #define IFDEBUG(x) 00010 #else 00011 #define IFDEBUG(x) x 00012 #endif 00013 00014 00015 template <class SuperHeap> 00016 class FIFOFreelistHeap : public SuperHeap { 00017 public: 00018 00019 FIFOFreelistHeap (void) 00020 IFDEBUG(: nObjects (0)) 00021 { 00022 head.next = &tail; 00023 tail.next = &tail; 00024 assert (isEmpty()); 00025 } 00026 00027 ~FIFOFreelistHeap (void) 00028 { 00029 // printf ("Adios free list.\n"); 00030 // Delete everything on the free list. 00031 freeObject * ptr = head.next; 00032 while (ptr != &tail) { 00033 void * oldptr = ptr; 00034 ptr = ptr->next; 00035 SuperHeap::free (oldptr); 00036 } 00037 } 00038 00039 inline void * malloc (size_t sz) { 00040 assert (isValid()); 00041 // Check the free list first. 00042 freeObject * ptr = head.next; 00043 if (ptr == &tail) { 00044 assert (nObjects == 0); 00045 assert (isEmpty()); 00046 ptr = (freeObject *) SuperHeap::malloc (sz); 00047 } else { 00048 assert (nObjects > 0); 00049 head.next = ptr->next; 00050 if (head.next == &tail) { 00051 tail.next = &tail; 00052 } 00053 IFDEBUG (nObjects--); 00054 } 00055 assert (isValid()); 00056 return (void *) ptr; 00057 } 00058 00059 inline void free (void * ptr) { 00060 assert (isValid()); 00061 // Add this object to the free list. 00062 assert (ptr != NULL); 00063 IFDEBUG (nObjects++); 00064 freeObject * p = (freeObject *) ptr; 00065 p->next = &tail; 00066 tail.next->next = p; 00067 tail.next = p; 00068 if (head.next == &tail) { 00069 head.next = p; 00070 } 00071 assert (!isEmpty()); 00072 assert (isValid()); 00073 } 00074 00075 private: 00076 00077 int isValid (void) { 00078 // Make sure every object is the right size. 00079 freeObject * ptr = head.next; 00080 if (ptr != &tail) { 00081 size_t sz = SuperHeap::getSize(ptr); 00082 while (ptr != &tail) { 00083 void * oldptr = ptr; 00084 ptr = ptr->next; 00085 assert (SuperHeap::getSize(oldptr) >= sz); 00086 } 00087 } 00088 return 1; 00089 } 00090 00091 int isEmpty (void) { 00092 return (head.next == &tail); 00093 } 00094 00095 class freeObject { 00096 public: 00097 freeObject * prev; 00098 freeObject * next; 00099 }; 00100 00101 freeObject head, tail; 00102 IFDEBUG (int nObjects); 00103 }; 00104 00105 #endif