17 #ifndef ARENA_RESOURCE_HH_
18 #define ARENA_RESOURCE_HH_
20 #include <memory_resource>
31 template <
class Resource>
37 return std::pmr::null_memory_resource();
39 return std::pmr::get_default_resource();
63 size_t used_size = (
char *)p - (
char *)
buffer_;
65 std::cout <<
"Allocated space of arena is " << used_size <<
" B, free space is " << free_space <<
" B." << std::endl;
77 size_t bytes =
sizeof(T) * n_items;
84 size_t bytes =
sizeof(T) * n_items;
94 char *c_buffer = (
char *)
buffer_;
102 ASSERT(!
full_data_).error(
"Allocation of new data is not possible because child arena was created.");
105 void* p =
resource_.allocate(bytes, alignment);
106 used_size_ += (bytes + alignment - 1) / alignment * alignment;
108 throw std::bad_alloc();
120 upstream_->deallocate(p, bytes, alignment);
124 bool do_is_equal(
const std::pmr::memory_resource& other)
const noexcept
override {
125 return this == &other;
138 template <
class Resource>
143 :
PatchArenaResource<Resource>(
std::pmr::get_default_resource()->allocate(buffer_size, simd_alignment), buffer_size, simd_alignment, upstream ) {}
157 size_t used_size = (
char *)p - (
char *)this->
buffer_;
#define ASSERT_PERMANENT_EQ(a, b)
Definition of comparative assert macro (EQual)
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
PatchArenaResource< Resource > * get_child_arena()
AssemblyArenaResource(size_t buffer_size, size_t simd_alignment, std::pmr::memory_resource *upstream=PatchArenaResource< Resource >::upstream_resource())
Constructor. Creates assembly arena.
virtual ~AssemblyArenaResource()
T * allocate_8(size_t n_items)
Allocate and return data pointer of n_item array of type T (alignment to length 8 bytes)
void print_space()
Compute and print free space and used space of arena buffer. Development method.
void * do_allocate(size_t bytes, size_t alignment) override
Override do_allocate to handle allocation logic.
std::pmr::memory_resource * upstream_
Pointer to upstream.
~PatchArenaResource()=default
size_t used_size_
Temporary data member, will be removed.
Resource & resource()
Getter for resource.
size_t buffer_size_
Size of buffer.
void do_deallocate(void *p, size_t bytes, size_t alignment) override
Override do_deallocate (no-op for monotonic buffer)
static std::pmr::memory_resource * upstream_resource()
Returns different upstream resource in debug / release mode.
PatchArenaResource(void *buffer, size_t buffer_size, size_t simd_alignment, std::pmr::memory_resource *upstream=PatchArenaResource< Resource >::upstream_resource())
Same as previous but doesn't construct buffer implicitly.
void * raw_allocate(size_t bytes, size_t alignment)
bool do_is_equal(const std::pmr::memory_resource &other) const noexcept override
Override do_is_equal for memory resource comparison.
void * buffer_
Pointer to buffer.
size_t simd_alignment_
Size of SIMD alignment.
T * allocate_simd(size_t n_items)
Allocate and return data pointer of n_item array of type T (alignment to length given by simd_alignme...
Resource resource_
Resource of arena.
bool full_data_
Flag signs full data (child arena is created)