ESP-NimBLE: Allocate os_msys_init_1_data and os_msys_init_2_data dynamically

Allocating buffers dynamically and freeing when the job is done saves static memory
This commit is contained in:
Hrishikesh Dhayagude
2024-02-14 14:51:03 +05:30
committed by Abhinav Kudnar
parent 8f8927d94a
commit ea41bd0bdb
+48 -13
View File
@@ -31,9 +31,9 @@ static STAILQ_HEAD(, os_mbuf_pool) g_msys_pool_list =
#define SYSINIT_MSYS_1_MEMPOOL_SIZE \
OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_1_BLOCK_COUNT), \
SYSINIT_MSYS_1_MEMBLOCK_SIZE)
static os_membuf_t os_msys_1_data[SYSINIT_MSYS_1_MEMPOOL_SIZE];
static struct os_mbuf_pool os_msys_1_mbuf_pool;
static struct os_mempool os_msys_1_mempool;
static os_membuf_t *os_msys_init_1_data;
static struct os_mbuf_pool os_msys_init_1_mbuf_pool;
static struct os_mempool os_msys_init_1_mempool;
#endif
#if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
@@ -42,9 +42,9 @@ static struct os_mempool os_msys_1_mempool;
#define SYSINIT_MSYS_2_MEMPOOL_SIZE \
OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_2_BLOCK_COUNT), \
SYSINIT_MSYS_2_MEMBLOCK_SIZE)
static os_membuf_t os_msys_2_data[SYSINIT_MSYS_2_MEMPOOL_SIZE];
static struct os_mbuf_pool os_msys_2_mbuf_pool;
static struct os_mempool os_msys_2_mempool;
static os_membuf_t *os_msys_init_2_data;
static struct os_mbuf_pool os_msys_init_2_mbuf_pool;
static struct os_mempool os_msys_init_2_mempool;
#endif
#define OS_MSYS_SANITY_ENABLED \
@@ -118,6 +118,41 @@ os_msys_init_once(void *data, struct os_mempool *mempool,
SYSINIT_PANIC_ASSERT(rc == 0);
}
int
os_msys_buf_alloc(void)
{
#if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
os_msys_init_1_data = (os_membuf_t *)calloc(1, (sizeof(os_membuf_t) * SYSINIT_MSYS_1_MEMPOOL_SIZE));
if (!os_msys_init_1_data) {
return -1;
}
#endif
#if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
os_msys_init_2_data = (os_membuf_t *)calloc(1, (sizeof(os_membuf_t) * SYSINIT_MSYS_2_MEMPOOL_SIZE));
if (!os_msys_init_2_data) {
return -1;
}
#endif
return 0;
}
void
os_msys_buf_free(void)
{
#if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
free(os_msys_init_1_data);
os_msys_init_1_data = NULL;
#endif
#if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
free(os_msys_init_2_data);
os_msys_init_2_data = NULL;
#endif
}
void
os_msys_init(void)
{
@@ -129,18 +164,18 @@ os_msys_init(void)
(void)rc;
#if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
os_msys_init_once(os_msys_1_data,
&os_msys_1_mempool,
&os_msys_1_mbuf_pool,
os_msys_init_once(os_msys_init_1_data,
&os_msys_init_1_mempool,
&os_msys_init_1_mbuf_pool,
MYNEWT_VAL(MSYS_1_BLOCK_COUNT),
SYSINIT_MSYS_1_MEMBLOCK_SIZE,
"msys_1");
#endif
#if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
os_msys_init_once(os_msys_2_data,
&os_msys_2_mempool,
&os_msys_2_mbuf_pool,
os_msys_init_once(os_msys_init_2_data,
&os_msys_init_2_mempool,
&os_msys_init_2_mbuf_pool,
MYNEWT_VAL(MSYS_2_BLOCK_COUNT),
SYSINIT_MSYS_2_MEMBLOCK_SIZE,
"msys_2");
@@ -153,4 +188,4 @@ os_msys_init(void)
rc = os_sanity_check_register(&os_msys_sc);
SYSINIT_PANIC_ASSERT(rc == 0);
#endif
}
}