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

Time:01-26

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.

CodePudding user response:

What exactly is "a pool of pre-allocated buffers",

A memory buffer is region of memory where objects can potentially be created. Allocation is the act of acquiring a resource and in this case the resource is a memory buffer.

"Pre" prefix implies that the allocation will have happened before prvGetBuffer is called. A "pool" is an abstract description of a collection of things, such as a data structure.

CodePudding user response:

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.

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

  •  Tags:  
  • Related