Skip to content

Commit 6aab060

Browse files
authored
[HAL] Improve memory tracer
Fixed a bug and compiler warning in the memory tracer implementation.
1 parent 9111aa4 commit 6aab060

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
/******************************************************************************/
@@ -259,12 +267,12 @@ extern "C" void* $Sub$$realloc(void *ptr, size_t size) {
259267
free(ptr);
260268
}
261269
#else // #ifdef MBED_HEAP_STATS_ENABLED
262-
mem_trace_mutex->lock();
263270
new_ptr = $Super$$realloc(ptr, size);
264-
mem_trace_mutex->unlock();
265271
#endif // #ifdef MBED_HEAP_STATS_ENABLED
266272
#ifdef MBED_MEM_TRACING_ENABLED
273+
mem_trace_mutex->lock();
267274
mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
275+
mem_trace_mutex->unlock();
268276
#endif // #ifdef MBED_MEM_TRACING_ENABLED
269277
return new_ptr;
270278
}

0 commit comments

Comments
 (0)