Skip to content

Add enable/disable cb function in mem_trace #7948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions platform/mbed_mem_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

/* The callback function that will be called after a traced memory operations finishes. */
static mbed_mem_trace_cb_t mem_trace_cb;
static mbed_mem_trace_cb_t mem_trace_cb_reserve;
/* 'trace_lock_count' 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. */
Expand All @@ -46,6 +47,24 @@ void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb)
mem_trace_cb = cb;
}

void mbed_mem_trace_disable()
{
mbed_mem_trace_lock();
if (mem_trace_cb) {
mem_trace_cb_reserve = mem_trace_cb;
mem_trace_cb = 0;
}
mbed_mem_trace_unlock();
}
void mbed_mem_trace_enable()
{
mbed_mem_trace_lock();
if (!mem_trace_cb && mem_trace_cb_reserve) {
mem_trace_cb = mem_trace_cb_reserve;
}
mbed_mem_trace_unlock();
}

void mbed_mem_trace_lock()
{
mem_trace_mutex->lock();
Expand Down
10 changes: 10 additions & 0 deletions platform/mbed_mem_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ typedef void (*mbed_mem_trace_cb_t)(uint8_t op, void *res, void *caller, ...);
*/
void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb);

/**
* Disable the memory trace output by disabling the callback function
*/
void mbed_mem_trace_disable();

/**
* Renable the memory trace output with the cb in use when disable was called
*/
void mbed_mem_trace_enable();

/**
* Trace lock.
* @note Locking prevent recursive tracing of malloc/free inside relloc/calloc
Expand Down