Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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