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