FreeRTOS C++ Wrappers
1.6.0
C++ interface to FreeRTOS
|
#include <mem_pool.hpp>
Public Member Functions | |
MemoryPool (int itemSize, int itemCount, int alignment) | |
MemoryPool (int itemSize, void *preallocatedMemory, int preallocatedMemorySize, int alignment) | |
void | AddMemory (int itemCount) |
void | AddMemory (void *preallocatedMemory, int preallocatedMemorySize) |
void * | Allocate () |
void | Free (void *item) |
Private Member Functions | |
void | CalculateValidAlignment () |
void | CalculateItemSize () |
~MemoryPool () | |
Private Attributes | |
Mutex * | Lock |
int | ItemSize |
int | Alignment |
std::list< void * > | FreeItems |
Memory Pools are fixed size allocations to prevent fragmentation.
This is a new feature to FreeRTOS Wrappers and is not in and of itself a wrapper.
Memory Pools are thread safe, but cannot be used in ISR context. The OS must be running, because these use Mutexes to protect internal data structures.
Definition at line 140 of file mem_pool.hpp.
MemoryPool::MemoryPool | ( | int | itemSize, |
int | itemCount, | ||
int | alignment | ||
) |
Constructor to create a Memory Pool.
This constructor uses the system malloc to actually obtain the memory.
itemSize | How big is each item you want to allocate. |
itemCount | How many items max do you want to allocate at once. |
Alignment | Power of 2 value denoting on which address boundary the memory will be aligned to. Must be at least sizeof(unsigned char *). |
MemoryPoolMallocException | on failure. |
MemoryPoolBadAlignmentException | on failure. |
Definition at line 96 of file cmem_pool.cpp.
MemoryPool::MemoryPool | ( | int | itemSize, |
void * | preallocatedMemory, | ||
int | preallocatedMemorySize, | ||
int | alignment | ||
) |
Constructor to create a Memory Pool.
This constructor uses memory you pass in to actually create the pool. This constructor does not throw.
itemSize | How big is each item you want to allocate. |
preallocatedMemory | Pointer to the preallocated memory you are dedicating to this pool. |
preallocatedMemorySize | How big is the buffer you are passing in. |
Alignment | Power of 2 value denoting on which address boundary the memory will be aligned to. Must be at least sizeof(unsigned char *). |
MemoryPoolBadAlignmentException | on failure. |
Definition at line 125 of file cmem_pool.cpp.
|
private |
To correctly delete a Memory Pool, we'd have to guarantee that all allocations had been returned to us. We side step this issue by making the destructor private so it can't be accessed.
void MemoryPool::AddMemory | ( | int | itemCount | ) |
Allows you to add memory to a MemoryPool.
Items will be the same size as you initially asked for.
itemCount | How many more items max do you want to allocate |
MemoryPoolMallocException | on failure. |
Definition at line 170 of file cmem_pool.cpp.
void MemoryPool::AddMemory | ( | void * | preallocatedMemory, |
int | preallocatedMemorySize | ||
) |
Allows you to add memory to a MemoryPool.
Items will be the same size as you initially asked for.
preallocatedMemory | Pointer to the preallocated memory you are dedicating to this pool. |
preallocatedMemorySize | How big is the buffer you are passing in. |
Definition at line 192 of file cmem_pool.cpp.
void * MemoryPool::Allocate | ( | ) |
Allocate an item from the pool.
Definition at line 149 of file cmem_pool.cpp.
|
private |
Calculate the true item size, based on alignment.
Definition at line 78 of file cmem_pool.cpp.
|
private |
Adjusts and validates the alignment argument passed in the ctor.
Guarantee that the alignment is the size of a pointer.
Definition at line 49 of file cmem_pool.cpp.
void MemoryPool::Free | ( | void * | item | ) |
Returns the item back to it's pool.
Definition at line 163 of file cmem_pool.cpp.
|
private |
The overall alignment of an item.
Definition at line 246 of file mem_pool.hpp.
|
private |
All of the real work is done with STL lists.
Definition at line 251 of file mem_pool.hpp.
|
private |
Save the item size for additions.
Definition at line 241 of file mem_pool.hpp.
|
private |
Standard Mutex to allow thread safety.
Definition at line 236 of file mem_pool.hpp.