From 5fd4cbb9d288f6d06c4e4279a1d592e8aaa353fd Mon Sep 17 00:00:00 2001 From: baohongde Date: Sat, 29 Sep 2018 16:04:50 +0800 Subject: [PATCH] component/bt: Add functions for heap memory debug --- components/bt/bluedroid/osi/allocator.c | 21 +++++++++++++++++++ .../bt/bluedroid/osi/include/osi/allocator.h | 8 ++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/components/bt/bluedroid/osi/allocator.c b/components/bt/bluedroid/osi/allocator.c index de8c8da722..624a11c176 100644 --- a/components/bt/bluedroid/osi/allocator.c +++ b/components/bt/bluedroid/osi/allocator.c @@ -38,6 +38,8 @@ typedef struct { static uint32_t mem_dbg_count = 0; static uint32_t mem_dbg_count2 = 0; static osi_mem_dbg_info_t mem_dbg_info[OSI_MEM_DBG_INFO_MAX]; +static uint32_t mem_dbg_total_size = 0; +static uint32_t mem_dbg_max_size = 0; void osi_mem_dbg_init(void) { @@ -51,6 +53,8 @@ void osi_mem_dbg_init(void) } mem_dbg_count = 0; mem_dbg_count2 = 0; + mem_dbg_total_size = 0; + mem_dbg_max_size = 0; } void osi_mem_dbg_record(void *p, int size, const char *func, int line) @@ -76,6 +80,11 @@ void osi_mem_dbg_record(void *p, int size, const char *func, int line) if (i >= OSI_MEM_DBG_INFO_MAX) { OSI_TRACE_ERROR("%s full %s %d !!\n", __func__, func, line); } + + mem_dbg_total_size += size; + if(mem_dbg_max_size < mem_dbg_total_size) { + mem_dbg_max_size = mem_dbg_total_size; + } } void osi_mem_dbg_clean(void *p, const char *func, int line) @@ -89,6 +98,7 @@ void osi_mem_dbg_clean(void *p, const char *func, int line) for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) { if (mem_dbg_info[i].p == p) { + mem_dbg_total_size -= mem_dbg_info[i].size; mem_dbg_info[i].p = NULL; mem_dbg_info[i].size = 0; mem_dbg_info[i].func = NULL; @@ -113,6 +123,17 @@ void osi_mem_dbg_show(void) } } OSI_TRACE_ERROR("--> count %d\n", mem_dbg_count); + OSI_TRACE_ERROR("--> size %dB\n--> max size %dB\n", mem_dbg_total_size, mem_dbg_max_size); +} + +uint32_t osi_mem_dbg_get_max_size(void) +{ + return mem_dbg_max_size; +} + +uint32_t osi_mem_dbg_get_total_size(void) +{ + return mem_dbg_total_size; } #endif diff --git a/components/bt/bluedroid/osi/include/osi/allocator.h b/components/bt/bluedroid/osi/include/osi/allocator.h index f30987e058..f5e87a473a 100644 --- a/components/bt/bluedroid/osi/include/osi/allocator.h +++ b/components/bt/bluedroid/osi/include/osi/allocator.h @@ -35,6 +35,8 @@ void osi_mem_dbg_init(void); void osi_mem_dbg_record(void *p, int size, const char *func, int line); void osi_mem_dbg_clean(void *p, const char *func, int line); void osi_mem_dbg_show(void); +uint32_t osi_mem_dbg_get_max_size(void); +uint32_t osi_mem_dbg_get_total_size(void); #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST #define osi_malloc(size) \ @@ -67,9 +69,9 @@ void osi_mem_dbg_show(void); (void *)p; \ }) -#define osi_calloc(size) \ -({ \ - void *p; \ +#define osi_calloc(size) \ +({ \ + void *p; \ p = calloc(1, (size)); \ osi_mem_dbg_record(p, size, __func__, __LINE__); \ (void *)p; \