FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
Public Member Functions | Private Member Functions | Private Attributes | List of all members
cpp_freertos::MemoryPool Class Reference

#include <mem_pool.hpp>

Collaboration diagram for cpp_freertos::MemoryPool:
Collaboration graph
[legend]

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

MutexLock
 
int ItemSize
 
int Alignment
 
std::list< void * > FreeItems
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ MemoryPool() [1/2]

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.

Parameters
itemSizeHow big is each item you want to allocate.
itemCountHow many items max do you want to allocate at once.
AlignmentPower of 2 value denoting on which address boundary the memory will be aligned to. Must be at least sizeof(unsigned char *).
Exceptions
MemoryPoolMallocExceptionon failure.
MemoryPoolBadAlignmentExceptionon failure.

Definition at line 96 of file cmem_pool.cpp.

◆ MemoryPool() [2/2]

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.

Parameters
itemSizeHow big is each item you want to allocate.
preallocatedMemoryPointer to the preallocated memory you are dedicating to this pool.
preallocatedMemorySizeHow big is the buffer you are passing in.
AlignmentPower of 2 value denoting on which address boundary the memory will be aligned to. Must be at least sizeof(unsigned char *).
Exceptions
MemoryPoolBadAlignmentExceptionon failure.

Definition at line 125 of file cmem_pool.cpp.

◆ ~MemoryPool()

cpp_freertos::MemoryPool::~MemoryPool ( )
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.

Member Function Documentation

◆ AddMemory() [1/2]

void MemoryPool::AddMemory ( int  itemCount)

Allows you to add memory to a MemoryPool.

Items will be the same size as you initially asked for.

Parameters
itemCountHow many more items max do you want to allocate
Exceptions
MemoryPoolMallocExceptionon failure.

Definition at line 170 of file cmem_pool.cpp.

◆ AddMemory() [2/2]

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.

Parameters
preallocatedMemoryPointer to the preallocated memory you are dedicating to this pool.
preallocatedMemorySizeHow big is the buffer you are passing in.

Definition at line 192 of file cmem_pool.cpp.

◆ Allocate()

void * MemoryPool::Allocate ( )

Allocate an item from the pool.

Returns
Pointer of the memory or NULL if the pool is empty.

Definition at line 149 of file cmem_pool.cpp.

◆ CalculateItemSize()

void MemoryPool::CalculateItemSize ( )
private

Calculate the true item size, based on alignment.

Definition at line 78 of file cmem_pool.cpp.

◆ CalculateValidAlignment()

void MemoryPool::CalculateValidAlignment ( )
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.

◆ Free()

void MemoryPool::Free ( void *  item)

Returns the item back to it's pool.

Note
There is no checking that the item is actually valid to be returned to this pool.

Definition at line 163 of file cmem_pool.cpp.

Member Data Documentation

◆ Alignment

int cpp_freertos::MemoryPool::Alignment
private

The overall alignment of an item.

Definition at line 246 of file mem_pool.hpp.

◆ FreeItems

std::list<void *> cpp_freertos::MemoryPool::FreeItems
private

All of the real work is done with STL lists.

Definition at line 251 of file mem_pool.hpp.

◆ ItemSize

int cpp_freertos::MemoryPool::ItemSize
private

Save the item size for additions.

Definition at line 241 of file mem_pool.hpp.

◆ Lock

Mutex* cpp_freertos::MemoryPool::Lock
private

Standard Mutex to allow thread safety.

Definition at line 236 of file mem_pool.hpp.


The documentation for this class was generated from the following files: