Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

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