mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:f782d9c66c49 1 /* mbed Microcontroller Library
dkato 0:f782d9c66c49 2 * Copyright (c) 2006-2016 ARM Limited
dkato 0:f782d9c66c49 3 *
dkato 0:f782d9c66c49 4 * Licensed under the Apache License, Version 2.0 (the "License");
dkato 0:f782d9c66c49 5 * you may not use this file except in compliance with the License.
dkato 0:f782d9c66c49 6 * You may obtain a copy of the License at
dkato 0:f782d9c66c49 7 *
dkato 0:f782d9c66c49 8 * http://www.apache.org/licenses/LICENSE-2.0
dkato 0:f782d9c66c49 9 *
dkato 0:f782d9c66c49 10 * Unless required by applicable law or agreed to in writing, software
dkato 0:f782d9c66c49 11 * distributed under the License is distributed on an "AS IS" BASIS,
dkato 0:f782d9c66c49 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dkato 0:f782d9c66c49 13 * See the License for the specific language governing permissions and
dkato 0:f782d9c66c49 14 * limitations under the License.
dkato 0:f782d9c66c49 15 */
dkato 0:f782d9c66c49 16
dkato 0:f782d9c66c49 17 #include <stdlib.h>
dkato 0:f782d9c66c49 18 #include <stdarg.h>
dkato 0:f782d9c66c49 19 #include <stdio.h>
dkato 0:f782d9c66c49 20 #include "platform/mbed_mem_trace.h"
dkato 0:f782d9c66c49 21 #include "platform/mbed_critical.h"
dkato 0:f782d9c66c49 22
dkato 0:f782d9c66c49 23 /******************************************************************************
dkato 0:f782d9c66c49 24 * Internal variables, functions and helpers
dkato 0:f782d9c66c49 25 *****************************************************************************/
dkato 0:f782d9c66c49 26
dkato 0:f782d9c66c49 27 /* The callback function that will be called after a traced memory operations finishes. */
dkato 0:f782d9c66c49 28 static mbed_mem_trace_cb_t mem_trace_cb;
dkato 0:f782d9c66c49 29 /* 'trave_level' guards "trace inside trace" situations (for example, the implementation
dkato 0:f782d9c66c49 30 * of realloc() might call malloc() internally, and since malloc() is also traced, this could
dkato 0:f782d9c66c49 31 * result in two calls to the callback function instead of one. */
dkato 0:f782d9c66c49 32 static uint8_t trace_level;
dkato 0:f782d9c66c49 33
dkato 0:f782d9c66c49 34 /******************************************************************************
dkato 0:f782d9c66c49 35 * Public interface
dkato 0:f782d9c66c49 36 *****************************************************************************/
dkato 0:f782d9c66c49 37
dkato 0:f782d9c66c49 38 void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb) {
dkato 0:f782d9c66c49 39 mem_trace_cb = cb;
dkato 0:f782d9c66c49 40 }
dkato 0:f782d9c66c49 41
dkato 0:f782d9c66c49 42 void *mbed_mem_trace_malloc(void *res, size_t size, void *caller) {
dkato 0:f782d9c66c49 43 if (mem_trace_cb) {
dkato 0:f782d9c66c49 44 if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
dkato 0:f782d9c66c49 45 mem_trace_cb(MBED_MEM_TRACE_MALLOC, res, caller, size);
dkato 0:f782d9c66c49 46 }
dkato 0:f782d9c66c49 47 core_util_atomic_decr_u8(&trace_level, 1);
dkato 0:f782d9c66c49 48 }
dkato 0:f782d9c66c49 49 return res;
dkato 0:f782d9c66c49 50 }
dkato 0:f782d9c66c49 51
dkato 0:f782d9c66c49 52 void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller) {
dkato 0:f782d9c66c49 53 if (mem_trace_cb) {
dkato 0:f782d9c66c49 54 if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
dkato 0:f782d9c66c49 55 mem_trace_cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size);
dkato 0:f782d9c66c49 56 }
dkato 0:f782d9c66c49 57 core_util_atomic_decr_u8(&trace_level, 1);
dkato 0:f782d9c66c49 58 }
dkato 0:f782d9c66c49 59 return res;
dkato 0:f782d9c66c49 60 }
dkato 0:f782d9c66c49 61
dkato 0:f782d9c66c49 62 void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller) {
dkato 0:f782d9c66c49 63 if (mem_trace_cb) {
dkato 0:f782d9c66c49 64 if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
dkato 0:f782d9c66c49 65 mem_trace_cb(MBED_MEM_TRACE_CALLOC, res, caller, num, size);
dkato 0:f782d9c66c49 66 }
dkato 0:f782d9c66c49 67 core_util_atomic_decr_u8(&trace_level, 1);
dkato 0:f782d9c66c49 68 }
dkato 0:f782d9c66c49 69 return res;
dkato 0:f782d9c66c49 70 }
dkato 0:f782d9c66c49 71
dkato 0:f782d9c66c49 72 void mbed_mem_trace_free(void *ptr, void *caller) {
dkato 0:f782d9c66c49 73 if (mem_trace_cb) {
dkato 0:f782d9c66c49 74 if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
dkato 0:f782d9c66c49 75 mem_trace_cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr);
dkato 0:f782d9c66c49 76 }
dkato 0:f782d9c66c49 77 core_util_atomic_decr_u8(&trace_level, 1);
dkato 0:f782d9c66c49 78 }
dkato 0:f782d9c66c49 79 }
dkato 0:f782d9c66c49 80
dkato 0:f782d9c66c49 81 void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...) {
dkato 0:f782d9c66c49 82 va_list va;
dkato 0:f782d9c66c49 83 size_t temp_s1, temp_s2;
dkato 0:f782d9c66c49 84 void *temp_ptr;
dkato 0:f782d9c66c49 85
dkato 0:f782d9c66c49 86 va_start(va, caller);
dkato 0:f782d9c66c49 87 switch(op) {
dkato 0:f782d9c66c49 88 case MBED_MEM_TRACE_MALLOC:
dkato 0:f782d9c66c49 89 temp_s1 = va_arg(va, size_t);
dkato 0:f782d9c66c49 90 printf(MBED_MEM_DEFAULT_TRACER_PREFIX "m:%p;%p-%u\n", res, caller, temp_s1);
dkato 0:f782d9c66c49 91 break;
dkato 0:f782d9c66c49 92
dkato 0:f782d9c66c49 93 case MBED_MEM_TRACE_REALLOC:
dkato 0:f782d9c66c49 94 temp_ptr = va_arg(va, void*);
dkato 0:f782d9c66c49 95 temp_s1 = va_arg(va, size_t);
dkato 0:f782d9c66c49 96 printf(MBED_MEM_DEFAULT_TRACER_PREFIX "r:%p;%p-%p;%u\n", res, caller, temp_ptr, temp_s1);
dkato 0:f782d9c66c49 97 break;
dkato 0:f782d9c66c49 98
dkato 0:f782d9c66c49 99 case MBED_MEM_TRACE_CALLOC:
dkato 0:f782d9c66c49 100 temp_s1 = va_arg(va, size_t);
dkato 0:f782d9c66c49 101 temp_s2 = va_arg(va, size_t);
dkato 0:f782d9c66c49 102 printf(MBED_MEM_DEFAULT_TRACER_PREFIX "c:%p;%p-%u;%u\n", res, caller, temp_s1, temp_s2);
dkato 0:f782d9c66c49 103 break;
dkato 0:f782d9c66c49 104
dkato 0:f782d9c66c49 105 case MBED_MEM_TRACE_FREE:
dkato 0:f782d9c66c49 106 temp_ptr = va_arg(va, void*);
dkato 0:f782d9c66c49 107 printf(MBED_MEM_DEFAULT_TRACER_PREFIX "f:%p;%p-%p\n", res, caller, temp_ptr);
dkato 0:f782d9c66c49 108 break;
dkato 0:f782d9c66c49 109
dkato 0:f782d9c66c49 110 default:
dkato 0:f782d9c66c49 111 printf("?\n");
dkato 0:f782d9c66c49 112 }
dkato 0:f782d9c66c49 113 va_end(va);
dkato 0:f782d9c66c49 114 }
dkato 0:f782d9c66c49 115