mbed library sources, mbed-dev only for TYBLE16

Fork of mbed-dev by mbed official

Please refer flowing link.
/users/kenjiArai/notebook/tyble16-module-will-become-a-mbed-family--mbedliza/

Committer:
kenjiArai
Date:
Sun Feb 25 02:00:51 2018 +0000
Revision:
182:976b131d2800
Parent:
181:57724642e740
Only for TYBLE16

Who changed what in which revision?

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