Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

77%
+5 −0
Q&A Embedded C - memory pool types

In embedded systems, dynamic memory allocation is discouraged (or forbidden) due to various reasons. A better idea is to statically allocate a chunk of memory whose size is known at compile time -...

2 answers  ·  posted 6d ago by Carl‭  ·  last activity 3d ago by Lundin‭

Question c memory memory-pool static-allocation
#3: Post edited by user avatar Carl‭ · 2026-09-11T06:20:38Z (6 days ago)
  • In embedded systems, dynamic memory allocation is discouraged (or forbidden) due to various reasons.
  • A better idea is to statically allocate a chunk of memory whose size is known at compile time - this is called a memory pool. Then, during run time you can partition that chunk into smaller blocks and "hand it out" to functions or processes that need it. An example of a memory pool is blatantly stolen from one of Lundin's answers from the past (I can't seem to find the link)
  • ```C
  • #define MAXSIZE 100
  • static uint8_t mempool[MAXSIZE];
  • static uint16_t mempool_size = 0u;
  • void alloc_init(void)
  • {
  • mempool_size = 0u;
  • }
  • void* static_alloc(size_t size)
  • {
  • uint8_t* result;
  • if(mempool_size + size > MAXSIZE)
  • {
  • return NULL;
  • }
  • result = &mempool[mempool_size];
  • mempool_size += size;
  • return result;
  • }
  • uint16_t alloc_get_size(void)
  • {
  • return mempool_size;
  • }
  • ```
  • This is apparently known as an ***arena allocator***. It is simple to understand and to use.
  • But one disadvantage is that if I have to "free" memory from processes that have finished using their memory block I need to "free" the entire memory pool at once by calling `alloc_init()`. In other words, I cannot deallocate parts of the memory pool, I can only deallocate the entire thing at once. This becomes a problem in some situations, because it means I have to wait for all users of the memory pool to finish their task before I can deallocate memory and hand it over to the next process.
  • What are some other memory pool architectures/types than this arena type?
  • In embedded systems, dynamic memory allocation is discouraged (or forbidden) due to various reasons.
  • A better idea is to statically allocate a chunk of memory whose size is known at compile time - this is called a memory pool. Then, during run time you can partition that chunk into smaller blocks and "hand it out" to functions or processes that need it. An example of a memory pool is blatantly stolen from one of Lundin's answers from the past (I can't seem to find the link)
  • ```C
  • #define MAXSIZE 100
  • static uint8_t mempool[MAXSIZE];
  • static size_t mempool_size = 0;
  • void alloc_init(void)
  • {
  • mempool_size = 0;
  • }
  • void* static_alloc(size_t size)
  • {
  • uint8_t* result;
  • if(mempool_size + size > MAXSIZE)
  • {
  • return NULL;
  • }
  • result = &mempool[mempool_size];
  • mempool_size += size;
  • return result;
  • }
  • size_t alloc_get_size(void)
  • {
  • return mempool_size;
  • }
  • ```
  • This is apparently known as an ***arena allocator***. It is simple to understand and to use.
  • But one disadvantage is that if I have to "free" memory from processes that have finished using their memory block I need to "free" the entire memory pool at once by calling `alloc_init()`. In other words, I cannot deallocate parts of the memory pool, I can only deallocate the entire thing at once. This becomes a problem in some situations, because it means I have to wait for all users of the memory pool to finish their task before I can deallocate memory and hand it over to the next process.
  • What are some other memory pool architectures/types than this arena type?
