mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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