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