Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

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