|
| 1 | +/** |
| 2 | + * \file libipc/central_cache_pool.h |
| 3 | + * \author mutouyun ([email protected]) |
| 4 | + * \brief The fixed-length memory block central cache pool. |
| 5 | + */ |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#include <cstddef> |
| 9 | +#include <deque> |
| 10 | +#include <utility> |
| 11 | +#include <array> |
| 12 | + |
| 13 | +#include "libipc/imp/byte.h" |
| 14 | +#include "libipc/concur/intrusive_stack.h" |
| 15 | +#include "libipc/mem/central_cache_allocator.h" |
| 16 | + |
| 17 | +namespace ipc { |
| 18 | +namespace mem { |
| 19 | + |
| 20 | +/** |
| 21 | + * \brief The block type. |
| 22 | + * \tparam BlockSize specifies the memory block size |
| 23 | +*/ |
| 24 | +template <std::size_t BlockSize> |
| 25 | +union block { |
| 26 | + block *next; |
| 27 | + alignas(std::max_align_t) std::array<byte, BlockSize> storage; |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * \brief A fixed-length memory block central cache pool. |
| 32 | + * \tparam BlockT specifies the memory block type |
| 33 | + */ |
| 34 | +template <typename BlockT, std::size_t BlockPoolExpansion> |
| 35 | +class central_cache_pool { |
| 36 | + |
| 37 | + /// \brief The block type, which should be a union of a pointer and a storage. |
| 38 | + using block_t = BlockT; |
| 39 | + /// \brief The chunk type, which is an array of blocks. |
| 40 | + using chunk_t = std::array<block_t, BlockPoolExpansion>; |
| 41 | + /// \brief The node type, which is used to store the block pointer. |
| 42 | + using node_t = typename concur::intrusive_stack<block_t *>::node; |
| 43 | + |
| 44 | + /// \brief The central cache stack. |
| 45 | + concur::intrusive_stack<block_t *> cached_; |
| 46 | + concur::intrusive_stack<block_t *> aqueired_; |
| 47 | + |
| 48 | + central_cache_pool() noexcept = default; |
| 49 | + |
| 50 | +public: |
| 51 | + block_t *aqueire() noexcept { |
| 52 | + auto *n = cached_.pop(); |
| 53 | + if (n != nullptr) { |
| 54 | + aqueired_.push(n); |
| 55 | + return n->value; |
| 56 | + } |
| 57 | + auto *chunk = central_cache_allocator().construct<chunk_t>(); |
| 58 | + if (chunk == nullptr) { |
| 59 | + return nullptr; |
| 60 | + } |
| 61 | + for (std::size_t i = 0; i < BlockPoolExpansion - 1; ++i) { |
| 62 | + (*chunk)[i].next = &(*chunk)[i + 1]; |
| 63 | + } |
| 64 | + chunk->back().next = nullptr; |
| 65 | + return chunk->data(); |
| 66 | + } |
| 67 | + |
| 68 | + void release(block_t *p) noexcept { |
| 69 | + if (p == nullptr) return; |
| 70 | + auto *a = aqueired_.pop(); |
| 71 | + if (a == nullptr) { |
| 72 | + a = central_cache_allocator().construct<node_t>(); |
| 73 | + if (a == nullptr) return; |
| 74 | + } |
| 75 | + a->value = p; |
| 76 | + cached_.push(a); |
| 77 | + } |
| 78 | + |
| 79 | + /// \brief Get the singleton instance. |
| 80 | + static central_cache_pool &instance() noexcept { |
| 81 | + static central_cache_pool pool; |
| 82 | + return pool; |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +/// \brief A fixed-length memory block central cache pool with no default expansion size. |
| 87 | +template <typename BlockT> |
| 88 | +class central_cache_pool<BlockT, 0> { |
| 89 | + |
| 90 | + /// \brief The block type, which should be a union of a pointer and a storage. |
| 91 | + using block_t = BlockT; |
| 92 | + /// \brief The node type, which is used to store the block pointer. |
| 93 | + using node_t = typename concur::intrusive_stack<block_t *>::node; |
| 94 | + |
| 95 | + /// \brief The central cache stack. |
| 96 | + concur::intrusive_stack<block_t *> cached_; |
| 97 | + concur::intrusive_stack<block_t *> aqueired_; |
| 98 | + |
| 99 | + central_cache_pool() noexcept = default; |
| 100 | + |
| 101 | +public: |
| 102 | + block_t *aqueire() noexcept { |
| 103 | + auto *n = cached_.pop(); |
| 104 | + if (n != nullptr) { |
| 105 | + aqueired_.push(n); |
| 106 | + return n->value; |
| 107 | + } |
| 108 | + // For pools with no default expansion size, |
| 109 | + // the central cache pool is only buffered, not allocated. |
| 110 | + return nullptr; |
| 111 | + } |
| 112 | + |
| 113 | + void release(block_t *p) noexcept { |
| 114 | + if (p == nullptr) return; |
| 115 | + auto *a = aqueired_.pop(); |
| 116 | + if (a == nullptr) { |
| 117 | + a = central_cache_allocator().construct<node_t>(); |
| 118 | + if (a == nullptr) return; |
| 119 | + } |
| 120 | + a->value = p; |
| 121 | + cached_.push(a); |
| 122 | + } |
| 123 | + |
| 124 | + /// \brief Get the singleton instance. |
| 125 | + static central_cache_pool &instance() noexcept { |
| 126 | + static central_cache_pool pool; |
| 127 | + return pool; |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +} // namespace mem |
| 132 | +} // namespace ipc |
0 commit comments