nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
Data Structures | Public Types | Public Member Functions | Static Public Attributes
nrfcxx::utility::memory_pool< ALIGN_BITS > Class Template Reference

Class supporting allocation from a fixed-size region. More...

#include <nrfcxx/utility.hpp>

Data Structures

class  scoped
 RAII class to maintain a shared memory pool within a block scope. More...
 

Public Types

using pool_type = memory_pool< ALIGN_BITS >
 

Public Member Functions

scoped make_scoped ()
 Construct an RAII holder that maintains the instance as a scratch pool. More...
 
constexpr memory_pool ()
 Construct the instance. More...
 
void configure (void *begin, void *end, unsigned int code=FailSafeCode::MEMORY_POOL)
 Provide the allocation region and failsafe code. More...
 
void clear ()
 Release all allocations from the pool. More...
 
size_t capacity () const
 Return the total number of bytes in the region (allocated plus unallocated).
 
size_t size () const
 Return the number of allocated bytes in the region.
 
size_t available () const
 Return the number of unallocated bytes in the region.
 
void * allocate (size_t span)
 Allocate a block of at least span bytes. More...
 

Static Public Attributes

static constexpr unsigned int align_bits = ALIGN_BITS
 The number of bits used for alignment of the return pointers.
 

Detailed Description

template<unsigned int ALIGN_BITS = 2>
class nrfcxx::utility::memory_pool< ALIGN_BITS >

Class supporting allocation from a fixed-size region.

This capability is intended to support allocation of aligned variable-sized blocks of memory from an externally supplied region. A prime use case is populating runtime state structures corresponding to enabled capabilities when an application is being initialized.

Allocation is done from a region that is supplied externally during configuration. Attempts to allocate more space than is available will result in a fail-safe reset using a code provided along with the region. API is available to release all allocations from the region at once, but individual allocations cannot be released.

Template Parameters
ALIGN_BITSinitializes align_bits.

Constructor & Destructor Documentation

◆ memory_pool()

template<unsigned int ALIGN_BITS = 2>
constexpr nrfcxx::utility::memory_pool< ALIGN_BITS >::memory_pool ( )
inlineconstexpr

Construct the instance.

A newly-constructed instance does not have an associated region. configure() must be called before allocate() is invoked. Violation of this requirement will produce a failsafe reset.

Member Function Documentation

◆ allocate()

template<unsigned int ALIGN_BITS = 2>
void* nrfcxx::utility::memory_pool< ALIGN_BITS >::allocate ( size_t  span)
inline

Allocate a block of at least span bytes.

If the allocation cannot be satisfied the system will failsafe reset using the code passed in configure().

Returns
A pointer to an align_bits-bit aligned block of memory.

◆ clear()

template<unsigned int ALIGN_BITS = 2>
void nrfcxx::utility::memory_pool< ALIGN_BITS >::clear ( )
inline

Release all allocations from the pool.

After invoking this pointers returned from allocate() are invalidated and must not be dereferenced.

◆ configure()

template<unsigned int ALIGN_BITS = 2>
void nrfcxx::utility::memory_pool< ALIGN_BITS >::configure ( void *  begin,
void *  end,
unsigned int  code = FailSafeCode::MEMORY_POOL 
)
inline

Provide the allocation region and failsafe code.

This method must be invoked before allocate().

Parameters
begina pointer to the start of the space available for allocation.
enda pointer just past the end of the space available for allocation.
codethe code value to pass to failsafe() if an unsatisfiable allocation is made.

◆ make_scoped()

template<unsigned int ALIGN_BITS = 2>
scoped nrfcxx::utility::memory_pool< ALIGN_BITS >::make_scoped ( )
inline

Construct an RAII holder that maintains the instance as a scratch pool.

The system will failsafe if the pool has allocations when the holder is constructed.

The pool will be cleared when the holder is destructed.


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