Skip to content

Commit b2ce50a

Browse files
authored
Merge pull request #2596 from neilt6/neilt6-memory-tracer-patch
[HAL] Improve memory tracer
2 parents fe8bd7d + 6aab060 commit b2ce50a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

hal/common/mbed_alloc_wrappers.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,23 @@ typedef struct {
4646
uint32_t pad;
4747
} alloc_info_t;
4848

49-
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
49+
#ifdef MBED_MEM_TRACING_ENABLED
5050
static SingletonPtr<PlatformMutex> mem_trace_mutex;
51+
#endif
52+
#ifdef MBED_HEAP_STATS_ENABLED
53+
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
5154
static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0};
55+
#endif
5256

5357
void mbed_stats_heap_get(mbed_stats_heap_t *stats)
5458
{
59+
#ifdef MBED_HEAP_STATS_ENABLED
5560
malloc_stats_mutex->lock();
5661
memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t));
5762
malloc_stats_mutex->unlock();
63+
#else
64+
memset(stats, 0, sizeof(mbed_stats_heap_t));
65+
#endif
5866
}
5967

6068
/******************************************************************************/
@@ -260,12 +268,12 @@ extern "C" void* $Sub$$realloc(void *ptr, size_t size) {
260268
free(ptr);
261269
}
262270
#else // #ifdef MBED_HEAP_STATS_ENABLED
263-
mem_trace_mutex->lock();
264271
new_ptr = $Super$$realloc(ptr, size);
265-
mem_trace_mutex->unlock();
266272
#endif // #ifdef MBED_HEAP_STATS_ENABLED
267273
#ifdef MBED_MEM_TRACING_ENABLED
274+
mem_trace_mutex->lock();
268275
mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
276+
mem_trace_mutex->unlock();
269277
#endif // #ifdef MBED_MEM_TRACING_ENABLED
270278
return new_ptr;
271279
}

0 commit comments

Comments
 (0)