#2: Post edited by user avatar Carl‭ · 2026-09-11T06:20:07Z (6 days ago)
  • In embedded systems, dynamic memory allocation is discouraged (or forbidden) due to various reasons.
  • A better idea is to statically allocate a chunk of memory whose size is known at compile time - this is called a memory pool. Then, during run time you can partition that chunk into smaller blocks and "hand it out" to functions or processes that need it. An example of a memory pool is blatantly stolen from one of Lundin's answers from the past
  • ```C
  • #define MAXSIZE 100
  • static uint8_t mempool[MAXSIZE];
  • static uint16_t mempool_size = 0u;
  • void alloc_init(void)
  • {
  • mempool_size = 0u;
  • }
  • void* static_alloc(size_t size)
  • {
  • uint8_t* result;
  • if(mempool_size + size > MAXSIZE)
  • {
  • return NULL;
  • }
  • result = &mempool[mempool_size];
  • mempool_size += size;
  • return result;
  • }
  • uint16_t alloc_get_size(void)
  • {
  • return mempool_size;
  • }
  • ```
  • This is apparently known as an ***arena allocator***. It is simple to understand and to use.
  • But one disadvantage is that if I have to "free" memory from processes that have finished using their memory block I need to "free" the entire memory pool at once by calling `alloc_init()`. In other words, I cannot deallocate parts of the memory pool, I can only deallocate the entire thing at once. This becomes a problem in some situations, because it means I have to wait for all users of the memory pool to finish their task before I can deallocate memory and hand it over to the next process.
  • What are some other memory pool architectures/types than this arena type?
  • In embedded systems, dynamic memory allocation is discouraged (or forbidden) due to various reasons.
  • A better idea is to statically allocate a chunk of memory whose size is known at compile time - this is called a memory pool. Then, during run time you can partition that chunk into smaller blocks and "hand it out" to functions or processes that need it. An example of a memory pool is blatantly stolen from one of Lundin's answers from the past (I can't seem to find the link)
  • ```C
  • #define MAXSIZE 100
  • static uint8_t mempool[MAXSIZE];
  • static uint16_t mempool_size = 0u;
  • void alloc_init(void)
  • {
  • mempool_size = 0u;
  • }
  • void* static_alloc(size_t size)
  • {
  • uint8_t* result;
  • if(mempool_size + size > MAXSIZE)
  • {
  • return NULL;
  • }
  • result = &mempool[mempool_size];
  • mempool_size += size;
  • return result;
  • }
  • uint16_t alloc_get_size(void)
  • {
  • return mempool_size;
  • }
  • ```
  • This is apparently known as an ***arena allocator***. It is simple to understand and to use.
  • But one disadvantage is that if I have to "free" memory from processes that have finished using their memory block I need to "free" the entire memory pool at once by calling `alloc_init()`. In other words, I cannot deallocate parts of the memory pool, I can only deallocate the entire thing at once. This becomes a problem in some situations, because it means I have to wait for all users of the memory pool to finish their task before I can deallocate memory and hand it over to the next process.
  • What are some other memory pool architectures/types than this arena type?
#1: Initial revision by user avatar Carl‭ · 2026-09-11T06:19:35Z (6 days ago)
Embedded C - memory pool types
In embedded systems, dynamic memory allocation is discouraged (or forbidden) due to various reasons.

A better idea is to statically allocate a chunk of memory whose size is known at compile time - this is called a memory pool. Then, during run time you can partition that chunk into smaller blocks and "hand it out" to functions or processes that need it. An example of a memory pool is blatantly stolen from one of Lundin's answers from the past

```C
#define MAXSIZE 100
static uint8_t mempool[MAXSIZE];
static uint16_t mempool_size = 0u;

void alloc_init(void)
{
	mempool_size = 0u;
}

void* static_alloc(size_t size)
{
	uint8_t* result;

	if(mempool_size + size > MAXSIZE)
	{
		return NULL;
	}

	result = &mempool[mempool_size];
	mempool_size += size;
	return result;
}

uint16_t alloc_get_size(void)
{
	return mempool_size;
}

```

This is apparently known as an ***arena allocator***. It is simple to understand and to use. 

But one disadvantage is that if I have to "free" memory from processes that have finished using their memory block I need to "free" the entire memory pool at once by calling `alloc_init()`. In other words, I cannot deallocate parts of the memory pool, I can only deallocate the entire thing at once. This becomes a problem in some situations, because it means I have to wait for all users of the memory pool to finish their task before I can deallocate memory and hand it over to the next process.

What are some other memory pool architectures/types than this arena type?