Skip to content

Commit 4832043

Browse files
carlos-stuartnashif
authored andcommitted
lib: cmsis_rtos_v2: Dynamic memory pools
Implemented dynamic allocation of memory pools in a similar to manner to what was already implemented for message queues. Added all the same checks on size vs. maximum allowed and current heap. Added an additional Kconfig variable to define the maximum size of a dynamically allocated memory pool. Signed-off-by: Carlos Stuart <[email protected]>
1 parent 17db516 commit 4832043

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

lib/cmsis_rtos_v2/Kconfig

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ config CMSIS_V2_MEM_SLAB_MAX_COUNT
6161
help
6262
Mention maximum number of memory slabs in CMSIS RTOS V2 compliant application.
6363

64+
config CMSIS_V2_MEM_SLAB_MAX_DYNAMIC_SIZE
65+
int "Maximum dynamic mem slab/pool size in CMSIS RTOS V2 application"
66+
default 0
67+
help
68+
Mention maximum dynamic size of memory slabs/pools in CMSIS RTOS V2 compliant application.
69+
6470
config CMSIS_V2_MSGQ_MAX_COUNT
6571
int "Maximum message queue count in CMSIS RTOS V2 application"
6672
default 5

lib/cmsis_rtos_v2/mempool.c

+28-9
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ osMemoryPoolId_t osMemoryPoolNew(uint32_t block_count, uint32_t block_size,
2929
{
3030
struct cv2_mslab *mslab;
3131

32+
BUILD_ASSERT_MSG(CONFIG_HEAP_MEM_POOL_SIZE >=
33+
CONFIG_CMSIS_V2_MEM_SLAB_MAX_DYNAMIC_SIZE,
34+
"heap must be configured to be at least the max dynamic size");
35+
3236
if (k_is_in_isr()) {
3337
return NULL;
3438
}
3539

36-
if (attr == NULL) {
37-
attr = &init_mslab_attrs;
40+
if ((attr != NULL) && (attr->mp_size < block_count * block_size)) {
41+
return NULL;
3842
}
3943

40-
/* This implementation requires memory to be allocated by the
41-
* App layer
42-
*/
43-
if ((attr->mp_mem == NULL) ||
44-
(attr->mp_size < block_count * block_size)) {
45-
return NULL;
44+
if (attr == NULL) {
45+
attr = &init_mslab_attrs;
4646
}
4747

4848
if (k_mem_slab_alloc(&cv2_mem_slab, (void **)&mslab, 100) == 0) {
@@ -51,7 +51,23 @@ osMemoryPoolId_t osMemoryPoolNew(uint32_t block_count, uint32_t block_size,
5151
return NULL;
5252
}
5353

54-
k_mem_slab_init(&mslab->z_mslab, attr->mp_mem, block_size, block_count);
54+
if (attr->mp_mem == NULL) {
55+
__ASSERT((block_count * block_size) <=
56+
CONFIG_CMSIS_V2_MEM_SLAB_MAX_DYNAMIC_SIZE,
57+
"memory slab/pool size exceeds dynamic maximum");
58+
59+
mslab->pool = k_calloc(block_count, block_size);
60+
if (mslab->pool == NULL) {
61+
k_mem_slab_free(&cv2_mem_slab, (void *) &mslab);
62+
return NULL;
63+
}
64+
mslab->is_dynamic_allocation = TRUE;
65+
} else {
66+
mslab->pool = attr->mp_mem;
67+
mslab->is_dynamic_allocation = FALSE;
68+
}
69+
70+
k_mem_slab_init(&mslab->z_mslab, mslab->pool, block_size, block_count);
5571
memcpy(mslab->name, attr->name, 16);
5672

5773
return (osMemoryPoolId_t)mslab;
@@ -207,6 +223,9 @@ osStatus_t osMemoryPoolDelete(osMemoryPoolId_t mp_id)
207223
* supported in Zephyr.
208224
*/
209225

226+
if (mslab->is_dynamic_allocation) {
227+
k_free(mslab->pool);
228+
}
210229
k_mem_slab_free(&cv2_mem_slab, (void *)&mslab);
211230

212231
return osOK;

lib/cmsis_rtos_v2/wrapper.h

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ struct cv2_sem {
4545

4646
struct cv2_mslab {
4747
struct k_mem_slab z_mslab;
48+
void *pool;
49+
char is_dynamic_allocation;
4850
char name[16];
4951
};
5052

0 commit comments

Comments
 (0)