Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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