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