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