|
8 | 8 | #include <cstddef>
|
9 | 9 | #include <algorithm>
|
10 | 10 | #include <type_traits>
|
| 11 | +#include <limits> |
11 | 12 |
|
12 | 13 | #include "libipc/imp/aligned.h"
|
13 | 14 | #include "libipc/imp/uninitialized.h"
|
14 | 15 | #include "libipc/imp/byte.h"
|
15 | 16 | #include "libipc/imp/detect_plat.h"
|
16 |
| - |
17 | 17 | #include "libipc/imp/export.h"
|
| 18 | +#include "libipc/mem/memory_resource.h" |
| 19 | +#include "libipc/mem/block_pool.h" |
18 | 20 |
|
19 | 21 | namespace ipc {
|
20 | 22 | namespace mem {
|
21 | 23 |
|
| 24 | +/// \brief Defines the memory block collector interface. |
| 25 | +class LIBIPC_EXPORT block_collector { |
| 26 | +public: |
| 27 | + virtual ~block_collector() noexcept = default; |
| 28 | + virtual void recycle(void *p, std::size_t bytes, std::size_t alignment) noexcept = 0; |
| 29 | +}; |
| 30 | + |
| 31 | +#if defined(LIBIPC_CPP_17) |
| 32 | +using get_block_collector_t = block_collector *(*)() noexcept; |
| 33 | +#else |
| 34 | +using get_block_collector_t = block_collector *(*)(); |
| 35 | +#endif |
| 36 | + |
| 37 | +static constexpr std::size_t regular_head_size |
| 38 | + = round_up(sizeof(get_block_collector_t), alignof(std::max_align_t)); |
| 39 | + |
| 40 | +/// \brief Select the incremental level based on the size. |
| 41 | +constexpr inline std::size_t regular_level(std::size_t s) noexcept { |
| 42 | + return (s <= 128 ) ? 0 : |
| 43 | + (s <= 1024 ) ? 1 : |
| 44 | + (s <= 8192 ) ? 2 : |
| 45 | + (s <= 65536) ? 3 : 4; |
| 46 | +} |
| 47 | + |
| 48 | +/// \brief Calculates the appropriate memory block size based on the increment level and size. |
| 49 | +constexpr inline std::size_t regular_sizeof_impl(std::size_t l, std::size_t s) noexcept { |
| 50 | + return (l == 0) ? round_up<std::size_t>(s, regular_head_size) : |
| 51 | + (l == 1) ? round_up<std::size_t>(s, 128 ) : |
| 52 | + (l == 2) ? round_up<std::size_t>(s, 1024) : |
| 53 | + (l == 3) ? round_up<std::size_t>(s, 8192) : (std::numeric_limits<std::size_t>::max)(); |
| 54 | +} |
| 55 | + |
| 56 | +/// \brief Calculates the appropriate memory block size based on the size. |
| 57 | +constexpr inline std::size_t regular_sizeof_impl(std::size_t s) noexcept { |
| 58 | + return regular_sizeof_impl(regular_level(s), s); |
| 59 | +} |
| 60 | + |
| 61 | +/// \brief Calculates the appropriate memory block size based on the specific type. |
| 62 | +template <typename T> |
| 63 | +constexpr inline std::size_t regular_sizeof() noexcept { |
| 64 | + return regular_sizeof_impl(regular_head_size + sizeof(T)); |
| 65 | +} |
| 66 | + |
| 67 | +/// \brief Use block pools to handle memory less than 64K. |
| 68 | +template <std::size_t BlockSize, std::size_t BlockPoolExpansion> |
| 69 | +class block_resource_base : public block_pool<BlockSize, BlockPoolExpansion> { |
| 70 | +public: |
| 71 | + void *allocate(std::size_t /*bytes*/, std::size_t /*alignment*/) noexcept { |
| 72 | + return block_pool<BlockSize, BlockPoolExpansion>::allocate(); |
| 73 | + } |
| 74 | + |
| 75 | + void deallocate(void *p, std::size_t /*bytes*/, std::size_t /*alignment*/) noexcept { |
| 76 | + block_pool<BlockSize, BlockPoolExpansion>::deallocate(p); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +/// \brief Use `new`/`delete` to handle memory larger than 64K. |
| 81 | +template <std::size_t BlockSize> |
| 82 | +class block_resource_base<BlockSize, 0> : public new_delete_resource { |
| 83 | +public: |
| 84 | + void *allocate(std::size_t bytes, std::size_t alignment) noexcept { |
| 85 | + return new_delete_resource::allocate(regular_head_size + bytes, alignment); |
| 86 | + } |
| 87 | + |
| 88 | + void deallocate(void *p, std::size_t bytes, std::size_t alignment) noexcept { |
| 89 | + new_delete_resource::deallocate(p, regular_head_size + bytes, alignment); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +/// \brief Defines block pool memory resource based on block pool. |
| 94 | +template <std::size_t BlockSize, std::size_t BlockPoolExpansion> |
| 95 | +class block_pool_resource : public block_resource_base<BlockSize, BlockPoolExpansion> |
| 96 | + , public block_collector { |
| 97 | + |
| 98 | + using base_t = block_resource_base<BlockSize, BlockPoolExpansion>; |
| 99 | + |
| 100 | + void recycle(void *p, std::size_t bytes, std::size_t alignment) noexcept override { |
| 101 | + base_t::deallocate(p, bytes, alignment); |
| 102 | + } |
| 103 | + |
| 104 | +public: |
| 105 | + static block_collector *get() noexcept { |
| 106 | + thread_local block_pool_resource instance; |
| 107 | + return &instance; |
| 108 | + } |
| 109 | + |
| 110 | + void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept { |
| 111 | + void *p = base_t::allocate(bytes, alignment); |
| 112 | + *static_cast<get_block_collector_t *>(p) = get; |
| 113 | + return static_cast<byte *>(p) + regular_head_size; |
| 114 | + } |
| 115 | + |
| 116 | + void deallocate(void *p, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept { |
| 117 | + p = static_cast<byte *>(p) - regular_head_size; |
| 118 | + auto g = *static_cast<get_block_collector_t *>(p); |
| 119 | + if (g == get) { |
| 120 | + base_t::deallocate(p, bytes, alignment); |
| 121 | + return; |
| 122 | + } |
| 123 | + g()->recycle(p, bytes, alignment); |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +/// \brief Different increment levels match different chunk sizes. |
| 128 | +/// 512 means that 512 consecutive memory blocks are allocated at a time. |
| 129 | +template <std::size_t L> |
| 130 | +constexpr std::size_t block_pool_expansion = 0; |
| 131 | + |
| 132 | +template <> constexpr std::size_t block_pool_expansion<0> = 512; |
| 133 | +template <> constexpr std::size_t block_pool_expansion<1> = 256; |
| 134 | +template <> constexpr std::size_t block_pool_expansion<2> = 128; |
| 135 | +template <> constexpr std::size_t block_pool_expansion<3> = 64; |
| 136 | + |
| 137 | +/// \brief Matches the appropriate memory block resource based on the specified type. |
| 138 | +template <typename T, std::size_t N = regular_sizeof<T>(), std::size_t L = regular_level(N)> |
| 139 | +auto *get_regular_resource() noexcept { |
| 140 | + using block_poll_resource_t = block_pool_resource<N, block_pool_expansion<L>>; |
| 141 | + return dynamic_cast<block_poll_resource_t *>(block_poll_resource_t::get()); |
| 142 | +} |
| 143 | + |
| 144 | +/// \brief Creates an object based on the specified type and parameters with block pool resource. |
| 145 | +/// \note This function is thread-safe. |
| 146 | +template <typename T, typename... A> |
| 147 | +T *$new(A &&... args) noexcept { |
| 148 | + auto *res = get_regular_resource<T>(); |
| 149 | + if (res == nullptr) return nullptr; |
| 150 | + return construct<T>(res->allocate(sizeof(T), alignof(T)), std::forward<A>(args)...); |
| 151 | +} |
| 152 | + |
| 153 | +/// \brief Destroys object previously allocated by the `$new` and releases obtained memory area. |
| 154 | +/// \note This function is thread-safe. If the pointer type passed in is different from `$new`, |
| 155 | +/// additional performance penalties may be incurred. |
| 156 | +template <typename T> |
| 157 | +void $delete(T *p) noexcept { |
| 158 | + if (p == nullptr) return; |
| 159 | + destroy(p); |
| 160 | + auto *res = get_regular_resource<T>(); |
| 161 | + if (res == nullptr) return; |
| 162 | +#if (LIBIPC_CC_MSVC > LIBIPC_CC_MSVC_2015) |
| 163 | + res->deallocate(p, sizeof(T), alignof(T)); |
| 164 | +#else |
| 165 | + // `alignof` of vs2015 requires that type must be able to be instantiated. |
| 166 | + res->deallocate(p, sizeof(T)); |
| 167 | +#endif |
| 168 | +} |
22 | 169 |
|
| 170 | +/// \brief The destruction policy used by std::unique_ptr. |
| 171 | +/// \see https://en.cppreference.com/w/cpp/memory/default_delete |
| 172 | +struct deleter { |
| 173 | + template <typename T> |
| 174 | + void operator()(T *p) const noexcept { |
| 175 | + $delete(p); |
| 176 | + } |
| 177 | +}; |
23 | 178 |
|
24 | 179 | } // namespace mem
|
25 | 180 | } // namespace ipc
|
0 commit comments