Rev | Line | |
---|
[176] | 1 | /* -*- C++ -*- */ |
---|
| 2 | |
---|
| 3 | #ifndef _FREELISTHEAP_H_ |
---|
| 4 | #define _FREELISTHEAP_H_ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @class FreelistHeap |
---|
| 8 | * @brief Manage freed memory on a linked list. |
---|
| 9 | * @warning This is for one "size class" only. |
---|
| 10 | * |
---|
| 11 | * Note that the linked list is threaded through the freed objects, |
---|
| 12 | * meaning that such objects must be at least the size of a pointer. |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #include "freesllist.h" |
---|
| 16 | #include <assert.h> |
---|
| 17 | |
---|
| 18 | #ifndef NULL |
---|
| 19 | #define NULL 0 |
---|
| 20 | #endif |
---|
| 21 | |
---|
| 22 | namespace HL { |
---|
| 23 | |
---|
| 24 | template <class SuperHeap> |
---|
| 25 | class FreelistHeap : public SuperHeap { |
---|
| 26 | public: |
---|
| 27 | |
---|
| 28 | inline void * malloc (size_t sz) { |
---|
| 29 | // Check the free list first. |
---|
| 30 | void * ptr = _freelist.get(); |
---|
| 31 | // If it's empty, get more memory; |
---|
| 32 | // otherwise, advance the free list pointer. |
---|
| 33 | if (ptr == 0) { |
---|
| 34 | ptr = SuperHeap::malloc (sz); |
---|
| 35 | } |
---|
| 36 | return ptr; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | inline void free (void * ptr) { |
---|
| 40 | if (ptr == 0) { |
---|
| 41 | return; |
---|
| 42 | } |
---|
| 43 | _freelist.insert (ptr); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | inline void clear (void) { |
---|
| 47 | void * ptr; |
---|
| 48 | while (ptr = _freelist.get()) { |
---|
| 49 | SuperHeap::free (ptr); |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | private: |
---|
| 54 | |
---|
| 55 | FreeSLList _freelist; |
---|
| 56 | |
---|
| 57 | }; |
---|
| 58 | |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.