Merge branch 'deepikabhavnani-heap_fix_7912' into rollup2

pull/8423/head
Cruz Monrreal II 2018-10-12 21:46:28 -05:00
commit 1ea9ba6b5a
1 changed files with 27 additions and 15 deletions

View File

@ -40,13 +40,14 @@ are active, the second one (MBED_MEM_TRACING_ENABLED) will trace the first one's
/* Implementation of the runtime max heap usage checker */ /* Implementation of the runtime max heap usage checker */
/******************************************************************************/ /******************************************************************************/
/* Size must be a multiple of 8 to keep alignment */
typedef struct { typedef struct {
uint32_t size; uint32_t size;
uint32_t pad; uint32_t signature;
} alloc_info_t; } alloc_info_t;
#ifdef MBED_HEAP_STATS_ENABLED #ifdef MBED_HEAP_STATS_ENABLED
#define MBED_HEAP_STATS_SIGNATURE (0xdeadbeef)
static SingletonPtr<PlatformMutex> malloc_stats_mutex; static SingletonPtr<PlatformMutex> malloc_stats_mutex;
static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0, 0, 0}; static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0, 0, 0};
@ -106,6 +107,7 @@ extern "C" void *malloc_wrapper(struct _reent *r, size_t size, void *caller)
alloc_info_t *alloc_info = (alloc_info_t *)__real__malloc_r(r, size + sizeof(alloc_info_t)); alloc_info_t *alloc_info = (alloc_info_t *)__real__malloc_r(r, size + sizeof(alloc_info_t));
if (alloc_info != NULL) { if (alloc_info != NULL) {
alloc_info->size = size; alloc_info->size = size;
alloc_info->signature = MBED_HEAP_STATS_SIGNATURE;
ptr = (void *)(alloc_info + 1); ptr = (void *)(alloc_info + 1);
heap_stats.current_size += size; heap_stats.current_size += size;
heap_stats.total_size += size; heap_stats.total_size += size;
@ -186,13 +188,18 @@ extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller)
alloc_info_t *alloc_info = NULL; alloc_info_t *alloc_info = NULL;
if (ptr != NULL) { if (ptr != NULL) {
alloc_info = ((alloc_info_t *)ptr) - 1; alloc_info = ((alloc_info_t *)ptr) - 1;
size_t user_size = alloc_info->size; if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature) {
size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info)); size_t user_size = alloc_info->size;
heap_stats.current_size -= user_size; size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info));
heap_stats.alloc_cnt -= 1; alloc_info->signature = 0x0;
heap_stats.overhead_size -= (alloc_size - user_size); heap_stats.current_size -= user_size;
heap_stats.alloc_cnt -= 1;
heap_stats.overhead_size -= (alloc_size - user_size);
__real__free_r(r, (void *)alloc_info);
} else {
__real__free_r(r, ptr);
}
} }
__real__free_r(r, (void *)alloc_info);
malloc_stats_mutex->unlock(); malloc_stats_mutex->unlock();
#else // #ifdef MBED_HEAP_STATS_ENABLED #else // #ifdef MBED_HEAP_STATS_ENABLED
@ -233,7 +240,6 @@ extern "C" void *__wrap__memalign_r(struct _reent *r, size_t alignment, size_t b
} }
/******************************************************************************/ /******************************************************************************/
/* ARMCC / IAR memory allocation wrappers */ /* ARMCC / IAR memory allocation wrappers */
/******************************************************************************/ /******************************************************************************/
@ -288,6 +294,7 @@ extern "C" void *malloc_wrapper(size_t size, void *caller)
alloc_info_t *alloc_info = (alloc_info_t *)SUPER_MALLOC(size + sizeof(alloc_info_t)); alloc_info_t *alloc_info = (alloc_info_t *)SUPER_MALLOC(size + sizeof(alloc_info_t));
if (alloc_info != NULL) { if (alloc_info != NULL) {
alloc_info->size = size; alloc_info->size = size;
alloc_info->signature = MBED_HEAP_STATS_SIGNATURE;
ptr = (void *)(alloc_info + 1); ptr = (void *)(alloc_info + 1);
heap_stats.current_size += size; heap_stats.current_size += size;
heap_stats.total_size += size; heap_stats.total_size += size;
@ -386,13 +393,18 @@ extern "C" void free_wrapper(void *ptr, void *caller)
alloc_info_t *alloc_info = NULL; alloc_info_t *alloc_info = NULL;
if (ptr != NULL) { if (ptr != NULL) {
alloc_info = ((alloc_info_t *)ptr) - 1; alloc_info = ((alloc_info_t *)ptr) - 1;
size_t user_size = alloc_info->size; if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature) {
size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info)); size_t user_size = alloc_info->size;
heap_stats.current_size -= user_size; size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info));
heap_stats.alloc_cnt -= 1; alloc_info->signature = 0x0;
heap_stats.overhead_size -= (alloc_size - user_size); heap_stats.current_size -= user_size;
heap_stats.alloc_cnt -= 1;
heap_stats.overhead_size -= (alloc_size - user_size);
SUPER_FREE((void *)alloc_info);
} else {
SUPER_FREE(ptr);
}
} }
SUPER_FREE((void *)alloc_info);
malloc_stats_mutex->unlock(); malloc_stats_mutex->unlock();
#else // #ifdef MBED_HEAP_STATS_ENABLED #else // #ifdef MBED_HEAP_STATS_ENABLED