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

A pool memory resource that manages fixed-size memory blocks from a pre-allocated buffer. The allocation size must be less or equal to the block size. The allocation alignment must be less or equal to the block alignment. Throws std::bad_alloc if the constraint is not met or run out of memory. More...

#include <flow_pool_memory_resource.h>

Inheritance diagram for flow::PoolMemoryResource:

Classes

struct  Header

Public Member Functions

 PoolMemoryResource (void *buffer, std::size_t capacity, std::size_t blockSize, std::size_t blockAlignment=sizeof(std::max_align_t))
 ~PoolMemoryResource ()
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

std::size_t blockSize_
std::size_t blockAlignment_
Headerhead_

Detailed Description

A pool memory resource that manages fixed-size memory blocks from a pre-allocated buffer. The allocation size must be less or equal to the block size. The allocation alignment must be less or equal to the block alignment. Throws std::bad_alloc if the constraint is not met or run out of memory.

Definition at line 14 of file flow_pool_memory_resource.h.

Constructor & Destructor Documentation

◆ PoolMemoryResource()

flow::PoolMemoryResource::PoolMemoryResource ( void * buffer,
std::size_t capacity,
std::size_t blockSize,
std::size_t blockAlignment = sizeof(std::max_align_t) )
inline

Definition at line 16 of file flow_pool_memory_resource.h.

17 : blockSize_(blockSize), blockAlignment_(blockAlignment), head_(nullptr) {
18
19 Header** headPtr = &head_;
20 for (;;) {
21 Header* header = alignWithHeader<Header>(blockAlignment_, blockSize_, buffer, capacity);
22 if (!header) {
23 break;
24 }
25
26 new (header) Header(nullptr);
27 *headPtr = header;
28 headPtr = &(header->next);
29
30 buffer = reinterpret_cast<std::byte*>(buffer) + sizeof(Header) + blockSize;
31 capacity -= sizeof(Header) + blockSize;
32 }
33 }
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(), blockAlignment_, blockSize_, head_, and flow::PoolMemoryResource::Header::next.

◆ ~PoolMemoryResource()

flow::PoolMemoryResource::~PoolMemoryResource ( )
inline

Definition at line 35 of file flow_pool_memory_resource.h.

35 {
36 // May be useless, since it has a trivial destructor.
37 while (head_) {
38 Header* next = head_->next;
39 head_->~Header();
40 head_ = next;
41 }
42 }

References head_.

Member Function Documentation

◆ allocateImp()

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

Implements flow::MemoryResource.

Definition at line 53 of file flow_pool_memory_resource.h.

53 {
54 if (blockSize_ < bytes || blockAlignment_ < alignment || !head_) {
55 throw std::bad_alloc();
56 }
57
58 void* allocatedBlock = head_ + 1;
59 head_ = head_->next;
60 return allocatedBlock;
61 }

References blockAlignment_, blockSize_, and head_.

◆ deallocateImp()

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

Implements flow::MemoryResource.

Definition at line 63 of file flow_pool_memory_resource.h.

66 {
67 Header* header = reinterpret_cast<Header*>(address) - 1;
68 header->next = head_;
69 head_ = header;
70 }

References head_, and flow::PoolMemoryResource::Header::next.

Member Data Documentation

◆ blockAlignment_

std::size_t flow::PoolMemoryResource::blockAlignment_
protected

Definition at line 50 of file flow_pool_memory_resource.h.

Referenced by allocateImp(), and PoolMemoryResource().

◆ blockSize_

std::size_t flow::PoolMemoryResource::blockSize_
protected

Definition at line 49 of file flow_pool_memory_resource.h.

Referenced by allocateImp(), and PoolMemoryResource().

◆ head_

Header* flow::PoolMemoryResource::head_
protected

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