diff --git a/TESTS/mbed_platform/stats_heap/main.cpp b/TESTS/mbed_platform/stats_heap/main.cpp index 2d7d6b27e90..837b2403626 100644 --- a/TESTS/mbed_platform/stats_heap/main.cpp +++ b/TESTS/mbed_platform/stats_heap/main.cpp @@ -66,11 +66,13 @@ void test_case_malloc_free_size() TEST_ASSERT_EQUAL_UINT32(stats_start.total_size + ALLOCATION_SIZE_DEFAULT * (i + 1), stats_current.total_size); TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt + 1, stats_current.alloc_cnt); TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt); - + // Library header 0x4-0x8, stats header 0x8 and alignment addition 0x4 + TEST_ASSERT_INT_WITHIN(0x8, stats_start.overhead_size + 0xC , stats_current.overhead_size); // Free memory and assert back to starting size free(data); mbed_stats_heap_get(&stats_current); TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size); + TEST_ASSERT_EQUAL_UINT32(stats_start.overhead_size, stats_current.overhead_size); TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt); TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt); } @@ -93,10 +95,14 @@ void test_case_allocate_zero() TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size); TEST_ASSERT_EQUAL_UINT32(stats_start.total_size, stats_current.total_size); TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt); - + // Library header 0x4-0x8, stats header 0x8 and alignment addition 0x4 + if (NULL != data) { + TEST_ASSERT_INT_WITHIN(0x8, stats_start.overhead_size + 0xC , stats_current.overhead_size); + } // Free memory and assert back to starting size free(data); mbed_stats_heap_get(&stats_current); + TEST_ASSERT_EQUAL_UINT32(stats_start.overhead_size, stats_current.overhead_size); TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size); TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt); TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt); @@ -121,6 +127,7 @@ void test_case_allocate_fail() TEST_ASSERT_EQUAL_UINT32(stats_start.total_size, stats_current.total_size); TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt); TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt + i + 1, stats_current.alloc_fail_cnt); + TEST_ASSERT_EQUAL_UINT32(stats_start.overhead_size, stats_current.overhead_size); } } diff --git a/platform/mbed_alloc_wrappers.cpp b/platform/mbed_alloc_wrappers.cpp index 00e2dd2e75d..3b4caf077a8 100644 --- a/platform/mbed_alloc_wrappers.cpp +++ b/platform/mbed_alloc_wrappers.cpp @@ -48,7 +48,15 @@ typedef struct { #ifdef MBED_HEAP_STATS_ENABLED static SingletonPtr malloc_stats_mutex; -static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0}; +static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0, 0, 0}; + +typedef struct { + size_t size; +}mbed_heap_overhead_t; + +#define MALLOC_HEADER_SIZE (sizeof(mbed_heap_overhead_t)) +#define MALLOC_HEADER_PTR(p) (mbed_heap_overhead_t *)((char *)(p) - MALLOC_HEADER_SIZE) +#define MALLOC_HEAP_TOTAL_SIZE(p) (((p)->size) & (~0x1)) #endif void mbed_stats_heap_get(mbed_stats_heap_t *stats) @@ -71,7 +79,6 @@ void mbed_stats_heap_get(mbed_stats_heap_t *stats) #if defined(TOOLCHAIN_GCC) - extern "C" { void *__real__malloc_r(struct _reent *r, size_t size); void *__real__memalign_r(struct _reent *r, size_t alignment, size_t bytes); @@ -106,6 +113,7 @@ extern "C" void *malloc_wrapper(struct _reent *r, size_t size, void *caller) if (heap_stats.current_size > heap_stats.max_size) { heap_stats.max_size = heap_stats.current_size; } + heap_stats.overhead_size += MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info)) - size; } else { heap_stats.alloc_fail_cnt += 1; } @@ -178,10 +186,14 @@ extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller) alloc_info_t *alloc_info = NULL; if (ptr != NULL) { alloc_info = ((alloc_info_t *)ptr) - 1; - heap_stats.current_size -= alloc_info->size; + size_t user_size = alloc_info->size; + size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info)); + 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); + malloc_stats_mutex->unlock(); #else // #ifdef MBED_HEAP_STATS_ENABLED __real__free_r(r, ptr); @@ -260,7 +272,6 @@ extern "C" { void free_wrapper(void *ptr, void *caller); } - extern "C" void *SUB_MALLOC(size_t size) { return malloc_wrapper(size, MBED_CALLER_ADDR()); @@ -284,6 +295,7 @@ extern "C" void *malloc_wrapper(size_t size, void *caller) if (heap_stats.current_size > heap_stats.max_size) { heap_stats.max_size = heap_stats.current_size; } + heap_stats.overhead_size += MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info)) - size; } else { heap_stats.alloc_fail_cnt += 1; } @@ -322,7 +334,7 @@ extern "C" void *SUB_REALLOC(void *ptr, size_t size) // If the new buffer has been allocated copy the data to it // and free the old buffer - if (new_ptr != NULL) { + if ((new_ptr != NULL) && (ptr != NULL)) { uint32_t copy_size = (old_size < size) ? old_size : size; memcpy(new_ptr, (void *)ptr, copy_size); free(ptr); @@ -374,10 +386,14 @@ extern "C" void free_wrapper(void *ptr, void *caller) alloc_info_t *alloc_info = NULL; if (ptr != NULL) { alloc_info = ((alloc_info_t *)ptr) - 1; - heap_stats.current_size -= alloc_info->size; + size_t user_size = alloc_info->size; + size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info)); + heap_stats.current_size -= user_size; heap_stats.alloc_cnt -= 1; + heap_stats.overhead_size -= (alloc_size - user_size); } SUPER_FREE((void *)alloc_info); + malloc_stats_mutex->unlock(); #else // #ifdef MBED_HEAP_STATS_ENABLED SUPER_FREE(ptr); diff --git a/platform/mbed_stats.h b/platform/mbed_stats.h index 4532041379c..415be895c21 100644 --- a/platform/mbed_stats.h +++ b/platform/mbed_stats.h @@ -48,6 +48,7 @@ typedef struct { uint32_t reserved_size; /**< Current number of bytes allocated for the heap. */ uint32_t alloc_cnt; /**< Current number of allocations. */ uint32_t alloc_fail_cnt; /**< Number of failed allocations. */ + uint32_t overhead_size; /**< Overhead added to heap for stats. */ } mbed_stats_heap_t; /**