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

A stack-based memory resource that allocates memory in a LIFO order from a fixed buffer. Deallocation must happen in reverse order of allocation. Throws std::bad_alloc if there is insufficient space for an allocation. More...

#include <flow_stack_memory_resource.h>

Inheritance diagram for flow::StackMemoryResource:

Classes

struct  Header

Public Member Functions

 StackMemoryResource (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 stack-based memory resource that allocates memory in a LIFO order from a fixed buffer. Deallocation must happen in reverse order of allocation. Throws std::bad_alloc if there is insufficient space for an allocation.

Definition at line 14 of file flow_stack_memory_resource.h.

Constructor & Destructor Documentation

◆ StackMemoryResource()

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

Definition at line 16 of file flow_stack_memory_resource.h.

Member Function Documentation

◆ allocateImp()

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

Implements flow::MemoryResource.

Definition at line 28 of file flow_stack_memory_resource.h.

28 {
29 void* oldBuffer = buffer_;
30
31 void* alignedHeader = flow::alignWithHeader<Header>(alignment, bytes, buffer_, capacity_);
32 if (!alignedHeader) {
33 throw std::bad_alloc();
34 }
35
36 new (alignedHeader) Header(oldBuffer);
37 void* allocatedBlock = reinterpret_cast<Header*>(alignedHeader) + 1;
38 buffer_ = reinterpret_cast<std::byte*>(allocatedBlock) + bytes;
39 capacity_ -= sizeof(Header) + bytes;
40 return allocatedBlock;
41 }
Header * alignWithHeader(std::size_t alignment, std::size_t size, void *&buffer, std::size_t &capacity) noexcept
Align the header + buffer to their corresponding alignments. If the capacity is not big enough,...

References flow::alignWithHeader(), buffer_, and capacity_.

◆ deallocateImp()

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

Implements flow::MemoryResource.

Definition at line 43 of file flow_stack_memory_resource.h.

43 {
44 // TODO: assert address is the top most valid address
45 assert(address == nullptr || address <= buffer_);
46 if (!address) {
47 return;
48 }
49
50 Header* header = reinterpret_cast<Header*>(address) - 1;
51 std::size_t padding = pointerDistance(header->oldBuffer, header);
52 buffer_ = header->oldBuffer;
53 capacity_ += padding + sizeof(Header) + bytes;
54 header->~Header();
55 }
std::size_t pointerDistance(const T *first, const U *last)
Calculate the distance in bytes from the first pointer to the last pointer.

References buffer_, capacity_, flow::StackMemoryResource::Header::oldBuffer, and flow::pointerDistance().

Member Data Documentation

◆ buffer_

void* flow::StackMemoryResource::buffer_
protected

Definition at line 25 of file flow_stack_memory_resource.h.

Referenced by allocateImp(), and deallocateImp().

◆ capacity_

std::size_t flow::StackMemoryResource::capacity_
protected

Definition at line 26 of file flow_stack_memory_resource.h.

Referenced by allocateImp(), and deallocateImp().


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