Lcd companion boards support (VKLCD50RTA & VKLCD70RT)

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_alloc_wrappers.cpp Source File

mbed_alloc_wrappers.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2016 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed_mem_trace.h"
00018 #include "mbed_stats.h"
00019 #include "toolchain.h"
00020 #include "SingletonPtr.h"
00021 #include "PlatformMutex.h"
00022 #include <stddef.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <stdlib.h>
00026 
00027 /* There are two memory tracers in mbed OS:
00028 
00029 - the first can be used to detect the maximum heap usage at runtime. It is
00030   activated by defining the MBED_HEAP_STATS_ENABLED macro.
00031 - the second can be used to trace each memory call by automatically invoking
00032   a callback on each memory operation (see hal/api/mbed_mem_trace.h). It is
00033   activated by defining the MBED_MEM_TRACING_ENABLED macro.
00034 
00035 Both tracers can be activated and deactivated in any combination. If both tracers
00036 are active, the second one (MBED_MEM_TRACING_ENABLED) will trace the first one's
00037 (MBED_HEAP_STATS_ENABLED) memory calls.*/
00038 
00039 /******************************************************************************/
00040 /* Implementation of the runtime max heap usage checker                       */
00041 /******************************************************************************/
00042 
00043 /* Size must be a multiple of 8 to keep alignment */
00044 typedef struct {
00045     uint32_t size;
00046     uint32_t pad;
00047 } alloc_info_t;
00048 
00049 static SingletonPtr<PlatformMutex>  malloc_stats_mutex;
00050 static SingletonPtr<PlatformMutex>  mem_trace_mutex;
00051 static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0};
00052 
00053 void mbed_stats_heap_get(mbed_stats_heap_t *stats)
00054 {
00055     malloc_stats_mutex->lock();
00056     memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t));
00057     malloc_stats_mutex->unlock();
00058 }
00059 
00060 /******************************************************************************/
00061 /* GCC memory allocation wrappers                                             */
00062 /******************************************************************************/
00063 
00064 #if defined(TOOLCHAIN_GCC)
00065 
00066 #ifdef   FEATURE_UVISOR
00067 #include "uvisor-lib/uvisor-lib.h"
00068 #endif/* FEATURE_UVISOR */
00069 
00070 // TODO: memory tracing doesn't work with uVisor enabled.
00071 #if !defined(FEATURE_UVISOR)
00072 
00073 extern "C" {
00074     void * __real__malloc_r(struct _reent * r, size_t size);
00075     void * __real__realloc_r(struct _reent * r, void * ptr, size_t size);
00076     void __real__free_r(struct _reent * r, void * ptr);
00077     void* __real__calloc_r(struct _reent * r, size_t nmemb, size_t size);
00078 }
00079 
00080 extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) {
00081     void *ptr = NULL;
00082 #ifdef MBED_HEAP_STATS_ENABLED
00083     malloc_stats_mutex->lock();
00084     alloc_info_t *alloc_info = (alloc_info_t*)__real__malloc_r(r, size + sizeof(alloc_info_t));
00085     if (alloc_info != NULL) {
00086         alloc_info->size = size;
00087         ptr = (void*)(alloc_info + 1);
00088         heap_stats.current_size += size;
00089         heap_stats.total_size += size;
00090         heap_stats.alloc_cnt += 1;
00091         if (heap_stats.current_size > heap_stats.max_size) {
00092             heap_stats.max_size = heap_stats.current_size;
00093         }
00094     } else {
00095         heap_stats.alloc_fail_cnt += 1;
00096     }
00097     malloc_stats_mutex->unlock();
00098 #else // #ifdef MBED_HEAP_STATS_ENABLED
00099     ptr = __real__malloc_r(r, size);
00100 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00101 #ifdef MBED_MEM_TRACING_ENABLED
00102     mem_trace_mutex->lock();
00103     mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR());
00104     mem_trace_mutex->unlock();
00105 #endif // #ifdef MBED_MEM_TRACING_ENABLED
00106     return ptr;
00107 }
00108 
00109 extern "C" void * __wrap__realloc_r(struct _reent * r, void * ptr, size_t size) {
00110     void *new_ptr = NULL;
00111 #ifdef MBED_HEAP_STATS_ENABLED
00112     // Implement realloc_r with malloc and free.
00113     // The function realloc_r can't be used here directly since
00114     // it can call into __wrap__malloc_r (returns ptr + 4) or
00115     // resize memory directly (returns ptr + 0).
00116 
00117     // Note - no lock needed since malloc and free are thread safe
00118 
00119     // Get old size
00120     uint32_t old_size = 0;
00121     if (ptr != NULL) {
00122         alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1;
00123         old_size = alloc_info->size;
00124     }
00125 
00126     // Allocate space
00127     if (size != 0) {
00128         new_ptr = malloc(size);
00129     }
00130 
00131     // If the new buffer has been allocated copy the data to it
00132     // and free the old buffer
00133     if (new_ptr != NULL) {
00134         uint32_t copy_size = (old_size < size) ? old_size : size;
00135         memcpy(new_ptr, (void*)ptr, copy_size);
00136         free(ptr);
00137     }
00138 #else // #ifdef MBED_HEAP_STATS_ENABLED
00139     new_ptr = __real__realloc_r(r, ptr, size);
00140 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00141 #ifdef MBED_MEM_TRACING_ENABLED
00142     mem_trace_mutex->lock();
00143     mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
00144     mem_trace_mutex->unlock();
00145 #endif // #ifdef MBED_MEM_TRACING_ENABLED
00146     return new_ptr;
00147 }
00148 
00149 extern "C" void __wrap__free_r(struct _reent * r, void * ptr) {
00150 #ifdef MBED_HEAP_STATS_ENABLED
00151     malloc_stats_mutex->lock();
00152     alloc_info_t *alloc_info = NULL;
00153     if (ptr != NULL) {
00154         alloc_info = ((alloc_info_t*)ptr) - 1;
00155         heap_stats.current_size -= alloc_info->size;
00156         heap_stats.alloc_cnt -= 1;
00157     }
00158     __real__free_r(r, (void*)alloc_info);
00159     malloc_stats_mutex->unlock();
00160 #else // #ifdef MBED_HEAP_STATS_ENABLED
00161     __real__free_r(r, ptr);
00162 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00163 #ifdef MBED_MEM_TRACING_ENABLED
00164     mem_trace_mutex->lock();
00165     mbed_mem_trace_free(ptr, MBED_CALLER_ADDR());
00166     mem_trace_mutex->unlock();
00167 #endif // #ifdef MBED_MEM_TRACING_ENABLED
00168 }
00169 
00170 extern "C" void * __wrap__calloc_r(struct _reent * r, size_t nmemb, size_t size) {
00171     void *ptr = NULL;
00172 #ifdef MBED_HEAP_STATS_ENABLED
00173     // Note - no lock needed since malloc is thread safe
00174 
00175     ptr = malloc(nmemb * size);
00176     if (ptr != NULL) {
00177         memset(ptr, 0, nmemb * size);
00178     }
00179 #else // #ifdef MBED_HEAP_STATS_ENABLED
00180     ptr = __real__calloc_r(r, nmemb, size);
00181 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00182 #ifdef MBED_MEM_TRACING_ENABLED
00183     mem_trace_mutex->lock();
00184     mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
00185     mem_trace_mutex->unlock();
00186 #endif // #ifdef MBED_MEM_TRACING_ENABLED
00187     return ptr;
00188 }
00189 
00190 #endif // if !defined(FEATURE_UVISOR)
00191 
00192 /******************************************************************************/
00193 /* ARMCC memory allocation wrappers                                           */
00194 /******************************************************************************/
00195 
00196 #elif defined(TOOLCHAIN_ARM) // #if defined(TOOLCHAIN_GCC)
00197 
00198 /* Enable hooking of memory function only if tracing is also enabled */
00199 #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
00200 
00201 extern "C" {
00202     void *$Super$$malloc(size_t size);
00203     void *$Super$$realloc(void *ptr, size_t size);
00204     void *$Super$$calloc(size_t nmemb, size_t size);
00205     void $Super$$free(void *ptr);
00206 }
00207 
00208 extern "C" void* $Sub$$malloc(size_t size) {
00209     void *ptr = NULL;
00210 #ifdef MBED_HEAP_STATS_ENABLED
00211     malloc_stats_mutex->lock();
00212     alloc_info_t *alloc_info = (alloc_info_t*)$Super$$malloc(size + sizeof(alloc_info_t));
00213     if (alloc_info != NULL) {
00214         alloc_info->size = size;
00215         ptr = (void*)(alloc_info + 1);
00216         heap_stats.current_size += size;
00217         heap_stats.total_size += size;
00218         heap_stats.alloc_cnt += 1;
00219         if (heap_stats.current_size > heap_stats.max_size) {
00220             heap_stats.max_size = heap_stats.current_size;
00221         }
00222     } else {
00223         heap_stats.alloc_fail_cnt += 1;
00224     }
00225     malloc_stats_mutex->unlock();
00226 #else // #ifdef MBED_HEAP_STATS_ENABLED
00227     ptr = $Super$$malloc(size);
00228 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00229 #ifdef MBED_MEM_TRACING_ENABLED
00230     mem_trace_mutex->lock();
00231     mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR());
00232     mem_trace_mutex->unlock();
00233 #endif // #ifdef MBED_MEM_TRACING_ENABLED
00234     return ptr;
00235 }
00236 
00237 extern "C" void* $Sub$$realloc(void *ptr, size_t size) {
00238     void *new_ptr = NULL;
00239 #ifdef MBED_HEAP_STATS_ENABLED
00240     // Note - no lock needed since malloc and free are thread safe
00241 
00242     // Get old size
00243     uint32_t old_size = 0;
00244     if (ptr != NULL) {
00245         alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1;
00246         old_size = alloc_info->size;
00247     }
00248 
00249     // Allocate space
00250     if (size != 0) {
00251         new_ptr = malloc(size);
00252     }
00253 
00254     // If the new buffer has been allocated copy the data to it
00255     // and free the old buffer
00256     if (new_ptr != NULL) {
00257         uint32_t copy_size = (old_size < size) ? old_size : size;
00258         memcpy(new_ptr, (void*)ptr, copy_size);
00259         free(ptr);
00260     }
00261 #else // #ifdef MBED_HEAP_STATS_ENABLED
00262     mem_trace_mutex->lock();
00263     new_ptr = $Super$$realloc(ptr, size);
00264     mem_trace_mutex->unlock();
00265 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00266 #ifdef MBED_MEM_TRACING_ENABLED
00267     mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
00268 #endif // #ifdef MBED_MEM_TRACING_ENABLED
00269     return new_ptr;
00270 }
00271 
00272 extern "C" void *$Sub$$calloc(size_t nmemb, size_t size) {
00273     void *ptr = NULL;
00274 #ifdef MBED_HEAP_STATS_ENABLED
00275     // Note - no lock needed since malloc is thread safe
00276     ptr = malloc(nmemb * size);
00277     if (ptr != NULL) {
00278         memset(ptr, 0, nmemb * size);
00279     }
00280 #else // #ifdef MBED_HEAP_STATS_ENABLED
00281     ptr = $Super$$calloc(nmemb, size);
00282 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00283 #ifdef MBED_MEM_TRACING_ENABLED
00284     mem_trace_mutex->lock();
00285     mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
00286     mem_trace_mutex->unlock();
00287 #endif // #ifdef MBED_MEM_TRACING_ENABLED
00288     return ptr;
00289 }
00290 
00291 extern "C" void $Sub$$free(void *ptr) {
00292 #ifdef MBED_HEAP_STATS_ENABLED
00293     malloc_stats_mutex->lock();
00294     alloc_info_t *alloc_info = NULL;
00295     if (ptr != NULL) {
00296         alloc_info = ((alloc_info_t*)ptr) - 1;
00297         heap_stats.current_size -= alloc_info->size;
00298         heap_stats.alloc_cnt -= 1;
00299     }
00300     $Super$$free((void*)alloc_info);
00301     malloc_stats_mutex->unlock();
00302 #else // #ifdef MBED_HEAP_STATS_ENABLED
00303     $Super$$free(ptr);
00304 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00305 #ifdef MBED_MEM_TRACING_ENABLED
00306     mem_trace_mutex->lock();
00307     mbed_mem_trace_free(ptr, MBED_CALLER_ADDR());
00308     mem_trace_mutex->unlock();
00309 #endif // #ifdef MBED_MEM_TRACING_ENABLED
00310 }
00311 
00312 #endif // #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
00313 
00314 /******************************************************************************/
00315 /* Allocation wrappers for other toolchains are not supported yet             */
00316 /******************************************************************************/
00317 
00318 #else // #if defined(TOOLCHAIN_GCC)
00319 
00320 #ifdef MBED_MEM_TRACING_ENABLED
00321 #warning Memory tracing is not supported with the current toolchain.
00322 #endif
00323 
00324 #ifdef MBED_HEAP_STATS_ENABLED
00325 #warning Heap statistics are not supported with the current toolchain.
00326 #endif
00327 
00328 #endif // #if defined(TOOLCHAIN_GCC)
00329 
00330