Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
FramePool.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <array>
10#include <cstddef>
11#include <new>
12
13namespace corium::async {
14
17 [[nodiscard]] static void* allocate(std::size_t size) {
18 return ::operator new(size);
19 }
20
21 static void deallocate(void* ptr, [[maybe_unused]] std::size_t size) noexcept {
22 ::operator delete(ptr);
23 }
24};
25
31template <std::size_t MaxFrames = 16, std::size_t FrameSize = 256>
33public:
34 static constexpr std::size_t max_frames = MaxFrames;
35 static constexpr std::size_t frame_size = FrameSize;
36
37 struct alignas(std::max_align_t) Slot {
38 std::byte storage[FrameSize];
39 bool inUse{false};
40 };
41
42 [[nodiscard]] static void* allocate(std::size_t size) noexcept {
43 if (size > FrameSize) {
44 return nullptr;
45 }
46
47 for (auto& slot : _slots) {
48 if (!slot.inUse) {
49 slot.inUse = true;
50 return static_cast<void*>(slot.storage);
51 }
52 }
53 return nullptr; // Pool exhausted
54 }
55
56 static void deallocate(void* ptr, std::size_t /*size*/) noexcept {
57 if (!ptr) {
58 return;
59 }
60
61 for (auto& slot : _slots) {
62 if (static_cast<void*>(slot.storage) == ptr) {
63 slot.inUse = false;
64 return;
65 }
66 }
67 }
68
70 [[nodiscard]] static std::size_t activeCount() noexcept {
71 std::size_t count = 0;
72 for (const auto& slot : _slots) {
73 if (slot.inUse) {
74 count++;
75 }
76 }
77 return count;
78 }
79
81 static void reset() noexcept {
82 for (auto& slot : _slots) {
83 slot.inUse = false;
84 }
85 }
86
87private:
88 static inline std::array<Slot, MaxFrames> _slots{};
89};
90
92template <std::size_t MaxFrames = 16, std::size_t FrameSize = 256>
95
96 [[nodiscard]] static void* allocate(std::size_t size) {
97 void* ptr = Pool::allocate(size);
98 if (!ptr) {
99#if defined(__cpp_exceptions) || defined(__EXCEPTIONS)
100 throw std::bad_alloc();
101#else
102 return nullptr;
103#endif
104 }
105 return ptr;
106 }
107
108 static void deallocate(void* ptr, std::size_t size) noexcept {
109 Pool::deallocate(ptr, size);
110 }
111};
112
113} // namespace corium::async
Statically-allocated fixed-capacity memory pool for coroutine state frames. Eliminates heap allocatio...
Definition FramePool.hpp:32
static void * allocate(std::size_t size) noexcept
Definition FramePool.hpp:42
static constexpr std::size_t max_frames
Definition FramePool.hpp:34
static void deallocate(void *ptr, std::size_t) noexcept
Definition FramePool.hpp:56
static void reset() noexcept
Reset all pool slots to available state.
Definition FramePool.hpp:81
static constexpr std::size_t frame_size
Definition FramePool.hpp:35
static std::size_t activeCount() noexcept
Number of active frames currently allocated from the pool.
Definition FramePool.hpp:70
Definition AsyncEvent.hpp:14
Default heap allocator policy for C++20 coroutine frames (HALO-eligible).
Definition FramePool.hpp:16
static void deallocate(void *ptr, std::size_t size) noexcept
Definition FramePool.hpp:21
static void * allocate(std::size_t size)
Definition FramePool.hpp:17
Allocator policy adapter wrapping a StaticFramePool.
Definition FramePool.hpp:93
static void * allocate(std::size_t size)
Definition FramePool.hpp:96
static void deallocate(void *ptr, std::size_t size) noexcept
Definition FramePool.hpp:108
Definition FramePool.hpp:37
std::byte storage[FrameSize]
Definition FramePool.hpp:38
bool inUse
Definition FramePool.hpp:39