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();
45 <<
"Allocation of ArenaResource failed. Please check if correct type of upstream is used.");
65 size_t used_size = (
char *)p - (
char *)
buffer_;
67 std::cout <<
"Allocated space of arena is " << used_size <<
" B, free space is " << free_space <<
" B." << std::endl;
79 size_t bytes =
sizeof(T) * n_items;
86 size_t bytes =
sizeof(T) * n_items;
95 char *c_buffer = (
char *)
buffer_;
103 ASSERT(!
full_data_).error(
"Allocation of new data is not possible because child arena was created.");
107 void* p =
resource_.allocate(bytes, alignment);
109 }
catch ( std::bad_alloc& ) {
110 THROW( ExcArenaAllocation() );
122 upstream_->deallocate(p, bytes, alignment);
126 bool do_is_equal(
const std::pmr::memory_resource& other)
const noexcept
override {
127 return this == &other;
139 template <
class Resource>
144 :
PatchArenaResource<Resource>(
std::pmr::get_default_resource()->allocate(buffer_size, simd_alignment), buffer_size, simd_alignment, upstream ) {}
158 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
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.
DECLARE_EXCEPTION(ExcArenaAllocation,<< "Allocation of ArenaResource failed. Please check if correct type of upstream is used.")
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)
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.