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