Template Class static_storage

Class Documentation

template<uint64_t Capacity, uint64_t Align = 1>
class static_storage

Static storage class to allocate memory for objects of type not yet known. This storage is not aware of any underlying type. It can be used where abstract static memory for some object is required. Currently this memory is allocated on the stack but it could be implemented to use memory from the static memory segment.

Note

We can define optimized_storage (or dynamic_storage) with a similar interface but other allocation policies and use them where we need to store objects with some interchangable storage policy (e.g. in storable_function) optimized_storage would have a dynamic memory fallback when static memory is insufficent.

Template Parameters:
  • Capacity – number of bytes the static_storage will allocate statically.

  • Align – alignment of the allocated memory.

Public Functions

constexpr static_storage() noexcept = default
~static_storage() noexcept
static_storage(const static_storage&) = delete

Note

: It is not supposed to be copied or moved for now (construct a new one explicitly and populate it instead). Move would just be a copy and copy has the problem that it would just can perform a memcpy regardless of what is stored leading to potentially missed dtor calls

static_storage &operator=(const static_storage&) = delete
static_storage(static_storage&&) = delete
static_storage &operator=(static_storage&&) = delete
template<typename T>
constexpr T *allocate() noexcept

provide static memory for an object of type T

Note

compilation fails if storage is insufficient for objects of type T

Returns:

pointer to memory where a T can be constructed if memory is available, nullptr otherwise

constexpr void *allocate(const uint64_t align, const uint64_t size) noexcept

request aligned memory with a specific size in bytes

Parameters:
  • align – alignment of the memory requested

  • size – number of bytes of the memory requested

Returns:

pointer to aligned memory of the requested size if available, nullptr otherwise

constexpr void deallocate() noexcept

mark the static memory as unused

Note

no dtor of the stored type is called (we do not know the type) nor is it overwritten. Setting the memory to zero can be done with clear.

constexpr bool clear() noexcept

set the managed static memory to all zeros if there is no object currently stored

Returns:

true if the memory was set to zero, false otherwise

Public Static Functions

template<typename T>
static constexpr bool is_allocatable() noexcept

check whether the type T will fit in the buffer

Note

can be checked at compile time

Returns:

true if the type fits in the buffer, false otherwise

static constexpr uint64_t capacity() noexcept

get the storage capacity in bytes

Note

this is an upper bound for the object size it can store

Returns:

maximum number of bytes available in the static storage

template<typename T>
static constexpr uint64_t allocation_size() noexcept

return the number of bytes that need to be allocated to store a T in this storage class

Note

the returned size s satisfies sizeof(T) <= s < sizeof(T) + alignof(T)

Returns:

number of bytes the storage will allocate if we allocate a T