Skip to content

Commit b72bd08

Browse files
authored
Merge pull request #4965 from deepikabhavnani/IAR_heap_stats
Add heap stats for IAR
2 parents 81fde11 + 459e7d4 commit b72bd08

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

TESTS/mbed_drivers/stats/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <stdlib.h>
2323
#include <stdio.h>
2424

25-
#if !defined(MBED_HEAP_STATS_ENABLED) || !MBED_HEAP_STATS_ENABLED || defined(__ICCARM__)
25+
#if !defined(MBED_HEAP_STATS_ENABLED)
2626
#error [NOT_SUPPORTED] test not supported
2727
#endif
2828

platform/mbed_alloc_wrappers.cpp

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -207,26 +207,46 @@ extern "C" void * __wrap__memalign_r(struct _reent * r, size_t alignment, size_t
207207

208208

209209
/******************************************************************************/
210-
/* ARMCC memory allocation wrappers */
210+
/* ARMCC / IAR memory allocation wrappers */
211211
/******************************************************************************/
212212

213-
#elif defined(TOOLCHAIN_ARM) // #if defined(TOOLCHAIN_GCC)
213+
#elif defined(TOOLCHAIN_ARM) || defined(__ICCARM__)
214+
215+
#if defined(TOOLCHAIN_ARM)
216+
#define SUPER_MALLOC $Super$$malloc
217+
#define SUB_MALLOC $Sub$$malloc
218+
#define SUPER_REALLOC $Super$$realloc
219+
#define SUB_REALLOC $Sub$$realloc
220+
#define SUPER_CALLOC $Super$$calloc
221+
#define SUB_CALLOC $Sub$$calloc
222+
#define SUPER_FREE $Super$$free
223+
#define SUB_FREE $Sub$$free
224+
#elif defined(__ICCARM__)
225+
#define SUPER_MALLOC $Super$$__iar_dlmalloc
226+
#define SUB_MALLOC $Sub$$__iar_dlmalloc
227+
#define SUPER_REALLOC $Super$$__iar_dlrealloc
228+
#define SUB_REALLOC $Sub$$__iar_dlrealloc
229+
#define SUPER_CALLOC $Super$$__iar_dlcalloc
230+
#define SUB_CALLOC $Sub$$__iar_dlcalloc
231+
#define SUPER_FREE $Super$$__iar_dlfree
232+
#define SUB_FREE $Sub$$__iar_dlfree
233+
#endif
214234

215235
/* Enable hooking of memory function only if tracing is also enabled */
216236
#if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
217237

218238
extern "C" {
219-
void *$Super$$malloc(size_t size);
220-
void *$Super$$realloc(void *ptr, size_t size);
221-
void *$Super$$calloc(size_t nmemb, size_t size);
222-
void $Super$$free(void *ptr);
239+
void *SUPER_MALLOC(size_t size);
240+
void *SUPER_REALLOC(void *ptr, size_t size);
241+
void *SUPER_CALLOC(size_t nmemb, size_t size);
242+
void SUPER_FREE(void *ptr);
223243
}
224244

225-
extern "C" void* $Sub$$malloc(size_t size) {
245+
extern "C" void* SUB_MALLOC(size_t size) {
226246
void *ptr = NULL;
227247
#ifdef MBED_HEAP_STATS_ENABLED
228248
malloc_stats_mutex->lock();
229-
alloc_info_t *alloc_info = (alloc_info_t*)$Super$$malloc(size + sizeof(alloc_info_t));
249+
alloc_info_t *alloc_info = (alloc_info_t*)SUPER_MALLOC(size + sizeof(alloc_info_t));
230250
if (alloc_info != NULL) {
231251
alloc_info->size = size;
232252
ptr = (void*)(alloc_info + 1);
@@ -241,7 +261,7 @@ extern "C" void* $Sub$$malloc(size_t size) {
241261
}
242262
malloc_stats_mutex->unlock();
243263
#else // #ifdef MBED_HEAP_STATS_ENABLED
244-
ptr = $Super$$malloc(size);
264+
ptr = SUPER_MALLOC(size);
245265
#endif // #ifdef MBED_HEAP_STATS_ENABLED
246266
#ifdef MBED_MEM_TRACING_ENABLED
247267
mem_trace_mutex->lock();
@@ -251,7 +271,7 @@ extern "C" void* $Sub$$malloc(size_t size) {
251271
return ptr;
252272
}
253273

254-
extern "C" void* $Sub$$realloc(void *ptr, size_t size) {
274+
extern "C" void* SUB_REALLOC(void *ptr, size_t size) {
255275
void *new_ptr = NULL;
256276
#ifdef MBED_HEAP_STATS_ENABLED
257277
// Note - no lock needed since malloc and free are thread safe
@@ -276,7 +296,7 @@ extern "C" void* $Sub$$realloc(void *ptr, size_t size) {
276296
free(ptr);
277297
}
278298
#else // #ifdef MBED_HEAP_STATS_ENABLED
279-
new_ptr = $Super$$realloc(ptr, size);
299+
new_ptr = SUPER_REALLOC(ptr, size);
280300
#endif // #ifdef MBED_HEAP_STATS_ENABLED
281301
#ifdef MBED_MEM_TRACING_ENABLED
282302
mem_trace_mutex->lock();
@@ -286,7 +306,7 @@ extern "C" void* $Sub$$realloc(void *ptr, size_t size) {
286306
return new_ptr;
287307
}
288308

289-
extern "C" void *$Sub$$calloc(size_t nmemb, size_t size) {
309+
extern "C" void *SUB_CALLOC(size_t nmemb, size_t size) {
290310
void *ptr = NULL;
291311
#ifdef MBED_HEAP_STATS_ENABLED
292312
// Note - no lock needed since malloc is thread safe
@@ -295,7 +315,7 @@ extern "C" void *$Sub$$calloc(size_t nmemb, size_t size) {
295315
memset(ptr, 0, nmemb * size);
296316
}
297317
#else // #ifdef MBED_HEAP_STATS_ENABLED
298-
ptr = $Super$$calloc(nmemb, size);
318+
ptr = SUPER_CALLOC(nmemb, size);
299319
#endif // #ifdef MBED_HEAP_STATS_ENABLED
300320
#ifdef MBED_MEM_TRACING_ENABLED
301321
mem_trace_mutex->lock();
@@ -305,7 +325,7 @@ extern "C" void *$Sub$$calloc(size_t nmemb, size_t size) {
305325
return ptr;
306326
}
307327

308-
extern "C" void $Sub$$free(void *ptr) {
328+
extern "C" void SUB_FREE(void *ptr) {
309329
#ifdef MBED_HEAP_STATS_ENABLED
310330
malloc_stats_mutex->lock();
311331
alloc_info_t *alloc_info = NULL;
@@ -314,10 +334,10 @@ extern "C" void $Sub$$free(void *ptr) {
314334
heap_stats.current_size -= alloc_info->size;
315335
heap_stats.alloc_cnt -= 1;
316336
}
317-
$Super$$free((void*)alloc_info);
337+
SUPER_FREE((void*)alloc_info);
318338
malloc_stats_mutex->unlock();
319339
#else // #ifdef MBED_HEAP_STATS_ENABLED
320-
$Super$$free(ptr);
340+
SUPER_FREE(ptr);
321341
#endif // #ifdef MBED_HEAP_STATS_ENABLED
322342
#ifdef MBED_MEM_TRACING_ENABLED
323343
mem_trace_mutex->lock();
@@ -332,15 +352,14 @@ extern "C" void $Sub$$free(void *ptr) {
332352
/* Allocation wrappers for other toolchains are not supported yet */
333353
/******************************************************************************/
334354

335-
#else // #if defined(TOOLCHAIN_GCC)
355+
#else
336356

337357
#ifdef MBED_MEM_TRACING_ENABLED
338-
#warning Memory tracing is not supported with the current toolchain.
358+
#error Memory tracing is not supported with the current toolchain.
339359
#endif
340360

341361
#ifdef MBED_HEAP_STATS_ENABLED
342-
#warning Heap statistics are not supported with the current toolchain.
362+
#error Heap statistics are not supported with the current toolchain.
343363
#endif
344364

345365
#endif // #if defined(TOOLCHAIN_GCC)
346-

0 commit comments

Comments
 (0)