Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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