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. 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...
 
void SwapUnsafe (LockFreeUnitHeap &rhs) noexcept
 Swaps an object. This is not thread-safe.
 

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 (nlib_is_error(e)) {
// 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 812 of file LockFree.h.

Member Function Documentation

◆ Alloc()

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

Allocates memory. This is thread-safe.

Returns
A non-NULL pointer when the allocation was successful.
Description
The return value type (MemHolder) is an internal class of this class and can be used only by the Get() member function. The allocated region can be obtained by executing Get() on the returned pointer to the object and obtaining the return value.

Definition at line 830 of file LockFree.h.

◆ AllocUnsafe()

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 841 of file LockFree.h.

◆ Free()

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

Frees memory. This is thread-safe.

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

Definition at line 834 of file LockFree.h.

◆ FreeUnsafe()

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 845 of file LockFree.h.

◆ Init()

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: