Implementing blocking memory management in RTOS
05:57 30 Nov 2025

I am reading about blocking memory allocation idea in book Real time concepts in embedded systems by QingLi.

A blocking memory allocation function can be implemented using both a counting semaphore and a mutex lock. These synchronization primitives are created for each memory pool and are kept in the control structure. The counting semaphore is initialized with total number of available memory blocks a the creation of the memory pool. Memory blocks are allocated and freed from the beginning of the list.

Pseudo code for malloc is shown below

Acquire(Counting semaphore)
Lock (mutex)
Retrive the memory block from pool
Unlock(mutex)

Below is pseudo code for memory deallocation

Lock(mutex)
Release the memory block back to the pool
Unlock(mutex)
Release(Couting semaphore)

It is mentioned that

This implementation enables memory allocation and deallocation functions to be safe for multi-tasking. The deployment of the counting semaphore and mutex eliminates the priority inversion problem when blocking memory allocation is enabled with these synchronization primitives.

My questions on above

  1. What is memory pool and memory blocks in this context?

  2. How counting semaphore and mutex eliminates priority inversion problem? instead of single mutex semaphore and with out using counting semaphore?

  3. Why counting semaphore is not released in memory allocation function and is released in deallocation function?

multithreading memory-management operating-system rtos vxworks