QP/C  7.3.3
Real-Time Embedded Framework
No Matches
QMPool Class Reference

Native QF Memory Pool. More...

#include "qmpool.h"

Public Member Functions

void QMPool_init (QMPool *const me, void *const poolSto, uint_fast32_t const poolSize, uint_fast16_t const blockSize)
void * QMPool_get (QMPool *const me, uint_fast16_t const margin, uint_fast8_t const qsId)
void QMPool_put (QMPool *const me, void *const block, uint_fast8_t const qsId)

Private Attributes

QFreeBlock *volatile free_head
QMPoolSize blockSize
QMPoolCtr nTot
QMPoolCtr volatile nFree
QMPoolCtr nMin

Detailed Description

Native QF Memory Pool.

A fixed block-size memory pool is a very fast and efficient data structure for dynamic allocation of fixed block-size chunks of memory. A memory pool offers fast and deterministic allocation and recycling of memory blocks and is not subject to fragmentation.

The QMPool class describes the native QF memory pool, which can be used as the event pool for mutable event allocation, or as a fast, deterministic fixed block-size heap for any other objects in your application.

QMPool contains only data members for managing a memory pool, but does not contain the pool storage, which must be provided externally during the pool initialization.

Definition at line 98 of file qmpool.h.

Member Function Documentation

◆ QMPool_init()

QMPool::QMPool_init ( QMPool *const  me,
void *const  poolSto,
uint_fast32_t const  poolSize,
uint_fast16_t const  blockSize 

Initializes the native QF memory pool

Initialize a fixed block-size memory pool by providing it with the pool memory to manage, size of this memory, and the block size.

[in,out]mecurrent instance pointer (see oop)
[in]poolStopointer to the memory buffer for pool storage
[in]poolSizesize of the storage buffer in bytes
[in]blockSizefixed-size of the memory blocks in bytes
Precondition qf_mem:100
  • the memory block must be valid
  • the poolSize must fit at least one free block
  • the blockSize must not be too close to the top of the dynamic range
The caller of QMPool::init() must make sure that the poolSto pointer is properly aligned. In particular, it must be possible to efficiently store a pointer at the location pointed to by poolSto. Internally, the QMPool_init() function rounds up the block size blockSize so that it can fit an integer number of pointers. This is done to achieve proper alignment of the blocks within the pool.
Due to the rounding of block size the actual capacity of the pool might be less than (poolSize / blockSize). You can check the capacity of the pool by calling the QF_getPoolMin() function.
This function uses a potentially long critical section, because it is intended to be called only during the initialization of the system, when timing is not critical.
Many QF ports use memory pools to implement the event pools.

◆ QMPool_get()

QMPool::QMPool_get ( QMPool *const  me,
uint_fast16_t const  margin,
uint_fast8_t const  qsId 

Obtain a memory block from a memory pool.

The function allocates a memory block from the pool and returns a pointer to the block back to the caller.

[in,out]mecurrent instance pointer (see oop)
[in]marginthe minimum number of unused blocks still available in the pool after the allocation.
[in]qsIdQS-id of this state machine (for QS local filter)
A pointer to a memory block or NULL if no more blocks are available in the memory pool.
This function can be called from any task level or ISR level.
The memory pool me must be initialized before any events can be requested from it. Also, the QMPool_get() function uses internally a QF critical section, so you should be careful not to call it from within a critical section when nesting of critical section is not supported.
An allocated block must be later returned back to the same pool from which it has been allocated.

Definition at line 128 of file qf_mem.c.

◆ QMPool_put()

QMPool::QMPool_put ( QMPool *const  me,
void *const  block,
uint_fast8_t const  qsId 

Recycles a memory block back to a memory pool.

Recycle a memory block to the fixed block-size memory pool.

[in,out]mecurrent instance pointer (see oop)
[in]blockpointer to the memory block that is being recycled
[in]qsIdQS-id of this state machine (for QS local filter)
Precondition qf_mem:200
  • the number of free blocks cannot exceed the total # blocks
  • the block pointer must be in range for this pool.
The recycled block must be allocated from the same memory pool to which it is returned.
This function can be called from any task level or ISR level.

Definition at line 206 of file qf_mem.c.

Member Data Documentation

◆ start


Start of the memory managed by this memory pool

Definition at line 102 of file qmpool.h.

◆ end


End of the memory managed by this memory pool

Definition at line 105 of file qmpool.h.

◆ free_head


Head of linked list of free memory blocks

Definition at line 108 of file qmpool.h.

◆ blockSize


Memory block size [bytes] held by this fixed-size pool.

Definition at line 111 of file qmpool.h.

◆ nTot


Total number of memory blocks in this pool

Definition at line 114 of file qmpool.h.

◆ nFree


Number of free memory blocks remaining in the pool at this point

Definition at line 117 of file qmpool.h.

◆ nMin


Minimum number of free blocks ever present in this pool

This attribute remembers the low watermark of the pool, which provides a valuable information for sizing event pools. (

See also

Definition at line 120 of file qmpool.h.

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