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
NYX 0:85b3fd62ea1a 2 /** \addtogroup platform */
NYX 0:85b3fd62ea1a 3 /** @{*/
NYX 0:85b3fd62ea1a 4 /* mbed Microcontroller Library
NYX 0:85b3fd62ea1a 5 * Copyright (c) 2006-2016 ARM Limited
NYX 0:85b3fd62ea1a 6 *
NYX 0:85b3fd62ea1a 7 * Licensed under the Apache License, Version 2.0 (the "License");
NYX 0:85b3fd62ea1a 8 * you may not use this file except in compliance with the License.
NYX 0:85b3fd62ea1a 9 * You may obtain a copy of the License at
NYX 0:85b3fd62ea1a 10 *
NYX 0:85b3fd62ea1a 11 * http://www.apache.org/licenses/LICENSE-2.0
NYX 0:85b3fd62ea1a 12 *
NYX 0:85b3fd62ea1a 13 * Unless required by applicable law or agreed to in writing, software
NYX 0:85b3fd62ea1a 14 * distributed under the License is distributed on an "AS IS" BASIS,
NYX 0:85b3fd62ea1a 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
NYX 0:85b3fd62ea1a 16 * See the License for the specific language governing permissions and
NYX 0:85b3fd62ea1a 17 * limitations under the License.
NYX 0:85b3fd62ea1a 18 */
NYX 0:85b3fd62ea1a 19
NYX 0:85b3fd62ea1a 20 #ifndef __MBED_MEM_TRACE_H__
NYX 0:85b3fd62ea1a 21 #define __MBED_MEM_TRACE_H__
NYX 0:85b3fd62ea1a 22
NYX 0:85b3fd62ea1a 23 #ifdef __cplusplus
NYX 0:85b3fd62ea1a 24 extern "C" {
NYX 0:85b3fd62ea1a 25 #endif
NYX 0:85b3fd62ea1a 26
NYX 0:85b3fd62ea1a 27 #include <stdint.h>
NYX 0:85b3fd62ea1a 28 #include <stddef.h>
NYX 0:85b3fd62ea1a 29
NYX 0:85b3fd62ea1a 30 /* Operation types for tracer */
NYX 0:85b3fd62ea1a 31 enum {
NYX 0:85b3fd62ea1a 32 MBED_MEM_TRACE_MALLOC,
NYX 0:85b3fd62ea1a 33 MBED_MEM_TRACE_REALLOC,
NYX 0:85b3fd62ea1a 34 MBED_MEM_TRACE_CALLOC,
NYX 0:85b3fd62ea1a 35 MBED_MEM_TRACE_FREE
NYX 0:85b3fd62ea1a 36 };
NYX 0:85b3fd62ea1a 37
NYX 0:85b3fd62ea1a 38 /* Prefix for the output of the default tracer */
NYX 0:85b3fd62ea1a 39 #define MBED_MEM_DEFAULT_TRACER_PREFIX "#"
NYX 0:85b3fd62ea1a 40
NYX 0:85b3fd62ea1a 41 /**
NYX 0:85b3fd62ea1a 42 * Type of the callback used by the memory tracer. This callback is called when a memory
NYX 0:85b3fd62ea1a 43 * allocation operation (malloc, realloc, calloc, free) is called and tracing is enabled
NYX 0:85b3fd62ea1a 44 * for that memory allocation function.
NYX 0:85b3fd62ea1a 45 *
NYX 0:85b3fd62ea1a 46 * @param op the ID of the operation (MBED_MEM_TRACE_MALLOC, MBED_MEM_TRACE_REALLOC,
NYX 0:85b3fd62ea1a 47 * MBED_MEM_TRACE_CALLOC or MBED_MEM_TRACE_FREE).
NYX 0:85b3fd62ea1a 48 * @param res the result that the memory operation returned (NULL for 'free').
NYX 0:85b3fd62ea1a 49 * @param caller the caller of the memory operation. Note that the value of 'caller' might be
NYX 0:85b3fd62ea1a 50 * unreliable.
NYX 0:85b3fd62ea1a 51 *
NYX 0:85b3fd62ea1a 52 * The rest of the parameters passed 'mbed_mem_trace_cb_t' are the same as the memory operations
NYX 0:85b3fd62ea1a 53 * that triggered its call (see 'man malloc' for details):
NYX 0:85b3fd62ea1a 54 *
NYX 0:85b3fd62ea1a 55 * - for malloc: cb(MBED_MEM_TRACE_MALLOC, res, caller, size).
NYX 0:85b3fd62ea1a 56 * - for realloc: cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size).
NYX 0:85b3fd62ea1a 57 * - for calloc: cb(MBED_MEM_TRACE_CALLOC, res, caller, nmemb, size).
NYX 0:85b3fd62ea1a 58 * - for free: cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr).
NYX 0:85b3fd62ea1a 59 */
NYX 0:85b3fd62ea1a 60 typedef void (*mbed_mem_trace_cb_t)(uint8_t op, void *res, void* caller, ...);
NYX 0:85b3fd62ea1a 61
NYX 0:85b3fd62ea1a 62 /**
NYX 0:85b3fd62ea1a 63 * Set the callback used by the memory tracer (use NULL for disable tracing).
NYX 0:85b3fd62ea1a 64 *
NYX 0:85b3fd62ea1a 65 * @param cb the callback to call on each memory operation.
NYX 0:85b3fd62ea1a 66 */
NYX 0:85b3fd62ea1a 67 void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb);
NYX 0:85b3fd62ea1a 68
NYX 0:85b3fd62ea1a 69 /**
NYX 0:85b3fd62ea1a 70 * Trace a call to 'malloc'.
NYX 0:85b3fd62ea1a 71 * @param res the result of running 'malloc'.
NYX 0:85b3fd62ea1a 72 * @param size the 'size' argument given to 'malloc'.
NYX 0:85b3fd62ea1a 73 * @param caller the caller of the memory operation.
NYX 0:85b3fd62ea1a 74 * @return 'res' (the first argument).
NYX 0:85b3fd62ea1a 75 */
NYX 0:85b3fd62ea1a 76 void *mbed_mem_trace_malloc(void *res, size_t size, void *caller);
NYX 0:85b3fd62ea1a 77
NYX 0:85b3fd62ea1a 78 /**
NYX 0:85b3fd62ea1a 79 * Trace a call to 'realloc'.
NYX 0:85b3fd62ea1a 80 * @param res the result of running 'realloc'.
NYX 0:85b3fd62ea1a 81 * @param ptr the 'ptr' argument given to 'realloc'.
NYX 0:85b3fd62ea1a 82 * @param size the 'size' argument given to 'realloc'.
NYX 0:85b3fd62ea1a 83 * @param caller the caller of the memory operation.
NYX 0:85b3fd62ea1a 84 * @return 'res' (the first argument).
NYX 0:85b3fd62ea1a 85 */
NYX 0:85b3fd62ea1a 86 void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller);
NYX 0:85b3fd62ea1a 87
NYX 0:85b3fd62ea1a 88 /**
NYX 0:85b3fd62ea1a 89 * Trace a call to 'calloc'.
NYX 0:85b3fd62ea1a 90 * @param res the result of running 'calloc'.
NYX 0:85b3fd62ea1a 91 * @param num the 'nmemb' argument given to 'calloc'.
NYX 0:85b3fd62ea1a 92 * @param size the 'size' argument given to 'calloc'.
NYX 0:85b3fd62ea1a 93 * @param caller the caller of the memory operation.
NYX 0:85b3fd62ea1a 94 * @return 'res' (the first argument).
NYX 0:85b3fd62ea1a 95 */
NYX 0:85b3fd62ea1a 96 void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller);
NYX 0:85b3fd62ea1a 97
NYX 0:85b3fd62ea1a 98 /**
NYX 0:85b3fd62ea1a 99 * Trace a call to 'free'.
NYX 0:85b3fd62ea1a 100 * @param ptr the 'ptr' argument given to 'free'.
NYX 0:85b3fd62ea1a 101 * @param caller the caller of the memory operation.
NYX 0:85b3fd62ea1a 102 */
NYX 0:85b3fd62ea1a 103 void mbed_mem_trace_free(void *ptr, void *caller);
NYX 0:85b3fd62ea1a 104
NYX 0:85b3fd62ea1a 105 /**
NYX 0:85b3fd62ea1a 106 * Default memory trace callback. DO NOT CALL DIRECTLY. It is meant to be used
NYX 0:85b3fd62ea1a 107 * as the second argument of 'mbed_mem_trace_setup'.
NYX 0:85b3fd62ea1a 108 *
NYX 0:85b3fd62ea1a 109 * The default callback outputs trace data using 'printf', in a format that's
NYX 0:85b3fd62ea1a 110 * easily parsable by an external tool. For each memory operation, the callback
NYX 0:85b3fd62ea1a 111 * outputs a line that begins with "#<op>:<0xresult>;<0xcaller>-":
NYX 0:85b3fd62ea1a 112 *
NYX 0:85b3fd62ea1a 113 * @param op identifies the memory operation ('m' for 'malloc', 'r' for 'realloc',
NYX 0:85b3fd62ea1a 114 * 'c' for 'calloc' and 'f' for 'free').
NYX 0:85b3fd62ea1a 115 * @param res (base 16) is the result of the memor operation. This is always NULL
NYX 0:85b3fd62ea1a 116 * for 'free', since 'free' doesn't return anything.
NYX 0:85b3fd62ea1a 117 * @param caller (base 16) is the caller of the memory operation. Note that the value
NYX 0:85b3fd62ea1a 118 * of 'caller' might be unreliable.
NYX 0:85b3fd62ea1a 119 *
NYX 0:85b3fd62ea1a 120 * The rest of the output depends on the operation being traced:
NYX 0:85b3fd62ea1a 121 *
NYX 0:85b3fd62ea1a 122 * - for 'malloc': 'size', where 'size' is the original argument to 'malloc'.
NYX 0:85b3fd62ea1a 123 * - for 'realloc': '0xptr;size', where 'ptr' (base 16) and 'size' are the original arguments to 'realloc'.
NYX 0:85b3fd62ea1a 124 * - for 'calloc': 'nmemb;size', where 'nmemb' and 'size' are the original arguments to 'calloc'.
NYX 0:85b3fd62ea1a 125 * - for 'free': '0xptr', where 'ptr' (base 16) is the original argument to 'free'.
NYX 0:85b3fd62ea1a 126 *
NYX 0:85b3fd62ea1a 127 * Examples:
NYX 0:85b3fd62ea1a 128 *
NYX 0:85b3fd62ea1a 129 * - "#m:0x20003240;0x600d-50" encodes a 'malloc' that returned 0x20003240, was called
NYX 0:85b3fd62ea1a 130 * by the instruction at 0x600D with a the 'size' argument equal to 50.
NYX 0:85b3fd62ea1a 131 * - "#f:0x0;0x602f-0x20003240" encodes a 'free' that was called by the instruction at
NYX 0:85b3fd62ea1a 132 * 0x602f with the 'ptr' argument equal to 0x20003240.
NYX 0:85b3fd62ea1a 133 */
NYX 0:85b3fd62ea1a 134 void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...);
NYX 0:85b3fd62ea1a 135
NYX 0:85b3fd62ea1a 136 #ifdef __cplusplus
NYX 0:85b3fd62ea1a 137 }
NYX 0:85b3fd62ea1a 138 #endif
NYX 0:85b3fd62ea1a 139
NYX 0:85b3fd62ea1a 140 #endif// #ifndef __MBED_MEM_TRACE_H__
NYX 0:85b3fd62ea1a 141
NYX 0:85b3fd62ea1a 142
NYX 0:85b3fd62ea1a 143 /** @}*/