mirror of https://github.com/ARMmbed/mbed-os.git
Merge pull request #2487 from ARMmbed/memory_instrumentation
Runtime dynamic memory tracingpull/2443/merge
commit
a3a9daf24a
|
@ -0,0 +1,289 @@
|
|||
/*
|
||||
* Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "mbed.h"
|
||||
#include "greentea-client/test_env.h"
|
||||
#include "unity/unity.h"
|
||||
#include "utest/utest.h"
|
||||
#include "mbed_mem_trace.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifndef MBED_MEM_TRACING_ENABLED
|
||||
#error [NOT_SUPPORTED] test not supported
|
||||
#endif
|
||||
|
||||
using namespace utest::v1;
|
||||
|
||||
/******************************************************************************/
|
||||
/* Helper functions and data structures */
|
||||
/******************************************************************************/
|
||||
|
||||
// This structure keeps data about the various memory allocation operations,
|
||||
// as traced by 'test_trace_cb' below.
|
||||
#define TEST_MAX_MEMORY_OPS 10
|
||||
// Trace results for all possible operations
|
||||
typedef struct {
|
||||
uint8_t op;
|
||||
void *res;
|
||||
union {
|
||||
struct {
|
||||
size_t arg_size;
|
||||
} malloc_info;
|
||||
struct {
|
||||
void *arg_ptr;
|
||||
size_t arg_size;
|
||||
} realloc_info;
|
||||
struct {
|
||||
size_t arg_nmemb;
|
||||
size_t arg_size;
|
||||
} calloc_info;
|
||||
struct {
|
||||
void *arg_ptr;
|
||||
} free_info;
|
||||
};
|
||||
} mem_trace_data_t;
|
||||
// Memory operation statistics
|
||||
typedef struct {
|
||||
mem_trace_data_t op_data[TEST_MAX_MEMORY_OPS];
|
||||
uint32_t total_ops;
|
||||
bool invalid_op, overflow;
|
||||
} stats_t;
|
||||
static stats_t stats;
|
||||
|
||||
// Clear all the memory statistics
|
||||
static void test_clear_stats() {
|
||||
memset(&stats, 0, sizeof(stats));
|
||||
}
|
||||
|
||||
// Memory tracer callback that records each operation in "stats" (above)
|
||||
extern "C" void test_trace_cb(uint8_t op, void *res, void *caller, ...) {
|
||||
va_list va;
|
||||
mem_trace_data_t *pmem = stats.op_data + stats.total_ops;
|
||||
|
||||
if (stats.total_ops >= TEST_MAX_MEMORY_OPS) {
|
||||
stats.overflow = true;
|
||||
return;
|
||||
}
|
||||
va_start(va, caller);
|
||||
pmem->op = op;
|
||||
pmem->res = res;
|
||||
switch(op) {
|
||||
case MBED_MEM_TRACE_MALLOC:
|
||||
pmem->malloc_info.arg_size = va_arg(va, size_t);
|
||||
break;
|
||||
|
||||
case MBED_MEM_TRACE_REALLOC:
|
||||
pmem->realloc_info.arg_ptr = va_arg(va, void *);
|
||||
pmem->realloc_info.arg_size = va_arg(va, size_t);
|
||||
break;
|
||||
|
||||
case MBED_MEM_TRACE_CALLOC:
|
||||
pmem->calloc_info.arg_nmemb = va_arg(va, size_t);
|
||||
pmem->calloc_info.arg_size = va_arg(va, size_t);
|
||||
break;
|
||||
|
||||
case MBED_MEM_TRACE_FREE:
|
||||
pmem->free_info.arg_ptr = va_arg(va, void *);
|
||||
break;
|
||||
|
||||
default:
|
||||
stats.invalid_op = true;
|
||||
}
|
||||
stats.total_ops ++;
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
// Generic sanity checks for the tracer
|
||||
static void check_sanity(uint32_t expected_ops) {
|
||||
TEST_ASSERT_FALSE(stats.overflow);
|
||||
TEST_ASSERT_FALSE(stats.invalid_op);
|
||||
TEST_ASSERT_EQUAL_UINT32(stats.total_ops, expected_ops);
|
||||
}
|
||||
|
||||
// Check a "malloc" operation
|
||||
static void check_malloc_op(const mem_trace_data_t *p, void *expected_res, size_t expected_arg_size) {
|
||||
TEST_ASSERT_EQUAL_UINT8(p->op, MBED_MEM_TRACE_MALLOC);
|
||||
TEST_ASSERT_EQUAL_PTR(p->res, expected_res);
|
||||
TEST_ASSERT_EQUAL_UINT32(p->malloc_info.arg_size, expected_arg_size);
|
||||
}
|
||||
|
||||
// Check a "free" operation
|
||||
static void check_free_op(const mem_trace_data_t *p, void *expected_arg_ptr) {
|
||||
TEST_ASSERT_EQUAL_UINT8(p->op, MBED_MEM_TRACE_FREE);
|
||||
TEST_ASSERT_EQUAL_PTR(p->free_info.arg_ptr, expected_arg_ptr);
|
||||
}
|
||||
|
||||
// Check a "realloc" operation
|
||||
static void check_realloc_op(const mem_trace_data_t *p, void *expected_res, void *expected_arg_ptr, size_t expected_arg_size) {
|
||||
TEST_ASSERT_EQUAL_UINT8(p->op, MBED_MEM_TRACE_REALLOC);
|
||||
TEST_ASSERT_EQUAL_PTR(p->res, expected_res);
|
||||
TEST_ASSERT_EQUAL_UINT32(p->realloc_info.arg_ptr, expected_arg_ptr);
|
||||
TEST_ASSERT_EQUAL_UINT32(p->realloc_info.arg_size, expected_arg_size);
|
||||
}
|
||||
|
||||
// Check a "calloc" operation
|
||||
static void check_calloc_op(const mem_trace_data_t *p, void *expected_res, size_t expected_arg_nmemb, size_t expected_arg_size) {
|
||||
TEST_ASSERT_EQUAL_UINT8(p->op, MBED_MEM_TRACE_CALLOC);
|
||||
TEST_ASSERT_EQUAL_PTR(p->res, expected_res);
|
||||
TEST_ASSERT_EQUAL_UINT32(p->calloc_info.arg_nmemb, expected_arg_nmemb);
|
||||
TEST_ASSERT_EQUAL_UINT32(p->calloc_info.arg_size, expected_arg_size);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* Tests */
|
||||
/******************************************************************************/
|
||||
|
||||
// Allocate a single buffer, then free it. Check that tracing matches the operations.
|
||||
static void test_case_single_malloc_free() {
|
||||
const size_t block_size = 126;
|
||||
const mem_trace_data_t *pmem = stats.op_data;
|
||||
|
||||
test_clear_stats();
|
||||
mbed_mem_trace_set_callback(test_trace_cb);
|
||||
// Allocate a single memory block
|
||||
void *p = malloc(block_size);
|
||||
TEST_ASSERT_NOT_EQUAL(p, NULL);
|
||||
// Free the memory block
|
||||
free(p);
|
||||
// Stop tracing
|
||||
mbed_mem_trace_set_callback(NULL);
|
||||
// Check tracer result
|
||||
check_sanity(2);
|
||||
check_malloc_op(pmem ++, p, block_size);
|
||||
check_free_op(pmem, p);
|
||||
}
|
||||
|
||||
// Test all memory operations (malloc, realloc, free, calloc)
|
||||
static void test_case_all_memory_ops() {
|
||||
const size_t malloc_size = 40, realloc_size = 80, nmemb = 25, size = 10;
|
||||
const mem_trace_data_t *pmem = stats.op_data;
|
||||
|
||||
test_clear_stats();
|
||||
mbed_mem_trace_set_callback(test_trace_cb);
|
||||
// Allocate a single memory block, the realloc it
|
||||
void *p_malloc = malloc(malloc_size);
|
||||
TEST_ASSERT_NOT_EQUAL(p_malloc, NULL);
|
||||
void *p_realloc = realloc(p_malloc, realloc_size);
|
||||
TEST_ASSERT_NOT_EQUAL(p_realloc, NULL);
|
||||
// Use calloc() now
|
||||
void *p_calloc = calloc(nmemb, size);
|
||||
//TEST_ASSERT_NOT_EQUAL(p_calloc, NULL);
|
||||
// Free the realloc() pointer first, then the calloc() one
|
||||
free(p_realloc);
|
||||
free(p_calloc);
|
||||
// Stop tracing
|
||||
mbed_mem_trace_set_callback(NULL);
|
||||
// Check tracer result
|
||||
check_sanity(6);
|
||||
check_malloc_op(pmem ++, p_malloc, malloc_size);
|
||||
check_realloc_op(pmem ++, p_realloc, p_malloc, realloc_size);
|
||||
// calloc() calls malloc() internally
|
||||
check_malloc_op(pmem ++, p_calloc, nmemb * size);
|
||||
check_calloc_op(pmem ++, p_calloc, nmemb, size);
|
||||
check_free_op(pmem ++, p_realloc);
|
||||
check_free_op(pmem, p_calloc);
|
||||
}
|
||||
|
||||
// Test that tracing is off when using a NULL callback
|
||||
static void test_case_trace_off() {
|
||||
const size_t malloc_size = 10;
|
||||
|
||||
test_clear_stats();
|
||||
// We don't want any tracing
|
||||
mbed_mem_trace_set_callback(NULL);
|
||||
// Allocate a buffer and free it
|
||||
void *p_malloc = malloc(malloc_size);
|
||||
TEST_ASSERT_NOT_EQUAL(p_malloc, NULL);
|
||||
free(p_malloc);
|
||||
// Check that we didn't trace anything
|
||||
check_sanity(0);
|
||||
}
|
||||
|
||||
// Test partial tracing (start tracing, stop tracing, restart later)
|
||||
static void test_case_partial_trace() {
|
||||
const size_t malloc_size_1 = 20, malloc_size_2 = 30;
|
||||
const mem_trace_data_t *pmem = stats.op_data;
|
||||
|
||||
test_clear_stats();
|
||||
// Start tracing
|
||||
mbed_mem_trace_set_callback(test_trace_cb);
|
||||
// Allocate a buffer
|
||||
void *p_malloc_1 = malloc(malloc_size_1);
|
||||
TEST_ASSERT_NOT_EQUAL(p_malloc_1, NULL);
|
||||
// Disable tracing before freeing the first buffer
|
||||
mbed_mem_trace_set_callback(NULL);
|
||||
free(p_malloc_1);
|
||||
// Allocate another buffer (still not traced)
|
||||
void *p_malloc_2 = malloc(malloc_size_2);
|
||||
TEST_ASSERT_NOT_EQUAL(p_malloc_2, NULL);
|
||||
// Re-enable tracing
|
||||
mbed_mem_trace_set_callback(test_trace_cb);
|
||||
// And free the second buffer (this operation should be tracer)
|
||||
free(p_malloc_2);
|
||||
// Stop tracing
|
||||
mbed_mem_trace_set_callback(NULL);
|
||||
// Check tracer result
|
||||
check_sanity(2);
|
||||
check_malloc_op(pmem ++, p_malloc_1, malloc_size_1);
|
||||
check_free_op(pmem, p_malloc_2);
|
||||
}
|
||||
|
||||
// Test new/delete tracing
|
||||
static void test_case_new_delete() {
|
||||
const mem_trace_data_t *pmem = stats.op_data;
|
||||
|
||||
test_clear_stats();
|
||||
// Start tracing
|
||||
mbed_mem_trace_set_callback(test_trace_cb);
|
||||
// Test new, new[], delete and delete[]
|
||||
int *p_int = new int;
|
||||
int *p_int_array = new int[10];
|
||||
delete p_int;
|
||||
delete[] p_int_array;
|
||||
// Stop tracing
|
||||
mbed_mem_trace_set_callback(NULL);
|
||||
// Check tracer result
|
||||
check_sanity(4);
|
||||
check_malloc_op(pmem ++, p_int, sizeof(int));
|
||||
check_malloc_op(pmem ++, p_int_array, 10 * sizeof(int));
|
||||
check_free_op(pmem ++, p_int);
|
||||
check_free_op(pmem ++, p_int_array);
|
||||
}
|
||||
|
||||
static Case cases[] = {
|
||||
Case("single malloc/free", test_case_single_malloc_free),
|
||||
Case("all memory operations", test_case_all_memory_ops),
|
||||
Case("trace off", test_case_trace_off),
|
||||
Case("partial trace", test_case_partial_trace),
|
||||
Case("test new/delete", test_case_new_delete)
|
||||
};
|
||||
|
||||
static status_t greentea_test_setup(const size_t number_of_cases) {
|
||||
GREENTEA_SETUP(20, "default_auto");
|
||||
return greentea_test_setup_handler(number_of_cases);
|
||||
}
|
||||
|
||||
static Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
|
||||
|
||||
int main() {
|
||||
// Disable stdout buffering to prevent any unwanted allocations
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
Harness::run(specification);
|
||||
}
|
||||
|
|
@ -0,0 +1,138 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2016 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __MBED_MEM_TRACE_H__
|
||||
#define __MBED_MEM_TRACE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* Operation types for tracer */
|
||||
enum {
|
||||
MBED_MEM_TRACE_MALLOC,
|
||||
MBED_MEM_TRACE_REALLOC,
|
||||
MBED_MEM_TRACE_CALLOC,
|
||||
MBED_MEM_TRACE_FREE
|
||||
};
|
||||
|
||||
/* Prefix for the output of the default tracer */
|
||||
#define MBED_MEM_DEFAULT_TRACER_PREFIX "#"
|
||||
|
||||
/**
|
||||
* Type of the callback used by the memory tracer. This callback is called when a memory
|
||||
* allocation operation (malloc, realloc, calloc, free) is called and tracing is enabled
|
||||
* for that memory allocation function.
|
||||
*
|
||||
* @param op the ID of the operation (MBED_MEM_TRACE_MALLOC, MBED_MEM_TRACE_REALLOC,
|
||||
* MBED_MEM_TRACE_CALLOC or MBED_MEM_TRACE_FREE).
|
||||
* @param res the result that the memory operation returned (NULL for 'free').
|
||||
* @param caller the caller of the memory operation. Note that the value of 'caller' might be
|
||||
* unreliable.
|
||||
*
|
||||
* The rest of the parameters passed 'mbed_mem_trace_cb_t' are the same as the memory operations
|
||||
* that triggered its call (see 'man malloc' for details):
|
||||
*
|
||||
* - for malloc: cb(MBED_MEM_TRACE_MALLOC, res, caller, size).
|
||||
* - for realloc: cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size).
|
||||
* - for calloc: cb(MBED_MEM_TRACE_CALLOC, res, caller, nmemb, size).
|
||||
* - for free: cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr).
|
||||
*/
|
||||
typedef void (*mbed_mem_trace_cb_t)(uint8_t op, void *res, void* caller, ...);
|
||||
|
||||
/**
|
||||
* Set the callback used by the memory tracer (use NULL for disable tracing).
|
||||
*
|
||||
* @param cb the callback to call on each memory operation.
|
||||
*/
|
||||
void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb);
|
||||
|
||||
/**
|
||||
* Trace a call to 'malloc'.
|
||||
* @param res the result of running 'malloc'.
|
||||
* @param size the 'size' argument given to 'malloc'.
|
||||
* @param caller the caller of the memory operation.
|
||||
* @return 'res' (the first argument).
|
||||
*/
|
||||
void *mbed_mem_trace_malloc(void *res, size_t size, void *caller);
|
||||
|
||||
/**
|
||||
* Trace a call to 'realloc'.
|
||||
* @param res the result of running 'realloc'.
|
||||
* @param ptr the 'ptr' argument given to 'realloc'.
|
||||
* @param size the 'size' argument given to 'realloc'.
|
||||
*
|
||||
* @return 'res' (the first argument).
|
||||
*/
|
||||
void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller);
|
||||
|
||||
/**
|
||||
* Trace a call to 'calloc'.
|
||||
* @param res the result of running 'calloc'.
|
||||
* @param nmemb the 'nmemb' argument given to 'calloc'.
|
||||
* @param size the 'size' argument given to 'calloc'.
|
||||
* @param caller the caller of the memory operation.
|
||||
* @Return 'res' (the first argument).
|
||||
*/
|
||||
void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller);
|
||||
|
||||
/**
|
||||
* Trace a call to 'free'.
|
||||
* @param ptr the 'ptr' argument given to 'free'.
|
||||
* @param caller the caller of the memory operation.
|
||||
*/
|
||||
void mbed_mem_trace_free(void *ptr, void *caller);
|
||||
|
||||
/**
|
||||
* Default memory trace callback. DO NOT CALL DIRECTLY. It is meant to be used
|
||||
* as the second argument of 'mbed_mem_trace_setup'.
|
||||
*
|
||||
* The default callback outputs trace data using 'printf', in a format that's
|
||||
* easily parsable by an external tool. For each memory operation, the callback
|
||||
* outputs a line that begins with '#<op>:<0xresult>;<0xcaller>-':
|
||||
*
|
||||
* - 'op' identifies the memory operation ('m' for 'malloc', 'r' for 'realloc',
|
||||
* 'c' for 'calloc' and 'f' for 'free').
|
||||
* - 'result' (base 16) is the result of the memor operation. This is always NULL
|
||||
* for 'free', since 'free' doesn't return anything.
|
||||
* -'caller' (base 16) is the caller of the memory operation. Note that the value
|
||||
* of 'caller' might be unreliable.
|
||||
*
|
||||
* The rest of the output depends on the operation being traced:
|
||||
*
|
||||
* - for 'malloc': 'size', where 'size' is the original argument to 'malloc'.
|
||||
* - for 'realloc': '0xptr;size', where 'ptr' (base 16) and 'size' are the original arguments to 'realloc'.
|
||||
* - for 'calloc': 'nmemb;size', where 'nmemb' and 'size' are the original arguments to 'calloc'.
|
||||
* - for 'free': '0xptr', where 'ptr' (base 16) is the original argument to 'free'.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* - '#m:0x20003240;0x600d-50' encodes a 'malloc' that returned 0x20003240, was called
|
||||
* by the instruction at 0x600D with a the 'size' argument equal to 50.
|
||||
* - '#f:0x0;0x602f-0x20003240' encodes a 'free' that was called by the instruction at
|
||||
* 0x602f with the 'ptr' argument equal to 0x20003240.
|
||||
*/
|
||||
void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif// #ifndef __MBED_MEM_TRACE_H__
|
||||
|
|
@ -240,6 +240,29 @@
|
|||
*/
|
||||
#define MBED_DEPRECATED_SINCE(D, M) MBED_DEPRECATED(M " [since " D "]")
|
||||
|
||||
/** MBED_CALLER_ADDR()
|
||||
* Returns the caller of the current function.
|
||||
*
|
||||
* @note
|
||||
* This macro is only implemented for GCC and ARMCC.
|
||||
*
|
||||
* @code
|
||||
* #include "toolchain.h"
|
||||
*
|
||||
* printf("This function was called from %p", MBED_CALLER_ADDR());
|
||||
* @endcode
|
||||
*
|
||||
* @return Address of the calling function
|
||||
*/
|
||||
#ifndef MBED_CALLER_ADDR
|
||||
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__CC_ARM)
|
||||
#define MBED_CALLER_ADDR() __builtin_extract_return_addr(__builtin_return_address(0))
|
||||
#elif defined(__CC_ARM)
|
||||
#define MBED_CALLER_ADDR() __builtin_return_address(0)
|
||||
#else
|
||||
#define MBED_CALLER_ADDR() (NULL)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// FILEHANDLE declaration
|
||||
#if defined(TOOLCHAIN_ARM)
|
||||
|
|
|
@ -0,0 +1,329 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2016 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "mbed_mem_trace.h"
|
||||
#include "mbed_stats.h"
|
||||
#include "toolchain.h"
|
||||
#include "SingletonPtr.h"
|
||||
#include "PlatformMutex.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* There are two memory tracers in mbed OS:
|
||||
|
||||
- the first can be used to detect the maximum heap usage at runtime. It is
|
||||
activated by defining the MBED_HEAP_STATS_ENABLED macro.
|
||||
- the second can be used to trace each memory call by automatically invoking
|
||||
a callback on each memory operation (see hal/api/mbed_mem_trace.h). It is
|
||||
activated by defining the MBED_MEM_TRACING_ENABLED macro.
|
||||
|
||||
Both tracers can be activated and deactivated in any combination. If both tracers
|
||||
are active, the second one (MBED_MEM_TRACING_ENABLED) will trace the first one's
|
||||
(MBED_HEAP_STATS_ENABLED) memory calls.*/
|
||||
|
||||
/******************************************************************************/
|
||||
/* Implementation of the runtime max heap usage checker */
|
||||
/******************************************************************************/
|
||||
|
||||
/* Size must be a multiple of 8 to keep alignment */
|
||||
typedef struct {
|
||||
uint32_t size;
|
||||
uint32_t pad;
|
||||
} alloc_info_t;
|
||||
|
||||
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
|
||||
static SingletonPtr<PlatformMutex> mem_trace_mutex;
|
||||
static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0};
|
||||
|
||||
void mbed_stats_heap_get(mbed_stats_heap_t *stats)
|
||||
{
|
||||
malloc_stats_mutex->lock();
|
||||
memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t));
|
||||
malloc_stats_mutex->unlock();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* GCC memory allocation wrappers */
|
||||
/******************************************************************************/
|
||||
|
||||
#if defined(TOOLCHAIN_GCC)
|
||||
|
||||
#ifdef FEATURE_UVISOR
|
||||
#include "uvisor-lib/uvisor-lib.h"
|
||||
#endif/* FEATURE_UVISOR */
|
||||
|
||||
// TODO: memory tracing doesn't work with uVisor enabled.
|
||||
#if !defined(FEATURE_UVISOR)
|
||||
|
||||
extern "C" {
|
||||
void * __real__malloc_r(struct _reent * r, size_t size);
|
||||
void * __real__realloc_r(struct _reent * r, void * ptr, size_t size);
|
||||
void __real__free_r(struct _reent * r, void * ptr);
|
||||
void* __real__calloc_r(struct _reent * r, size_t nmemb, size_t size);
|
||||
}
|
||||
|
||||
extern "C" void * __wrap__malloc_r(struct _reent * r, size_t size) {
|
||||
void *ptr = NULL;
|
||||
#ifdef MBED_HEAP_STATS_ENABLED
|
||||
malloc_stats_mutex->lock();
|
||||
alloc_info_t *alloc_info = (alloc_info_t*)__real__malloc_r(r, size + sizeof(alloc_info_t));
|
||||
if (alloc_info != NULL) {
|
||||
alloc_info->size = size;
|
||||
ptr = (void*)(alloc_info + 1);
|
||||
heap_stats.current_size += size;
|
||||
heap_stats.total_size += size;
|
||||
heap_stats.alloc_cnt += 1;
|
||||
if (heap_stats.current_size > heap_stats.max_size) {
|
||||
heap_stats.max_size = heap_stats.current_size;
|
||||
}
|
||||
} else {
|
||||
heap_stats.alloc_fail_cnt += 1;
|
||||
}
|
||||
malloc_stats_mutex->unlock();
|
||||
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
ptr = __real__malloc_r(r, size);
|
||||
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
#ifdef MBED_MEM_TRACING_ENABLED
|
||||
mem_trace_mutex->lock();
|
||||
mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR());
|
||||
mem_trace_mutex->unlock();
|
||||
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
||||
return ptr;
|
||||
}
|
||||
|
||||
extern "C" void * __wrap__realloc_r(struct _reent * r, void * ptr, size_t size) {
|
||||
void *new_ptr = NULL;
|
||||
#ifdef MBED_HEAP_STATS_ENABLED
|
||||
// Implement realloc_r with malloc and free.
|
||||
// The function realloc_r can't be used here directly since
|
||||
// it can call into __wrap__malloc_r (returns ptr + 4) or
|
||||
// resize memory directly (returns ptr + 0).
|
||||
|
||||
// Note - no lock needed since malloc and free are thread safe
|
||||
|
||||
// Get old size
|
||||
uint32_t old_size = 0;
|
||||
if (ptr != NULL) {
|
||||
alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1;
|
||||
old_size = alloc_info->size;
|
||||
}
|
||||
|
||||
// Allocate space
|
||||
if (size != 0) {
|
||||
new_ptr = malloc(size);
|
||||
}
|
||||
|
||||
// If the new buffer has been allocated copy the data to it
|
||||
// and free the old buffer
|
||||
if (new_ptr != NULL) {
|
||||
uint32_t copy_size = (old_size < size) ? old_size : size;
|
||||
memcpy(new_ptr, (void*)ptr, copy_size);
|
||||
free(ptr);
|
||||
}
|
||||
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
new_ptr = __real__realloc_r(r, ptr, size);
|
||||
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
#ifdef MBED_MEM_TRACING_ENABLED
|
||||
mem_trace_mutex->lock();
|
||||
mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
|
||||
mem_trace_mutex->unlock();
|
||||
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
extern "C" void __wrap__free_r(struct _reent * r, void * ptr) {
|
||||
#ifdef MBED_HEAP_STATS_ENABLED
|
||||
malloc_stats_mutex->lock();
|
||||
alloc_info_t *alloc_info = NULL;
|
||||
if (ptr != NULL) {
|
||||
alloc_info = ((alloc_info_t*)ptr) - 1;
|
||||
heap_stats.current_size -= alloc_info->size;
|
||||
heap_stats.alloc_cnt -= 1;
|
||||
}
|
||||
__real__free_r(r, (void*)alloc_info);
|
||||
malloc_stats_mutex->unlock();
|
||||
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
__real__free_r(r, ptr);
|
||||
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
#ifdef MBED_MEM_TRACING_ENABLED
|
||||
mem_trace_mutex->lock();
|
||||
mbed_mem_trace_free(ptr, MBED_CALLER_ADDR());
|
||||
mem_trace_mutex->unlock();
|
||||
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
||||
}
|
||||
|
||||
extern "C" void * __wrap__calloc_r(struct _reent * r, size_t nmemb, size_t size) {
|
||||
void *ptr = NULL;
|
||||
#ifdef MBED_HEAP_STATS_ENABLED
|
||||
// Note - no lock needed since malloc is thread safe
|
||||
|
||||
ptr = malloc(nmemb * size);
|
||||
if (ptr != NULL) {
|
||||
memset(ptr, 0, nmemb * size);
|
||||
}
|
||||
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
ptr = __real__calloc_r(r, nmemb, size);
|
||||
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
#ifdef MBED_MEM_TRACING_ENABLED
|
||||
mem_trace_mutex->lock();
|
||||
mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
|
||||
mem_trace_mutex->unlock();
|
||||
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
||||
return ptr;
|
||||
}
|
||||
|
||||
#endif // if !defined(FEATURE_UVISOR)
|
||||
|
||||
/******************************************************************************/
|
||||
/* ARMCC memory allocation wrappers */
|
||||
/******************************************************************************/
|
||||
|
||||
#elif defined(TOOLCHAIN_ARM) // #if defined(TOOLCHAIN_GCC)
|
||||
|
||||
/* Enable hooking of memory function only if tracing is also enabled */
|
||||
#if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
|
||||
|
||||
extern "C" {
|
||||
void *$Super$$malloc(size_t size);
|
||||
void *$Super$$realloc(void *ptr, size_t size);
|
||||
void *$Super$$calloc(size_t nmemb, size_t size);
|
||||
void $Super$$free(void *ptr);
|
||||
}
|
||||
|
||||
extern "C" void* $Sub$$malloc(size_t size) {
|
||||
void *ptr = NULL;
|
||||
#ifdef MBED_HEAP_STATS_ENABLED
|
||||
malloc_stats_mutex->lock();
|
||||
alloc_info_t *alloc_info = (alloc_info_t*)$Super$$malloc(size + sizeof(alloc_info_t));
|
||||
if (alloc_info != NULL) {
|
||||
alloc_info->size = size;
|
||||
ptr = (void*)(alloc_info + 1);
|
||||
heap_stats.current_size += size;
|
||||
heap_stats.total_size += size;
|
||||
heap_stats.alloc_cnt += 1;
|
||||
if (heap_stats.current_size > heap_stats.max_size) {
|
||||
heap_stats.max_size = heap_stats.current_size;
|
||||
}
|
||||
} else {
|
||||
heap_stats.alloc_fail_cnt += 1;
|
||||
}
|
||||
malloc_stats_mutex->unlock();
|
||||
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
ptr = $Super$$malloc(size);
|
||||
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
#ifdef MBED_MEM_TRACING_ENABLED
|
||||
mem_trace_mutex->lock();
|
||||
mbed_mem_trace_malloc(ptr, size, MBED_CALLER_ADDR());
|
||||
mem_trace_mutex->unlock();
|
||||
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
||||
return ptr;
|
||||
}
|
||||
|
||||
extern "C" void* $Sub$$realloc(void *ptr, size_t size) {
|
||||
void *new_ptr = NULL;
|
||||
#ifdef MBED_HEAP_STATS_ENABLED
|
||||
// Note - no lock needed since malloc and free are thread safe
|
||||
|
||||
// Get old size
|
||||
uint32_t old_size = 0;
|
||||
if (ptr != NULL) {
|
||||
alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1;
|
||||
old_size = alloc_info->size;
|
||||
}
|
||||
|
||||
// Allocate space
|
||||
if (size != 0) {
|
||||
new_ptr = malloc(size);
|
||||
}
|
||||
|
||||
// If the new buffer has been allocated copy the data to it
|
||||
// and free the old buffer
|
||||
if (new_ptr != NULL) {
|
||||
uint32_t copy_size = (old_size < size) ? old_size : size;
|
||||
memcpy(new_ptr, (void*)ptr, copy_size);
|
||||
free(ptr);
|
||||
}
|
||||
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
mem_trace_mutex->lock();
|
||||
new_ptr = $Super$$realloc(ptr, size);
|
||||
mem_trace_mutex->unlock();
|
||||
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
#ifdef MBED_MEM_TRACING_ENABLED
|
||||
mbed_mem_trace_realloc(new_ptr, ptr, size, MBED_CALLER_ADDR());
|
||||
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
extern "C" void *$Sub$$calloc(size_t nmemb, size_t size) {
|
||||
void *ptr = NULL;
|
||||
#ifdef MBED_HEAP_STATS_ENABLED
|
||||
// Note - no lock needed since malloc is thread safe
|
||||
ptr = malloc(nmemb * size);
|
||||
if (ptr != NULL) {
|
||||
memset(ptr, 0, nmemb * size);
|
||||
}
|
||||
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
ptr = $Super$$calloc(nmemb, size);
|
||||
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
#ifdef MBED_MEM_TRACING_ENABLED
|
||||
mem_trace_mutex->lock();
|
||||
mbed_mem_trace_calloc(ptr, nmemb, size, MBED_CALLER_ADDR());
|
||||
mem_trace_mutex->unlock();
|
||||
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
||||
return ptr;
|
||||
}
|
||||
|
||||
extern "C" void $Sub$$free(void *ptr) {
|
||||
#ifdef MBED_HEAP_STATS_ENABLED
|
||||
malloc_stats_mutex->lock();
|
||||
alloc_info_t *alloc_info = NULL;
|
||||
if (ptr != NULL) {
|
||||
alloc_info = ((alloc_info_t*)ptr) - 1;
|
||||
heap_stats.current_size -= alloc_info->size;
|
||||
heap_stats.alloc_cnt -= 1;
|
||||
}
|
||||
$Super$$free((void*)alloc_info);
|
||||
malloc_stats_mutex->unlock();
|
||||
#else // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
$Super$$free(ptr);
|
||||
#endif // #ifdef MBED_HEAP_STATS_ENABLED
|
||||
#ifdef MBED_MEM_TRACING_ENABLED
|
||||
mem_trace_mutex->lock();
|
||||
mbed_mem_trace_free(ptr, MBED_CALLER_ADDR());
|
||||
mem_trace_mutex->unlock();
|
||||
#endif // #ifdef MBED_MEM_TRACING_ENABLED
|
||||
}
|
||||
|
||||
#endif // #if defined(MBED_MEM_TRACING_ENABLED) || defined(MBED_HEAP_STATS_ENABLED)
|
||||
|
||||
/******************************************************************************/
|
||||
/* Allocation wrappers for other toolchains are not supported yet */
|
||||
/******************************************************************************/
|
||||
|
||||
#else // #if defined(TOOLCHAIN_GCC)
|
||||
|
||||
#ifdef MBED_MEM_TRACING_ENABLED
|
||||
#warning Memory tracing is not supported with the current toolchain.
|
||||
#endif
|
||||
|
||||
#ifdef MBED_HEAP_STATS_ENABLED
|
||||
#warning Heap statistics are not supported with the current toolchain.
|
||||
#endif
|
||||
|
||||
#endif // #if defined(TOOLCHAIN_GCC)
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2016 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include "mbed_mem_trace.h"
|
||||
#include "critical.h"
|
||||
|
||||
/******************************************************************************
|
||||
* Internal variables, functions and helpers
|
||||
*****************************************************************************/
|
||||
|
||||
/* The callback function that will be called after a traced memory operations finishes. */
|
||||
static mbed_mem_trace_cb_t mem_trace_cb;
|
||||
/* 'trave_level' guards "trace inside trace" situations (for example, the implementation
|
||||
* of realloc() might call malloc() internally, and since malloc() is also traced, this could
|
||||
* result in two calls to the callback function instead of one. */
|
||||
static uint8_t trace_level;
|
||||
|
||||
/******************************************************************************
|
||||
* Public interface
|
||||
*****************************************************************************/
|
||||
|
||||
void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb) {
|
||||
mem_trace_cb = cb;
|
||||
}
|
||||
|
||||
void *mbed_mem_trace_malloc(void *res, size_t size, void *caller) {
|
||||
if (mem_trace_cb) {
|
||||
if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
|
||||
mem_trace_cb(MBED_MEM_TRACE_MALLOC, res, caller, size);
|
||||
}
|
||||
core_util_atomic_decr_u8(&trace_level, 1);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void *mbed_mem_trace_realloc(void *res, void *ptr, size_t size, void *caller) {
|
||||
if (mem_trace_cb) {
|
||||
if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
|
||||
mem_trace_cb(MBED_MEM_TRACE_REALLOC, res, caller, ptr, size);
|
||||
}
|
||||
core_util_atomic_decr_u8(&trace_level, 1);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void *mbed_mem_trace_calloc(void *res, size_t num, size_t size, void *caller) {
|
||||
if (mem_trace_cb) {
|
||||
if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
|
||||
mem_trace_cb(MBED_MEM_TRACE_CALLOC, res, caller, num, size);
|
||||
}
|
||||
core_util_atomic_decr_u8(&trace_level, 1);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void mbed_mem_trace_free(void *ptr, void *caller) {
|
||||
if (mem_trace_cb) {
|
||||
if (core_util_atomic_incr_u8(&trace_level, 1) == 1) {
|
||||
mem_trace_cb(MBED_MEM_TRACE_FREE, NULL, caller, ptr);
|
||||
}
|
||||
core_util_atomic_decr_u8(&trace_level, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void mbed_mem_trace_default_callback(uint8_t op, void *res, void *caller, ...) {
|
||||
va_list va;
|
||||
size_t temp_s1, temp_s2;
|
||||
void *temp_ptr;
|
||||
|
||||
va_start(va, caller);
|
||||
switch(op) {
|
||||
case MBED_MEM_TRACE_MALLOC:
|
||||
temp_s1 = va_arg(va, size_t);
|
||||
printf(MBED_MEM_DEFAULT_TRACER_PREFIX "m:%p;%p-%u\n", res, caller, temp_s1);
|
||||
break;
|
||||
|
||||
case MBED_MEM_TRACE_REALLOC:
|
||||
temp_ptr = va_arg(va, void*);
|
||||
temp_s1 = va_arg(va, size_t);
|
||||
printf(MBED_MEM_DEFAULT_TRACER_PREFIX "r:%p;%p-%p;%u\n", res, caller, temp_ptr, temp_s1);
|
||||
break;
|
||||
|
||||
case MBED_MEM_TRACE_CALLOC:
|
||||
temp_s1 = va_arg(va, size_t);
|
||||
temp_s2 = va_arg(va, size_t);
|
||||
printf(MBED_MEM_DEFAULT_TRACER_PREFIX "c:%p;%p-%u;%u\n", res, caller, temp_s1, temp_s2);
|
||||
break;
|
||||
|
||||
case MBED_MEM_TRACE_FREE:
|
||||
temp_ptr = va_arg(va, void*);
|
||||
printf(MBED_MEM_DEFAULT_TRACER_PREFIX "f:%p;%p-%p\n", res, caller, temp_ptr);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("?\n");
|
||||
}
|
||||
va_end(va);
|
||||
}
|
||||
|
|
@ -479,135 +479,13 @@ extern "C" WEAK void __cxa_pure_virtual(void) {
|
|||
|
||||
#endif
|
||||
|
||||
/* Size must be a multiple of 8 to keep alignment */
|
||||
typedef struct {
|
||||
uint32_t size;
|
||||
uint32_t pad;
|
||||
} alloc_info_t;
|
||||
|
||||
static SingletonPtr<PlatformMutex> malloc_stats_mutex;
|
||||
static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0};
|
||||
|
||||
void mbed_stats_heap_get(mbed_stats_heap_t *stats)
|
||||
{
|
||||
malloc_stats_mutex->lock();
|
||||
memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t));
|
||||
malloc_stats_mutex->unlock();
|
||||
}
|
||||
|
||||
#if defined(TOOLCHAIN_GCC)
|
||||
#ifdef FEATURE_UVISOR
|
||||
#include "uvisor-lib/uvisor-lib.h"
|
||||
#endif/* FEATURE_UVISOR */
|
||||
|
||||
extern "C" {
|
||||
|
||||
/* uVisor wraps malloc_r, realloc_r and free_r, but not calloc_r! */
|
||||
#ifndef FEATURE_UVISOR
|
||||
|
||||
extern "C" void __malloc_lock( struct _reent *_r );
|
||||
extern "C" void __malloc_unlock( struct _reent *_r );
|
||||
|
||||
void * __wrap__malloc_r(struct _reent * r, size_t size) {
|
||||
extern void * __real__malloc_r(struct _reent * r, size_t size);
|
||||
#if !defined(MBED_HEAP_STATS_ENABLED ) || !MBED_HEAP_STATS_ENABLED
|
||||
return __real__malloc_r(r, size);
|
||||
#else
|
||||
|
||||
malloc_stats_mutex->lock();
|
||||
alloc_info_t *alloc_info = (alloc_info_t*)__real__malloc_r(r, size + sizeof(alloc_info_t));
|
||||
void *ptr = NULL;
|
||||
if (alloc_info != NULL) {
|
||||
alloc_info->size = size;
|
||||
ptr = (void*)(alloc_info + 1);
|
||||
heap_stats.current_size += size;
|
||||
heap_stats.total_size += size;
|
||||
heap_stats.alloc_cnt += 1;
|
||||
if (heap_stats.current_size > heap_stats.max_size) {
|
||||
heap_stats.max_size = heap_stats.current_size;
|
||||
}
|
||||
} else {
|
||||
heap_stats.alloc_fail_cnt += 1;
|
||||
}
|
||||
malloc_stats_mutex->unlock();
|
||||
|
||||
return ptr;
|
||||
#endif
|
||||
}
|
||||
void * __wrap__realloc_r(struct _reent * r, void * ptr, size_t size) {
|
||||
#if !defined(MBED_HEAP_STATS_ENABLED ) || !MBED_HEAP_STATS_ENABLED
|
||||
extern void * __real__realloc_r(struct _reent * r, void * ptr, size_t size);
|
||||
return __real__realloc_r(r, ptr, size);
|
||||
#else
|
||||
|
||||
// Implement realloc_r with malloc and free.
|
||||
// The function realloc_r can't be used here directly since
|
||||
// it can call into __wrap__malloc_r (returns ptr + 4) or
|
||||
// resize memory directly (returns ptr + 0).
|
||||
|
||||
// Note - no lock needed since malloc and free are thread safe
|
||||
|
||||
// Get old size
|
||||
uint32_t old_size = 0;
|
||||
if (ptr != NULL) {
|
||||
alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1;
|
||||
old_size = alloc_info->size;
|
||||
}
|
||||
|
||||
// Allocate space
|
||||
void *new_ptr = NULL;
|
||||
if (size != 0) {
|
||||
new_ptr = malloc(size);
|
||||
}
|
||||
|
||||
// If the new buffer has been allocated copy the data to it
|
||||
// and free the old buffer
|
||||
if (new_ptr != NULL) {
|
||||
uint32_t copy_size = (old_size < size) ? old_size : size;
|
||||
memcpy(new_ptr, (void*)ptr, copy_size);
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
return new_ptr;
|
||||
#endif
|
||||
}
|
||||
void __wrap__free_r(struct _reent * r, void * ptr) {
|
||||
extern void __real__free_r(struct _reent * r, void * ptr);
|
||||
#if !defined(MBED_HEAP_STATS_ENABLED ) || !MBED_HEAP_STATS_ENABLED
|
||||
__real__free_r(r, ptr);
|
||||
#else
|
||||
|
||||
malloc_stats_mutex->lock();
|
||||
alloc_info_t *alloc_info = NULL;
|
||||
if (ptr != NULL) {
|
||||
alloc_info = ((alloc_info_t*)ptr) - 1;
|
||||
heap_stats.current_size -= alloc_info->size;
|
||||
heap_stats.alloc_cnt -= 1;
|
||||
}
|
||||
__real__free_r(r, (void*)alloc_info);
|
||||
malloc_stats_mutex->unlock();
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif/* FEATURE_UVISOR */
|
||||
|
||||
void* __wrap__calloc_r(struct _reent * r, size_t num, size_t size) {
|
||||
#if !defined(MBED_HEAP_STATS_ENABLED ) || !MBED_HEAP_STATS_ENABLED
|
||||
extern void* __real__calloc_r(struct _reent * r, size_t num, size_t size);
|
||||
return __real__calloc_r(r, num, size);
|
||||
#else
|
||||
|
||||
// Note - no lock needed since malloc is thread safe
|
||||
|
||||
void *ptr = malloc(num * size);
|
||||
if (ptr != NULL) {
|
||||
memset(ptr, 0, num * size);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" WEAK void software_init_hook_rtos(void)
|
||||
{
|
||||
|
@ -629,85 +507,6 @@ extern "C" void software_init_hook(void)
|
|||
}
|
||||
#endif
|
||||
|
||||
#if defined(TOOLCHAIN_ARM) && (defined(MBED_HEAP_STATS_ENABLED ) && MBED_HEAP_STATS_ENABLED )
|
||||
|
||||
extern "C" void *$Super$$malloc(size_t size);
|
||||
extern "C" void *$Super$$realloc(void * ptr, size_t size);
|
||||
extern "C" void $Super$$free(void * ptr);
|
||||
|
||||
extern "C" void *$Sub$$malloc(size_t size)
|
||||
{
|
||||
malloc_stats_mutex->lock();
|
||||
alloc_info_t *alloc_info = (alloc_info_t*)$Super$$malloc(size + sizeof(alloc_info_t));
|
||||
void *ptr = NULL;
|
||||
if (alloc_info != NULL) {
|
||||
alloc_info->size = size;
|
||||
ptr = (void*)(alloc_info + 1);
|
||||
heap_stats.current_size += size;
|
||||
heap_stats.total_size += size;
|
||||
heap_stats.alloc_cnt += 1;
|
||||
if (heap_stats.current_size > heap_stats.max_size) {
|
||||
heap_stats.max_size = heap_stats.current_size;
|
||||
}
|
||||
} else {
|
||||
heap_stats.alloc_fail_cnt += 1;
|
||||
}
|
||||
malloc_stats_mutex->unlock();
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
extern "C" void *$Sub$$realloc(void * ptr, size_t size)
|
||||
{
|
||||
// Note - no lock needed since malloc and free are thread safe
|
||||
|
||||
// Get old size
|
||||
uint32_t old_size = 0;
|
||||
if (ptr != NULL) {
|
||||
alloc_info_t *alloc_info = ((alloc_info_t*)ptr) - 1;
|
||||
old_size = alloc_info->size;
|
||||
}
|
||||
|
||||
// Allocate space
|
||||
void *new_ptr = NULL;
|
||||
if (size != 0) {
|
||||
new_ptr = malloc(size);
|
||||
}
|
||||
|
||||
// If the new buffer has been allocated copy the data to it
|
||||
// and free the old buffer
|
||||
if (new_ptr != NULL) {
|
||||
uint32_t copy_size = (old_size < size) ? old_size : size;
|
||||
memcpy(new_ptr, (void*)ptr, copy_size);
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
return new_ptr;
|
||||
}
|
||||
extern "C" void $Sub$$free(void * ptr)
|
||||
{
|
||||
malloc_stats_mutex->lock();
|
||||
alloc_info_t *alloc_info = NULL;
|
||||
if (ptr != NULL) {
|
||||
alloc_info = ((alloc_info_t*)ptr) - 1;
|
||||
heap_stats.current_size -= alloc_info->size;
|
||||
heap_stats.alloc_cnt -= 1;
|
||||
}
|
||||
$Super$$free((void*)alloc_info);
|
||||
malloc_stats_mutex->unlock();
|
||||
}
|
||||
extern "C" void *$Sub$$calloc(size_t num, size_t size)
|
||||
{
|
||||
// Note - no lock needed since malloc is thread safe
|
||||
void *ptr = malloc(num * size);
|
||||
if (ptr != NULL) {
|
||||
memset(ptr, 0, num * size);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
#endif /* defined(TOOLCHAIN_ARM) && (defined(MBED_HEAP_STATS_ENABLED ) && MBED_HEAP_STATS_ENABLED ) */
|
||||
|
||||
// ****************************************************************************
|
||||
// mbed_main is a function that is called before main()
|
||||
// mbed_sdk_init() is also a function that is called before main(), but unlike
|
||||
|
|
Loading…
Reference in New Issue