[176] | 1 | /* -*- C++ -*- */ |
---|
| 2 | |
---|
| 3 | #ifndef _BATCHHEAP_H_ |
---|
| 4 | #define _BATCHHEAP_H_ |
---|
| 5 | |
---|
| 6 | #include <assert.h> |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | template <class SuperHeap> |
---|
| 10 | class MultiMalloc : public SuperHeap { |
---|
| 11 | public: |
---|
| 12 | // Sets ptr to a list of up to num objects of size sz |
---|
| 13 | // and returns how many free objects ptr points to. |
---|
| 14 | int multimalloc (int num, size_t sz, void *& ptr) |
---|
| 15 | { |
---|
| 16 | int i = 0; |
---|
| 17 | ptr = (freeObject *) SuperHeap::malloc (sz); |
---|
| 18 | freeObject * p = (freeObject *) ptr; |
---|
| 19 | if (ptr != NULL) { |
---|
| 20 | for (i = 1; i < num; i++) { |
---|
| 21 | p->next = (freeObject *) SuperHeap::malloc (sz); |
---|
| 22 | if (p->next == NULL) |
---|
| 23 | break; |
---|
| 24 | p = p->next; |
---|
| 25 | } |
---|
| 26 | p->next = NULL; |
---|
| 27 | } |
---|
| 28 | return i; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | // Frees all num items pointed to by ptr |
---|
| 32 | // and sets ptr to NULL. |
---|
| 33 | void multifree (int num, void *& ptr) |
---|
| 34 | { |
---|
| 35 | freeObject * p; |
---|
| 36 | freeObject * prev = (freeObject *) ptr; |
---|
| 37 | for (int i = 0; i < num; i++) { |
---|
| 38 | p = prev->next; |
---|
| 39 | SuperHeap::free (prev); |
---|
| 40 | prev = p; |
---|
| 41 | } |
---|
| 42 | ptr = NULL; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | private: |
---|
| 46 | |
---|
| 47 | class freeObject { |
---|
| 48 | public: |
---|
| 49 | freeObject * next; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | template <int BatchNumber, class SuperHeap> |
---|
| 57 | class BatchHeap : public SuperHeap { |
---|
| 58 | public: |
---|
| 59 | |
---|
| 60 | BatchHeap (void) |
---|
| 61 | : nObjects (0) |
---|
| 62 | { |
---|
| 63 | freeList[0] = NULL; |
---|
| 64 | freeList[1] = NULL; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | ~BatchHeap (void) { |
---|
| 69 | if (nObjects <= BatchNumber) { |
---|
| 70 | SuperHeap::multifree (nObjects, (void *&) freeList[0]); |
---|
| 71 | } else { |
---|
| 72 | SuperHeap::multifree (BatchNumber, (void *&) freeList[0]); |
---|
| 73 | SuperHeap::multifree (nObjects - BatchNumber, (void *&) freeList[1]); |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | inline void * malloc (size_t sz) { |
---|
| 78 | if (nObjects == 0) { |
---|
| 79 | // Obtain BatchNumber objects if we're out. |
---|
| 80 | nObjects = SuperHeap::multimalloc (BatchNumber, sz, (void *&) freeList[0]); |
---|
| 81 | } |
---|
| 82 | assert (nObjects >= 1); |
---|
| 83 | freeObject * ptr; |
---|
| 84 | if (nObjects > BatchNumber) { |
---|
| 85 | freeObject *& head = freeList[1]; |
---|
| 86 | ptr = head; |
---|
| 87 | nObjects--; |
---|
| 88 | head = head->next; |
---|
| 89 | return (void *) ptr; |
---|
| 90 | } else { |
---|
| 91 | freeObject *& head = freeList[0]; |
---|
| 92 | ptr = head; |
---|
| 93 | nObjects--; |
---|
| 94 | head = head->next; |
---|
| 95 | return (void *) ptr; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | inline void free (void * ptr) { |
---|
| 100 | if (nObjects <= BatchNumber) { |
---|
| 101 | freeObject *& head = freeList[0]; |
---|
| 102 | ((freeObject *) ptr)->next = head; |
---|
| 103 | head = (freeObject *) ptr; |
---|
| 104 | } else { |
---|
| 105 | freeObject *& head = freeList[1]; |
---|
| 106 | ((freeObject *) ptr)->next = head; |
---|
| 107 | head = (freeObject *) ptr; |
---|
| 108 | } |
---|
| 109 | nObjects++; |
---|
| 110 | if (nObjects == 2 * BatchNumber) { |
---|
| 111 | // Free half of them. |
---|
| 112 | assert (freeList[1] != NULL); |
---|
| 113 | SuperHeap::multifree (BatchNumber, (void *&) freeList[1]); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | private: |
---|
| 118 | |
---|
| 119 | class freeObject { |
---|
| 120 | public: |
---|
| 121 | freeObject * next; |
---|
| 122 | }; |
---|
| 123 | |
---|
| 124 | int nObjects; |
---|
| 125 | |
---|
| 126 | // The first free list holds the first BatchNumber objects, |
---|
| 127 | // while the second free list holds the rest. |
---|
| 128 | freeObject * freeList[2]; |
---|
| 129 | }; |
---|
| 130 | |
---|
| 131 | #endif |
---|