mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
platform/source/mbed_mem_trace.cpp@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 1:9db0e321a9f4 | 1 | /* mbed Microcontroller Library |
kenjiArai | 1:9db0e321a9f4 | 2 | * Copyright (c) 2006-2019 ARM Limited |
kenjiArai | 1:9db0e321a9f4 | 3 | * SPDX-License-Identifier: Apache-2.0 |
kenjiArai | 1:9db0e321a9f4 | 4 | * |
kenjiArai | 1:9db0e321a9f4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 1:9db0e321a9f4 | 6 | * you may not use this file except in compliance with the License. |
kenjiArai | 1:9db0e321a9f4 | 7 | * You may obtain a copy of the License at |
kenjiArai | 1:9db0e321a9f4 | 8 | * |
kenjiArai | 1:9db0e321a9f4 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 1:9db0e321a9f4 | 10 | * |
kenjiArai | 1:9db0e321a9f4 | 11 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 1:9db0e321a9f4 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 1:9db0e321a9f4 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 1:9db0e321a9f4 | 14 | * See the License for the specific language governing permissions and |
kenjiArai | 1:9db0e321a9f4 | 15 | * limitations under the License. |
kenjiArai | 1:9db0e321a9f4 | 16 | */ |
kenjiArai | 1:9db0e321a9f4 | 17 | |
kenjiArai | 1:9db0e321a9f4 | 18 | #include <stdlib.h> |
kenjiArai | 1:9db0e321a9f4 | 19 | #include <stdarg.h> |
kenjiArai | 1:9db0e321a9f4 | 20 | #include <stdio.h> |
kenjiArai | 1:9db0e321a9f4 | 21 | #include "platform/mbed_mem_trace.h" |
kenjiArai | 1:9db0e321a9f4 | 22 | #include "platform/SingletonPtr.h" |
kenjiArai | 1:9db0e321a9f4 | 23 | #include "platform/PlatformMutex.h" |
kenjiArai | 1:9db0e321a9f4 | 24 | |
kenjiArai | 1:9db0e321a9f4 | 25 | /****************************************************************************** |
kenjiArai | 1:9db0e321a9f4 | 26 | * Internal variables, functions and helpers |
kenjiArai | 1:9db0e321a9f4 | 27 | *****************************************************************************/ |
kenjiArai | 1:9db0e321a9f4 | 28 | |
kenjiArai | 1:9db0e321a9f4 | 29 | /* The callback function that will be called after a traced memory operations finishes. */ |
kenjiArai | 1:9db0e321a9f4 | 30 | static mbed_mem_trace_cb_t mem_trace_cb; |
kenjiArai | 1:9db0e321a9f4 | 31 | static mbed_mem_trace_cb_t mem_trace_cb_reserve; |
kenjiArai | 1:9db0e321a9f4 | 32 | /* 'trace_lock_count' guards "trace inside trace" situations (for example, the implementation |
kenjiArai | 1:9db0e321a9f4 | 33 | * of realloc() might call malloc() internally, and since malloc() is also traced, this could |
kenjiArai | 1:9db0e321a9f4 | 34 | * result in two calls to the callback function instead of one. */ |
kenjiArai | 1:9db0e321a9f4 | 35 | static uint8_t trace_lock_count; |
kenjiArai | 1:9db0e321a9f4 | 36 | static SingletonPtr<PlatformMutex> mem_trace_mutex; |
kenjiArai | 1:9db0e321a9f4 | 37 | |
kenjiArai | 1:9db0e321a9f4 | 38 | #define TRACE_FIRST_LOCK() (trace_lock_count < 2) |
kenjiArai | 1:9db0e321a9f4 | 39 | |
kenjiArai | 1:9db0e321a9f4 | 40 | |
kenjiArai | 1:9db0e321a9f4 | 41 | /****************************************************************************** |
kenjiArai | 1:9db0e321a9f4 | 42 | * Public interface |
kenjiArai | 1:9db0e321a9f4 | 43 | *****************************************************************************/ |
kenjiArai | 1:9db0e321a9f4 | 44 | |
kenjiArai | 1:9db0e321a9f4 | 45 | void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb) |
kenjiArai | 1:9db0e321a9f4 | 46 | { |
kenjiArai | 1:9db0e321a9f4 | 47 | mem_trace_cb = cb; |
kenjiArai | 1:9db0e321a9f4 | 48 | } |
kenjiArai | 1:9db0e321a9f4 | 49 | |
kenjiArai | 1:9db0e321a9f4 | 50 | void mbed_mem_trace_disable() |
kenjiArai | 1:9db0e321a9f4 | 51 | { |
kenjiArai | 1:9db0e321a9f4 | 52 | mbed_mem_trace_lock(); |
kenjiArai | 1:9db0e321a9f4 | 53 | if (mem_trace_cb) { |
kenjiArai | 1:9db0e321a9f4 | 54 | mem_trace_cb_reserve = mem_trace_cb; |
kenjiArai | 1:9db0e321a9f4 | 55 | mem_trace_cb = 0; |
kenjiArai | 1:9db0e321a9f4 | 56 | } |
kenjiArai | 1:9db0e321a9f4 | 57 | mbed_mem_trace_unlock(); |
kenjiArai | 1:9db0e321a9f4 | 58 | } |
kenjiArai | 1:9db0e321a9f4 | 59 | void mbed_mem_trace_enable() |
kenjiArai | 1:9db0e321a9f4 | 60 | { |
kenjiArai | 1:9db0e321a9f4 | 61 | mbed_mem_trace_lock(); |
kenjiArai | 1:9db0e321a9f4 | 62 | if (!mem_trace_cb && mem_trace_cb_reserve) { |
kenjiArai | 1:9db0e321a9f4 | 63 | mem_trace_cb = mem_trace_cb_reserve; |
kenjiArai | 1:9db0e321a9f4 | 64 | } |
kenjiArai | 1:9db0e321a9f4 | 65 | mbed_mem_trace_unlock(); |
kenjiArai | 1:9db0e321a9f4 | 66 | } |
kenjiArai | 1:9db0e321a9f4 | 67 | |
kenjiArai | 1:9db0e321a9f4 | 68 | void mbed_mem_trace_lock() |
kenjiArai | 1:9db0e321a9f4 | 69 | { |
kenjiArai | 1:9db0e321a9f4 | 70 | mem_trace_mutex->lock(); |
kenjiArai | 1:9db0e321a9f4 | 71 | trace_lock_count++; |
kenjiArai | 1:9db0e321a9f4 | 72 | } |
kenjiArai | 1:9db0e321a9f4 | 73 | |
kenjiArai | 1:9db0e321a9f4 | 74 | void mbed_mem_trace_unlock() |
kenjiArai | 1:9db0e321a9f4 | 75 | { |
kenjiArai | 1:9db0e321a9f4 | 76 | trace_lock_count--; |
kenjiArai | 1:9db0e321a9f4 | 77 | mem_trace_mutex->unlock(); |
kenjiArai | 1:9db0e321a9f4 | 78 | } |
kenjiArai | 1:9db0e321a9f4 | 79 | |
kenjiArai | 1:9db0e321a9f4 | 80 | void *mbed_mem_trace_malloc(void *res, size_t size, void *caller) |
kenjiArai | 1:9db0e321a9f4 | 81 | { |
kenjiArai | 1:9db0e321a9f4 | 82 | if (mem_trace_cb) { |
kenjiArai | 1:9db0e321a9f4 | 83 | if (TRACE_FIRST_LOCK()) { |
kenjiArai | 1:9db0e321a9f4 | 84 | mem_trace_cb(MBED_MEM_TRACE_MALLOC, res, caller, size); |
kenjiArai | 1:9db0e321a9f4 | 85 | } |
kenjiArai | 1:9db0e321a9f4 | 86 | } |
kenjiArai | 1:9db0e321a9f4 | 87 | return res; |
kenjiArai | 1:9db0e321a9f4 | 88 | } |
kenjiArai | 1:9db0e321a9f4 | 89 | |
kenjiArai | 1:9db0e321a9f4 | 90 | void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller) |
kenjiArai | 1:9db0e321a9f4 | 91 | { |
kenjiArai | 1:9db0e321a9f4 | 92 | if (mem_trace_cb) { |
kenjiArai | 1:9db0e321a9f4 | 93 | if (TRACE_FIRST_LOCK()) { |
kenjiArai | 1:9db0e321a9f4 | 94 | mem_trace_cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size); |
kenjiArai | 1:9db0e321a9f4 | 95 | } |
kenjiArai | 1:9db0e321a9f4 | 96 | } |
kenjiArai | 1:9db0e321a9f4 | 97 | return res; |
kenjiArai | 1:9db0e321a9f4 | 98 | } |
kenjiArai | 1:9db0e321a9f4 | 99 | |
kenjiArai | 1:9db0e321a9f4 | 100 | void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller) |
kenjiArai | 1:9db0e321a9f4 | 101 | { |
kenjiArai | 1:9db0e321a9f4 | 102 | if (mem_trace_cb) { |
kenjiArai | 1:9db0e321a9f4 | 103 | if (TRACE_FIRST_LOCK()) { |
kenjiArai | 1:9db0e321a9f4 | 104 | mem_trace_cb(MBED_MEM_TRACE_CALLOC, res, caller, num, size); |
kenjiArai | 1:9db0e321a9f4 | 105 | } |
kenjiArai | 1:9db0e321a9f4 | 106 | } |
kenjiArai | 1:9db0e321a9f4 | 107 | return res; |
kenjiArai | 1:9db0e321a9f4 | 108 | } |
kenjiArai | 1:9db0e321a9f4 | 109 | |
kenjiArai | 1:9db0e321a9f4 | 110 | void mbed_mem_trace_free(void *ptr, void *caller) |
kenjiArai | 1:9db0e321a9f4 | 111 | { |
kenjiArai | 1:9db0e321a9f4 | 112 | if (mem_trace_cb) { |
kenjiArai | 1:9db0e321a9f4 | 113 | if (TRACE_FIRST_LOCK()) { |
kenjiArai | 1:9db0e321a9f4 | 114 | mem_trace_cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr); |
kenjiArai | 1:9db0e321a9f4 | 115 | } |
kenjiArai | 1:9db0e321a9f4 | 116 | } |
kenjiArai | 1:9db0e321a9f4 | 117 | } |
kenjiArai | 1:9db0e321a9f4 | 118 | |
kenjiArai | 1:9db0e321a9f4 | 119 | void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...) |
kenjiArai | 1:9db0e321a9f4 | 120 | { |
kenjiArai | 1:9db0e321a9f4 | 121 | va_list va; |
kenjiArai | 1:9db0e321a9f4 | 122 | size_t temp_s1, temp_s2; |
kenjiArai | 1:9db0e321a9f4 | 123 | void *temp_ptr; |
kenjiArai | 1:9db0e321a9f4 | 124 | |
kenjiArai | 1:9db0e321a9f4 | 125 | va_start(va, caller); |
kenjiArai | 1:9db0e321a9f4 | 126 | switch (op) { |
kenjiArai | 1:9db0e321a9f4 | 127 | case MBED_MEM_TRACE_MALLOC: |
kenjiArai | 1:9db0e321a9f4 | 128 | temp_s1 = va_arg(va, size_t); |
kenjiArai | 1:9db0e321a9f4 | 129 | printf(MBED_MEM_DEFAULT_TRACER_PREFIX "m:%p;%p-%u\n", res, caller, temp_s1); |
kenjiArai | 1:9db0e321a9f4 | 130 | break; |
kenjiArai | 1:9db0e321a9f4 | 131 | |
kenjiArai | 1:9db0e321a9f4 | 132 | case MBED_MEM_TRACE_REALLOC: |
kenjiArai | 1:9db0e321a9f4 | 133 | temp_ptr = va_arg(va, void *); |
kenjiArai | 1:9db0e321a9f4 | 134 | temp_s1 = va_arg(va, size_t); |
kenjiArai | 1:9db0e321a9f4 | 135 | printf(MBED_MEM_DEFAULT_TRACER_PREFIX "r:%p;%p-%p;%u\n", res, caller, temp_ptr, temp_s1); |
kenjiArai | 1:9db0e321a9f4 | 136 | break; |
kenjiArai | 1:9db0e321a9f4 | 137 | |
kenjiArai | 1:9db0e321a9f4 | 138 | case MBED_MEM_TRACE_CALLOC: |
kenjiArai | 1:9db0e321a9f4 | 139 | temp_s1 = va_arg(va, size_t); |
kenjiArai | 1:9db0e321a9f4 | 140 | temp_s2 = va_arg(va, size_t); |
kenjiArai | 1:9db0e321a9f4 | 141 | printf(MBED_MEM_DEFAULT_TRACER_PREFIX "c:%p;%p-%u;%u\n", res, caller, temp_s1, temp_s2); |
kenjiArai | 1:9db0e321a9f4 | 142 | break; |
kenjiArai | 1:9db0e321a9f4 | 143 | |
kenjiArai | 1:9db0e321a9f4 | 144 | case MBED_MEM_TRACE_FREE: |
kenjiArai | 1:9db0e321a9f4 | 145 | temp_ptr = va_arg(va, void *); |
kenjiArai | 1:9db0e321a9f4 | 146 | printf(MBED_MEM_DEFAULT_TRACER_PREFIX "f:%p;%p-%p\n", res, caller, temp_ptr); |
kenjiArai | 1:9db0e321a9f4 | 147 | break; |
kenjiArai | 1:9db0e321a9f4 | 148 | |
kenjiArai | 1:9db0e321a9f4 | 149 | default: |
kenjiArai | 1:9db0e321a9f4 | 150 | printf("?\n"); |
kenjiArai | 1:9db0e321a9f4 | 151 | } |
kenjiArai | 1:9db0e321a9f4 | 152 | va_end(va); |
kenjiArai | 1:9db0e321a9f4 | 153 | } |