0
0
Embedded Cprogramming~10 mins

Memory pool (fixed-size block allocator) in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Memory pool (fixed-size block allocator)
Initialize pool with fixed blocks
Request allocation?
NoExit
Yes
Check free blocks available
Return NULL
User uses block
User frees block
Return block to pool
Repeat
This flow shows how a fixed-size memory pool allocates and frees blocks step-by-step.
Execution Sample
Embedded C
typedef struct Block {
  struct Block* next;
} Block;

Block* pool = NULL;

void init_pool(Block* blocks, int count) {
  for (int i = 0; i < count - 1; i++) {
    blocks[i].next = &blocks[i+1];
  }
  blocks[count-1].next = NULL;
  pool = &blocks[0];
}

Block* allocate() {
  if (!pool) return NULL;
  Block* block = pool;
  pool = pool->next;
  return block;
}

void free_block(Block* block) {
  block->next = pool;
  pool = block;
}
This code initializes a pool of fixed blocks, allocates a block by removing it from the pool, and frees a block by returning it to the pool.
Execution Table
StepActionPool State (addresses)Allocated BlockOutput
1Initialize pool with 3 blocks&blocks[0] -> &blocks[1] -> &blocks[2] -> NULLNULLPool ready
2allocate() called&blocks[1] -> &blocks[2] -> NULL&blocks[0]Block allocated: &blocks[0]
3allocate() called&blocks[2] -> NULL&blocks[1]Block allocated: &blocks[1]
4allocate() calledNULL&blocks[2]Block allocated: &blocks[2]
5allocate() calledNULLNULLNo blocks left, return NULL
6free_block(&blocks[1]) called&blocks[1] -> NULLNULLBlock &blocks[1] returned to pool
7allocate() calledNULL&blocks[1]Block allocated: &blocks[1]
8free_block(&blocks[0]) called&blocks[0] -> NULLNULLBlock &blocks[0] returned to pool
9free_block(&blocks[2]) called&blocks[2] -> &blocks[0] -> NULLNULLBlock &blocks[2] returned to pool
10allocate() called&blocks[0] -> NULL&blocks[2]Block allocated: &blocks[2]
11allocate() calledNULL&blocks[0]Block allocated: &blocks[0]
12allocate() calledNULLNULLNo blocks left, return NULL
💡 Allocation stops when pool is empty (NULL), free returns blocks to pool.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10After Step 11After Step 12
poolNULL&blocks[0]&blocks[1]&blocks[2]NULLNULL&blocks[1]NULL&blocks[0]&blocks[2]&blocks[0]NULLNULL
allocated_blockNULLNULL&blocks[0]&blocks[1]&blocks[2]NULLNULL&blocks[1]NULLNULL&blocks[2]&blocks[0]NULL
Key Moments - 3 Insights
Why does allocate() return NULL at step 5 and 12?
Because the pool pointer is NULL, meaning no free blocks are left to allocate, as shown in execution_table rows 5 and 12.
How does free_block() add a block back to the pool?
It sets the freed block's next pointer to the current pool head, then updates pool to point to this block, shown in steps 6, 8, and 9.
Why does the pool state change after each allocate() and free_block()?
Because allocate() removes the first block from the pool, and free_block() adds a block back at the front, updating the linked list head each time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, which block is allocated?
A&blocks[1]
B&blocks[2]
C&blocks[0]
DNULL
💡 Hint
Check the 'Allocated Block' column at step 4 in the execution_table.
At which step does the pool become empty (NULL) after allocation?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look at the 'Pool State' column in execution_table rows 4 and 5.
If free_block(&blocks[1]) was not called at step 6, what would happen at step 7?
Aallocate() would return &blocks[1]
Ballocate() would return &blocks[0]
Callocate() would return NULL
Dallocate() would crash
💡 Hint
Refer to variable_tracker for pool state after step 5 and before step 7.
Concept Snapshot
Memory pool manages fixed-size blocks.
Initialize pool as linked list of free blocks.
allocate() removes and returns first free block.
free_block() adds block back to front of pool.
Returns NULL if no blocks free.
Simple, fast fixed-size memory management.
Full Transcript
This visual execution shows how a fixed-size memory pool works in embedded C. We start by initializing a pool of blocks linked together. When allocate() is called, it removes the first block from the pool and returns it. If no blocks are left, allocate() returns NULL. When free_block() is called, it returns a block to the pool by adding it back to the front of the linked list. The execution table tracks each step, showing pool state and allocated blocks. Variable tracker shows how the pool pointer and allocated block change over time. Key moments clarify why allocate returns NULL when empty and how free_block updates the pool. The quiz tests understanding of allocation steps and pool emptiness. This method is a simple, efficient way to manage fixed-size memory blocks in embedded systems.