Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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