forked from ARMmbed/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbed_alloc_wrappers.cpp
471 lines (416 loc) · 15 KB
/
mbed_alloc_wrappers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* mbed Microcontroller Library
* Copyright (c) 2006-2016 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "platform/mbed_mem_trace.h"
#include "platform/mbed_stats.h"
#include "platform/mbed_toolchain.h"
#include "platform/SingletonPtr.h"
#include "platform/PlatformMutex.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* There are two memory tracers in mbed OS:
- the first can be used to detect the maximum heap usage at runtime. It is
activated by setting the configuration option MBED_HEAP_STATS_ENABLED to true.
- the second can be used to trace each memory call by automatically invoking
a callback on each memory operation (see hal/api/mbed_mem_trace.h). It is
activated by setting the configuration option MBED_MEM_TRACING_ENABLED to true.
Both tracers can be activated and deactivated in any combination. If both tracers
are active, the second one (MBED_MEM_TRACING_ENABLED) will trace the first one's
(MBED_HEAP_STATS_ENABLED) memory calls.*/
/******************************************************************************/
/* Implementation of the runtime max heap usage checker */
/******************************************************************************/
typedef struct {
uint32_t size;
uint32_t signature;
} alloc_info_t;
#if MBED_HEAP_STATS_ENABLED
#define MBED_HEAP_STATS_SIGNATURE (0xdeadbeef)
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0, 0, 0};
typedef struct mbed_heap_overhead {
int size; // Size of the allocated memory block, including internal overhead size
struct mbed_heap_overhead *next; // The memory is either the next free block, or allocated memory block
} mbed_heap_overhead_t;
static int get_malloc_block_total_size(void *ptr)
{
mbed_heap_overhead_t *c = (mbed_heap_overhead_t *)((char *)ptr - offsetof(mbed_heap_overhead, next));
// Skip the padding area
if (c->size < 0) {
c = (mbed_heap_overhead_t *)((char *)c + c->size);
}
// Mask LSB as it is used for usage flags
return (c->size & ~0x1);
}
#endif
void mbed_stats_heap_get(mbed_stats_heap_t *stats)
{
#if MBED_HEAP_STATS_ENABLED
extern uint32_t mbed_heap_size;
heap_stats.reserved_size = mbed_heap_size;
malloc_stats_mutex->lock();
memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t));
malloc_stats_mutex->unlock();
#else
memset(stats, 0, sizeof(mbed_stats_heap_t));
#endif
}
/******************************************************************************/
/* GCC memory allocation wrappers */
/******************************************************************************/
#if defined(TOOLCHAIN_GCC)
extern "C" {
void *__real__malloc_r(struct _reent *r, size_t size);
void *__real__memalign_r(struct _reent *r, size_t alignment, size_t bytes);
void *__real__realloc_r(struct _reent *r, void *ptr, size_t size);
void __real__free_r(struct _reent *r, void *ptr);
void *__real__calloc_r(struct _reent *r, size_t nmemb, size_t size);
void *malloc_wrapper(struct _reent *r, size_t size, void *caller);
void free_wrapper(struct _reent *r, void *ptr, void *caller);
}
extern "C" void *__wrap__malloc_r(struct _reent *r, size_t size)
{
return malloc_wrapper(r, size, MBED_CALLER_ADDR());
}
extern "C" void *malloc_wrapper(struct _reent *r, size_t size, void *caller)
{
void *ptr = NULL;
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock();
#endif
#if MBED_HEAP_STATS_ENABLED
malloc_stats_mutex->lock();
alloc_info_t *alloc_info = NULL;
if (size <= SIZE_MAX - sizeof(alloc_info_t)) {
alloc_info = (alloc_info_t *)__real__malloc_r(r, size + sizeof(alloc_info_t));
}
if (alloc_info != NULL) {
alloc_info->size = size;
alloc_info->signature = MBED_HEAP_STATS_SIGNATURE;
ptr = (void *)(alloc_info + 1);
heap_stats.current_size += size;
heap_stats.total_size += size;
heap_stats.alloc_cnt += 1;
if (heap_stats.current_size > heap_stats.max_size) {
heap_stats.max_size = heap_stats.current_size;
}
heap_stats.overhead_size += get_malloc_block_total_size((void *)alloc_info) - size;
} else {
heap_stats.alloc_fail_cnt += 1;
}
malloc_stats_mutex->unlock();
#else // #if MBED_HEAP_STATS_ENABLED
ptr = __real__malloc_r(r, size);
#endif // #if MBED_HEAP_STATS_ENABLED
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_malloc(ptr, size, caller);
mbed_mem_trace_unlock();
#endif // #if MBED_MEM_TRACING_ENABLED
return ptr;
}
extern "C" void *__wrap__realloc_r(struct _reent *r, void *ptr, size_t size)
{
void *new_ptr = NULL;
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock();
#endif
#if MBED_HEAP_STATS_ENABLED
// Implement realloc_r with malloc and free.
// The function realloc_r can't be used here directly since
// it can call into __wrap__malloc_r (returns ptr + 4) or
// resize memory directly (returns ptr + 0).
// Note - no lock needed since malloc and free are thread safe
// Get old size
uint32_t old_size = 0;
if (ptr != NULL) {
alloc_info_t *alloc_info = ((alloc_info_t *)ptr) - 1;
old_size = alloc_info->size;
}
// Allocate space
if (size != 0) {
new_ptr = malloc(size);
}
// If the new buffer has been allocated copy the data to it
// and free the old buffer
if (new_ptr != NULL) {
uint32_t copy_size = (old_size < size) ? old_size : size;
memcpy(new_ptr, (void *)ptr, copy_size);
free(ptr);
}
#else // #if MBED_HEAP_STATS_ENABLED
new_ptr = __real__realloc_r(r, ptr, size);
#endif // #if MBED_HEAP_STATS_ENABLED
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
mbed_mem_trace_unlock();
#endif // #if MBED_MEM_TRACING_ENABLED
return new_ptr;
}
extern "C" void __wrap__free_r(struct _reent *r, void *ptr)
{
free_wrapper(r, ptr, MBED_CALLER_ADDR());
}
extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller)
{
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock();
#endif
#if MBED_HEAP_STATS_ENABLED
malloc_stats_mutex->lock();
alloc_info_t *alloc_info = NULL;
if (ptr != NULL) {
alloc_info = ((alloc_info_t *)ptr) - 1;
if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature) {
size_t user_size = alloc_info->size;
size_t alloc_size = get_malloc_block_total_size((void *)alloc_info);
alloc_info->signature = 0x0;
heap_stats.current_size -= user_size;
heap_stats.alloc_cnt -= 1;
heap_stats.overhead_size -= (alloc_size - user_size);
__real__free_r(r, (void *)alloc_info);
} else {
__real__free_r(r, ptr);
}
}
malloc_stats_mutex->unlock();
#else // #if MBED_HEAP_STATS_ENABLED
__real__free_r(r, ptr);
#endif // #if MBED_HEAP_STATS_ENABLED
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_free(ptr, caller);
mbed_mem_trace_unlock();
#endif // #if MBED_MEM_TRACING_ENABLED
}
extern "C" void *__wrap__calloc_r(struct _reent *r, size_t nmemb, size_t size)
{
void *ptr = NULL;
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock();
#endif
#if MBED_HEAP_STATS_ENABLED
// Note - no lock needed since malloc is thread safe
ptr = malloc(nmemb * size);
if (ptr != NULL) {
memset(ptr, 0, nmemb * size);
}
#else // #if MBED_HEAP_STATS_ENABLED
ptr = __real__calloc_r(r, nmemb, size);
#endif // #if MBED_HEAP_STATS_ENABLED
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
mbed_mem_trace_unlock();
#endif // #if MBED_MEM_TRACING_ENABLED
return ptr;
}
extern "C" void *__wrap__memalign_r(struct _reent *r, size_t alignment, size_t bytes)
{
return __real__memalign_r(r, alignment, bytes);
}
/******************************************************************************/
/* ARMCC / IAR memory allocation wrappers */
/******************************************************************************/
#elif defined(TOOLCHAIN_ARM) || defined(__ICCARM__)
#if defined(TOOLCHAIN_ARM)
#define SUPER_MALLOC $Super$$malloc
#define SUB_MALLOC $Sub$$malloc
#define SUPER_REALLOC $Super$$realloc
#define SUB_REALLOC $Sub$$realloc
#define SUPER_CALLOC $Super$$calloc
#define SUB_CALLOC $Sub$$calloc
#define SUPER_FREE $Super$$free
#define SUB_FREE $Sub$$free
#elif defined(__ICCARM__)
#define SUPER_MALLOC $Super$$__iar_dlmalloc
#define SUB_MALLOC $Sub$$__iar_dlmalloc
#define SUPER_REALLOC $Super$$__iar_dlrealloc
#define SUB_REALLOC $Sub$$__iar_dlrealloc
#define SUPER_CALLOC $Super$$__iar_dlcalloc
#define SUB_CALLOC $Sub$$__iar_dlcalloc
#define SUPER_FREE $Super$$__iar_dlfree
#define SUB_FREE $Sub$$__iar_dlfree
#endif
/* Enable hooking of memory function only if tracing is also enabled */
#if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
extern "C" {
void *SUPER_MALLOC(size_t size);
void *SUPER_REALLOC(void *ptr, size_t size);
void *SUPER_CALLOC(size_t nmemb, size_t size);
void SUPER_FREE(void *ptr);
void *malloc_wrapper(size_t size, void *caller);
void free_wrapper(void *ptr, void *caller);
}
extern "C" void *SUB_MALLOC(size_t size)
{
return malloc_wrapper(size, MBED_CALLER_ADDR());
}
extern "C" void *malloc_wrapper(size_t size, void *caller)
{
void *ptr = NULL;
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock();
#endif
#if MBED_HEAP_STATS_ENABLED
malloc_stats_mutex->lock();
alloc_info_t *alloc_info = NULL;
if (size <= SIZE_MAX - sizeof(alloc_info_t)) {
alloc_info = (alloc_info_t *)SUPER_MALLOC(size + sizeof(alloc_info_t));
}
if (alloc_info != NULL) {
alloc_info->size = size;
alloc_info->signature = MBED_HEAP_STATS_SIGNATURE;
ptr = (void *)(alloc_info + 1);
heap_stats.current_size += size;
heap_stats.total_size += size;
heap_stats.alloc_cnt += 1;
if (heap_stats.current_size > heap_stats.max_size) {
heap_stats.max_size = heap_stats.current_size;
}
heap_stats.overhead_size += get_malloc_block_total_size((void *)alloc_info) - size;
} else {
heap_stats.alloc_fail_cnt += 1;
}
malloc_stats_mutex->unlock();
#else // #if MBED_HEAP_STATS_ENABLED
ptr = SUPER_MALLOC(size);
#endif // #if MBED_HEAP_STATS_ENABLED
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_malloc(ptr, size, caller);
mbed_mem_trace_unlock();
#endif // #if MBED_MEM_TRACING_ENABLED
return ptr;
}
extern "C" void *SUB_REALLOC(void *ptr, size_t size)
{
void *new_ptr = NULL;
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock();
#endif
#if MBED_HEAP_STATS_ENABLED
// Note - no lock needed since malloc and free are thread safe
// Get old size
uint32_t old_size = 0;
if (ptr != NULL) {
alloc_info_t *alloc_info = ((alloc_info_t *)ptr) - 1;
old_size = alloc_info->size;
}
// Allocate space
if (size != 0) {
new_ptr = malloc(size);
}
// If the new buffer has been allocated copy the data to it
// and free the old buffer
if ((new_ptr != NULL) && (ptr != NULL)) {
uint32_t copy_size = (old_size < size) ? old_size : size;
memcpy(new_ptr, (void *)ptr, copy_size);
free(ptr);
}
{
volatile uint8_t dummy = 0;
if (dummy != 0) { // always false
// this code will never be executed
// it's just to tell the compiler/linker to preserve SUB_REALLOC symbol
// when LTO enabled
SUPER_REALLOC(NULL, 0);
}
}
#else // #if MBED_HEAP_STATS_ENABLED
new_ptr = SUPER_REALLOC(ptr, size);
#endif // #if MBED_HEAP_STATS_ENABLED
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
mbed_mem_trace_unlock();
#endif // #if MBED_MEM_TRACING_ENABLED
return new_ptr;
}
extern "C" void *SUB_CALLOC(size_t nmemb, size_t size)
{
void *ptr = NULL;
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock();
#endif
#if MBED_HEAP_STATS_ENABLED
// Note - no lock needed since malloc is thread safe
ptr = malloc(nmemb * size);
if (ptr != NULL) {
memset(ptr, 0, nmemb * size);
}
{
volatile uint8_t dummy = 0;
if (dummy != 0) { // always false
// this code will never be executed
// it's just to tell the compiler/linker to preserve SUB_CALLOC symbol
// when LTO enabled
SUPER_CALLOC(NULL, 0);
}
}
#else // #if MBED_HEAP_STATS_ENABLED
ptr = SUPER_CALLOC(nmemb, size);
#endif // #if MBED_HEAP_STATS_ENABLED
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
mbed_mem_trace_unlock();
#endif // #if MBED_MEM_TRACING_ENABLED
return ptr;
}
extern "C" void SUB_FREE(void *ptr)
{
free_wrapper(ptr, MBED_CALLER_ADDR());
}
extern "C" void free_wrapper(void *ptr, void *caller)
{
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_lock();
#endif
#if MBED_HEAP_STATS_ENABLED
malloc_stats_mutex->lock();
alloc_info_t *alloc_info = NULL;
if (ptr != NULL) {
alloc_info = ((alloc_info_t *)ptr) - 1;
if (MBED_HEAP_STATS_SIGNATURE == alloc_info->signature) {
size_t user_size = alloc_info->size;
size_t alloc_size = get_malloc_block_total_size((void *)alloc_info);
alloc_info->signature = 0x0;
heap_stats.current_size -= user_size;
heap_stats.alloc_cnt -= 1;
heap_stats.overhead_size -= (alloc_size - user_size);
SUPER_FREE((void *)alloc_info);
} else {
SUPER_FREE(ptr);
}
}
malloc_stats_mutex->unlock();
#else // #if MBED_HEAP_STATS_ENABLED
SUPER_FREE(ptr);
#endif // #if MBED_HEAP_STATS_ENABLED
#if MBED_MEM_TRACING_ENABLED
mbed_mem_trace_free(ptr, caller);
mbed_mem_trace_unlock();
#endif // #if MBED_MEM_TRACING_ENABLED
}
#endif // #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
/******************************************************************************/
/* Allocation wrappers for other toolchains are not supported yet */
/******************************************************************************/
#else
#if MBED_MEM_TRACING_ENABLED
#error Memory tracing is not supported with the current toolchain.
#endif
#if MBED_HEAP_STATS_ENABLED
#error Heap statistics are not supported with the current toolchain.
#endif
#endif // #if defined(TOOLCHAIN_GCC)