inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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