Merge branch 'feat/support_blecrt_515_v5.4' into 'release/v5.4'

feat(bt/bluedroid): Support get allocated heap size (v5.4)

See merge request espressif/esp-idf!44862
This commit is contained in:
Island
2026-01-16 14:19:05 +08:00
8 changed files with 111 additions and 4 deletions
+6
View File
@@ -61,6 +61,12 @@
#define HEAP_MEMORY_DEBUG FALSE
#endif
#if UC_BT_BLUEDROID_MEM_STATS
#define HEAP_MEMORY_STATS TRUE
#else
#define HEAP_MEMORY_STATS FALSE
#endif
#if UC_BT_BLUEDROID_THREAD_DEBUG
#define OSI_THREAD_DEBUG TRUE
#else
@@ -107,6 +107,12 @@
#define UC_BT_BLUEDROID_MEM_DEBUG FALSE
#endif
#ifdef CONFIG_BT_BLUEDROID_MEM_STATS
#define UC_BT_BLUEDROID_MEM_STATS TRUE
#else
#define UC_BT_BLUEDROID_MEM_STATS FALSE
#endif
#ifdef CONFIG_BT_BLUEDROID_THREAD_DEBUG
#define UC_BT_BLUEDROID_THREAD_DEBUG TRUE
#else
+73 -1
View File
@@ -20,6 +20,12 @@
#include "bt_common.h"
#include "osi/allocator.h"
#if HEAP_MEMORY_STATS
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#endif
extern void *pvPortZalloc(size_t size);
extern void vPortFree(void *pv);
@@ -198,6 +204,28 @@ uint32_t osi_mem_dbg_get_max_size_section(uint8_t index)
}
#endif
#if HEAP_MEMORY_STATS
static size_t s_mem_used_size = 0;
static SemaphoreHandle_t s_mem_mutex = NULL;
int osi_mem_init(void)
{
s_mem_mutex = xSemaphoreCreateMutex();
if (s_mem_mutex == NULL) {
return -1;
}
return 0;
}
void osi_mem_deinit(void)
{
if (s_mem_mutex) {
vSemaphoreDelete(s_mem_mutex);
s_mem_mutex = NULL;
}
}
#endif
char *osi_strdup(const char *str)
{
size_t size = strlen(str) + 1; // + 1 for the null terminator
@@ -224,6 +252,15 @@ void *osi_malloc_func(size_t size)
#endif
}
#if HEAP_MEMORY_STATS
if (s_mem_mutex != NULL && p != NULL) {
size_t alloc_size = heap_caps_get_allocated_size(p);
xSemaphoreTake(s_mem_mutex, portMAX_DELAY);
s_mem_used_size += alloc_size;
xSemaphoreGive(s_mem_mutex);
}
#endif
return p;
}
@@ -240,10 +277,45 @@ void *osi_calloc_func(size_t size)
#endif
}
#if HEAP_MEMORY_STATS
if (s_mem_mutex != NULL && p != NULL) {
size_t alloc_size = heap_caps_get_allocated_size(p);
xSemaphoreTake(s_mem_mutex, portMAX_DELAY);
s_mem_used_size += alloc_size;
xSemaphoreGive(s_mem_mutex);
}
#endif
return p;
}
void osi_free_func(void *ptr)
{
osi_free(ptr);
#if HEAP_MEMORY_DEBUG
osi_mem_dbg_clean(ptr, __func__, __LINE__);
#endif
#if HEAP_MEMORY_STATS
if (s_mem_mutex != NULL && ptr != NULL) {
size_t free_size = heap_caps_get_allocated_size(ptr);
xSemaphoreTake(s_mem_mutex, portMAX_DELAY);
if (s_mem_used_size >= free_size) {
s_mem_used_size -= free_size;
} else {
OSI_TRACE_ERROR("The size of malloc and free not match: alloc_size=%u free_size=%u",
s_mem_used_size, free_size);
}
xSemaphoreGive(s_mem_mutex);
}
#endif
free(ptr);
}
#if HEAP_MEMORY_STATS
// Get the size of memory allocated by Bluedroid but not yet freed
uint32_t esp_host_used_heap_size_get(void)
{
return s_mem_used_size;
}
#endif
@@ -24,6 +24,10 @@
#include "bt_common.h"
#include "esp_heap_caps.h"
#if HEAP_MEMORY_STATS
int osi_mem_init(void);
void osi_mem_deinit(void);
#endif
char *osi_strdup(const char *str);
void *osi_malloc_func(size_t size);
@@ -109,7 +113,7 @@ do { \
// Memory alloc function with print and assertion when fails
#define osi_malloc(size) osi_malloc_func((size))
#define osi_calloc(size) osi_calloc_func((size))
#define osi_free(p) free((p))
#define osi_free(p) osi_free_func((p))
#endif /* HEAP_MEMORY_DEBUG */
+1 -1
View File
@@ -19,7 +19,7 @@ struct pkt_queue {
struct pkt_queue *pkt_queue_create(void)
{
struct pkt_queue *queue = calloc(1, sizeof(struct pkt_queue));
struct pkt_queue *queue = osi_calloc(sizeof(struct pkt_queue));
if (queue == NULL) {
return NULL;
}