Skip to content

Commit 111d1ca

Browse files
authored
Merge pull request #11962 from evva-sfw/feature/Bug_in_mbed_alloc_wrappers.cpp
Bug in mbed_alloc_wrappers.cpp
2 parents 0edce8a + 0b36529 commit 111d1ca

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

platform/source/mbed_alloc_wrappers.cpp

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef struct {
4646
uint32_t signature;
4747
} alloc_info_t;
4848

49-
#ifdef MBED_HEAP_STATS_ENABLED
49+
#if MBED_HEAP_STATS_ENABLED
5050
#define MBED_HEAP_STATS_SIGNATURE (0xdeadbeef)
5151

5252
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
@@ -63,7 +63,7 @@ typedef struct {
6363

6464
void mbed_stats_heap_get(mbed_stats_heap_t *stats)
6565
{
66-
#ifdef MBED_HEAP_STATS_ENABLED
66+
#if MBED_HEAP_STATS_ENABLED
6767
extern uint32_t mbed_heap_size;
6868
heap_stats.reserved_size = mbed_heap_size;
6969

@@ -100,10 +100,10 @@ extern "C" void *__wrap__malloc_r(struct _reent *r, size_t size)
100100
extern "C" void *malloc_wrapper(struct _reent *r, size_t size, void *caller)
101101
{
102102
void *ptr = NULL;
103-
#ifdef MBED_MEM_TRACING_ENABLED
103+
#if MBED_MEM_TRACING_ENABLED
104104
mbed_mem_trace_lock();
105105
#endif
106-
#ifdef MBED_HEAP_STATS_ENABLED
106+
#if MBED_HEAP_STATS_ENABLED
107107
malloc_stats_mutex->lock();
108108
alloc_info_t *alloc_info = (alloc_info_t *)__real__malloc_r(r, size + sizeof(alloc_info_t));
109109
if (alloc_info != NULL) {
@@ -121,23 +121,23 @@ extern "C" void *malloc_wrapper(struct _reent *r, size_t size, void *caller)
121121
heap_stats.alloc_fail_cnt += 1;
122122
}
123123
malloc_stats_mutex->unlock();
124-
#else // #ifdef MBED_HEAP_STATS_ENABLED
124+
#else // #if MBED_HEAP_STATS_ENABLED
125125
ptr = __real__malloc_r(r, size);
126-
#endif // #ifdef MBED_HEAP_STATS_ENABLED
127-
#ifdef MBED_MEM_TRACING_ENABLED
126+
#endif // #if MBED_HEAP_STATS_ENABLED
127+
#if MBED_MEM_TRACING_ENABLED
128128
mbed_mem_trace_malloc(ptr, size, caller);
129129
mbed_mem_trace_unlock();
130-
#endif // #ifdef MBED_MEM_TRACING_ENABLED
130+
#endif // #if MBED_MEM_TRACING_ENABLED
131131
return ptr;
132132
}
133133

134134
extern "C" void *__wrap__realloc_r(struct _reent *r, void *ptr, size_t size)
135135
{
136136
void *new_ptr = NULL;
137-
#ifdef MBED_MEM_TRACING_ENABLED
137+
#if MBED_MEM_TRACING_ENABLED
138138
mbed_mem_trace_lock();
139139
#endif
140-
#ifdef MBED_HEAP_STATS_ENABLED
140+
#if MBED_HEAP_STATS_ENABLED
141141
// Implement realloc_r with malloc and free.
142142
// The function realloc_r can't be used here directly since
143143
// it can call into __wrap__malloc_r (returns ptr + 4) or
@@ -164,13 +164,13 @@ extern "C" void *__wrap__realloc_r(struct _reent *r, void *ptr, size_t size)
164164
memcpy(new_ptr, (void *)ptr, copy_size);
165165
free(ptr);
166166
}
167-
#else // #ifdef MBED_HEAP_STATS_ENABLED
167+
#else // #if MBED_HEAP_STATS_ENABLED
168168
new_ptr = __real__realloc_r(r, ptr, size);
169-
#endif // #ifdef MBED_HEAP_STATS_ENABLED
170-
#ifdef MBED_MEM_TRACING_ENABLED
169+
#endif // #if MBED_HEAP_STATS_ENABLED
170+
#if MBED_MEM_TRACING_ENABLED
171171
mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
172172
mbed_mem_trace_unlock();
173-
#endif // #ifdef MBED_MEM_TRACING_ENABLED
173+
#endif // #if MBED_MEM_TRACING_ENABLED
174174
return new_ptr;
175175
}
176176

@@ -181,10 +181,10 @@ extern "C" void __wrap__free_r(struct _reent *r, void *ptr)
181181

182182
extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller)
183183
{
184-
#ifdef MBED_MEM_TRACING_ENABLED
184+
#if MBED_MEM_TRACING_ENABLED
185185
mbed_mem_trace_lock();
186186
#endif
187-
#ifdef MBED_HEAP_STATS_ENABLED
187+
#if MBED_HEAP_STATS_ENABLED
188188
malloc_stats_mutex->lock();
189189
alloc_info_t *alloc_info = NULL;
190190
if (ptr != NULL) {
@@ -203,35 +203,35 @@ extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller)
203203
}
204204

