ecor
Loading...
Searching...
No Matches
ecor::circular_buffer_memory< IndexType, Base > Struct Template Reference

Circular buffer memory resource, manages a provided memory block as a circular buffer for dynamic allocations. More...

#include <ecor.hpp>

Classes

struct  _deleter
 
struct  _node
 Internal node structure for tracking allocated blocks. More...
 

Public Types

using index_type = IndexType
 Type of the index used for tracking allocations.
 
template<typename T >
using uptr = std::unique_ptr< T, _deleter >
 Unique pointer type for objects allocated from this memory resource, with automatic deallocation.
 
template<typename T , std::size_t Extent = std::dynamic_extent>
using uspan = unique_span< T, Extent, _deleter >
 Unique span type for arrays allocated from this memory resource, with automatic deallocation.
 

Public Member Functions

void * allocate (std::size_t bytes, std::size_t align) noexcept
 Allocate bytes with align, returns nullptr if no space is available. More...
 
std::size_t capacity () const noexcept
 Get total capacity of the buffer in bytes.
 
template<std::size_t N>
requires ( std::numeric_limits< IndexType >::max() >= N )
constexpr circular_buffer_memory (std::span< uint8_t, N > b) noexcept
 
template<std::size_t N>
requires ( std::numeric_limits< IndexType >::max() >= N )
constexpr circular_buffer_memory (uint8_t(&b)[N]) noexcept
 
void deallocate (void *p) noexcept
 Deallocate pointer previously allocated by allocate(), overload without size and alignment. More...
 
void deallocate (void *p, std::size_t bytes, std::size_t align) noexcept
 Deallocate pointer previously allocated by allocate()
 
template<typename T , typename... Args>
uptr< T > make (Args &&... args)
 Allocate and construct an object of type T with given arguments, returning a unique pointer that manages the allocated object. More...
 
template<typename T , std::size_t Extent>
uspan< T, Extent > make_span ()
 Overload for fixed-size arrays, deduces the size from the template parameter. More...
 
template<typename T >
uspan< T > make_span (std::size_t n)
 Allocate and construct an array of type T with given size, returning a unique span that manages the allocated array. More...
 
std::size_t used_bytes () const noexcept
 Get total used bytes in the buffer.
 

Static Public Attributes

static constexpr index_type npos = std::numeric_limits< index_type >::max()
 Special value indicating no position.
 

Detailed Description

template<typename IndexType, typename Base = noop_base>
struct ecor::circular_buffer_memory< IndexType, Base >

Circular buffer memory resource, manages a provided memory block as a circular buffer for dynamic allocations.

Uses an index-based linked list to track allocated blocks.

The buffer is treated as a circular space, and allocations are made by finding a suitable space after the last allocated block. Deallocations mark blocks as free, and the allocator can reuse freed space for future allocations. The internal linked list structure allows for efficient tracking of allocated blocks and free space.

Note that if first block is allocated and is not freed, the buffer is considered full, even if there is free space between first block and the last block.

The template parameters are:

  • IndexType: An unsigned integer type used for indexing into the buffer. The maximum capacity of the buffer is limited by the maximum value of this type. The implementation provides a helper to automatically select an appropriate index type based on the buffer size.
  • Base: An optional base class that can be used for customization. This allows the user to pick an interface used for the memory resource.

Member Function Documentation

◆ allocate()

template<typename IndexType , typename Base = noop_base>
void * ecor::circular_buffer_memory< IndexType, Base >::allocate ( std::size_t  bytes,
std::size_t  align 
)
inlinenoexcept

Allocate bytes with align, returns nullptr if no space is available.

The returned pointer is valid until the memory resource is destroyed or the memory is deallocated.

User is expected to call deallocate with the same size and alignment when the memory is no longer needed.

◆ deallocate()

template<typename IndexType , typename Base = noop_base>
void ecor::circular_buffer_memory< IndexType, Base >::deallocate ( void *  p)
inlinenoexcept

Deallocate pointer previously allocated by allocate(), overload without size and alignment.

This is provided for convenience.

◆ make()

template<typename IndexType , typename Base = noop_base>
template<typename T , typename... Args>
uptr< T > ecor::circular_buffer_memory< IndexType, Base >::make ( Args &&...  args)
inline

Allocate and construct an object of type T with given arguments, returning a unique pointer that manages the allocated object.

The memory for the object is allocated from this memory resource, and will be automatically deallocated when the unique pointer is destroyed.

Undefined behavior if circular_buffer_memory ends lifetime before the unique pointer.

◆ make_span() [1/2]

template<typename IndexType , typename Base = noop_base>
template<typename T , std::size_t Extent>
uspan< T, Extent > ecor::circular_buffer_memory< IndexType, Base >::make_span ( )
inline

Overload for fixed-size arrays, deduces the size from the template parameter.

Rest is same as for the other make_span.

◆ make_span() [2/2]

template<typename IndexType , typename Base = noop_base>
template<typename T >
uspan< T > ecor::circular_buffer_memory< IndexType, Base >::make_span ( std::size_t  n)
inline

Allocate and construct an array of type T with given size, returning a unique span that manages the allocated array.

The memory for the array is allocated from this memory resource, and will be automatically deallocated when the unique span is destroyed.

Undefined behavior if circular_buffer_memory ends lifetime before the unique span.


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