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