takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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 "platform/mbed_mem_trace.h"
00018 #include "platform/mbed_stats.h"
00019 #include "platform/mbed_toolchain.h"
00020 #include "platform/SingletonPtr.h"
00021 #include "platform/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 setting the configuration option MBED_MEM_TRACING_ENABLED to true.
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 #ifdef MBED_HEAP_STATS_ENABLED
00050 static SingletonPtr<PlatformMutex>  malloc_stats_mutex;
00051 static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0, 0, 0};
00052 
00053 typedef struct  {
00054     size_t size;
00055 }mbed_heap_overhead_t;
00056 
00057 #define MALLOC_HEADER_SIZE          (sizeof(mbed_heap_overhead_t))
00058 #define MALLOC_HEADER_PTR(p)        (mbed_heap_overhead_t *)((char *)(p) - MALLOC_HEADER_SIZE)
00059 #define MALLOC_HEAP_TOTAL_SIZE(p)   (((p)->size) & (~0x1))
00060 #endif
00061 
00062 void mbed_stats_heap_get(mbed_stats_heap_t *stats)
00063 {
00064 #ifdef MBED_HEAP_STATS_ENABLED
00065     extern uint32_t mbed_heap_size;
00066     heap_stats.reserved_size = mbed_heap_size;
00067 
00068     malloc_stats_mutex->lock();
00069     memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t));
00070     malloc_stats_mutex->unlock();
00071 #else
00072     memset(stats, 0, sizeof(mbed_stats_heap_t));
00073 #endif
00074 }
00075 
00076 /******************************************************************************/
00077 /* GCC memory allocation wrappers                                             */
00078 /******************************************************************************/
00079 
00080 #if defined(TOOLCHAIN_GCC)
00081 
00082 extern "C" {
00083     void *__real__malloc_r(struct _reent *r, size_t size);
00084     void *__real__memalign_r(struct _reent *r, size_t alignment, size_t bytes);
00085     void *__real__realloc_r(struct _reent *r, void *ptr, size_t size);
00086     void __real__free_r(struct _reent *r, void *ptr);
00087     void *__real__calloc_r(struct _reent *r, size_t nmemb, size_t size);
00088     void *malloc_wrapper(struct _reent *r, size_t size, void *caller);
00089     void free_wrapper(struct _reent *r, void *ptr, void *caller);
00090 }
00091 
00092 
00093 extern "C" void *__wrap__malloc_r(struct _reent *r, size_t size)
00094 {
00095     return malloc_wrapper(r, size, MBED_CALLER_ADDR());
00096 }
00097 
00098 extern "C" void *malloc_wrapper(struct _reent *r, size_t size, void *caller)
00099 {
00100     void *ptr = NULL;
00101 #if MBED_MEM_TRACING_ENABLED
00102     mbed_mem_trace_lock();
00103 #endif
00104 #ifdef MBED_HEAP_STATS_ENABLED
00105     malloc_stats_mutex->lock();
00106     alloc_info_t *alloc_info = (alloc_info_t *)__real__malloc_r(r, size + sizeof(alloc_info_t));
00107     if (alloc_info != NULL) {
00108         alloc_info->size = size;
00109         ptr = (void *)(alloc_info + 1);
00110         heap_stats.current_size += size;
00111         heap_stats.total_size += size;
00112         heap_stats.alloc_cnt += 1;
00113         if (heap_stats.current_size > heap_stats.max_size) {
00114             heap_stats.max_size = heap_stats.current_size;
00115         }
00116         heap_stats.overhead_size += MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info)) - size;
00117     } else {
00118         heap_stats.alloc_fail_cnt += 1;
00119     }
00120     malloc_stats_mutex->unlock();
00121 #else // #ifdef MBED_HEAP_STATS_ENABLED
00122     ptr = __real__malloc_r(r, size);
00123 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00124 #if MBED_MEM_TRACING_ENABLED
00125     mbed_mem_trace_malloc(ptr, size, caller);
00126     mbed_mem_trace_unlock();
00127 #endif // #if MBED_MEM_TRACING_ENABLED
00128     return ptr;
00129 }
00130 
00131 extern "C" void *__wrap__realloc_r(struct _reent *r, void *ptr, size_t size)
00132 {
00133     void *new_ptr = NULL;
00134 #if MBED_MEM_TRACING_ENABLED
00135     mbed_mem_trace_lock();
00136 #endif
00137 #ifdef MBED_HEAP_STATS_ENABLED
00138     // Implement realloc_r with malloc and free.
00139     // The function realloc_r can't be used here directly since
00140     // it can call into __wrap__malloc_r (returns ptr + 4) or
00141     // resize memory directly (returns ptr + 0).
00142 
00143     // Note - no lock needed since malloc and free are thread safe
00144 
00145     // Get old size
00146     uint32_t old_size = 0;
00147     if (ptr != NULL) {
00148         alloc_info_t *alloc_info = ((alloc_info_t *)ptr) - 1;
00149         old_size = alloc_info->size;
00150     }
00151 
00152     // Allocate space
00153     if (size != 0) {
00154         new_ptr = malloc(size);
00155     }
00156 
00157     // If the new buffer has been allocated copy the data to it
00158     // and free the old buffer
00159     if (new_ptr != NULL) {
00160         uint32_t copy_size = (old_size < size) ? old_size : size;
00161         memcpy(new_ptr, (void *)ptr, copy_size);
00162         free(ptr);
00163     }
00164 #else // #ifdef MBED_HEAP_STATS_ENABLED
00165     new_ptr = __real__realloc_r(r, ptr, size);
00166 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00167 #if MBED_MEM_TRACING_ENABLED
00168     mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
00169     mbed_mem_trace_unlock();
00170 #endif // #if MBED_MEM_TRACING_ENABLED
00171     return new_ptr;
00172 }
00173 
00174 extern "C" void __wrap__free_r(struct _reent *r, void *ptr)
00175 {
00176     free_wrapper(r, ptr, MBED_CALLER_ADDR());
00177 }
00178 
00179 extern "C" void free_wrapper(struct _reent *r, void *ptr, void *caller)
00180 {
00181 #if MBED_MEM_TRACING_ENABLED
00182     mbed_mem_trace_lock();
00183 #endif
00184 #ifdef MBED_HEAP_STATS_ENABLED
00185     malloc_stats_mutex->lock();
00186     alloc_info_t *alloc_info = NULL;
00187     if (ptr != NULL) {
00188         alloc_info = ((alloc_info_t *)ptr) - 1;
00189         size_t user_size = alloc_info->size;
00190         size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info));
00191         heap_stats.current_size -= user_size;
00192         heap_stats.alloc_cnt -= 1;
00193         heap_stats.overhead_size -= (alloc_size - user_size);
00194     }
00195     __real__free_r(r, (void *)alloc_info);
00196 
00197     malloc_stats_mutex->unlock();
00198 #else // #ifdef MBED_HEAP_STATS_ENABLED
00199     __real__free_r(r, ptr);
00200 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00201 #if MBED_MEM_TRACING_ENABLED
00202     mbed_mem_trace_free(ptr, caller);
00203     mbed_mem_trace_unlock();
00204 #endif // #if MBED_MEM_TRACING_ENABLED
00205 }
00206 
00207 extern "C" void *__wrap__calloc_r(struct _reent *r, size_t nmemb, size_t size)
00208 {
00209     void *ptr = NULL;
00210 #if MBED_MEM_TRACING_ENABLED
00211     mbed_mem_trace_lock();
00212 #endif
00213 #ifdef MBED_HEAP_STATS_ENABLED
00214     // Note - no lock needed since malloc is thread safe
00215 
00216     ptr = malloc(nmemb * size);
00217     if (ptr != NULL) {
00218         memset(ptr, 0, nmemb * size);
00219     }
00220 #else // #ifdef MBED_HEAP_STATS_ENABLED
00221     ptr = __real__calloc_r(r, nmemb, size);
00222 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00223 #if MBED_MEM_TRACING_ENABLED
00224     mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
00225     mbed_mem_trace_unlock();
00226 #endif // #if MBED_MEM_TRACING_ENABLED
00227     return ptr;
00228 }
00229 
00230 extern "C" void *__wrap__memalign_r(struct _reent *r, size_t alignment, size_t bytes)
00231 {
00232     return __real__memalign_r(r, alignment, bytes);
00233 }
00234 
00235 
00236 
00237 /******************************************************************************/
00238 /* ARMCC / IAR memory allocation wrappers                                     */
00239 /******************************************************************************/
00240 
00241 #elif defined(TOOLCHAIN_ARM) || defined(__ICCARM__)
00242 
00243 #if defined(TOOLCHAIN_ARM)
00244 #define SUPER_MALLOC    $Super$$malloc
00245 #define SUB_MALLOC      $Sub$$malloc
00246 #define SUPER_REALLOC   $Super$$realloc
00247 #define SUB_REALLOC     $Sub$$realloc
00248 #define SUPER_CALLOC    $Super$$calloc
00249 #define SUB_CALLOC      $Sub$$calloc
00250 #define SUPER_FREE      $Super$$free
00251 #define SUB_FREE        $Sub$$free
00252 #elif defined(__ICCARM__)
00253 #define SUPER_MALLOC    $Super$$__iar_dlmalloc
00254 #define SUB_MALLOC      $Sub$$__iar_dlmalloc
00255 #define SUPER_REALLOC   $Super$$__iar_dlrealloc
00256 #define SUB_REALLOC     $Sub$$__iar_dlrealloc
00257 #define SUPER_CALLOC    $Super$$__iar_dlcalloc
00258 #define SUB_CALLOC      $Sub$$__iar_dlcalloc
00259 #define SUPER_FREE      $Super$$__iar_dlfree
00260 #define SUB_FREE        $Sub$$__iar_dlfree
00261 #endif
00262 
00263 /* Enable hooking of memory function only if tracing is also enabled */
00264 #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
00265 
00266 extern "C" {
00267     void *SUPER_MALLOC(size_t size);
00268     void *SUPER_REALLOC(void *ptr, size_t size);
00269     void *SUPER_CALLOC(size_t nmemb, size_t size);
00270     void SUPER_FREE(void *ptr);
00271     void *malloc_wrapper(size_t size, void *caller);
00272     void free_wrapper(void *ptr, void *caller);
00273 }
00274 
00275 extern "C" void *SUB_MALLOC(size_t size)
00276 {
00277     return malloc_wrapper(size, MBED_CALLER_ADDR());
00278 }
00279 
00280 extern "C" void *malloc_wrapper(size_t size, void *caller)
00281 {
00282     void *ptr = NULL;
00283 #if MBED_MEM_TRACING_ENABLED
00284     mbed_mem_trace_lock();
00285 #endif
00286 #ifdef MBED_HEAP_STATS_ENABLED
00287     malloc_stats_mutex->lock();
00288     alloc_info_t *alloc_info = (alloc_info_t *)SUPER_MALLOC(size + sizeof(alloc_info_t));
00289     if (alloc_info != NULL) {
00290         alloc_info->size = size;
00291         ptr = (void *)(alloc_info + 1);
00292         heap_stats.current_size += size;
00293         heap_stats.total_size += size;
00294         heap_stats.alloc_cnt += 1;
00295         if (heap_stats.current_size > heap_stats.max_size) {
00296             heap_stats.max_size = heap_stats.current_size;
00297         }
00298         heap_stats.overhead_size += MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info)) - size;
00299     } else {
00300         heap_stats.alloc_fail_cnt += 1;
00301     }
00302     malloc_stats_mutex->unlock();
00303 #else // #ifdef MBED_HEAP_STATS_ENABLED
00304     ptr = SUPER_MALLOC(size);
00305 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00306 #if MBED_MEM_TRACING_ENABLED
00307     mbed_mem_trace_malloc(ptr, size, caller);
00308     mbed_mem_trace_unlock();
00309 #endif // #if MBED_MEM_TRACING_ENABLED
00310     return ptr;
00311 }
00312 
00313 
00314 extern "C" void *SUB_REALLOC(void *ptr, size_t size)
00315 {
00316     void *new_ptr = NULL;
00317 #if MBED_MEM_TRACING_ENABLED
00318     mbed_mem_trace_lock();
00319 #endif
00320 #ifdef MBED_HEAP_STATS_ENABLED
00321     // Note - no lock needed since malloc and free are thread safe
00322 
00323     // Get old size
00324     uint32_t old_size = 0;
00325     if (ptr != NULL) {
00326         alloc_info_t *alloc_info = ((alloc_info_t *)ptr) - 1;
00327         old_size = alloc_info->size;
00328     }
00329 
00330     // Allocate space
00331     if (size != 0) {
00332         new_ptr = malloc(size);
00333     }
00334 
00335     // If the new buffer has been allocated copy the data to it
00336     // and free the old buffer
00337     if ((new_ptr != NULL) && (ptr != NULL)) {
00338         uint32_t copy_size = (old_size < size) ? old_size : size;
00339         memcpy(new_ptr, (void *)ptr, copy_size);
00340         free(ptr);
00341     }
00342 #else // #ifdef MBED_HEAP_STATS_ENABLED
00343     new_ptr = SUPER_REALLOC(ptr, size);
00344 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00345 #if MBED_MEM_TRACING_ENABLED
00346     mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
00347     mbed_mem_trace_unlock();
00348 #endif // #if MBED_MEM_TRACING_ENABLED
00349     return new_ptr;
00350 }
00351 
00352 extern "C" void *SUB_CALLOC(size_t nmemb, size_t size)
00353 {
00354     void *ptr = NULL;
00355 #if MBED_MEM_TRACING_ENABLED
00356     mbed_mem_trace_lock();
00357 #endif
00358 #ifdef MBED_HEAP_STATS_ENABLED
00359     // Note - no lock needed since malloc is thread safe
00360     ptr = malloc(nmemb * size);
00361     if (ptr != NULL) {
00362         memset(ptr, 0, nmemb * size);
00363     }
00364 #else // #ifdef MBED_HEAP_STATS_ENABLED
00365     ptr = SUPER_CALLOC(nmemb, size);
00366 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00367 #if MBED_MEM_TRACING_ENABLED
00368     mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
00369     mbed_mem_trace_unlock();
00370 #endif // #if MBED_MEM_TRACING_ENABLED
00371     return ptr;
00372 }
00373 
00374 extern "C" void SUB_FREE(void *ptr)
00375 {
00376     free_wrapper(ptr, MBED_CALLER_ADDR());
00377 }
00378 
00379 extern "C" void free_wrapper(void *ptr, void *caller)
00380 {
00381 #if MBED_MEM_TRACING_ENABLED
00382     mbed_mem_trace_lock();
00383 #endif
00384 #ifdef MBED_HEAP_STATS_ENABLED
00385     malloc_stats_mutex->lock();
00386     alloc_info_t *alloc_info = NULL;
00387     if (ptr != NULL) {
00388         alloc_info = ((alloc_info_t *)ptr) - 1;
00389         size_t user_size = alloc_info->size;
00390         size_t alloc_size = MALLOC_HEAP_TOTAL_SIZE(MALLOC_HEADER_PTR(alloc_info));
00391         heap_stats.current_size -= user_size;
00392         heap_stats.alloc_cnt -= 1;
00393         heap_stats.overhead_size -= (alloc_size - user_size);
00394     }
00395     SUPER_FREE((void *)alloc_info);
00396 
00397     malloc_stats_mutex->unlock();
00398 #else // #ifdef MBED_HEAP_STATS_ENABLED
00399     SUPER_FREE(ptr);
00400 #endif // #ifdef MBED_HEAP_STATS_ENABLED
00401 #if MBED_MEM_TRACING_ENABLED
00402     mbed_mem_trace_free(ptr, caller);
00403     mbed_mem_trace_unlock();
00404 #endif // #if MBED_MEM_TRACING_ENABLED
00405 }
00406 
00407 #endif // #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
00408 
00409 /******************************************************************************/
00410 /* Allocation wrappers for other toolchains are not supported yet             */
00411 /******************************************************************************/
00412 
00413 #else
00414 
00415 #if MBED_MEM_TRACING_ENABLED
00416 #error Memory tracing is not supported with the current toolchain.
00417 #endif
00418 
00419 #ifdef MBED_HEAP_STATS_ENABLED
00420 #error Heap statistics are not supported with the current toolchain.
00421 #endif
00422 
00423 #endif // #if defined(TOOLCHAIN_GCC)