@@ -52,13 +52,22 @@ typedef struct {
5252static SingletonPtr<PlatformMutex> malloc_stats_mutex;
5353static mbed_stats_heap_t heap_stats = {0 , 0 , 0 , 0 , 0 , 0 , 0 };
5454
55- typedef struct {
56- size_t size;
55+ typedef struct mbed_heap_overhead {
56+ int size; // Size of the allocated memory block, including internal overhead size
57+ struct mbed_heap_overhead *next; // The memory is either the next free block, or allocated memory block
5758} mbed_heap_overhead_t ;
5859
59- #define MALLOC_HEADER_SIZE (sizeof (mbed_heap_overhead_t ))
60- #define MALLOC_HEADER_PTR (p ) (mbed_heap_overhead_t *)((char *)(p) - MALLOC_HEADER_SIZE)
61- #define MALLOC_HEAP_TOTAL_SIZE (p ) (((p)->size) & (~0x1 ))
60+ static int get_malloc_block_total_size (void *ptr)
61+ {
62+ mbed_heap_overhead_t *c = (mbed_heap_overhead_t *)((char *)ptr - offsetof (mbed_heap_overhead, next));
63+
64+ // Skip the padding area
65+ if (c->size < 0 ) {
66+ c = (mbed_heap_overhead_t *)((char *)c + c->size );
67+ }
68+ // Mask LSB as it is used for usage flags
69+ return (c->size & ~0x1 );
70+ }
6271#endif
6372
6473void mbed_stats_heap_get (mbed_stats_heap_t *stats)
@@ -116,7 +125,7 @@ extern "C" void *malloc_wrapper(struct _reent *r, size_t size, void *caller)
116125 if (heap_stats.current_size > heap_stats.max_size ) {
117126 heap_stats.max_size = heap_stats.current_size ;
118127 }
119- heap_stats.overhead_size += MALLOC_HEAP_TOTAL_SIZE ( MALLOC_HEADER_PTR (alloc_info) ) - size;
128+ heap_stats.overhead_size += get_malloc_block_total_size (( void *)alloc_info ) - size;
120129 } else {
121130 heap_stats.alloc_fail_cnt += 1 ;
122131 }
@@ -191,7 +200,7 @@ extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller)
191200 alloc_info = ((alloc_info_t *)ptr) - 1 ;
192201 if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature ) {
193202 size_t user_size = alloc_info->size ;
194- size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE ( MALLOC_HEADER_PTR (alloc_info) );
203+ size_t alloc_size = get_malloc_block_total_size (( void *)alloc_info );
195204 alloc_info->signature = 0x0 ;
196205 heap_stats.current_size -= user_size;
197206 heap_stats.alloc_cnt -= 1 ;
@@ -303,7 +312,7 @@ extern "C" void *malloc_wrapper(size_t size, void *caller)
303312 if (heap_stats.current_size > heap_stats.max_size ) {
304313 heap_stats.max_size = heap_stats.current_size ;
305314 }
306- heap_stats.overhead_size += MALLOC_HEAP_TOTAL_SIZE ( MALLOC_HEADER_PTR (alloc_info) ) - size;
315+ heap_stats.overhead_size += get_malloc_block_total_size (( void *)alloc_info ) - size;
307316 } else {
308317 heap_stats.alloc_fail_cnt += 1 ;
309318 }
@@ -416,7 +425,7 @@ extern "C" void free_wrapper(void *ptr, void *caller)
416425 alloc_info = ((alloc_info_t *)ptr) - 1 ;
417426 if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature ) {
418427 size_t user_size = alloc_info->size ;
419- size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE ( MALLOC_HEADER_PTR (alloc_info) );
428+ size_t alloc_size = get_malloc_block_total_size (( void *)alloc_info );
420429 alloc_info->signature = 0x0 ;
421430 heap_stats.current_size -= user_size;
422431 heap_stats.alloc_cnt -= 1 ;
0 commit comments