1

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.

scottc11
  • 716
  • 2
  • 7
  • 20
  • FreeRTOS is open-source, with the actual source available. So you can download it and look for yourself. – Some programmer dude Jan 25 '22 at 14:17
  • What does `sudo` have to do with it? If you meant *pseudo*, there's nothing "pseudo" about it - it's an actual function. – molbdnilo Jan 25 '22 at 14:26
  • @molbdnilo I always spell pseudo wrong ‍♂️. Where is this function then? – scottc11 Jan 25 '22 at 18:41
  • Check out this answer I wrote recently: https://stackoverflow.com/a/70667901/584518. This is for the purpose of private encapsulation, but the static memory allocator pool could be used for other purposes. It's very simple code really. – Lundin Jan 26 '22 at 08:43

2 Answers2

2

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

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.

kkrambo
  • 6,643
  • 1
  • 17
  • 30