001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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