Rev | Line | |
---|
[176] | 1 | /* -*- C++ -*- */ |
---|
| 2 | |
---|
| 3 | #ifndef _FIFOFREELIST_H_ |
---|
| 4 | #define _FIFOFREELIST_H_ |
---|
| 5 | |
---|
| 6 | #include <assert.h> |
---|
| 7 | |
---|
| 8 | #ifdef NDEBUG |
---|
| 9 | #define IFDEBUG(x) |
---|
| 10 | #else |
---|
| 11 | #define IFDEBUG(x) x |
---|
| 12 | #endif |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | template <class SuperHeap> |
---|
| 16 | class FIFOFreelistHeap : public SuperHeap { |
---|
| 17 | public: |
---|
| 18 | |
---|
| 19 | FIFOFreelistHeap (void) |
---|
| 20 | IFDEBUG(: nObjects (0)) |
---|
| 21 | { |
---|
| 22 | head.next = &tail; |
---|
| 23 | tail.next = &tail; |
---|
| 24 | assert (isEmpty()); |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | ~FIFOFreelistHeap (void) |
---|
| 28 | { |
---|
| 29 | // printf ("Adios free list.\n"); |
---|
| 30 | // Delete everything on the free list. |
---|
| 31 | freeObject * ptr = head.next; |
---|
| 32 | while (ptr != &tail) { |
---|
| 33 | void * oldptr = ptr; |
---|
| 34 | ptr = ptr->next; |
---|
| 35 | SuperHeap::free (oldptr); |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | inline void * malloc (size_t sz) { |
---|
| 40 | assert (isValid()); |
---|
| 41 | // Check the free list first. |
---|
| 42 | freeObject * ptr = head.next; |
---|
| 43 | if (ptr == &tail) { |
---|
| 44 | assert (nObjects == 0); |
---|
| 45 | assert (isEmpty()); |
---|
| 46 | ptr = (freeObject *) SuperHeap::malloc (sz); |
---|
| 47 | } else { |
---|
| 48 | assert (nObjects > 0); |
---|
| 49 | head.next = ptr->next; |
---|
| 50 | if (head.next == &tail) { |
---|
| 51 | tail.next = &tail; |
---|
| 52 | } |
---|
| 53 | IFDEBUG (nObjects--); |
---|
| 54 | } |
---|
| 55 | assert (isValid()); |
---|
| 56 | return (void *) ptr; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | inline void free (void * ptr) { |
---|
| 60 | assert (isValid()); |
---|
| 61 | // Add this object to the free list. |
---|
| 62 | assert (ptr != NULL); |
---|
| 63 | IFDEBUG (nObjects++); |
---|
| 64 | freeObject * p = (freeObject *) ptr; |
---|
| 65 | p->next = &tail; |
---|
| 66 | tail.next->next = p; |
---|
| 67 | tail.next = p; |
---|
| 68 | if (head.next == &tail) { |
---|
| 69 | head.next = p; |
---|
| 70 | } |
---|
| 71 | assert (!isEmpty()); |
---|
| 72 | assert (isValid()); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | private: |
---|
| 76 | |
---|
| 77 | int isValid (void) { |
---|
| 78 | // Make sure every object is the right size. |
---|
| 79 | freeObject * ptr = head.next; |
---|
| 80 | if (ptr != &tail) { |
---|
| 81 | size_t sz = SuperHeap::getSize(ptr); |
---|
| 82 | while (ptr != &tail) { |
---|
| 83 | void * oldptr = ptr; |
---|
| 84 | ptr = ptr->next; |
---|
| 85 | assert (SuperHeap::getSize(oldptr) >= sz); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | return 1; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | int isEmpty (void) { |
---|
| 92 | return (head.next == &tail); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | class freeObject { |
---|
| 96 | public: |
---|
| 97 | freeObject * prev; |
---|
| 98 | freeObject * next; |
---|
| 99 | }; |
---|
| 100 | |
---|
| 101 | freeObject head, tail; |
---|
| 102 | IFDEBUG (int nObjects); |
---|
| 103 | }; |
---|
| 104 | |
---|
| 105 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.