mbed library sources. Supersedes mbed-src. removed serial

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Oct 28 11:17:30 2016 +0100
Revision:
149:156823d33999
Child:
152:9a67f0b066fc
This updates the lib to the mbed lib v128

NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.

Who changed what in which revision?

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