inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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