TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elmot 1:d0dfbce63a89 1 /* mbed Microcontroller Library
elmot 1:d0dfbce63a89 2 * Copyright (c) 2006-2016 ARM Limited
elmot 1:d0dfbce63a89 3 *
elmot 1:d0dfbce63a89 4 * Licensed under the Apache License, Version 2.0 (the "License");
elmot 1:d0dfbce63a89 5 * you may not use this file except in compliance with the License.
elmot 1:d0dfbce63a89 6 * You may obtain a copy of the License at
elmot 1:d0dfbce63a89 7 *
elmot 1:d0dfbce63a89 8 * http://www.apache.org/licenses/LICENSE-2.0
elmot 1:d0dfbce63a89 9 *
elmot 1:d0dfbce63a89 10 * Unless required by applicable law or agreed to in writing, software
elmot 1:d0dfbce63a89 11 * distributed under the License is distributed on an "AS IS" BASIS,
elmot 1:d0dfbce63a89 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elmot 1:d0dfbce63a89 13 * See the License for the specific language governing permissions and
elmot 1:d0dfbce63a89 14 * limitations under the License.
elmot 1:d0dfbce63a89 15 */
elmot 1:d0dfbce63a89 16
elmot 1:d0dfbce63a89 17 #include "platform/mbed_mem_trace.h"
elmot 1:d0dfbce63a89 18 #include "platform/mbed_stats.h"
elmot 1:d0dfbce63a89 19 #include "platform/toolchain.h"
elmot 1:d0dfbce63a89 20 #include "platform/SingletonPtr.h"
elmot 1:d0dfbce63a89 21 #include "platform/PlatformMutex.h"
elmot 1:d0dfbce63a89 22 #include <stddef.h>
elmot 1:d0dfbce63a89 23 #include <stdio.h>
elmot 1:d0dfbce63a89 24 #include <string.h>
elmot 1:d0dfbce63a89 25 #include <stdlib.h>
elmot 1:d0dfbce63a89 26
elmot 1:d0dfbce63a89 27 /* There are two memory tracers in mbed OS:
elmot 1:d0dfbce63a89 28
elmot 1:d0dfbce63a89 29 - the first can be used to detect the maximum heap usage at runtime. It is
elmot 1:d0dfbce63a89 30 activated by defining the MBED_HEAP_STATS_ENABLED macro.
elmot 1:d0dfbce63a89 31 - the second can be used to trace each memory call by automatically invoking
elmot 1:d0dfbce63a89 32 a callback on each memory operation (see hal/api/mbed_mem_trace.h). It is
elmot 1:d0dfbce63a89 33 activated by defining the MBED_MEM_TRACING_ENABLED macro.
elmot 1:d0dfbce63a89 34
elmot 1:d0dfbce63a89 35 Both tracers can be activated and deactivated in any combination. If both tracers
elmot 1:d0dfbce63a89 36 are active, the second one (MBED_MEM_TRACING_ENABLED) will trace the first one's
elmot 1:d0dfbce63a89 37 (MBED_HEAP_STATS_ENABLED) memory calls.*/
elmot 1:d0dfbce63a89 38
elmot 1:d0dfbce63a89 39 /******************************************************************************/
elmot 1:d0dfbce63a89 40 /* Implementation of the runtime max heap usage checker */
elmot 1:d0dfbce63a89 41 /******************************************************************************/
elmot 1:d0dfbce63a89 42
elmot 1:d0dfbce63a89 43 /* Size must be a multiple of 8 to keep alignment */
elmot 1:d0dfbce63a89 44 typedef struct {
elmot 1:d0dfbce63a89 45 uint32_t size;
elmot 1:d0dfbce63a89 46 uint32_t pad;
elmot 1:d0dfbce63a89 47 } alloc_info_t;
elmot 1:d0dfbce63a89 48
elmot 1:d0dfbce63a89 49 #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 50 static SingletonPtr<PlatformMutex> mem_trace_mutex;
elmot 1:d0dfbce63a89 51 #endif
elmot 1:d0dfbce63a89 52 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 53 static SingletonPtr<PlatformMutex> malloc_stats_mutex;
elmot 1:d0dfbce63a89 54 static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0};
elmot 1:d0dfbce63a89 55 #endif
elmot 1:d0dfbce63a89 56
elmot 1:d0dfbce63a89 57 void mbed_stats_heap_get(mbed_stats_heap_t *stats)
elmot 1:d0dfbce63a89 58 {
elmot 1:d0dfbce63a89 59 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 60 extern uint32_t mbed_heap_size;
elmot 1:d0dfbce63a89 61 heap_stats.reserved_size = mbed_heap_size;
elmot 1:d0dfbce63a89 62
elmot 1:d0dfbce63a89 63 malloc_stats_mutex->lock();
elmot 1:d0dfbce63a89 64 memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t));
elmot 1:d0dfbce63a89 65 malloc_stats_mutex->unlock();
elmot 1:d0dfbce63a89 66 #else
elmot 1:d0dfbce63a89 67 memset(stats, 0, sizeof(mbed_stats_heap_t));
elmot 1:d0dfbce63a89 68 #endif
elmot 1:d0dfbce63a89 69 }
elmot 1:d0dfbce63a89 70
elmot 1:d0dfbce63a89 71 /******************************************************************************/
elmot 1:d0dfbce63a89 72 /* GCC memory allocation wrappers */
elmot 1:d0dfbce63a89 73 /******************************************************************************/
elmot 1:d0dfbce63a89 74
elmot 1:d0dfbce63a89 75 #if defined(TOOLCHAIN_GCC)
elmot 1:d0dfbce63a89 76
elmot 1:d0dfbce63a89 77 #ifdef FEATURE_UVISOR
elmot 1:d0dfbce63a89 78 #include "uvisor-lib/uvisor-lib.h"
elmot 1:d0dfbce63a89 79 #endif/* FEATURE_UVISOR */
elmot 1:d0dfbce63a89 80
elmot 1:d0dfbce63a89 81 extern "C" {
elmot 1:d0dfbce63a89 82 void * __real__malloc_r(struct _reent * r, size_t size);
elmot 1:d0dfbce63a89 83 void * __real__realloc_r(struct _reent * r, void * ptr, size_t size);
elmot 1:d0dfbce63a89 84 void __real__free_r(struct _reent * r, void * ptr);
elmot 1:d0dfbce63a89 85 void* __real__calloc_r(struct _reent * r, size_t nmemb, size_t size);
elmot 1:d0dfbce63a89 86 }
elmot 1:d0dfbce63a89 87
elmot 1:d0dfbce63a89 88 // TODO: memory tracing doesn't work with uVisor enabled.
elmot 1:d0dfbce63a89 89 #if !defined(FEATURE_UVISOR)
elmot 1:d0dfbce63a89 90
elmot 1:d0dfbce63a89 91 extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) {
elmot 1:d0dfbce63a89 92 void *ptr = NULL;
elmot 1:d0dfbce63a89 93 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 94 malloc_stats_mutex->lock();
elmot 1:d0dfbce63a89 95 alloc_info_t *alloc_info = (alloc_info_t*)__real__malloc_r(r, size + sizeof(alloc_info_t));
elmot 1:d0dfbce63a89 96 if (alloc_info != NULL) {
elmot 1:d0dfbce63a89 97 alloc_info->size = size;
elmot 1:d0dfbce63a89 98 ptr = (void*)(alloc_info + 1);
elmot 1:d0dfbce63a89 99 heap_stats.current_size += size;
elmot 1:d0dfbce63a89 100 heap_stats.total_size += size;
elmot 1:d0dfbce63a89 101 heap_stats.alloc_cnt += 1;
elmot 1:d0dfbce63a89 102 if (heap_stats.current_size > heap_stats.max_size) {
elmot 1:d0dfbce63a89 103 heap_stats.max_size = heap_stats.current_size;
elmot 1:d0dfbce63a89 104 }
elmot 1:d0dfbce63a89 105 } else {
elmot 1:d0dfbce63a89 106 heap_stats.alloc_fail_cnt += 1;
elmot 1:d0dfbce63a89 107 }
elmot 1:d0dfbce63a89 108 malloc_stats_mutex->unlock();
elmot 1:d0dfbce63a89 109 #else // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 110 ptr = __real__malloc_r(r, size);
elmot 1:d0dfbce63a89 111 #endif // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 112 #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 113 mem_trace_mutex->lock();
elmot 1:d0dfbce63a89 114 mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR());
elmot 1:d0dfbce63a89 115 mem_trace_mutex->unlock();
elmot 1:d0dfbce63a89 116 #endif // #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 117 return ptr;
elmot 1:d0dfbce63a89 118 }
elmot 1:d0dfbce63a89 119
elmot 1:d0dfbce63a89 120 extern "C" void * __wrap__realloc_r(struct _reent * r, void * ptr, size_t size) {
elmot 1:d0dfbce63a89 121 void *new_ptr = NULL;
elmot 1:d0dfbce63a89 122 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 123 // Implement realloc_r with malloc and free.
elmot 1:d0dfbce63a89 124 // The function realloc_r can't be used here directly since
elmot 1:d0dfbce63a89 125 // it can call into __wrap__malloc_r (returns ptr + 4) or
elmot 1:d0dfbce63a89 126 // resize memory directly (returns ptr + 0).
elmot 1:d0dfbce63a89 127
elmot 1:d0dfbce63a89 128 // Note - no lock needed since malloc and free are thread safe
elmot 1:d0dfbce63a89 129
elmot 1:d0dfbce63a89 130 // Get old size
elmot 1:d0dfbce63a89 131 uint32_t old_size = 0;
elmot 1:d0dfbce63a89 132 if (ptr != NULL) {
elmot 1:d0dfbce63a89 133 alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1;
elmot 1:d0dfbce63a89 134 old_size = alloc_info->size;
elmot 1:d0dfbce63a89 135 }
elmot 1:d0dfbce63a89 136
elmot 1:d0dfbce63a89 137 // Allocate space
elmot 1:d0dfbce63a89 138 if (size != 0) {
elmot 1:d0dfbce63a89 139 new_ptr = malloc(size);
elmot 1:d0dfbce63a89 140 }
elmot 1:d0dfbce63a89 141
elmot 1:d0dfbce63a89 142 // If the new buffer has been allocated copy the data to it
elmot 1:d0dfbce63a89 143 // and free the old buffer
elmot 1:d0dfbce63a89 144 if (new_ptr != NULL) {
elmot 1:d0dfbce63a89 145 uint32_t copy_size = (old_size < size) ? old_size : size;
elmot 1:d0dfbce63a89 146 memcpy(new_ptr, (void*)ptr, copy_size);
elmot 1:d0dfbce63a89 147 free(ptr);
elmot 1:d0dfbce63a89 148 }
elmot 1:d0dfbce63a89 149 #else // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 150 new_ptr = __real__realloc_r(r, ptr, size);
elmot 1:d0dfbce63a89 151 #endif // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 152 #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 153 mem_trace_mutex->lock();
elmot 1:d0dfbce63a89 154 mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
elmot 1:d0dfbce63a89 155 mem_trace_mutex->unlock();
elmot 1:d0dfbce63a89 156 #endif // #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 157 return new_ptr;
elmot 1:d0dfbce63a89 158 }
elmot 1:d0dfbce63a89 159
elmot 1:d0dfbce63a89 160 extern "C" void __wrap__free_r(struct _reent * r, void * ptr) {
elmot 1:d0dfbce63a89 161 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 162 malloc_stats_mutex->lock();
elmot 1:d0dfbce63a89 163 alloc_info_t *alloc_info = NULL;
elmot 1:d0dfbce63a89 164 if (ptr != NULL) {
elmot 1:d0dfbce63a89 165 alloc_info = ((alloc_info_t*)ptr) - 1;
elmot 1:d0dfbce63a89 166 heap_stats.current_size -= alloc_info->size;
elmot 1:d0dfbce63a89 167 heap_stats.alloc_cnt -= 1;
elmot 1:d0dfbce63a89 168 }
elmot 1:d0dfbce63a89 169 __real__free_r(r, (void*)alloc_info);
elmot 1:d0dfbce63a89 170 malloc_stats_mutex->unlock();
elmot 1:d0dfbce63a89 171 #else // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 172 __real__free_r(r, ptr);
elmot 1:d0dfbce63a89 173 #endif // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 174 #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 175 mem_trace_mutex->lock();
elmot 1:d0dfbce63a89 176 mbed_mem_trace_free(ptr, MBED_CALLER_ADDR());
elmot 1:d0dfbce63a89 177 mem_trace_mutex->unlock();
elmot 1:d0dfbce63a89 178 #endif // #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 179 }
elmot 1:d0dfbce63a89 180
elmot 1:d0dfbce63a89 181 #endif // if !defined(FEATURE_UVISOR)
elmot 1:d0dfbce63a89 182
elmot 1:d0dfbce63a89 183 extern "C" void * __wrap__calloc_r(struct _reent * r, size_t nmemb, size_t size) {
elmot 1:d0dfbce63a89 184 void *ptr = NULL;
elmot 1:d0dfbce63a89 185 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 186 // Note - no lock needed since malloc is thread safe
elmot 1:d0dfbce63a89 187
elmot 1:d0dfbce63a89 188 ptr = malloc(nmemb * size);
elmot 1:d0dfbce63a89 189 if (ptr != NULL) {
elmot 1:d0dfbce63a89 190 memset(ptr, 0, nmemb * size);
elmot 1:d0dfbce63a89 191 }
elmot 1:d0dfbce63a89 192 #else // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 193 ptr = __real__calloc_r(r, nmemb, size);
elmot 1:d0dfbce63a89 194 #endif // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 195 #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 196 mem_trace_mutex->lock();
elmot 1:d0dfbce63a89 197 mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
elmot 1:d0dfbce63a89 198 mem_trace_mutex->unlock();
elmot 1:d0dfbce63a89 199 #endif // #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 200 return ptr;
elmot 1:d0dfbce63a89 201 }
elmot 1:d0dfbce63a89 202
elmot 1:d0dfbce63a89 203
elmot 1:d0dfbce63a89 204 /******************************************************************************/
elmot 1:d0dfbce63a89 205 /* ARMCC memory allocation wrappers */
elmot 1:d0dfbce63a89 206 /******************************************************************************/
elmot 1:d0dfbce63a89 207
elmot 1:d0dfbce63a89 208 #elif defined(TOOLCHAIN_ARM) // #if defined(TOOLCHAIN_GCC)
elmot 1:d0dfbce63a89 209
elmot 1:d0dfbce63a89 210 /* Enable hooking of memory function only if tracing is also enabled */
elmot 1:d0dfbce63a89 211 #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
elmot 1:d0dfbce63a89 212
elmot 1:d0dfbce63a89 213 extern "C" {
elmot 1:d0dfbce63a89 214 void *$Super$$malloc(size_t size);
elmot 1:d0dfbce63a89 215 void *$Super$$realloc(void *ptr, size_t size);
elmot 1:d0dfbce63a89 216 void *$Super$$calloc(size_t nmemb, size_t size);
elmot 1:d0dfbce63a89 217 void $Super$$free(void *ptr);
elmot 1:d0dfbce63a89 218 }
elmot 1:d0dfbce63a89 219
elmot 1:d0dfbce63a89 220 extern "C" void* $Sub$$malloc(size_t size) {
elmot 1:d0dfbce63a89 221 void *ptr = NULL;
elmot 1:d0dfbce63a89 222 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 223 malloc_stats_mutex->lock();
elmot 1:d0dfbce63a89 224 alloc_info_t *alloc_info = (alloc_info_t*)$Super$$malloc(size + sizeof(alloc_info_t));
elmot 1:d0dfbce63a89 225 if (alloc_info != NULL) {
elmot 1:d0dfbce63a89 226 alloc_info->size = size;
elmot 1:d0dfbce63a89 227 ptr = (void*)(alloc_info + 1);
elmot 1:d0dfbce63a89 228 heap_stats.current_size += size;
elmot 1:d0dfbce63a89 229 heap_stats.total_size += size;
elmot 1:d0dfbce63a89 230 heap_stats.alloc_cnt += 1;
elmot 1:d0dfbce63a89 231 if (heap_stats.current_size > heap_stats.max_size) {
elmot 1:d0dfbce63a89 232 heap_stats.max_size = heap_stats.current_size;
elmot 1:d0dfbce63a89 233 }
elmot 1:d0dfbce63a89 234 } else {
elmot 1:d0dfbce63a89 235 heap_stats.alloc_fail_cnt += 1;
elmot 1:d0dfbce63a89 236 }
elmot 1:d0dfbce63a89 237 malloc_stats_mutex->unlock();
elmot 1:d0dfbce63a89 238 #else // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 239 ptr = $Super$$malloc(size);
elmot 1:d0dfbce63a89 240 #endif // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 241 #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 242 mem_trace_mutex->lock();
elmot 1:d0dfbce63a89 243 mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR());
elmot 1:d0dfbce63a89 244 mem_trace_mutex->unlock();
elmot 1:d0dfbce63a89 245 #endif // #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 246 return ptr;
elmot 1:d0dfbce63a89 247 }
elmot 1:d0dfbce63a89 248
elmot 1:d0dfbce63a89 249 extern "C" void* $Sub$$realloc(void *ptr, size_t size) {
elmot 1:d0dfbce63a89 250 void *new_ptr = NULL;
elmot 1:d0dfbce63a89 251 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 252 // Note - no lock needed since malloc and free are thread safe
elmot 1:d0dfbce63a89 253
elmot 1:d0dfbce63a89 254 // Get old size
elmot 1:d0dfbce63a89 255 uint32_t old_size = 0;
elmot 1:d0dfbce63a89 256 if (ptr != NULL) {
elmot 1:d0dfbce63a89 257 alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1;
elmot 1:d0dfbce63a89 258 old_size = alloc_info->size;
elmot 1:d0dfbce63a89 259 }
elmot 1:d0dfbce63a89 260
elmot 1:d0dfbce63a89 261 // Allocate space
elmot 1:d0dfbce63a89 262 if (size != 0) {
elmot 1:d0dfbce63a89 263 new_ptr = malloc(size);
elmot 1:d0dfbce63a89 264 }
elmot 1:d0dfbce63a89 265
elmot 1:d0dfbce63a89 266 // If the new buffer has been allocated copy the data to it
elmot 1:d0dfbce63a89 267 // and free the old buffer
elmot 1:d0dfbce63a89 268 if (new_ptr != NULL) {
elmot 1:d0dfbce63a89 269 uint32_t copy_size = (old_size < size) ? old_size : size;
elmot 1:d0dfbce63a89 270 memcpy(new_ptr, (void*)ptr, copy_size);
elmot 1:d0dfbce63a89 271 free(ptr);
elmot 1:d0dfbce63a89 272 }
elmot 1:d0dfbce63a89 273 #else // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 274 new_ptr = $Super$$realloc(ptr, size);
elmot 1:d0dfbce63a89 275 #endif // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 276 #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 277 mem_trace_mutex->lock();
elmot 1:d0dfbce63a89 278 mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
elmot 1:d0dfbce63a89 279 mem_trace_mutex->unlock();
elmot 1:d0dfbce63a89 280 #endif // #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 281 return new_ptr;
elmot 1:d0dfbce63a89 282 }
elmot 1:d0dfbce63a89 283
elmot 1:d0dfbce63a89 284 extern "C" void *$Sub$$calloc(size_t nmemb, size_t size) {
elmot 1:d0dfbce63a89 285 void *ptr = NULL;
elmot 1:d0dfbce63a89 286 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 287 // Note - no lock needed since malloc is thread safe
elmot 1:d0dfbce63a89 288 ptr = malloc(nmemb * size);
elmot 1:d0dfbce63a89 289 if (ptr != NULL) {
elmot 1:d0dfbce63a89 290 memset(ptr, 0, nmemb * size);
elmot 1:d0dfbce63a89 291 }
elmot 1:d0dfbce63a89 292 #else // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 293 ptr = $Super$$calloc(nmemb, size);
elmot 1:d0dfbce63a89 294 #endif // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 295 #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 296 mem_trace_mutex->lock();
elmot 1:d0dfbce63a89 297 mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
elmot 1:d0dfbce63a89 298 mem_trace_mutex->unlock();
elmot 1:d0dfbce63a89 299 #endif // #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 300 return ptr;
elmot 1:d0dfbce63a89 301 }
elmot 1:d0dfbce63a89 302
elmot 1:d0dfbce63a89 303 extern "C" void $Sub$$free(void *ptr) {
elmot 1:d0dfbce63a89 304 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 305 malloc_stats_mutex->lock();
elmot 1:d0dfbce63a89 306 alloc_info_t *alloc_info = NULL;
elmot 1:d0dfbce63a89 307 if (ptr != NULL) {
elmot 1:d0dfbce63a89 308 alloc_info = ((alloc_info_t*)ptr) - 1;
elmot 1:d0dfbce63a89 309 heap_stats.current_size -= alloc_info->size;
elmot 1:d0dfbce63a89 310 heap_stats.alloc_cnt -= 1;
elmot 1:d0dfbce63a89 311 }
elmot 1:d0dfbce63a89 312 $Super$$free((void*)alloc_info);
elmot 1:d0dfbce63a89 313 malloc_stats_mutex->unlock();
elmot 1:d0dfbce63a89 314 #else // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 315 $Super$$free(ptr);
elmot 1:d0dfbce63a89 316 #endif // #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 317 #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 318 mem_trace_mutex->lock();
elmot 1:d0dfbce63a89 319 mbed_mem_trace_free(ptr, MBED_CALLER_ADDR());
elmot 1:d0dfbce63a89 320 mem_trace_mutex->unlock();
elmot 1:d0dfbce63a89 321 #endif // #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 322 }
elmot 1:d0dfbce63a89 323
elmot 1:d0dfbce63a89 324 #endif // #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
elmot 1:d0dfbce63a89 325
elmot 1:d0dfbce63a89 326 /******************************************************************************/
elmot 1:d0dfbce63a89 327 /* Allocation wrappers for other toolchains are not supported yet */
elmot 1:d0dfbce63a89 328 /******************************************************************************/
elmot 1:d0dfbce63a89 329
elmot 1:d0dfbce63a89 330 #else // #if defined(TOOLCHAIN_GCC)
elmot 1:d0dfbce63a89 331
elmot 1:d0dfbce63a89 332 #ifdef MBED_MEM_TRACING_ENABLED
elmot 1:d0dfbce63a89 333 #warning Memory tracing is not supported with the current toolchain.
elmot 1:d0dfbce63a89 334 #endif
elmot 1:d0dfbce63a89 335
elmot 1:d0dfbce63a89 336 #ifdef MBED_HEAP_STATS_ENABLED
elmot 1:d0dfbce63a89 337 #warning Heap statistics are not supported with the current toolchain.
elmot 1:d0dfbce63a89 338 #endif
elmot 1:d0dfbce63a89 339
elmot 1:d0dfbce63a89 340 #endif // #if defined(TOOLCHAIN_GCC)
elmot 1:d0dfbce63a89 341