@@ -46,9 +46,6 @@ typedef struct {
46
46
uint32_t pad;
47
47
} alloc_info_t ;
48
48
49
- #ifdef MBED_MEM_TRACING_ENABLED
50
- static SingletonPtr<PlatformMutex> mem_trace_mutex;
51
- #endif
52
49
#ifdef MBED_HEAP_STATS_ENABLED
53
50
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
54
51
static mbed_stats_heap_t heap_stats = {0 , 0 , 0 , 0 , 0 };
@@ -91,6 +88,9 @@ extern "C" {
91
88
92
89
extern " C" void * __wrap__malloc_r (struct _reent * r, size_t size) {
93
90
void *ptr = NULL ;
91
+ #ifdef MBED_MEM_TRACING_ENABLED
92
+ mbed_mem_trace_lock ();
93
+ #endif
94
94
#ifdef MBED_HEAP_STATS_ENABLED
95
95
malloc_stats_mutex->lock ();
96
96
alloc_info_t *alloc_info = (alloc_info_t *)__real__malloc_r (r, size + sizeof (alloc_info_t ));
@@ -111,15 +111,17 @@ extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) {
111
111
ptr = __real__malloc_r (r, size);
112
112
#endif // #ifdef MBED_HEAP_STATS_ENABLED
113
113
#ifdef MBED_MEM_TRACING_ENABLED
114
- mem_trace_mutex->lock ();
115
114
mbed_mem_trace_malloc (ptr, size, MBED_CALLER_ADDR ());
116
- mem_trace_mutex-> unlock ();
115
+ mbed_mem_trace_unlock ();
117
116
#endif // #ifdef MBED_MEM_TRACING_ENABLED
118
117
return ptr;
119
118
}
120
119
121
120
extern " C" void * __wrap__realloc_r (struct _reent * r, void * ptr, size_t size) {
122
121
void *new_ptr = NULL ;
122
+ #ifdef MBED_MEM_TRACING_ENABLED
123
+ mbed_mem_trace_lock ();
124
+ #endif
123
125
#ifdef MBED_HEAP_STATS_ENABLED
124
126
// Implement realloc_r with malloc and free.
125
127
// The function realloc_r can't be used here directly since
@@ -151,14 +153,16 @@ extern "C" void * __wrap__realloc_r(struct _reent * r, void * ptr, size_t size)
151
153
new_ptr = __real__realloc_r (r, ptr, size);
152
154
#endif // #ifdef MBED_HEAP_STATS_ENABLED
153
155
#ifdef MBED_MEM_TRACING_ENABLED
154
- mem_trace_mutex->lock ();
155
156
mbed_mem_trace_realloc (new_ptr, ptr, size, MBED_CALLER_ADDR ());
156
- mem_trace_mutex-> unlock ();
157
+ mbed_mem_trace_unlock ();
157
158
#endif // #ifdef MBED_MEM_TRACING_ENABLED
158
159
return new_ptr;
159
160
}
160
161
161
162
extern " C" void __wrap__free_r (struct _reent * r, void * ptr) {
163
+ #ifdef MBED_MEM_TRACING_ENABLED
164
+ mbed_mem_trace_lock ();
165
+ #endif
162
166
#ifdef MBED_HEAP_STATS_ENABLED
163
167
malloc_stats_mutex->lock ();
164
168
alloc_info_t *alloc_info = NULL ;
@@ -173,14 +177,16 @@ extern "C" void __wrap__free_r(struct _reent * r, void * ptr) {
173
177
__real__free_r (r, ptr);
174
178
#endif // #ifdef MBED_HEAP_STATS_ENABLED
175
179
#ifdef MBED_MEM_TRACING_ENABLED
176
- mem_trace_mutex->lock ();
177
180
mbed_mem_trace_free (ptr, MBED_CALLER_ADDR ());
178
- mem_trace_mutex-> unlock ();
181
+ mbed_mem_trace_unlock ();
179
182
#endif // #ifdef MBED_MEM_TRACING_ENABLED
180
183
}
181
184
182
185
extern " C" void * __wrap__calloc_r (struct _reent * r, size_t nmemb, size_t size) {
183
186
void *ptr = NULL ;
187
+ #ifdef MBED_MEM_TRACING_ENABLED
188
+ mbed_mem_trace_lock ();
189
+ #endif
184
190
#ifdef MBED_HEAP_STATS_ENABLED
185
191
// Note - no lock needed since malloc is thread safe
186
192
@@ -192,9 +198,8 @@ extern "C" void * __wrap__calloc_r(struct _reent * r, size_t nmemb, size_t size)
192
198
ptr = __real__calloc_r (r, nmemb, size);
193
199
#endif // #ifdef MBED_HEAP_STATS_ENABLED
194
200
#ifdef MBED_MEM_TRACING_ENABLED
195
- mem_trace_mutex->lock ();
196
201
mbed_mem_trace_calloc (ptr, nmemb, size, MBED_CALLER_ADDR ());
197
- mem_trace_mutex-> unlock ();
202
+ mbed_mem_trace_unlock ();
198
203
#endif // #ifdef MBED_MEM_TRACING_ENABLED
199
204
return ptr;
200
205
}
@@ -244,6 +249,9 @@ extern "C" {
244
249
245
250
extern " C" void * SUB_MALLOC (size_t size) {
246
251
void *ptr = NULL ;
252
+ #ifdef MBED_MEM_TRACING_ENABLED
253
+ mbed_mem_trace_lock ();
254
+ #endif
247
255
#ifdef MBED_HEAP_STATS_ENABLED
248
256
malloc_stats_mutex->lock ();
249
257
alloc_info_t *alloc_info = (alloc_info_t *)SUPER_MALLOC (size + sizeof (alloc_info_t ));
@@ -264,15 +272,17 @@ extern "C" void* SUB_MALLOC(size_t size) {
264
272
ptr = SUPER_MALLOC (size);
265
273
#endif // #ifdef MBED_HEAP_STATS_ENABLED
266
274
#ifdef MBED_MEM_TRACING_ENABLED
267
- mem_trace_mutex->lock ();
268
275
mbed_mem_trace_malloc (ptr, size, MBED_CALLER_ADDR ());
269
- mem_trace_mutex-> unlock ();
276
+ mbed_mem_trace_unlock ();
270
277
#endif // #ifdef MBED_MEM_TRACING_ENABLED
271
278
return ptr;
272
279
}
273
280
274
281
extern " C" void * SUB_REALLOC (void *ptr, size_t size) {
275
282
void *new_ptr = NULL ;
283
+ #ifdef MBED_MEM_TRACING_ENABLED
284
+ mbed_mem_trace_lock ();
285
+ #endif
276
286
#ifdef MBED_HEAP_STATS_ENABLED
277
287
// Note - no lock needed since malloc and free are thread safe
278
288
@@ -299,15 +309,17 @@ extern "C" void* SUB_REALLOC(void *ptr, size_t size) {
299
309
new_ptr = SUPER_REALLOC (ptr, size);
300
310
#endif // #ifdef MBED_HEAP_STATS_ENABLED
301
311
#ifdef MBED_MEM_TRACING_ENABLED
302
- mem_trace_mutex->lock ();
303
312
mbed_mem_trace_realloc (new_ptr, ptr, size, MBED_CALLER_ADDR ());
304
- mem_trace_mutex-> unlock ();
313
+ mbed_mem_trace_unlock ();
305
314
#endif // #ifdef MBED_MEM_TRACING_ENABLED
306
315
return new_ptr;
307
316
}
308
317
309
318
extern " C" void *SUB_CALLOC (size_t nmemb, size_t size) {
310
319
void *ptr = NULL ;
320
+ #ifdef MBED_MEM_TRACING_ENABLED
321
+ mbed_mem_trace_lock ();
322
+ #endif
311
323
#ifdef MBED_HEAP_STATS_ENABLED
312
324
// Note - no lock needed since malloc is thread safe
313
325
ptr = malloc (nmemb * size);
@@ -318,14 +330,16 @@ extern "C" void *SUB_CALLOC(size_t nmemb, size_t size) {
318
330
ptr = SUPER_CALLOC (nmemb, size);
319
331
#endif // #ifdef MBED_HEAP_STATS_ENABLED
320
332
#ifdef MBED_MEM_TRACING_ENABLED
321
- mem_trace_mutex->lock ();
322
333
mbed_mem_trace_calloc (ptr, nmemb, size, MBED_CALLER_ADDR ());
323
- mem_trace_mutex-> unlock ();
334
+ mbed_mem_trace_unlock ();
324
335
#endif // #ifdef MBED_MEM_TRACING_ENABLED
325
336
return ptr;
326
337
}
327
338
328
339
extern " C" void SUB_FREE (void *ptr) {
340
+ #ifdef MBED_MEM_TRACING_ENABLED
341
+ mbed_mem_trace_lock ();
342
+ #endif
329
343
#ifdef MBED_HEAP_STATS_ENABLED
330
344
malloc_stats_mutex->lock ();
331
345
alloc_info_t *alloc_info = NULL ;
@@ -340,9 +354,8 @@ extern "C" void SUB_FREE(void *ptr) {
340
354
SUPER_FREE (ptr);
341
355
#endif // #ifdef MBED_HEAP_STATS_ENABLED
342
356
#ifdef MBED_MEM_TRACING_ENABLED
343
- mem_trace_mutex->lock ();
344
357
mbed_mem_trace_free (ptr, MBED_CALLER_ADDR ());
345
- mem_trace_mutex-> unlock ();
358
+ mbed_mem_trace_unlock ();
346
359
#endif // #ifdef MBED_MEM_TRACING_ENABLED
347
360
}
348
361
0 commit comments