From 18b82d2959a7cafba6d22fb9b99cb23cccf2d84d Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Tue, 28 Oct 2025 17:38:28 +0800 Subject: [PATCH] feat(ble/nimble): support runtime allocation for mempool --- components/bt/host/nimble/Kconfig.in | 20 +++++++++++++++++++ .../host/nimble/esp-hci/src/esp_nimble_hci.c | 4 ++++ components/bt/host/nimble/nimble | 2 +- .../host/nimble/port/include/esp_nimble_cfg.h | 16 +++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/components/bt/host/nimble/Kconfig.in b/components/bt/host/nimble/Kconfig.in index 29e867c96a3..d816de18408 100644 --- a/components/bt/host/nimble/Kconfig.in +++ b/components/bt/host/nimble/Kconfig.in @@ -485,6 +485,26 @@ menu "Memory Settings" default 1 help This is the service data unit buffer count for l2cap coc. + + config BT_NIMBLE_MEMPOOL_RUNTIME_ALLOC + bool "Support on-demand runtime memory allocation for mempool" + depends on BT_NIMBLE_ENABLED && !SOC_ESP_NIMBLE_CONTROLLER + default n + help + When this option is enabled, mempool does not require pre-allocating memory. + Instead, memory for each block will be dynamically allocated and released + during mempool usage. This can significantly reduce memory consumption + after mempool initialization, but may have some impact on performance. + + config BT_NIMBLE_MEMPOOL_BLOCK_REUSED + bool "Support block reuse for mempool runtime memory allocation" + depends on BT_NIMBLE_MEMPOOL_RUNTIME_ALLOC + default n + help + When this option is enabled, dynamically allocated blocks will not be freed + but will be reused instead. This ensures virtually no impact on performance + while reducing the memory consumption of the mempool. + endmenu #Memory menu "BLE 5.x Features" diff --git a/components/bt/host/nimble/esp-hci/src/esp_nimble_hci.c b/components/bt/host/nimble/esp-hci/src/esp_nimble_hci.c index 04778593caf..bffd7ee1403 100644 --- a/components/bt/host/nimble/esp-hci/src/esp_nimble_hci.c +++ b/components/bt/host/nimble/esp-hci/src/esp_nimble_hci.c @@ -96,7 +96,11 @@ int ble_hci_trans_hs_cmd_tx(uint8_t *cmd) rc = BLE_HS_ETIMEOUT_HCI; } +#if MYNEWT_VAL(MP_RUNTIME_ALLOC) + ble_transport_free(BLE_HCI_CMD, cmd); +#else ble_transport_free(cmd); +#endif return rc; } diff --git a/components/bt/host/nimble/nimble b/components/bt/host/nimble/nimble index 8fd41d86c4e..c6709f6efa1 160000 --- a/components/bt/host/nimble/nimble +++ b/components/bt/host/nimble/nimble @@ -1 +1 @@ -Subproject commit 8fd41d86c4ed8657e6c97d6da8b3d214622db737 +Subproject commit c6709f6efa1f1adc594fbb5793b2ebacc36e5601 diff --git a/components/bt/host/nimble/port/include/esp_nimble_cfg.h b/components/bt/host/nimble/port/include/esp_nimble_cfg.h index 0d063aeca40..445c0e29572 100644 --- a/components/bt/host/nimble/port/include/esp_nimble_cfg.h +++ b/components/bt/host/nimble/port/include/esp_nimble_cfg.h @@ -2214,4 +2214,20 @@ #endif #endif +#ifndef MYNEWT_VAL_MP_RUNTIME_ALLOC +#ifdef CONFIG_BT_NIMBLE_MEMPOOL_RUNTIME_ALLOC +#define MYNEWT_VAL_MP_RUNTIME_ALLOC (1) +#else +#define MYNEWT_VAL_MP_RUNTIME_ALLOC (0) +#endif +#endif + +#ifndef MYNEWT_VAL_MP_BLOCK_REUSED +#ifdef CONFIG_BT_NIMBLE_MEMPOOL_BLOCK_REUSED +#define MYNEWT_VAL_MP_BLOCK_REUSED (1) +#else +#define MYNEWT_VAL_MP_BLOCK_REUSED (0) +#endif +#endif + #endif