bn::best_fit_allocator class

Manages a chunk of memory with a best fit allocation strategy.

Public types

using size_type = int
Size type alias.

Constructors, destructors, conversion operators

best_fit_allocator() defaulted
Default constructor.
best_fit_allocator(void* start, size_type bytes)
Constructor.
~best_fit_allocator() noexcept
Destructor.

Public functions

auto used_bytes() const -> size_type
Returns the size in bytes of all allocated items.
auto available_bytes() const -> size_type
Returns the number of bytes that still can be allocated.
auto empty() const -> bool
Indicates if it doesn't contain any item.
auto full() const -> bool
Indicates if it can't contain any more items.
auto alloc(size_type bytes) -> void*
Allocates uninitialized storage.
auto calloc(size_type num, size_type bytes) -> void*
Allocates storage for an array of num objects of bytes size and initializes all bytes in it to zero.
auto realloc(void* ptr, size_type new_bytes) -> void*
Reallocates the given storage.
void free(void* ptr)
Deallocates the storage previously allocated by alloc, calloc or realloc.
template<typename Type, typename... Args>
auto create(Args && ... args) -> Type&
Constructs a value inside of the allocator.
template<typename Type>
void destroy(Type& value)
Destroys the given value, previously allocated with the create method.
void reset(void* start, size_type bytes)
Setups the allocator to manage a new chunk of memory.
void log_status() const
Logs the current status of the allocator.

Function documentation

bn::best_fit_allocator::best_fit_allocator(void* start, size_type bytes)

Constructor.

Parameters
start Pointer to the first element of the memory to manage.
bytes Size in bytes of the memory to manage.

bn::best_fit_allocator::~best_fit_allocator() noexcept

Destructor.

It doesn't destroy its elements, they must be destroyed manually.

void* bn::best_fit_allocator::alloc(size_type bytes)

Allocates uninitialized storage.

Parameters
bytes Bytes to allocate.
Returns On success, returns the pointer to the beginning of newly allocated memory. On failure, returns nullptr.

To avoid a memory leak, the returned pointer must be deallocated with free.

void* bn::best_fit_allocator::calloc(size_type num, size_type bytes)

Allocates storage for an array of num objects of bytes size and initializes all bytes in it to zero.

Parameters
num Number of objects.
bytes Size in bytes of each object.
Returns On success, returns the pointer to the beginning of newly allocated memory. On failure, returns nullptr.

To avoid a memory leak, the returned pointer must be deallocated with free.

void* bn::best_fit_allocator::realloc(void* ptr, size_type new_bytes)

Reallocates the given storage.

Parameters
ptr Pointer to the storage to reallocate.
new_bytes New size in bytes of the reallocated storage.
Returns On success, returns the pointer to the beginning of newly allocated storage. On failure, returns nullptr.

If ptr was not previously allocated by alloc, calloc or realloc, the behavior is undefined.

On success, the original pointer ptr is invalidated and any access to it is undefined behavior (even if reallocation was in-place).

To avoid a memory leak, the returned pointer must be deallocated with free.

void bn::best_fit_allocator::free(void* ptr)

Deallocates the storage previously allocated by alloc, calloc or realloc.

Parameters
ptr Pointer to the storage to deallocate. It is invalidated and any access to it is undefined behavior.

If ptr is nullptr, the function does nothing.

If ptr was not previously allocated by alloc, calloc or realloc, the behavior is undefined.

template<typename Type, typename... Args>
Type& bn::best_fit_allocator::create(Args && ... args)

Constructs a value inside of the allocator.

Parameters
args Parameters of the value to construct.
Returns Reference to the new value.

void bn::best_fit_allocator::reset(void* start, size_type bytes)

Setups the allocator to manage a new chunk of memory.

Parameters
start Pointer to the first element of the memory to manage.
bytes Size in bytes of the memory to manage.