nlib
nn::nlib::LockFreeUnitHeap Class Referencefinal

A pool allocator that can allocate or free a fixed size of memory region in a lock free manner. More...

#include "nn/nlib/LockFree.h"

Public Member Functions

 LockFreeUnitHeap () noexcept
 Instantiates the object with default parameters (default constructor).
 
errno_t Init (size_t unit_size, size_t align, size_t count) noexcept
 Initializes an allocator. This is not thread-safe. More...
 
 ~LockFreeUnitHeap () noexcept
 Destructor.
 
MemHolder * Alloc () noexcept
 Allocates memory. The allocated region can be obtained by executing Get() on the returned pointer to the object. This is thread-safe. More...
 
void Free (MemHolder *p) noexcept
 Frees memory. This is thread-safe. More...
 
MemHolder * AllocUnsafe () noexcept
 This function is similar to Alloc(), but not thread-safe. More...
 
void FreeUnsafe (MemHolder *p) noexcept
 This function is similar to Free(), but not thread-safe. More...
 

Detailed Description

A pool allocator that can allocate or free a fixed size of memory region in a lock free manner.

Description
While, in general, threads may be internally blocked with functions including malloc() and free(), LockFreeUnitHeap can allocate or free memory without a possibility of threads to be blocked. Using this class may be considered when threads are not allowed to be blocked or a relatively large size of memory needs to be dynamically allocated or freed.
Sample code
// Can allocate 32 bytes aligned, 1024 memory blocks from heap
e = heap.Init(sizeof(MyStructure), 32, 1024);
if (e != 0) {
// ERROR
}
.....
p = heap.Alloc();
if (!p) {
// ERROR
}
// Note that p is not what you want. p->Get() is it.
MyStructure* s = reinterpret_cast<MyStructure*>(p->Get());
.....
heap.Free(p);

Definition at line 769 of file LockFree.h.

Member Function Documentation

nn::nlib::LockFreeUnitHeap::Alloc ( )
inlinenoexcept

Allocates memory. The allocated region can be obtained by executing Get() on the returned pointer to the object. This is thread-safe.

Returns
A non-NULL pointer when the allocation was successful.

Definition at line 787 of file LockFree.h.

nn::nlib::LockFreeUnitHeap::AllocUnsafe ( )
inlinenoexcept

This function is similar to Alloc(), but not thread-safe.

Returns
A non-NULL pointer when the allocation was successful.

Definition at line 798 of file LockFree.h.

nn::nlib::LockFreeUnitHeap::Free ( MemHolder *  p)
inlinenoexcept

Frees memory. This is thread-safe.

Parameters
[in]pA return value of Alloc().

Definition at line 791 of file LockFree.h.

nn::nlib::LockFreeUnitHeap::FreeUnsafe ( MemHolder *  p)
inlinenoexcept

This function is similar to Free(), but not thread-safe.

Parameters
[in]pA return value of Alloc().

Definition at line 802 of file LockFree.h.

nn::nlib::LockFreeUnitHeap::Init ( size_t  unit_size,
size_t  align,
size_t  count 
)
noexcept

Initializes an allocator. This is not thread-safe.

Parameters
[in]unit_sizeThe size of each region to be allocated.
[in]alignThe alignment of the region to be allocated.
[in]countThe maximum number of regions that can be allocated.
Return values
0Success.
ENOMEMMemory allocation has failed.
EINVALThe alignment was not a power of two.

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