|
| 1 | +#ifndef LF_POOL_ALLOC_CHUNK_LIST |
| 2 | +#define LF_POOL_ALLOC_CHUNK_LIST |
| 3 | + |
| 4 | +#include <cstdint> |
| 5 | +#include <atomic> |
| 6 | +#include <type_traits> |
| 7 | + |
| 8 | +#ifndef LFP_ALLOW_BLOCKING |
| 9 | +static_assert(ATOMIC_POINTER_LOCK_FREE == 2, |
| 10 | + "Atomic pointer is not lock-free."); |
| 11 | +#endif |
| 12 | + |
| 13 | +namespace lfpAlloc { |
| 14 | + |
| 15 | +template <std::size_t Size> |
| 16 | +struct Cell { |
| 17 | + uint8_t val_[Size]; |
| 18 | + Cell* next_ = this + 1; |
| 19 | +}; |
| 20 | + |
| 21 | +// For small types (less than the size of void*), no additional |
| 22 | +// space is needed, so union val_ with next_ to avoid overhead. |
| 23 | +template <> |
| 24 | +struct Cell<0> { |
| 25 | + Cell() : next_{this + 1} {} |
| 26 | + union { |
| 27 | + uint8_t val_[sizeof(Cell*)]; |
| 28 | + Cell* next_; |
| 29 | + }; |
| 30 | +}; |
| 31 | + |
| 32 | +template <std::size_t Size, std::size_t AllocationsPerChunk> |
| 33 | +struct Chunk { |
| 34 | + Chunk() noexcept { |
| 35 | + auto& last = memBlock_[AllocationsPerChunk - 1]; |
| 36 | + last.next_ = nullptr; |
| 37 | + } |
| 38 | + Cell<Size> memBlock_[AllocationsPerChunk]; |
| 39 | +}; |
| 40 | + |
| 41 | +template <typename T> |
| 42 | +struct Node { |
| 43 | + Node() : val_(), next_(nullptr) {} |
| 44 | + Node(const T& val) : val_(val), next_(nullptr) {} |
| 45 | + T val_; |
| 46 | + std::atomic<Node<T>*> next_; |
| 47 | +}; |
| 48 | + |
| 49 | +template <std::size_t Size, std::size_t AllocationsPerChunk> |
| 50 | +class ChunkList { |
| 51 | + static constexpr auto CellSize = |
| 52 | + (Size > sizeof(void*)) ? Size - sizeof(void*) : 0; |
| 53 | + using Chunk_t = Chunk<CellSize, AllocationsPerChunk>; |
| 54 | + using Cell_t = Cell<CellSize>; |
| 55 | + |
| 56 | + using ChunkNode = Node<Chunk_t>; |
| 57 | + using CellNode = Node<Cell_t*>; |
| 58 | + |
| 59 | +public: |
| 60 | + static ChunkList& getInstance() { |
| 61 | + static ChunkList c; |
| 62 | + return c; |
| 63 | + } |
| 64 | + |
| 65 | + Cell_t* allocateChain() { |
| 66 | + CellNode* recentHead = head_.load(); |
| 67 | + CellNode* currentNext = nullptr; |
| 68 | + do { |
| 69 | + // If there are no available chains, allocate a new chunk |
| 70 | + if (!recentHead) { |
| 71 | + ChunkNode* currentHandle; |
| 72 | + |
| 73 | + // Make a new node |
| 74 | + auto newChunk = new ChunkNode(); |
| 75 | + |
| 76 | + // Add the chunk to the chain |
| 77 | + do { |
| 78 | + currentHandle = handle_.load(); |
| 79 | + newChunk->next_ = currentHandle; |
| 80 | + } while ( |
| 81 | + !handle_.compare_exchange_weak(currentHandle, newChunk)); |
| 82 | + return &newChunk->val_.memBlock_[0]; |
| 83 | + } |
| 84 | + |
| 85 | + currentNext = recentHead->next_; |
| 86 | + } while (!head_.compare_exchange_weak(recentHead, currentNext)); |
| 87 | + |
| 88 | + auto retnValue = recentHead->val_; |
| 89 | + delete recentHead; |
| 90 | + return retnValue; |
| 91 | + } |
| 92 | + |
| 93 | + void deallocateChain(Cell_t* newCell) { |
| 94 | + if (!newCell) { |
| 95 | + return; |
| 96 | + } |
| 97 | + CellNode* currentHead = head_.load(); |
| 98 | + |
| 99 | + // Construct a new node to be added to the linked list |
| 100 | + CellNode* newHead = new CellNode(newCell); |
| 101 | + |
| 102 | + // Add the chain to the linked list |
| 103 | + do { |
| 104 | + newHead->next_.store(currentHead, std::memory_order_release); |
| 105 | + } while (!head_.compare_exchange_weak(currentHead, newHead)); |
| 106 | + } |
| 107 | + |
| 108 | +private: |
| 109 | + ChunkList() : handle_(nullptr), head_(nullptr) {} |
| 110 | + |
| 111 | + std::atomic<ChunkNode*> handle_; |
| 112 | + std::atomic<CellNode*> head_; |
| 113 | +}; |
| 114 | +} |
| 115 | + |
| 116 | +#endif |
0 commit comments