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"
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
e = heap.Init(sizeof(MyStructure), 32, 1024);
}
.....
p = heap.Alloc();
if (!p) {
}
MyStructure* s = reinterpret_cast<MyStructure*>(p->Get());
.....
heap.Free(p);
Definition at line 812 of file LockFree.h.
◆ 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
-
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
-
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_size | The size of each region to be allocated. |
[in] | align | The alignment of the region to be allocated. |
[in] | count | The maximum number of regions that can be allocated. |
- Return values
-
0 | Success. |
ENOMEM | Memory allocation has failed. |
EINVAL | The alignment was not a power of two. |
The documentation for this class was generated from the following files: