feat(ble): support memory allocation check debug feature on ESP32-C2

(cherry picked from commit 7ef90d4113)

Co-authored-by: cjin <jinchen@espressif.com>
This commit is contained in:
Zhou Xiao
2025-07-09 13:13:54 +08:00
parent c22207208d
commit 24c2f79910
5 changed files with 130 additions and 78 deletions

View File

@@ -281,6 +281,7 @@ config BT_LE_CONTROLLER_TASK_STACK_SIZE
help
This configures stack size of NimBLE controller task
menu "Controller debug features"
menuconfig BT_LE_CONTROLLER_LOG_ENABLED
bool "Controller log enable"
default n
@@ -369,6 +370,14 @@ config BT_LE_CONTROLLER_LOG_TASK_WDT_USER_HANDLER_ENABLE
default n
help
Implement esp_task_wdt_isr_user_handler to get controller logs when task wdt issue is triggered.
config BT_LE_MEM_CHECK_ENABLED
bool "Enable memory allocation check"
default n
help
Used in internal tests only. Enable the memory allocation check.
endmenu
config BT_LE_LL_RESOLV_LIST_SIZE
int "BLE LL Resolving list size"
range 1 5

View File

@@ -1571,3 +1571,10 @@ int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv)
#endif // CONFIG_BT_LE_SM_LEGACY || CONFIG_BT_LE_SM_SC
#endif // (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED)
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
void ble_memory_count_limit_set(uint16_t count_limit)
{
bt_osi_mem_count_limit_set(count_limit);
}
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED

View File

@@ -446,6 +446,10 @@ uint32_t esp_bt_get_lpclk_freq(void);
void esp_bt_set_lpclk_freq(uint32_t clk_freq);
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
void ble_memory_count_limit_set(uint16_t count_limit);
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED
#ifdef __cplusplus
}
#endif

View File

@@ -19,3 +19,7 @@ void *bt_osi_mem_malloc_internal(size_t size);
void *bt_osi_mem_calloc_internal(size_t n, size_t size);
void bt_osi_mem_free(void *ptr);
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
void bt_osi_mem_count_limit_set(uint16_t count_limit);
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -11,6 +11,10 @@
#include <assert.h>
static uint8_t log_count;
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
static uint16_t mem_count_limit = 0;
static uint16_t curr_mem_count;
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED
IRAM_ATTR void *bt_osi_mem_malloc(size_t size)
{
void *mem = NULL;
@@ -49,11 +53,27 @@ IRAM_ATTR void *bt_osi_mem_calloc(size_t n, size_t size)
IRAM_ATTR void *bt_osi_mem_malloc_internal(size_t size)
{
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
if (mem_count_limit) {
if (curr_mem_count > mem_count_limit) {
return NULL;
}
curr_mem_count ++;
}
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED
return heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT|MALLOC_CAP_DMA);
}
IRAM_ATTR void *bt_osi_mem_calloc_internal(size_t n, size_t size)
{
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
if (mem_count_limit) {
if (curr_mem_count > mem_count_limit) {
return NULL;
}
curr_mem_count ++;
}
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED
return heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT|MALLOC_CAP_DMA);
}
@@ -61,3 +81,11 @@ IRAM_ATTR void bt_osi_mem_free(void *ptr)
{
heap_caps_free(ptr);
}
#if CONFIG_BT_LE_MEM_CHECK_ENABLED
void bt_osi_mem_count_limit_set(uint16_t count_limit)
{
mem_count_limit = count_limit;
curr_mem_count = 0;
}
#endif // CONFIG_BT_LE_MEM_CHECK_ENABLED