| 1 | // -*- C++ -*- |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 | |
| 7 | Copyright (C) 2000-2020 by Emery Berger |
| 8 | http://www.emeryberger.com |
| 9 | [email protected] |
| 10 | |
| 11 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | #ifndef HL_SLLIST_H_ |
| 19 | #define HL_SLLIST_H_ |
| 20 | |
| 21 | #include <assert.h> |
| 22 | |
| 23 | /** |
| 24 | * @class SLList |
| 25 | * @brief A "memory neutral" (intrusive) singly-linked list. |
| 26 | * @author Emery Berger |
| 27 | */ |
| 28 | |
| 29 | namespace HL { |
| 30 | |
| 31 | class SLList { |
| 32 | public: |
| 33 | |
| 34 | inline SLList() { |
| 35 | clear(); |
| 36 | } |
| 37 | |
| 38 | class Entry; |
| 39 | |
| 40 | /// Clear the list. |
| 41 | inline void clear() { |
| 42 | head.next = nullptr; |
| 43 | } |
| 44 | |
| 45 | /// Is the list empty? |
| 46 | inline bool isEmpty() const { |
| 47 | return (head.next == nullptr); |
| 48 | } |
| 49 | |
| 50 | /// Get the head of the list. |
| 51 | inline Entry * get() { |
| 52 | const Entry * e = head.next; |
| 53 | if (e == nullptr) { |
| 54 | return nullptr; |
| 55 | } |
| 56 | head.next = e->next; |
| 57 | return (Entry *) e; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | |
| 62 | /** |
| 63 | * @brief Remove one item from the list. |
| 64 | * @warning This method aborts the program if called. |
| 65 | */ |
| 66 | inline void remove (Entry *) { |
| 67 | abort(); |
| 68 | } |
| 69 | |
| 70 | public: |
| 71 | |
| 72 | /// Inserts an entry into the head of the list. |
| 73 | inline void insert (void * ePtr) { |
| 74 | Entry * e = (Entry *) ePtr; |
| 75 | e->next = head.next; |
| 76 | head.next = e; |
| 77 | } |
| 78 | |
| 79 | /// An entry in the list. |
| 80 | class Entry { |
| 81 | public: |
| 82 | inline Entry() |
| 83 | : next (nullptr) |
| 84 | {} |
| 85 | // private: |
| 86 | // Entry * prev; |
| 87 | public: |
| 88 | Entry * next; |
| 89 | }; |
| 90 | |
| 91 | private: |
| 92 | |
| 93 | /// The head of the list. |
| 94 | Entry head; |
| 95 | |
| 96 | }; |
| 97 | |
| 98 | } |
| 99 | |
| 100 | #endif |