A lock-free (not wait-free) statically-sized free-list implemented with CAS. More...
#include <free_list.h>
Public Member Functions | |
void * | allocate () |
Allocate a single block from this FreeList. More... | |
template<typename T > | |
void | constructAll (const T &tmpl) |
Construct all the blocks with a specific template. If you call this you must call destructAll() prior to destroying this FreeList. This is mainly for use by the ObjectPool class, or for writing a similar class, and generally that class should be used instead of FreeList directly. More... | |
template<typename T > | |
void | constructAll () |
Construct all the blocks with a default constructor. If you call this you must call destructAll() prior to destroying this FreeList. This is mainly for use by the ObjectPool class, or for writing a similar class, and generally that class should be used instead of FreeList directly. More... | |
template<typename T > | |
void | destructAll () |
Destruct all the objects in this FreeList. You must have called constructAll() first. More... | |
void | free (void const *mem) |
Free a block of memory allocated from this FreeList. More... | |
FreeList () | |
Default constructor. You must call initialize() if you use this constructor. More... | |
FreeList (uint32_t block_size, uint32_t block_count) | |
Constructor with initialization. More... | |
bool | hasOutstandingAllocations () |
Returns whether or not this FreeList currently has any outstanding allocations. More... | |
void | initialize (uint32_t block_size, uint32_t block_count) |
Initialize this FreeList. Only use if you used to default constructor. More... | |
bool | owns (void const *mem) |
Returns whether or not this FreeList owns a block of memory. More... | |
~FreeList () | |
Private Member Functions | |
uint32_t | getTag (uint64_t val) |
uint32_t | getVal (uint64_t val) |
void | setTag (uint64_t &val, uint32_t tag) |
void | setVal (uint64_t &val, uint32_t v) |
Private Attributes | |
ros::atomic_uint32_t | alloc_count_ |
uint32_t | block_count_ |
uint32_t | block_size_ |
uint8_t * | blocks_ |
ros::atomic_uint64_t | head_ |
ros::atomic_uint32_t * | next_ |
A lock-free (not wait-free) statically-sized free-list implemented with CAS.
FreeList is implemented as a forward-linked list using indices instead of pointers. The array of blocks is allocated separately from the array of next indices, and both are always allocated aligned to a cache line.
Indices are stored as 32-bits with a 64-bit head index whose upper 32-bits are tagged to avoid ABA problems
Definition at line 67 of file free_list.h.
lockfree::FreeList::FreeList | ( | ) |
Default constructor. You must call initialize() if you use this constructor.
Definition at line 44 of file free_list.cpp.
lockfree::FreeList::FreeList | ( | uint32_t | block_size, |
uint32_t | block_count | ||
) |
Constructor with initialization.
block_size | The size of each block allocate() will return |
block_count | The number of blocks to allocate |
Definition at line 52 of file free_list.cpp.
lockfree::FreeList::~FreeList | ( | ) |
Definition at line 61 of file free_list.cpp.
void * lockfree::FreeList::allocate | ( | ) |
Allocate a single block from this FreeList.
Definition at line 108 of file free_list.cpp.
|
inline |
Construct all the blocks with a specific template. If you call this you must call destructAll() prior to destroying this FreeList. This is mainly for use by the ObjectPool class, or for writing a similar class, and generally that class should be used instead of FreeList directly.
tmpl | The object template to use |
Definition at line 121 of file free_list.h.
|
inline |
Construct all the blocks with a default constructor. If you call this you must call destructAll() prior to destroying this FreeList. This is mainly for use by the ObjectPool class, or for writing a similar class, and generally that class should be used instead of FreeList directly.
Definition at line 138 of file free_list.h.
|
inline |
Destruct all the objects in this FreeList. You must have called constructAll() first.
Definition at line 152 of file free_list.h.
void lockfree::FreeList::free | ( | void const * | mem | ) |
Free a block of memory allocated from this FreeList.
mem | The block to be freed |
Definition at line 170 of file free_list.cpp.
|
inlineprivate |
Definition at line 171 of file free_list.h.
|
inlineprivate |
Definition at line 176 of file free_list.h.
bool lockfree::FreeList::hasOutstandingAllocations | ( | ) |
Returns whether or not this FreeList currently has any outstanding allocations.
Definition at line 103 of file free_list.cpp.
void lockfree::FreeList::initialize | ( | uint32_t | block_size, |
uint32_t | block_count | ||
) |
Initialize this FreeList. Only use if you used to default constructor.
block_size | The size of each block allocate() will return |
block_count | The number of blocks to allocate |
Definition at line 72 of file free_list.cpp.
bool lockfree::FreeList::owns | ( | void const * | mem | ) |
Returns whether or not this FreeList owns a block of memory.
mem | the block to check |
Definition at line 239 of file free_list.cpp.
|
inlineprivate |
Definition at line 181 of file free_list.h.
|
inlineprivate |
Definition at line 186 of file free_list.h.
|
private |
Definition at line 194 of file free_list.h.
|
private |
Definition at line 197 of file free_list.h.
|
private |
Definition at line 196 of file free_list.h.
|
private |
Definition at line 191 of file free_list.h.
|
private |
Definition at line 193 of file free_list.h.
|
private |
Definition at line 192 of file free_list.h.