Skip to content

Commit f3c9132

Browse files
committed
Incorporated the review comments
1 parent d30ba6b commit f3c9132

File tree

1 file changed

+14
-43
lines changed

1 file changed

+14
-43
lines changed

platform/source/mbed_alloc_wrappers.cpp

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,53 +52,24 @@ typedef struct {
5252
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
5353
static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0, 0, 0};
5454

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;
5659

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)
8361
{
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));
8563

86-
/* Skip the padding area */
64+
// Skip the padding area
8765
if (c->size < 0) {
88-
c = (malloc_internal_overhead_t *)((char *)c + c->size);
66+
c = (mbed_heap_overhead_t *)((char *)c + c->size);
8967
}
68+
// Mask LSB as it is used for usage flags
9069
return (c->size & ~0x1);
9170
}
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
10171
#endif
72+
10273
void mbed_stats_heap_get(mbed_stats_heap_t *stats)
10374
{
10475
#if MBED_HEAP_STATS_ENABLED
@@ -154,7 +125,7 @@ extern "C" void *malloc_wrapper(struct _reent *r, size_t size, void *caller)
154125
if (heap_stats.current_size > heap_stats.max_size) {
155126
heap_stats.max_size = heap_stats.current_size;
156127
}
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;
158129
} else {
159130
heap_stats.alloc_fail_cnt += 1;
160131
}
@@ -229,7 +200,7 @@ extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller)
229200
alloc_info = ((alloc_info_t *)ptr) - 1;
230201
if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature) {
231202
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);
233204
alloc_info->signature = 0x0;
234205
heap_stats.current_size -= user_size;
235206
heap_stats.alloc_cnt -= 1;
@@ -341,7 +312,7 @@ extern "C" void *malloc_wrapper(size_t size, void *caller)
341312
if (heap_stats.current_size > heap_stats.max_size) {
342313
heap_stats.max_size = heap_stats.current_size;
343314
}
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;
345316
} else {
346317
heap_stats.alloc_fail_cnt += 1;
347318
}
@@ -454,7 +425,7 @@ extern "C" void free_wrapper(void *ptr, void *caller)
454425
alloc_info = ((alloc_info_t *)ptr) - 1;
455426
if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature) {
456427
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);
458429
alloc_info->signature = 0x0;
459430
heap_stats.current_size -= user_size;
460431
heap_stats.alloc_cnt -= 1;

0 commit comments

Comments
 (0)