@@ -52,53 +52,24 @@ 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
- #if defined(TOOLCHAIN_GCC)
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
58
+ } mbed_heap_overhead_t ;
56
59
57
- typedef struct malloc_internal_overhead {
58
- /* ------------------
59
- * malloc_internal_overhead_t->| size (4 bytes) |
60
- * ------------------
61
- * | Padding for |
62
- * | alignment |
63
- * | holding neg |
64
- * | offset to size |
65
- * ------------------
66
- * mem_ptr->| point to next |
67
- * | free when freed|
68
- * | or data load |
69
- * | when allocated |
70
- * ------------------
71
- */
72
- /* size of the allocated payload area, including size before
73
- OVERHEAD_OFFSET */
74
- int size;
75
-
76
- /* since here, the memory is either the next free block, or data load */
77
- struct malloc_internal_overhead *next;
78
- } malloc_internal_overhead_t ;
79
-
80
- #define OVERHEAD_OFFSET ((size_t )(&(((struct malloc_internal_overhead *)0 )->next)))
81
-
82
- static int get_malloc_internal_overhead (void *ptr)
60
+ static int get_malloc_block_total_size (void *ptr)
83
61
{
84
- malloc_internal_overhead_t *c = (malloc_internal_overhead_t *)((char *)ptr - OVERHEAD_OFFSET );
62
+ mbed_heap_overhead_t *c = (mbed_heap_overhead_t *)((char *)ptr - offsetof (mbed_heap_overhead, next) );
85
63
86
- /* Skip the padding area */
64
+ // Skip the padding area
87
65
if (c->size < 0 ) {
88
- c = (malloc_internal_overhead_t *)((char *)c + c->size );
66
+ c = (mbed_heap_overhead_t *)((char *)c + c->size );
89
67
}
68
+ // Mask LSB as it is used for usage flags
90
69
return (c->size & ~0x1 );
91
70
}
92
- #else
93
- typedef struct {
94
- size_t size;
95
- } mbed_heap_overhead_t ;
96
-
97
- #define MALLOC_HEADER_SIZE (sizeof (mbed_heap_overhead_t ))
98
- #define MALLOC_HEADER_PTR (p ) (mbed_heap_overhead_t *)((char *)(p) - MALLOC_HEADER_SIZE)
99
- #define MALLOC_HEAP_TOTAL_SIZE (p ) (((p)->size) & (~0x1 ))
100
- #endif
101
71
#endif
72
+
102
73
void mbed_stats_heap_get (mbed_stats_heap_t *stats)
103
74
{
104
75
#if MBED_HEAP_STATS_ENABLED
@@ -154,7 +125,7 @@ extern "C" void *malloc_wrapper(struct _reent *r, size_t size, void *caller)
154
125
if (heap_stats.current_size > heap_stats.max_size ) {
155
126
heap_stats.max_size = heap_stats.current_size ;
156
127
}
157
- heap_stats.overhead_size += get_malloc_internal_overhead ((void *)alloc_info) - size;
128
+ heap_stats.overhead_size += get_malloc_block_total_size ((void *)alloc_info) - size;
158
129
} else {
159
130
heap_stats.alloc_fail_cnt += 1 ;
160
131
}
@@ -229,7 +200,7 @@ extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller)
229
200
alloc_info = ((alloc_info_t *)ptr) - 1 ;
230
201
if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature ) {
231
202
size_t user_size = alloc_info->size ;
232
- size_t alloc_size = get_malloc_internal_overhead ((void *)alloc_info);
203
+ size_t alloc_size = get_malloc_block_total_size ((void *)alloc_info);
233
204
alloc_info->signature = 0x0 ;
234
205
heap_stats.current_size -= user_size;
235
206
heap_stats.alloc_cnt -= 1 ;
@@ -341,7 +312,7 @@ extern "C" void *malloc_wrapper(size_t size, void *caller)
341
312
if (heap_stats.current_size > heap_stats.max_size ) {
342
313
heap_stats.max_size = heap_stats.current_size ;
343
314
}
344
- 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;
345
316
} else {
346
317
heap_stats.alloc_fail_cnt += 1 ;
347
318
}
@@ -454,7 +425,7 @@ extern "C" void free_wrapper(void *ptr, void *caller)
454
425
alloc_info = ((alloc_info_t *)ptr) - 1 ;
455
426
if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature ) {
456
427
size_t user_size = alloc_info->size ;
457
- 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 );
458
429
alloc_info->signature = 0x0 ;
459
430
heap_stats.current_size -= user_size;
460
431
heap_stats.alloc_cnt -= 1 ;
0 commit comments