The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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