From fd11beebcd64a3ef6d7f89473967aff88b053292 Mon Sep 17 00:00:00 2001 From: Tian Hao Date: Tue, 13 Dec 2016 14:57:30 +0800 Subject: [PATCH] component/bt : fix the init/deinit memory leak 1. modify the memory debug 2. fix init/deinit memory leak --- components/bt/bluedroid/gki/include/gki.h | 4 +-- components/bt/bluedroid/osi/allocator.c | 25 ++++++++++++++++--- components/bt/bluedroid/osi/future.c | 11 +------- .../bt/bluedroid/osi/include/allocator.h | 20 +++++++-------- components/bt/bluedroid/osi/include/future.h | 9 ++++++- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/components/bt/bluedroid/gki/include/gki.h b/components/bt/bluedroid/gki/include/gki.h index cf0207e75..4e5f3c2b6 100644 --- a/components/bt/bluedroid/gki/include/gki.h +++ b/components/bt/bluedroid/gki/include/gki.h @@ -90,12 +90,12 @@ UINT16 GKI_poolutilization (UINT8); header->p_next = NULL; \ header->Type = 0; \ header->size = (_size); \ - (header + 1); \ + (void *)(header + 1); \ }) #define GKI_getpoolbuf(_pool_id) \ ({ \ - GKI_getbuf(gki_cb.com.pool_size[(_pool_id)]); \ + (void *)GKI_getbuf(gki_cb.com.pool_size[(_pool_id)]); \ }) #else diff --git a/components/bt/bluedroid/osi/allocator.c b/components/bt/bluedroid/osi/allocator.c index 99aed5965..9dc3ee910 100644 --- a/components/bt/bluedroid/osi/allocator.c +++ b/components/bt/bluedroid/osi/allocator.c @@ -73,11 +73,11 @@ void osi_mem_dbg_record(void *p, int size, const char *func, int line) } if (i >= OSI_MEM_DBG_INFO_MAX) { - LOG_ERROR("%s full !!\n", __func__); + LOG_ERROR("%s full %s %d !!\n", __func__, func, line); } } -void osi_mem_dbg_clean(void *p) +void osi_mem_dbg_clean(void *p, const char *func, int line) { int i; @@ -98,7 +98,7 @@ void osi_mem_dbg_clean(void *p) } if (i >= OSI_MEM_DBG_INFO_MAX) { - LOG_ERROR("%s full !!\n", __func__); + LOG_ERROR("%s full %s %d !!\n", __func__, func, line); } } @@ -130,16 +130,35 @@ char *osi_strdup(const char *str) void *osi_malloc_func(size_t size) { +#ifdef CONFIG_BLUEDROID_MEM_DEBUG + void *p; + + p = calloc(1, size); + osi_mem_dbg_record(p, size, __func__, __LINE__); + return p; +#else return calloc(1, size); +#endif } void *osi_calloc_func(size_t size) { +#ifdef CONFIG_BLUEDROID_MEM_DEBUG + void *p; + + p = calloc(1, size); + osi_mem_dbg_record(p, size, __func__, __LINE__); + return p; +#else return calloc(1, size); +#endif } void osi_free_func(void *ptr) { +#ifdef CONFIG_BLUEDROID_MEM_DEBUG + osi_mem_dbg_clean(ptr, __func__, __LINE__); +#endif free(ptr); } diff --git a/components/bt/bluedroid/osi/future.c b/components/bt/bluedroid/osi/future.c index b2dc40465..23c207f4d 100644 --- a/components/bt/bluedroid/osi/future.c +++ b/components/bt/bluedroid/osi/future.c @@ -16,22 +16,13 @@ * ******************************************************************************/ -// #define LOG_TAG "bt_osi_future" -// #include #include "bt_trace.h" #include "allocator.h" #include "future.h" #include "osi.h" -//#include "osi/include/log.h" #include "osi_arch.h" -struct future_t { - bool ready_can_be_called; - osi_sem_t semaphore; // NULL semaphore means immediate future - void *result; -}; - static void future_free(future_t *future); future_t *future_new(void) @@ -100,7 +91,7 @@ static void future_free(future_t *future) return; } - if (!future->semaphore) { + if (future->semaphore) { osi_sem_free(&future->semaphore); } diff --git a/components/bt/bluedroid/osi/include/allocator.h b/components/bt/bluedroid/osi/include/allocator.h index ce57afc02..17a3a2463 100644 --- a/components/bt/bluedroid/osi/include/allocator.h +++ b/components/bt/bluedroid/osi/include/allocator.h @@ -44,38 +44,38 @@ void osi_free_func(void *ptr); 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); +void osi_mem_dbg_clean(void *p, const char *func, int line); void osi_mem_dbg_show(void); #define osi_malloc(size) \ ({ \ void *p; \ \ - p = osi_malloc_func(size); \ + p = calloc(1, (size)); \ osi_mem_dbg_record(p, size, __func__, __LINE__); \ - (void *)p; \ + (void *)p; \ }) #define osi_calloc(size) \ ({ \ void *p; \ \ - p = osi_calloc_func(size); \ + p = calloc(1, (size)); \ osi_mem_dbg_record(p, size, __func__, __LINE__); \ - (void *)p; \ + (void *)p; \ }) #define osi_free(ptr) \ ({ \ - osi_mem_dbg_clean(ptr); \ - osi_free_func((ptr)); \ + osi_mem_dbg_clean(ptr, __func__, __LINE__); \ + free((ptr)); \ }) #else -#define osi_malloc osi_malloc_func -#define osi_calloc osi_calloc_func -#define osi_free osi_free_func +#define osi_malloc(size) calloc(1, (size)) +#define osi_calloc(size) calloc(1, (size)) +#define osi_free(p) free((p)) #endif /* CONFIG_BLUEDROID_MEM_DEBUG */ diff --git a/components/bt/bluedroid/osi/include/future.h b/components/bt/bluedroid/osi/include/future.h index d4601a93f..d54f23785 100644 --- a/components/bt/bluedroid/osi/include/future.h +++ b/components/bt/bluedroid/osi/include/future.h @@ -20,7 +20,14 @@ #define __FUTURE_H__ // #pragma once -typedef struct future_t future_t; +#include "osi_arch.h" + +struct future { + bool ready_can_be_called; + osi_sem_t semaphore; // NULL semaphore means immediate future + void *result; +}; +typedef struct future future_t; #define FUTURE_SUCCESS ((void *)1) #define FUTURE_FAIL ((void *)0)