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