205205
malloc_stats_mutex->unlock();
206-
#else // #ifdef MBED_HEAP_STATS_ENABLED
206+
#else // #if MBED_HEAP_STATS_ENABLED
207207
__real__free_r(r, ptr);
208-
#endif // #ifdef MBED_HEAP_STATS_ENABLED
209-
#ifdef MBED_MEM_TRACING_ENABLED
208+
#endif // #if MBED_HEAP_STATS_ENABLED
209+
#if MBED_MEM_TRACING_ENABLED
210210
mbed_mem_trace_free(ptr, caller);
211211
mbed_mem_trace_unlock();
212-
#endif // #ifdef MBED_MEM_TRACING_ENABLED
212+
#endif // #if MBED_MEM_TRACING_ENABLED
213213
}
214214

215215
extern "C" void *__wrap__calloc_r(struct _reent *r, size_t nmemb, size_t size)
216216
{
217217
void *ptr = NULL;
218-
#ifdef MBED_MEM_TRACING_ENABLED
218+
#if MBED_MEM_TRACING_ENABLED
219219
mbed_mem_trace_lock();
220220
#endif
221-
#ifdef MBED_HEAP_STATS_ENABLED
221+
#if MBED_HEAP_STATS_ENABLED
222222
// Note - no lock needed since malloc is thread safe
223223

224224
ptr = malloc(nmemb * size);
225225
if (ptr != NULL) {
226226
memset(ptr, 0, nmemb * size);
227227
}
228-
#else // #ifdef MBED_HEAP_STATS_ENABLED
228+
#else // #if MBED_HEAP_STATS_ENABLED
229229
ptr = __real__calloc_r(r, nmemb, size);
230-
#endif // #ifdef MBED_HEAP_STATS_ENABLED
231-
#ifdef MBED_MEM_TRACING_ENABLED
230+
#endif // #if MBED_HEAP_STATS_ENABLED
231+
#if MBED_MEM_TRACING_ENABLED
232232
mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
233233
mbed_mem_trace_unlock();
234-
#endif // #ifdef MBED_MEM_TRACING_ENABLED
234+
#endif // #if MBED_MEM_TRACING_ENABLED
235235
return ptr;
236236
}
237237

@@ -287,10 +287,10 @@ extern "C" void *SUB_MALLOC(size_t size)
287287
extern "C" void *malloc_wrapper(size_t size, void *caller)
288288
{
289289
void *ptr = NULL;
290-
#ifdef MBED_MEM_TRACING_ENABLED
290+
#if MBED_MEM_TRACING_ENABLED
291291
mbed_mem_trace_lock();
292292
#endif
293-
#ifdef MBED_HEAP_STATS_ENABLED
293+
#if MBED_HEAP_STATS_ENABLED
294294
malloc_stats_mutex->lock();
295295
alloc_info_t *alloc_info = (alloc_info_t *)SUPER_MALLOC(size + sizeof(alloc_info_t));
296296
if (alloc_info != NULL) {
@@ -308,24 +308,24 @@ extern "C" void *malloc_wrapper(size_t size, void *caller)
308308
heap_stats.alloc_fail_cnt += 1;
309309
}
310310
malloc_stats_mutex->unlock();
311-
#else // #ifdef MBED_HEAP_STATS_ENABLED
311+
#else // #if MBED_HEAP_STATS_ENABLED
312312
ptr = SUPER_MALLOC(size);
313-
#endif // #ifdef MBED_HEAP_STATS_ENABLED
314-
#ifdef MBED_MEM_TRACING_ENABLED
313+
#endif // #if MBED_HEAP_STATS_ENABLED
314+
#if MBED_MEM_TRACING_ENABLED
315315
mbed_mem_trace_malloc(ptr, size, caller);
316316
mbed_mem_trace_unlock();
317-
#endif // #ifdef MBED_MEM_TRACING_ENABLED
317+
#endif // #if MBED_MEM_TRACING_ENABLED
318318
return ptr;
319319
}
320320

321321

