Search code examples
c++embeddedbufferfreertos

What is a pre-allocated buffer, and how should you use one?


I am studying FreeRTOS, and in their tutorial book they talk about using "a pool of pre-allocated buffers" for holding pointers to char[] arrays (to pass between tasks via a queue).

In the example found in Chapter 4.5 Working with Large or Variable Sized Data, they reference this sudo function called prvGetBuffer() and state "The implementation of prvGetBuffer() is not shown – it might obtain the buffer from a pool of pre-allocated buffers, or just allocate the buffer dynamically.". This seems like a funciton I would like to take a look at, but its nowhere to be found in their docs / examples.

What exactly is "a pool of pre-allocated buffers", and what does an implementation look like in C/C++? I can't find much on the internets about this.

I want to assume it is perhaps a 2-dimensional array that is "statically" allocated prior to run-time. Maybe it gets declared like char strBuffer[5][50]; or something - idk.


Solution

  • A pool is a collection of shared items such as a secretarial pool or motorpool.

    A pool of pre-allocated buffers is a pool of chunks of memory. Typically all the chunks in the pool are the same size. The application can allocate a chunk from the pool, use the memory as needed, and then return the chunk to the pool when it is no longer needed. "Pre-allocated" means that the chunks are not dynamically allocated from the heap repeatedly over the life of the program. Typically, the chunks are statically allocated and assigned to the pool during initialization.

    A memory pool is usually implemented as a FIFO queue, often using a linked list data structure. Allocation from the pool is getting the next chunk from the beginning of the queue. Returning a chunk to the pool is adding the chunk to the end of the queue. A linked list is nice because the order of the items in the queue can change over the life of the program because items are not necessarily returned to the queue in the same order that they were allocated from the queue.

    Using a pool of pre-allocated buffers is a great way to avoid dynamic memory allocation, which is undesirable in embedded systems.