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