Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow::ArenaMemoryResource Class Reference

A linear arena memory resource that allocates memory sequentially from a fixed buffer. Throws std::bad_alloc if there is insufficient space for the requested allocation. More...

#include <flow_arena_memory_resource.h>

Inheritance diagram for flow::ArenaMemoryResource:

Public Member Functions

 ArenaMemoryResource (void *buffer, std::size_t capacity) noexcept
Public Member Functions inherited from flow::MemoryResource
virtual ~MemoryResource ()=default
void * allocate (std::size_t bytes, std::size_t alignment=alignof(std::max_align_t))
void deallocate (void *address, std::size_t bytes, std::size_t alignment=alignof(std::max_align_t))

Protected Member Functions

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

Protected Attributes

void * buffer_
std::size_t capacity_

Detailed Description

A linear arena memory resource that allocates memory sequentially from a fixed buffer. Throws std::bad_alloc if there is insufficient space for the requested allocation.

Definition at line 13 of file flow_arena_memory_resource.h.

Constructor & Destructor Documentation

◆ ArenaMemoryResource()

flow::ArenaMemoryResource::ArenaMemoryResource ( void * buffer,
std::size_t capacity )
inlineexplicitnoexcept

Definition at line 15 of file flow_arena_memory_resource.h.

16 : buffer_(buffer), capacity_(capacity) {
17#ifdef _DEBUG
18 beginBuffer_ = buffer;
19#endif
20 }

Member Function Documentation

◆ allocateImp()

virtual void * flow::ArenaMemoryResource::allocateImp ( std::size_t bytes,
std::size_t alignment )
inlineoverrideprotectedvirtual

Implements flow::MemoryResource.

Definition at line 29 of file flow_arena_memory_resource.h.

29 {
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 }

References buffer_, and capacity_.

◆ deallocateImp()

virtual void flow::ArenaMemoryResource::deallocateImp ( void * address,
std::size_t bytes,
std::size_t alignment )
inlineoverrideprotectedvirtual

Implements flow::MemoryResource.

Definition at line 40 of file flow_arena_memory_resource.h.

43 {
44 assert(address == nullptr || (beginBuffer_ <= address && address <= buffer_));
45 }

References buffer_.

Member Data Documentation

◆ buffer_

void* flow::ArenaMemoryResource::buffer_
protected

Definition at line 26 of file flow_arena_memory_resource.h.

Referenced by allocateImp(), and deallocateImp().

◆ capacity_

std::size_t flow::ArenaMemoryResource::capacity_
protected

Definition at line 27 of file flow_arena_memory_resource.h.

Referenced by allocateImp().


The documentation for this class was generated from the following file: