component/bt: Add functions for heap memory debug

This commit is contained in:
baohongde 2018-09-29 16:04:50 +08:00
parent bd9fed4400
commit 5fd4cbb9d2
2 changed files with 26 additions and 3 deletions

View File

@ -38,6 +38,8 @@ typedef struct {
static uint32_t mem_dbg_count = 0; static uint32_t mem_dbg_count = 0;
static uint32_t mem_dbg_count2 = 0; static uint32_t mem_dbg_count2 = 0;
static osi_mem_dbg_info_t mem_dbg_info[OSI_MEM_DBG_INFO_MAX]; 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) void osi_mem_dbg_init(void)
{ {
@ -51,6 +53,8 @@ void osi_mem_dbg_init(void)
} }
mem_dbg_count = 0; mem_dbg_count = 0;
mem_dbg_count2 = 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) 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) { if (i >= OSI_MEM_DBG_INFO_MAX) {
OSI_TRACE_ERROR("%s full %s %d !!\n", __func__, func, line); 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) 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++) { for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
if (mem_dbg_info[i].p == p) { 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].p = NULL;
mem_dbg_info[i].size = 0; mem_dbg_info[i].size = 0;
mem_dbg_info[i].func = NULL; 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("--> 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 #endif

View File

@ -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_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_clean(void *p, const char *func, int line);
void osi_mem_dbg_show(void); 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 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
#define osi_malloc(size) \ #define osi_malloc(size) \
@ -67,9 +69,9 @@ void osi_mem_dbg_show(void);
(void *)p; \ (void *)p; \
}) })
#define osi_calloc(size) \ #define osi_calloc(size) \
({ \ ({ \
void *p; \ void *p; \
p = calloc(1, (size)); \ p = calloc(1, (size)); \
osi_mem_dbg_record(p, size, __func__, __LINE__); \ osi_mem_dbg_record(p, size, __func__, __LINE__); \
(void *)p; \ (void *)p; \