322322
extern "C" void *SUB_REALLOC(void *ptr, size_t size)
323323
{
324324
void *new_ptr = NULL;
325-
#ifdef MBED_MEM_TRACING_ENABLED
325+
#if MBED_MEM_TRACING_ENABLED
326326
mbed_mem_trace_lock();
327327
#endif
328-
#ifdef MBED_HEAP_STATS_ENABLED
328+
#if MBED_HEAP_STATS_ENABLED
329329
// Note - no lock needed since malloc and free are thread safe
330330

331331
// Get old size
@@ -347,35 +347,35 @@ extern "C" void *SUB_REALLOC(void *ptr, size_t size)
347347
memcpy(new_ptr, (void *)ptr, copy_size);
348348
free(ptr);
349349
}
350-
#else // #ifdef MBED_HEAP_STATS_ENABLED
350+
#else // #if MBED_HEAP_STATS_ENABLED
351351
new_ptr = SUPER_REALLOC(ptr, size);
352-
#endif // #ifdef MBED_HEAP_STATS_ENABLED
353-
#ifdef MBED_MEM_TRACING_ENABLED
352+
#endif // #if MBED_HEAP_STATS_ENABLED
353+
#if MBED_MEM_TRACING_ENABLED
354354
mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
355355
mbed_mem_trace_unlock();
356-
#endif // #ifdef MBED_MEM_TRACING_ENABLED
356+
#endif // #if MBED_MEM_TRACING_ENABLED
357357
return new_ptr;
358358
}
359359

360360
extern "C" void *SUB_CALLOC(size_t nmemb, size_t size)
361361
{
362362
void *ptr = NULL;
363-
#ifdef MBED_MEM_TRACING_ENABLED
363+
#if MBED_MEM_TRACING_ENABLED
364364
mbed_mem_trace_lock();
365365
#endif
366-
#ifdef MBED_HEAP_STATS_ENABLED
366+
#if MBED_HEAP_STATS_ENABLED
367367
// Note - no lock needed since malloc is thread safe
368368
ptr = malloc(nmemb * size);
369369
if (ptr != NULL) {
370370
memset(ptr, 0, nmemb * size);
371371
}
372-
#else // #ifdef MBED_HEAP_STATS_ENABLED
372+
#else // #if MBED_HEAP_STATS_ENABLED
373373
ptr = SUPER_CALLOC(nmemb, size);
374-
#endif // #ifdef MBED_HEAP_STATS_ENABLED
375-
#ifdef MBED_MEM_TRACING_ENABLED
374+
#endif // #if MBED_HEAP_STATS_ENABLED
375+
#if MBED_MEM_TRACING_ENABLED
376376
mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
377377
mbed_mem_trace_unlock();
378-
#endif // #ifdef MBED_MEM_TRACING_ENABLED
378+
#endif // #if MBED_MEM_TRACING_ENABLED
379379
return ptr;
380380
}
381381

@@ -386,10 +386,10 @@ extern "C" void SUB_FREE(void *ptr)
386386

387387
extern "C" void free_wrapper(void *ptr, void *caller)
388388
{
389-
#ifdef MBED_MEM_TRACING_ENABLED
389+
#if MBED_MEM_TRACING_ENABLED
390390
mbed_mem_trace_lock();
391391
#endif
392-
#ifdef MBED_HEAP_STATS_ENABLED
392+
#if MBED_HEAP_STATS_ENABLED
393393
malloc_stats_mutex->lock();
394394
alloc_info_t *alloc_info = NULL;
395395
if (ptr != NULL) {
@@ -408,13 +408,13 @@ extern "C" void free_wrapper(void *ptr, void *caller)
408408
}
409409

410410
malloc_stats_mutex->unlock();
411-
#else // #ifdef MBED_HEAP_STATS_ENABLED
411+
#else // #if MBED_HEAP_STATS_ENABLED
412412
SUPER_FREE(ptr);
413-
#endif // #ifdef MBED_HEAP_STATS_ENABLED
414-
#ifdef MBED_MEM_TRACING_ENABLED
413+
#endif // #if MBED_HEAP_STATS_ENABLED
414+
#if MBED_MEM_TRACING_ENABLED
415415
mbed_mem_trace_free(ptr, caller);
416416
mbed_mem_trace_unlock();
417-
#endif // #ifdef MBED_MEM_TRACING_ENABLED
417+
#endif // #if MBED_MEM_TRACING_ENABLED
418418
}
419419

420420
#endif // #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
@@ -425,11 +425,11 @@ extern "C" void free_wrapper(void *ptr, void *caller)
425425

426426
#else
427427

428-
#ifdef MBED_MEM_TRACING_ENABLED
428+
#if MBED_MEM_TRACING_ENABLED
429429
#error Memory tracing is not supported with the current toolchain.
430430
#endif
431431

432-
#ifdef MBED_HEAP_STATS_ENABLED
432+
#if MBED_HEAP_STATS_ENABLED
433433
#error Heap statistics are not supported with the current toolchain.
434434
#endif
435435

0 commit comments

Comments
 (0)