@@ -52,13 +52,22 @@ typedef struct {
52
52
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
53
53
static mbed_stats_heap_t heap_stats = {0 , 0 , 0 , 0 , 0 , 0 , 0 };
54
54
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
57
58
} mbed_heap_overhead_t ;
58
59
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
+ }
62
71
#endif
63
72
64
73
void 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)
116
125
if (heap_stats.current_size > heap_stats.max_size ) {
117
126
heap_stats.max_size = heap_stats.current_size ;
118
127
}
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;
120
129
} else {
121
130
heap_stats.alloc_fail_cnt += 1 ;
122
131
}
@@ -191,7 +200,7 @@ extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller)
191
200
alloc_info = ((alloc_info_t *)ptr) - 1 ;
192
201
if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature ) {
193
202
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 );
195
204
alloc_info->signature = 0x0 ;
196
205
heap_stats.current_size -= user_size;
197
206
heap_stats.alloc_cnt -= 1 ;
@@ -303,7 +312,7 @@ extern "C" void *malloc_wrapper(size_t size, void *caller)
303
312
if (heap_stats.current_size > heap_stats.max_size ) {
304
313
heap_stats.max_size = heap_stats.current_size ;
305
314
}
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;
307
316
} else {
308
317
heap_stats.alloc_fail_cnt += 1 ;
309
318
}
@@ -416,7 +425,7 @@ extern "C" void free_wrapper(void *ptr, void *caller)
416
425
alloc_info = ((alloc_info_t *)ptr) - 1 ;
417
426
if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature ) {
418
427
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 );
420
429
alloc_info->signature = 0x0 ;
421
430
heap_stats.current_size -= user_size;
422
431
heap_stats.alloc_cnt -= 1 ;
0 commit comments