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
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 -...
#3: Post edited
- 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
- 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
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?
