| 1 | // -*- C++ -*- |
| 2 | |
| 3 | #ifndef HL_FREESLLIST_H_ |
| 4 | #define HL_FREESLLIST_H_ |
| 5 | |
| 6 | #include <assert.h> |
| 7 | |
| 8 | /** |
| 9 | * @class FreeSLList |
| 10 | * @brief A "memory neutral" singly-linked list, |
| 11 | * |
| 12 | * Uses the free space in objects to store |
| 13 | * the pointers. |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | class FreeSLList { |
| 18 | public: |
| 19 | |
| 20 | inline void clear() { |
| 21 | head.next = nullptr; |
| 22 | } |
| 23 | |
| 24 | class Entry; |
| 25 | |
| 26 | /// Get the head of the list. |
| 27 | inline Entry * get() { |
| 28 | const Entry * e = head.next; |
| 29 | if (e == nullptr) { |
| 30 | return nullptr; |
| 31 | } |
| 32 | head.next = e->next; |
| 33 | return const_cast<Entry *>(e); |
| 34 | } |
| 35 | |
| 36 | inline Entry * remove() { |
| 37 | const Entry * e = head.next; |
| 38 | if (e == nullptr) { |
| 39 | return nullptr; |
| 40 | } |
| 41 | head.next = e->next; |
| 42 | return const_cast<Entry *>(e); |
| 43 | } |
| 44 | |
| 45 | inline void insert (void * e) { |
| 46 | Entry * entry = reinterpret_cast<Entry *>(e); |
| 47 | entry->next = head.next; |
| 48 | head.next = entry; |
| 49 | } |
| 50 | |
| 51 | class Entry { |
| 52 | public: |
| 53 | Entry() |
| 54 | : next (nullptr) |
| 55 | {} |
| 56 | Entry * next; |
| 57 | private: |
| 58 | Entry (const Entry&); |
| 59 | Entry& operator=(const Entry&); |
| 60 | }; |
| 61 | |
| 62 | private: |
| 63 | Entry head; |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | #endif |
| 68 | |
| 69 | |
| 70 | |
| 71 |