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"
|
| 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...
|
|
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);
if (e != 0) {
}
.....
p = heap.Alloc();
if (!p) {
}
MyStructure* s = reinterpret_cast<MyStructure*>(p->Get());
.....
heap.Free(p);
Definition at line 769 of file LockFree.h.
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
-
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
-
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_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: