t
Fork of mbed-dev by
platform/mbed_alloc_wrappers.cpp@174:b96e65c34a4d, 2017-10-02 (annotated)
- Committer:
- AnnaBridge
- Date:
- Mon Oct 02 15:33:19 2017 +0100
- Revision:
- 174:b96e65c34a4d
- Parent:
- 167:e84263d55307
This updates the lib to the mbed lib v 152
Who changed what in which revision?
User | Revision | Line number | New 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_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 50 | static SingletonPtr<PlatformMutex> mem_trace_mutex; |
<> | 149:156823d33999 | 51 | #endif |
<> | 149:156823d33999 | 52 | #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 53 | static SingletonPtr<PlatformMutex> malloc_stats_mutex; |
<> | 149:156823d33999 | 54 | static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0}; |
<> | 149:156823d33999 | 55 | #endif |
<> | 149:156823d33999 | 56 | |
<> | 149:156823d33999 | 57 | void mbed_stats_heap_get(mbed_stats_heap_t *stats) |
<> | 149:156823d33999 | 58 | { |
<> | 149:156823d33999 | 59 | #ifdef MBED_HEAP_STATS_ENABLED |
<> | 152:9a67f0b066fc | 60 | extern uint32_t mbed_heap_size; |
<> | 152:9a67f0b066fc | 61 | heap_stats.reserved_size = mbed_heap_size; |
<> | 152:9a67f0b066fc | 62 | |
<> | 149:156823d33999 | 63 | malloc_stats_mutex->lock(); |
<> | 149:156823d33999 | 64 | memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t)); |
<> | 149:156823d33999 | 65 | malloc_stats_mutex->unlock(); |
<> | 149:156823d33999 | 66 | #else |
<> | 149:156823d33999 | 67 | memset(stats, 0, sizeof(mbed_stats_heap_t)); |
<> | 149:156823d33999 | 68 | #endif |
<> | 149:156823d33999 | 69 | } |
<> | 149:156823d33999 | 70 | |
<> | 149:156823d33999 | 71 | /******************************************************************************/ |
<> | 149:156823d33999 | 72 | /* GCC memory allocation wrappers */ |
<> | 149:156823d33999 | 73 | /******************************************************************************/ |
<> | 149:156823d33999 | 74 | |
<> | 149:156823d33999 | 75 | #if defined(TOOLCHAIN_GCC) |
<> | 149:156823d33999 | 76 | |
<> | 149:156823d33999 | 77 | #ifdef FEATURE_UVISOR |
<> | 149:156823d33999 | 78 | #include "uvisor-lib/uvisor-lib.h" |
<> | 149:156823d33999 | 79 | #endif/* FEATURE_UVISOR */ |
<> | 149:156823d33999 | 80 | |
<> | 149:156823d33999 | 81 | extern "C" { |
<> | 149:156823d33999 | 82 | void * __real__malloc_r(struct _reent * r, size_t size); |
AnnaBridge | 167:e84263d55307 | 83 | void * __real__memalign_r(struct _reent * r, size_t alignment, size_t bytes); |
<> | 149:156823d33999 | 84 | void * __real__realloc_r(struct _reent * r, void * ptr, size_t size); |
<> | 149:156823d33999 | 85 | void __real__free_r(struct _reent * r, void * ptr); |
<> | 149:156823d33999 | 86 | void* __real__calloc_r(struct _reent * r, size_t nmemb, size_t size); |
<> | 149:156823d33999 | 87 | } |
<> | 149:156823d33999 | 88 | |
<> | 149:156823d33999 | 89 | // TODO: memory tracing doesn't work with uVisor enabled. |
<> | 149:156823d33999 | 90 | #if !defined(FEATURE_UVISOR) |
<> | 149:156823d33999 | 91 | |
<> | 149:156823d33999 | 92 | extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) { |
<> | 149:156823d33999 | 93 | void *ptr = NULL; |
<> | 149:156823d33999 | 94 | #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 95 | malloc_stats_mutex->lock(); |
<> | 149:156823d33999 | 96 | alloc_info_t *alloc_info = (alloc_info_t*)__real__malloc_r(r, size + sizeof(alloc_info_t)); |
<> | 149:156823d33999 | 97 | if (alloc_info != NULL) { |
<> | 149:156823d33999 | 98 | alloc_info->size = size; |
<> | 149:156823d33999 | 99 | ptr = (void*)(alloc_info + 1); |
<> | 149:156823d33999 | 100 | heap_stats.current_size += size; |
<> | 149:156823d33999 | 101 | heap_stats.total_size += size; |
<> | 149:156823d33999 | 102 | heap_stats.alloc_cnt += 1; |
<> | 149:156823d33999 | 103 | if (heap_stats.current_size > heap_stats.max_size) { |
<> | 149:156823d33999 | 104 | heap_stats.max_size = heap_stats.current_size; |
<> | 149:156823d33999 | 105 | } |
<> | 149:156823d33999 | 106 | } else { |
<> | 149:156823d33999 | 107 | heap_stats.alloc_fail_cnt += 1; |
<> | 149:156823d33999 | 108 | } |
<> | 149:156823d33999 | 109 | malloc_stats_mutex->unlock(); |
<> | 149:156823d33999 | 110 | #else // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 111 | ptr = __real__malloc_r(r, size); |
<> | 149:156823d33999 | 112 | #endif // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 113 | #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 114 | mem_trace_mutex->lock(); |
<> | 149:156823d33999 | 115 | mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR()); |
<> | 149:156823d33999 | 116 | mem_trace_mutex->unlock(); |
<> | 149:156823d33999 | 117 | #endif // #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 118 | return ptr; |
<> | 149:156823d33999 | 119 | } |
<> | 149:156823d33999 | 120 | |
<> | 149:156823d33999 | 121 | extern "C" void * __wrap__realloc_r(struct _reent * r, void * ptr, size_t size) { |
<> | 149:156823d33999 | 122 | void *new_ptr = NULL; |
<> | 149:156823d33999 | 123 | #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 124 | // Implement realloc_r with malloc and free. |
<> | 149:156823d33999 | 125 | // The function realloc_r can't be used here directly since |
<> | 149:156823d33999 | 126 | // it can call into __wrap__malloc_r (returns ptr + 4) or |
<> | 149:156823d33999 | 127 | // resize memory directly (returns ptr + 0). |
<> | 149:156823d33999 | 128 | |
<> | 149:156823d33999 | 129 | // Note - no lock needed since malloc and free are thread safe |
<> | 149:156823d33999 | 130 | |
<> | 149:156823d33999 | 131 | // Get old size |
<> | 149:156823d33999 | 132 | uint32_t old_size = 0; |
<> | 149:156823d33999 | 133 | if (ptr != NULL) { |
<> | 149:156823d33999 | 134 | alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1; |
<> | 149:156823d33999 | 135 | old_size = alloc_info->size; |
<> | 149:156823d33999 | 136 | } |
<> | 149:156823d33999 | 137 | |
<> | 149:156823d33999 | 138 | // Allocate space |
<> | 149:156823d33999 | 139 | if (size != 0) { |
<> | 149:156823d33999 | 140 | new_ptr = malloc(size); |
<> | 149:156823d33999 | 141 | } |
<> | 149:156823d33999 | 142 | |
<> | 149:156823d33999 | 143 | // If the new buffer has been allocated copy the data to it |
<> | 149:156823d33999 | 144 | // and free the old buffer |
<> | 149:156823d33999 | 145 | if (new_ptr != NULL) { |
<> | 149:156823d33999 | 146 | uint32_t copy_size = (old_size < size) ? old_size : size; |
<> | 149:156823d33999 | 147 | memcpy(new_ptr, (void*)ptr, copy_size); |
<> | 149:156823d33999 | 148 | free(ptr); |
<> | 149:156823d33999 | 149 | } |
<> | 149:156823d33999 | 150 | #else // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 151 | new_ptr = __real__realloc_r(r, ptr, size); |
<> | 149:156823d33999 | 152 | #endif // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 153 | #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 154 | mem_trace_mutex->lock(); |
<> | 149:156823d33999 | 155 | mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR()); |
<> | 149:156823d33999 | 156 | mem_trace_mutex->unlock(); |
<> | 149:156823d33999 | 157 | #endif // #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 158 | return new_ptr; |
<> | 149:156823d33999 | 159 | } |
<> | 149:156823d33999 | 160 | |
<> | 149:156823d33999 | 161 | extern "C" void __wrap__free_r(struct _reent * r, void * ptr) { |
<> | 149:156823d33999 | 162 | #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 163 | malloc_stats_mutex->lock(); |
<> | 149:156823d33999 | 164 | alloc_info_t *alloc_info = NULL; |
<> | 149:156823d33999 | 165 | if (ptr != NULL) { |
<> | 149:156823d33999 | 166 | alloc_info = ((alloc_info_t*)ptr) - 1; |
<> | 149:156823d33999 | 167 | heap_stats.current_size -= alloc_info->size; |
<> | 149:156823d33999 | 168 | heap_stats.alloc_cnt -= 1; |
<> | 149:156823d33999 | 169 | } |
<> | 149:156823d33999 | 170 | __real__free_r(r, (void*)alloc_info); |
<> | 149:156823d33999 | 171 | malloc_stats_mutex->unlock(); |
<> | 149:156823d33999 | 172 | #else // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 173 | __real__free_r(r, ptr); |
<> | 149:156823d33999 | 174 | #endif // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 175 | #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 176 | mem_trace_mutex->lock(); |
<> | 149:156823d33999 | 177 | mbed_mem_trace_free(ptr, MBED_CALLER_ADDR()); |
<> | 149:156823d33999 | 178 | mem_trace_mutex->unlock(); |
<> | 149:156823d33999 | 179 | #endif // #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 180 | } |
<> | 149:156823d33999 | 181 | |
<> | 149:156823d33999 | 182 | extern "C" void * __wrap__calloc_r(struct _reent * r, size_t nmemb, size_t size) { |
<> | 149:156823d33999 | 183 | void *ptr = NULL; |
<> | 149:156823d33999 | 184 | #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 185 | // Note - no lock needed since malloc is thread safe |
<> | 149:156823d33999 | 186 | |
<> | 149:156823d33999 | 187 | ptr = malloc(nmemb * size); |
<> | 149:156823d33999 | 188 | if (ptr != NULL) { |
<> | 149:156823d33999 | 189 | memset(ptr, 0, nmemb * size); |
<> | 149:156823d33999 | 190 | } |
<> | 149:156823d33999 | 191 | #else // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 192 | ptr = __real__calloc_r(r, nmemb, size); |
<> | 149:156823d33999 | 193 | #endif // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 194 | #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 195 | mem_trace_mutex->lock(); |
<> | 149:156823d33999 | 196 | mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR()); |
<> | 149:156823d33999 | 197 | mem_trace_mutex->unlock(); |
<> | 149:156823d33999 | 198 | #endif // #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 199 | return ptr; |
<> | 149:156823d33999 | 200 | } |
<> | 149:156823d33999 | 201 | |
AnnaBridge | 167:e84263d55307 | 202 | extern "C" void * __wrap__memalign_r(struct _reent * r, size_t alignment, size_t bytes) { |
AnnaBridge | 167:e84263d55307 | 203 | return __real__memalign_r(r, alignment, bytes); |
AnnaBridge | 167:e84263d55307 | 204 | } |
AnnaBridge | 167:e84263d55307 | 205 | |
AnnaBridge | 167:e84263d55307 | 206 | #endif // if !defined(FEATURE_UVISOR) |
AnnaBridge | 167:e84263d55307 | 207 | |
<> | 149:156823d33999 | 208 | |
<> | 149:156823d33999 | 209 | /******************************************************************************/ |
AnnaBridge | 174:b96e65c34a4d | 210 | /* ARMCC / IAR memory allocation wrappers */ |
<> | 149:156823d33999 | 211 | /******************************************************************************/ |
<> | 149:156823d33999 | 212 | |
AnnaBridge | 174:b96e65c34a4d | 213 | #elif defined(TOOLCHAIN_ARM) || defined(__ICCARM__) |
AnnaBridge | 174:b96e65c34a4d | 214 | |
AnnaBridge | 174:b96e65c34a4d | 215 | #if defined(TOOLCHAIN_ARM) |
AnnaBridge | 174:b96e65c34a4d | 216 | #define SUPER_MALLOC $Super$$malloc |
AnnaBridge | 174:b96e65c34a4d | 217 | #define SUB_MALLOC $Sub$$malloc |
AnnaBridge | 174:b96e65c34a4d | 218 | #define SUPER_REALLOC $Super$$realloc |
AnnaBridge | 174:b96e65c34a4d | 219 | #define SUB_REALLOC $Sub$$realloc |
AnnaBridge | 174:b96e65c34a4d | 220 | #define SUPER_CALLOC $Super$$calloc |
AnnaBridge | 174:b96e65c34a4d | 221 | #define SUB_CALLOC $Sub$$calloc |
AnnaBridge | 174:b96e65c34a4d | 222 | #define SUPER_FREE $Super$$free |
AnnaBridge | 174:b96e65c34a4d | 223 | #define SUB_FREE $Sub$$free |
AnnaBridge | 174:b96e65c34a4d | 224 | #elif defined(__ICCARM__) |
AnnaBridge | 174:b96e65c34a4d | 225 | #define SUPER_MALLOC $Super$$__iar_dlmalloc |
AnnaBridge | 174:b96e65c34a4d | 226 | #define SUB_MALLOC $Sub$$__iar_dlmalloc |
AnnaBridge | 174:b96e65c34a4d | 227 | #define SUPER_REALLOC $Super$$__iar_dlrealloc |
AnnaBridge | 174:b96e65c34a4d | 228 | #define SUB_REALLOC $Sub$$__iar_dlrealloc |
AnnaBridge | 174:b96e65c34a4d | 229 | #define SUPER_CALLOC $Super$$__iar_dlcalloc |
AnnaBridge | 174:b96e65c34a4d | 230 | #define SUB_CALLOC $Sub$$__iar_dlcalloc |
AnnaBridge | 174:b96e65c34a4d | 231 | #define SUPER_FREE $Super$$__iar_dlfree |
AnnaBridge | 174:b96e65c34a4d | 232 | #define SUB_FREE $Sub$$__iar_dlfree |
AnnaBridge | 174:b96e65c34a4d | 233 | #endif |
<> | 149:156823d33999 | 234 | |
<> | 149:156823d33999 | 235 | /* Enable hooking of memory function only if tracing is also enabled */ |
<> | 149:156823d33999 | 236 | #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED) |
<> | 149:156823d33999 | 237 | |
<> | 149:156823d33999 | 238 | extern "C" { |
AnnaBridge | 174:b96e65c34a4d | 239 | void *SUPER_MALLOC(size_t size); |
AnnaBridge | 174:b96e65c34a4d | 240 | void *SUPER_REALLOC(void *ptr, size_t size); |
AnnaBridge | 174:b96e65c34a4d | 241 | void *SUPER_CALLOC(size_t nmemb, size_t size); |
AnnaBridge | 174:b96e65c34a4d | 242 | void SUPER_FREE(void *ptr); |
<> | 149:156823d33999 | 243 | } |
<> | 149:156823d33999 | 244 | |
AnnaBridge | 174:b96e65c34a4d | 245 | extern "C" void* SUB_MALLOC(size_t size) { |
<> | 149:156823d33999 | 246 | void *ptr = NULL; |
<> | 149:156823d33999 | 247 | #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 248 | malloc_stats_mutex->lock(); |
AnnaBridge | 174:b96e65c34a4d | 249 | alloc_info_t *alloc_info = (alloc_info_t*)SUPER_MALLOC(size + sizeof(alloc_info_t)); |
<> | 149:156823d33999 | 250 | if (alloc_info != NULL) { |
<> | 149:156823d33999 | 251 | alloc_info->size = size; |
<> | 149:156823d33999 | 252 | ptr = (void*)(alloc_info + 1); |
<> | 149:156823d33999 | 253 | heap_stats.current_size += size; |
<> | 149:156823d33999 | 254 | heap_stats.total_size += size; |
<> | 149:156823d33999 | 255 | heap_stats.alloc_cnt += 1; |
<> | 149:156823d33999 | 256 | if (heap_stats.current_size > heap_stats.max_size) { |
<> | 149:156823d33999 | 257 | heap_stats.max_size = heap_stats.current_size; |
<> | 149:156823d33999 | 258 | } |
<> | 149:156823d33999 | 259 | } else { |
<> | 149:156823d33999 | 260 | heap_stats.alloc_fail_cnt += 1; |
<> | 149:156823d33999 | 261 | } |
<> | 149:156823d33999 | 262 | malloc_stats_mutex->unlock(); |
<> | 149:156823d33999 | 263 | #else // #ifdef MBED_HEAP_STATS_ENABLED |
AnnaBridge | 174:b96e65c34a4d | 264 | ptr = SUPER_MALLOC(size); |
<> | 149:156823d33999 | 265 | #endif // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 266 | #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 267 | mem_trace_mutex->lock(); |
<> | 149:156823d33999 | 268 | mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR()); |
<> | 149:156823d33999 | 269 | mem_trace_mutex->unlock(); |
<> | 149:156823d33999 | 270 | #endif // #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 271 | return ptr; |
<> | 149:156823d33999 | 272 | } |
<> | 149:156823d33999 | 273 | |
AnnaBridge | 174:b96e65c34a4d | 274 | extern "C" void* SUB_REALLOC(void *ptr, size_t size) { |
<> | 149:156823d33999 | 275 | void *new_ptr = NULL; |
<> | 149:156823d33999 | 276 | #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 277 | // Note - no lock needed since malloc and free are thread safe |
<> | 149:156823d33999 | 278 | |
<> | 149:156823d33999 | 279 | // Get old size |
<> | 149:156823d33999 | 280 | uint32_t old_size = 0; |
<> | 149:156823d33999 | 281 | if (ptr != NULL) { |
<> | 149:156823d33999 | 282 | alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1; |
<> | 149:156823d33999 | 283 | old_size = alloc_info->size; |
<> | 149:156823d33999 | 284 | } |
<> | 149:156823d33999 | 285 | |
<> | 149:156823d33999 | 286 | // Allocate space |
<> | 149:156823d33999 | 287 | if (size != 0) { |
<> | 149:156823d33999 | 288 | new_ptr = malloc(size); |
<> | 149:156823d33999 | 289 | } |
<> | 149:156823d33999 | 290 | |
<> | 149:156823d33999 | 291 | // If the new buffer has been allocated copy the data to it |
<> | 149:156823d33999 | 292 | // and free the old buffer |
<> | 149:156823d33999 | 293 | if (new_ptr != NULL) { |
<> | 149:156823d33999 | 294 | uint32_t copy_size = (old_size < size) ? old_size : size; |
<> | 149:156823d33999 | 295 | memcpy(new_ptr, (void*)ptr, copy_size); |
<> | 149:156823d33999 | 296 | free(ptr); |
<> | 149:156823d33999 | 297 | } |
<> | 149:156823d33999 | 298 | #else // #ifdef MBED_HEAP_STATS_ENABLED |
AnnaBridge | 174:b96e65c34a4d | 299 | new_ptr = SUPER_REALLOC(ptr, size); |
<> | 149:156823d33999 | 300 | #endif // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 301 | #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 302 | mem_trace_mutex->lock(); |
<> | 149:156823d33999 | 303 | mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR()); |
<> | 149:156823d33999 | 304 | mem_trace_mutex->unlock(); |
<> | 149:156823d33999 | 305 | #endif // #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 306 | return new_ptr; |
<> | 149:156823d33999 | 307 | } |
<> | 149:156823d33999 | 308 | |
AnnaBridge | 174:b96e65c34a4d | 309 | extern "C" void *SUB_CALLOC(size_t nmemb, size_t size) { |
<> | 149:156823d33999 | 310 | void *ptr = NULL; |
<> | 149:156823d33999 | 311 | #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 312 | // Note - no lock needed since malloc is thread safe |
<> | 149:156823d33999 | 313 | ptr = malloc(nmemb * size); |
<> | 149:156823d33999 | 314 | if (ptr != NULL) { |
<> | 149:156823d33999 | 315 | memset(ptr, 0, nmemb * size); |
<> | 149:156823d33999 | 316 | } |
<> | 149:156823d33999 | 317 | #else // #ifdef MBED_HEAP_STATS_ENABLED |
AnnaBridge | 174:b96e65c34a4d | 318 | ptr = SUPER_CALLOC(nmemb, size); |
<> | 149:156823d33999 | 319 | #endif // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 320 | #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 321 | mem_trace_mutex->lock(); |
<> | 149:156823d33999 | 322 | mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR()); |
<> | 149:156823d33999 | 323 | mem_trace_mutex->unlock(); |
<> | 149:156823d33999 | 324 | #endif // #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 325 | return ptr; |
<> | 149:156823d33999 | 326 | } |
<> | 149:156823d33999 | 327 | |
AnnaBridge | 174:b96e65c34a4d | 328 | extern "C" void SUB_FREE(void *ptr) { |
<> | 149:156823d33999 | 329 | #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 330 | malloc_stats_mutex->lock(); |
<> | 149:156823d33999 | 331 | alloc_info_t *alloc_info = NULL; |
<> | 149:156823d33999 | 332 | if (ptr != NULL) { |
<> | 149:156823d33999 | 333 | alloc_info = ((alloc_info_t*)ptr) - 1; |
<> | 149:156823d33999 | 334 | heap_stats.current_size -= alloc_info->size; |
<> | 149:156823d33999 | 335 | heap_stats.alloc_cnt -= 1; |
<> | 149:156823d33999 | 336 | } |
AnnaBridge | 174:b96e65c34a4d | 337 | SUPER_FREE((void*)alloc_info); |
<> | 149:156823d33999 | 338 | malloc_stats_mutex->unlock(); |
<> | 149:156823d33999 | 339 | #else // #ifdef MBED_HEAP_STATS_ENABLED |
AnnaBridge | 174:b96e65c34a4d | 340 | SUPER_FREE(ptr); |
<> | 149:156823d33999 | 341 | #endif // #ifdef MBED_HEAP_STATS_ENABLED |
<> | 149:156823d33999 | 342 | #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 343 | mem_trace_mutex->lock(); |
<> | 149:156823d33999 | 344 | mbed_mem_trace_free(ptr, MBED_CALLER_ADDR()); |
<> | 149:156823d33999 | 345 | mem_trace_mutex->unlock(); |
<> | 149:156823d33999 | 346 | #endif // #ifdef MBED_MEM_TRACING_ENABLED |
<> | 149:156823d33999 | 347 | } |
<> | 149:156823d33999 | 348 | |
<> | 149:156823d33999 | 349 | #endif // #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED) |
<> | 149:156823d33999 | 350 | |
<> | 149:156823d33999 | 351 | /******************************************************************************/ |
<> | 149:156823d33999 | 352 | /* Allocation wrappers for other toolchains are not supported yet */ |
<> | 149:156823d33999 | 353 | /******************************************************************************/ |
<> | 149:156823d33999 | 354 | |
AnnaBridge | 174:b96e65c34a4d | 355 | #else |
<> | 149:156823d33999 | 356 | |
<> | 149:156823d33999 | 357 | #ifdef MBED_MEM_TRACING_ENABLED |
AnnaBridge | 174:b96e65c34a4d | 358 | #error Memory tracing is not supported with the current toolchain. |
<> | 149:156823d33999 | 359 | #endif |
<> | 149:156823d33999 | 360 | |
<> | 149:156823d33999 | 361 | #ifdef MBED_HEAP_STATS_ENABLED |
AnnaBridge | 174:b96e65c34a4d | 362 | #error Heap statistics are not supported with the current toolchain. |
<> | 149:156823d33999 | 363 | #endif |
<> | 149:156823d33999 | 364 | |
<> | 149:156823d33999 | 365 | #endif // #if defined(TOOLCHAIN_GCC) |