@@ -48,7 +48,15 @@ typedef struct {
48
48
49
49
#ifdef MBED_HEAP_STATS_ENABLED
50
50
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
51
- static mbed_stats_heap_t heap_stats = {0 , 0 , 0 , 0 , 0 };
51
+ static mbed_stats_heap_t heap_stats = {0 , 0 , 0 , 0 , 0 , 0 , 0 };
52
+
53
+ typedef struct {
54
+ size_t size;
55
+ }mbed_heap_overhead_t ;
56
+
57
+ #define MALLOC_HEADER_SIZE (sizeof (mbed_heap_overhead_t ))
58
+ #define MALLOC_HEADER_PTR (p ) (mbed_heap_overhead_t *)((char *)(p) - MALLOC_HEADER_SIZE)
59
+ #define MALLOC_HEAP_TOTAL_SIZE (p ) (((p)->size) & (~0x1 ))
52
60
#endif
53
61
54
62
void mbed_stats_heap_get (mbed_stats_heap_t *stats)
@@ -71,7 +79,6 @@ void mbed_stats_heap_get(mbed_stats_heap_t *stats)
71
79
72
80
#if defined(TOOLCHAIN_GCC)
73
81
74
-
75
82
extern " C" {
76
83
void *__real__malloc_r (struct _reent *r, size_t size);
77
84
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)
106
113
if (heap_stats.current_size > heap_stats.max_size ) {
107
114
heap_stats.max_size = heap_stats.current_size ;
108
115
}
116
+ heap_stats.overhead_size += MALLOC_HEAP_TOTAL_SIZE (MALLOC_HEADER_PTR (alloc_info)) - size;
109
117
} else {
110
118
heap_stats.alloc_fail_cnt += 1 ;
111
119
}
@@ -178,10 +186,14 @@ extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller)
178
186
alloc_info_t *alloc_info = NULL ;
179
187
if (ptr != NULL ) {
180
188
alloc_info = ((alloc_info_t *)ptr) - 1 ;
181
- heap_stats.current_size -= alloc_info->size ;
189
+ size_t user_size = alloc_info->size ;
190
+ size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE (MALLOC_HEADER_PTR (alloc_info));
191
+ heap_stats.current_size -= user_size;
182
192
heap_stats.alloc_cnt -= 1 ;
193
+ heap_stats.overhead_size -= (alloc_size - user_size);
183
194
}
184
195
__real__free_r (r, (void *)alloc_info);
196
+
185
197
malloc_stats_mutex->unlock ();
186
198
#else // #ifdef MBED_HEAP_STATS_ENABLED
187
199
__real__free_r (r, ptr);
@@ -260,7 +272,6 @@ extern "C" {
260
272
void free_wrapper (void *ptr, void *caller);
261
273
}
262
274
263
-
264
275
extern " C" void *SUB_MALLOC (size_t size)
265
276
{
266
277
return malloc_wrapper (size, MBED_CALLER_ADDR ());
@@ -284,6 +295,7 @@ extern "C" void *malloc_wrapper(size_t size, void *caller)
284
295
if (heap_stats.current_size > heap_stats.max_size ) {
285
296
heap_stats.max_size = heap_stats.current_size ;
286
297
}
298
+ heap_stats.overhead_size += MALLOC_HEAP_TOTAL_SIZE (MALLOC_HEADER_PTR (alloc_info)) - size;
287
299
} else {
288
300
heap_stats.alloc_fail_cnt += 1 ;
289
301
}
@@ -322,7 +334,7 @@ extern "C" void *SUB_REALLOC(void *ptr, size_t size)
322
334
323
335
// If the new buffer has been allocated copy the data to it
324
336
// and free the old buffer
325
- if (new_ptr != NULL ) {
337
+ if (( new_ptr != NULL ) && (ptr != NULL ) ) {
326
338
uint32_t copy_size = (old_size < size) ? old_size : size;
327
339
memcpy (new_ptr, (void *)ptr, copy_size);
328
340
free (ptr);
@@ -374,10 +386,14 @@ extern "C" void free_wrapper(void *ptr, void *caller)
374
386
alloc_info_t *alloc_info = NULL ;
375
387
if (ptr != NULL ) {
376
388
alloc_info = ((alloc_info_t *)ptr) - 1 ;
377
- heap_stats.current_size -= alloc_info->size ;
389
+ size_t user_size = alloc_info->size ;
390
+ size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE (MALLOC_HEADER_PTR (alloc_info));
391
+ heap_stats.current_size -= user_size;
378
392
heap_stats.alloc_cnt -= 1 ;
393
+ heap_stats.overhead_size -= (alloc_size - user_size);
379
394
}
380
395
SUPER_FREE ((void *)alloc_info);
396
+
381
397
malloc_stats_mutex->unlock ();
382
398
#else // #ifdef MBED_HEAP_STATS_ENABLED
383
399
SUPER_FREE (ptr);
0 commit comments