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