60 :
public FFL_PMR::memory_resource {
78 return busy_blocks_count_.load(std::memory_order_relaxed);
90 using pattern_type = size_t;
104 pattern_type pattern;
109 constexpr
static size_t min_allocation_size{
sizeof(prefix_t) +
sizeof(suffix_t) };
111 constexpr
static pattern_type busy_block_prefix_pattern{ 0xBEEFABCD };
112 constexpr
static pattern_type free_block_prefix_pattern{ 0xBEEFABCE };
113 constexpr
static pattern_type busy_block_suffix_pattern{ 0xDEADABCD };
114 constexpr
static pattern_type free_block_suffix_pattern{ 0xDEADABCE };
116 constexpr
static int fill_pattern{ 0xFE };
125 void* do_allocate(
size_t bytes, [[maybe_unused]]
size_t alignment)
override {
127 size_t bytes_with_debug_info = bytes + min_allocation_size;
129 if (
nullptr == ptr) {
130 throw std::bad_alloc{};
133 prefix_t *prefix{ static_cast<prefix_t *>(ptr) };
134 suffix_t *suffix{ reinterpret_cast<suffix_t *>( static_cast<char *>(ptr) + bytes_with_debug_info) - 1 };
136 prefix->size = bytes_with_debug_info;
137 prefix->alignment = alignment;
138 prefix->memory_resource =
this;
139 prefix->pattern = busy_block_prefix_pattern;
140 suffix->pattern = busy_block_suffix_pattern;
142 increment_busy_block_count();
144 char *user_buffer = reinterpret_cast<char *>(prefix + 1);
156 void do_deallocate(
void* p,
size_t bytes, [[maybe_unused]]
size_t alignment) noexcept
override {
158 decrement_busy_block_count();
160 prefix_t *prefix{ static_cast<prefix_t *>(p) - 1 };
164 size_t const user_allocation_size = prefix->size - min_allocation_size;
167 suffix_t *suffix{ reinterpret_cast<suffix_t *>(reinterpret_cast<char *>(prefix) + prefix->size) - 1 };
170 prefix->memory_resource =
nullptr;
171 prefix->pattern = free_block_prefix_pattern;
172 suffix->pattern = free_block_suffix_pattern;
183 bool do_is_equal(memory_resource
const & other)
const noexcept
override {
184 return (&other ==
this);
189 void increment_busy_block_count() noexcept {
190 size_t const prev_busy_blocks_count = busy_blocks_count_.fetch_add(1, std::memory_order_relaxed);
196 void decrement_busy_block_count() noexcept {
197 size_t const prev_busy_blocks_count = busy_blocks_count_.fetch_sub(1, std::memory_order_relaxed);
203 std::atomic<size_t> busy_blocks_count_{ 0 };
265 :
public FFL_PMR::memory_resource {
275 size_t buffer_size) noexcept
276 : used_(buffer ?
false :
true)
278 , buffer_size_{ buffer_size } {
293 return (used_ ? 1 : 0);
313 void*
do_allocate(
size_t bytes, [[maybe_unused]]
size_t alignment)
override {
314 if (!used_ && buffer_ && bytes <= buffer_size_) {
318 throw std::bad_alloc{};
329 void do_deallocate(
void* p,
size_t bytes, [[maybe_unused]]
size_t alignment) noexcept
override {
341 bool do_is_equal(memory_resource
const & other)
const noexcept
override {
342 return (&other ==
this);
347 void *buffer_{
nullptr };
348 size_t buffer_size_{ 0 };
#define FFL_ALLIGNED_FREE(PTR)
In MSVC define to function matching to _aligned_malloc.
Definition: iffl_common.h:92
This file is supposed to be generated by cmake. Cmake pushes output to the folder where it generates ...
implements std::pmr::memory_resource interface.
Definition: iffl_allocator.h:59
void fill_buffer(char *buffer, int value, size_t length) noexcept
sets "length" consecutive bytes of "to_buffer" to "value".
Definition: iffl_common.h:336
#define FFL_CODDING_ERROR_IF(C)
If expression is true then fail fast with error ENOTRECOVERABLE(127 or 0x7f)
Definition: iffl_common.h:113
intrusive flat forward list
void validate_no_busy_blocks() const noexcept
Triggers fail fast if there are outstanding allocations.
Definition: iffl_allocator.h:84
Common definitions, utility functions and vocabulary types.
size_t get_busy_blocks_count() const noexcept
Can be used to query number of outstanding allocations.
Definition: iffl_allocator.h:77
#define FFL_ALLIGNED_ALLOC(BYTES, ALIGNMENT)
In MSVC define to function that allocates aligned buffer.
Definition: iffl_common.h:88
~debug_memory_resource() noexcept
Destructor triggers fail fast if there are outstanding allocations.
Definition: iffl_allocator.h:69
#define FFL_CODDING_ERROR_IF_NOT(C)
If expression is false then fail fast with error ENOTRECOVERABLE(127 or 0x7f)
Definition: iffl_common.h:120