Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow_arena_memory_resource.h
Go to the documentation of this file.
1#pragma once
3#include <cassert>
4#include <cstddef>
5#include <exception>
6#include <memory>
7
8namespace flow {
9
14 public:
15 explicit ArenaMemoryResource(void* buffer, std::size_t capacity) noexcept
16 : buffer_(buffer), capacity_(capacity) {
17#ifdef _DEBUG
18 beginBuffer_ = buffer;
19#endif
20 }
21
22 protected:
23#ifdef _DEBUG
24 void* beginBuffer_;
25#endif
26 void* buffer_;
27 std::size_t capacity_;
28
29 virtual void* allocateImp(std::size_t bytes, std::size_t alignment) override {
30 void* aligned = std::align(alignment, bytes, buffer_, capacity_);
31 if (!aligned || capacity_ < bytes) {
32 throw std::bad_alloc();
33 }
34
35 buffer_ = static_cast<std::byte*>(aligned) + bytes;
36 capacity_ -= bytes;
37 return aligned;
38 }
39
40 virtual void deallocateImp(
41 [[maybe_unused]] void* address,
42 [[maybe_unused]] std::size_t bytes,
43 [[maybe_unused]] std::size_t alignment) override {
44 assert(address == nullptr || (beginBuffer_ <= address && address <= buffer_));
45 }
46 };
47}
virtual void * allocateImp(std::size_t bytes, std::size_t alignment) override
virtual void deallocateImp(void *address, std::size_t bytes, std::size_t alignment) override
ArenaMemoryResource(void *buffer, std::size_t capacity) noexcept
A memory resource holder interface for the PolymorphicAllocator. Responsible for allocate and dealloc...