Use allocation strategy for dynamic buffers in NimBLE host stack

This commit is contained in:
Hrishikesh Dhayagude
2024-02-14 14:51:15 +05:30
committed by Abhinav Kudnar
parent ea41bd0bdb
commit 3b44ea024d
5 changed files with 21 additions and 16 deletions
+2 -1
View File
@@ -28,6 +28,7 @@
#include "bleuart/bleuart.h" #include "bleuart/bleuart.h"
#include "os/endian.h" #include "os/endian.h"
#include "console/console.h" #include "console/console.h"
#include "esp_nimble_mem.h"
/* ble uart attr read handle */ /* ble uart attr read handle */
uint16_t g_bleuart_attr_read_handle; uint16_t g_bleuart_attr_read_handle;
@@ -198,6 +199,6 @@ bleuart_init(void)
rc = console_init(bleuart_uart_read); rc = console_init(bleuart_uart_read);
SYSINIT_PANIC_ASSERT(rc == 0); SYSINIT_PANIC_ASSERT(rc == 0);
console_buf = malloc(MYNEWT_VAL(BLEUART_MAX_INPUT)); console_buf = nimble_platform_mem_malloc(MYNEWT_VAL(BLEUART_MAX_INPUT));
SYSINIT_PANIC_ASSERT(console_buf != NULL); SYSINIT_PANIC_ASSERT(console_buf != NULL);
} }
+3 -2
View File
@@ -25,6 +25,7 @@
#include "nimble/ble.h" #include "nimble/ble.h"
#include "host/ble_uuid.h" #include "host/ble_uuid.h"
#include "ble_hs_priv.h" #include "ble_hs_priv.h"
#include "esp_nimble_mem.h"
#if NIMBLE_BLE_CONNECT #if NIMBLE_BLE_CONNECT
/** /**
@@ -2710,7 +2711,7 @@ ble_att_svr_reset(void)
static void static void
ble_att_svr_free_start_mem(void) ble_att_svr_free_start_mem(void)
{ {
free(ble_att_svr_entry_mem); nimble_platform_mem_free(ble_att_svr_entry_mem);
ble_att_svr_entry_mem = NULL; ble_att_svr_entry_mem = NULL;
} }
@@ -2722,7 +2723,7 @@ ble_att_svr_start(void)
ble_att_svr_free_start_mem(); ble_att_svr_free_start_mem();
if (ble_hs_max_attrs > 0) { if (ble_hs_max_attrs > 0) {
ble_att_svr_entry_mem = malloc( ble_att_svr_entry_mem = nimble_platform_mem_malloc(
OS_MEMPOOL_BYTES(ble_hs_max_attrs, OS_MEMPOOL_BYTES(ble_hs_max_attrs,
sizeof (struct ble_att_svr_entry))); sizeof (struct ble_att_svr_entry)));
if (ble_att_svr_entry_mem == NULL) { if (ble_att_svr_entry_mem == NULL) {
+6 -5
View File
@@ -25,6 +25,7 @@
#include "host/ble_uuid.h" #include "host/ble_uuid.h"
#include "host/ble_store.h" #include "host/ble_store.h"
#include "ble_hs_priv.h" #include "ble_hs_priv.h"
#include "esp_nimble_mem.h"
#define BLE_GATTS_INCLUDE_SZ 6 #define BLE_GATTS_INCLUDE_SZ 6
#define BLE_GATTS_CHR_MAX_SZ 19 #define BLE_GATTS_CHR_MAX_SZ 19
@@ -1155,7 +1156,7 @@ ble_gatts_connection_broken(uint16_t conn_handle)
static void static void
ble_gatts_free_svc_defs(void) ble_gatts_free_svc_defs(void)
{ {
free(ble_gatts_svc_defs); nimble_platform_mem_free(ble_gatts_svc_defs);
ble_gatts_svc_defs = NULL; ble_gatts_svc_defs = NULL;
ble_gatts_num_svc_defs = 0; ble_gatts_num_svc_defs = 0;
} }
@@ -1163,10 +1164,10 @@ ble_gatts_free_svc_defs(void)
static void static void
ble_gatts_free_mem(void) ble_gatts_free_mem(void)
{ {
free(ble_gatts_clt_cfg_mem); nimble_platform_mem_free(ble_gatts_clt_cfg_mem);
ble_gatts_clt_cfg_mem = NULL; ble_gatts_clt_cfg_mem = NULL;
free(ble_gatts_svc_entries); nimble_platform_mem_free(ble_gatts_svc_entries);
ble_gatts_svc_entries = NULL; ble_gatts_svc_entries = NULL;
} }
@@ -1210,7 +1211,7 @@ ble_gatts_start(void)
} }
if (ble_hs_max_client_configs > 0) { if (ble_hs_max_client_configs > 0) {
ble_gatts_clt_cfg_mem = malloc( ble_gatts_clt_cfg_mem = nimble_platform_mem_malloc(
OS_MEMPOOL_BYTES(ble_hs_max_client_configs, OS_MEMPOOL_BYTES(ble_hs_max_client_configs,
sizeof (struct ble_gatts_clt_cfg))); sizeof (struct ble_gatts_clt_cfg)));
if (ble_gatts_clt_cfg_mem == NULL) { if (ble_gatts_clt_cfg_mem == NULL) {
@@ -1221,7 +1222,7 @@ ble_gatts_start(void)
if (ble_hs_max_services > 0) { if (ble_hs_max_services > 0) {
ble_gatts_svc_entries = ble_gatts_svc_entries =
malloc(ble_hs_max_services * sizeof *ble_gatts_svc_entries); nimble_platform_mem_malloc(ble_hs_max_services * sizeof *ble_gatts_svc_entries);
if (ble_gatts_svc_entries == NULL) { if (ble_gatts_svc_entries == NULL) {
rc = BLE_HS_ENOMEM; rc = BLE_HS_ENOMEM;
goto done; goto done;
+5 -4
View File
@@ -20,6 +20,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "os/os.h" #include "os/os.h"
#include "mem/mem.h" #include "mem/mem.h"
#include "esp_nimble_mem.h"
/** /**
* Generic mempool allocation function. Used with basic and extended mempools. * Generic mempool allocation function. Used with basic and extended mempools.
@@ -31,7 +32,7 @@ mem_malloc_mempool_gen(uint16_t num_blocks, uint32_t block_size,
block_size = OS_ALIGN(block_size, OS_ALIGNMENT); block_size = OS_ALIGN(block_size, OS_ALIGNMENT);
if (num_blocks > 0) { if (num_blocks > 0) {
*out_buf = malloc(OS_MEMPOOL_BYTES(num_blocks, block_size)); *out_buf = nimble_platform_mem_malloc(OS_MEMPOOL_BYTES(num_blocks, block_size));
if (*out_buf == NULL) { if (*out_buf == NULL) {
return OS_ENOMEM; return OS_ENOMEM;
} }
@@ -72,7 +73,7 @@ mem_malloc_mempool(struct os_mempool *mempool, uint16_t num_blocks,
rc = os_mempool_init(mempool, num_blocks, block_size, buf, name); rc = os_mempool_init(mempool, num_blocks, block_size, buf, name);
if (rc != 0) { if (rc != 0) {
free(buf); nimble_platform_mem_free(buf);
return rc; return rc;
} }
@@ -113,7 +114,7 @@ mem_malloc_mempool_ext(struct os_mempool_ext *mpe, uint16_t num_blocks,
rc = os_mempool_ext_init(mpe, num_blocks, block_size, buf, name); rc = os_mempool_ext_init(mpe, num_blocks, block_size, buf, name);
if (rc != 0) { if (rc != 0) {
free(buf); nimble_platform_mem_free(buf);
return rc; return rc;
} }
@@ -160,7 +161,7 @@ mem_malloc_mbuf_pool(struct os_mempool *mempool,
rc = os_mbuf_pool_init(mbuf_pool, mempool, block_size, num_blocks); rc = os_mbuf_pool_init(mbuf_pool, mempool, block_size, num_blocks);
if (rc != 0) { if (rc != 0) {
free(buf); nimble_platform_mem_free(buf);
return rc; return rc;
} }
+5 -4
View File
@@ -21,6 +21,7 @@
#include "os/os.h" #include "os/os.h"
#include "mem/mem.h" #include "mem/mem.h"
#include "sysinit/sysinit.h" #include "sysinit/sysinit.h"
#include "esp_nimble_mem.h"
static STAILQ_HEAD(, os_mbuf_pool) g_msys_pool_list = static STAILQ_HEAD(, os_mbuf_pool) g_msys_pool_list =
STAILQ_HEAD_INITIALIZER(g_msys_pool_list); STAILQ_HEAD_INITIALIZER(g_msys_pool_list);
@@ -122,14 +123,14 @@ int
os_msys_buf_alloc(void) os_msys_buf_alloc(void)
{ {
#if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0 #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)); os_msys_init_1_data = (os_membuf_t *)nimble_platform_mem_calloc(1, (sizeof(os_membuf_t) * SYSINIT_MSYS_1_MEMPOOL_SIZE));
if (!os_msys_init_1_data) { if (!os_msys_init_1_data) {
return -1; return -1;
} }
#endif #endif
#if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0 #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)); os_msys_init_2_data = (os_membuf_t *)nimble_platform_mem_calloc(1, (sizeof(os_membuf_t) * SYSINIT_MSYS_2_MEMPOOL_SIZE));
if (!os_msys_init_2_data) { if (!os_msys_init_2_data) {
return -1; return -1;
} }
@@ -142,12 +143,12 @@ void
os_msys_buf_free(void) os_msys_buf_free(void)
{ {
#if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0 #if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
free(os_msys_init_1_data); nimble_platform_mem_free(os_msys_init_1_data);
os_msys_init_1_data = NULL; os_msys_init_1_data = NULL;
#endif #endif
#if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0 #if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
free(os_msys_init_2_data); nimble_platform_mem_free(os_msys_init_2_data);
os_msys_init_2_data = NULL; os_msys_init_2_data = NULL;
#endif #endif