Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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