mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-02-21 23:33:57 -04:00
added memcheck library
git-svn-id: https://arducopter.googlecode.com/svn/trunk@3206 f9c3cf11-9bcb-44bc-f272-b75c42450872
This commit is contained in:
parent
bba6b90104
commit
5cac97ca8d
62
libraries/memcheck/memcheck.cpp
Normal file
62
libraries/memcheck/memcheck.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
||||
|
||||
/*
|
||||
note that we use a 32 bit sentinal to reduce the chance
|
||||
of false positives with uninitialised stack variables
|
||||
*/
|
||||
|
||||
#include <AP_Common.h>
|
||||
|
||||
static const uint32_t *stack_low;
|
||||
extern unsigned __brkval;
|
||||
|
||||
#define STACK_OFFSET 2
|
||||
#define SENTINAL 0x28021967
|
||||
|
||||
/*
|
||||
return the current stack pointer
|
||||
*/
|
||||
static __attribute__((noinline)) const uint32_t *current_stackptr(void)
|
||||
{
|
||||
return (const uint32_t *)__builtin_frame_address(0);
|
||||
}
|
||||
|
||||
/*
|
||||
this can be added in deeply nested code to ensure we catch
|
||||
deep stack usage. It should be caught by the sentinal, but this
|
||||
is an added protection
|
||||
*/
|
||||
void memcheck_update_stackptr(void)
|
||||
{
|
||||
if (current_stackptr() < stack_low) {
|
||||
unsigned s = (unsigned)(current_stackptr() - STACK_OFFSET);
|
||||
stack_low = (uint32_t *)(s & ~3);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
initialise memcheck, setting up the sentinals
|
||||
*/
|
||||
void memcheck_init(void)
|
||||
{
|
||||
uint32_t *p;
|
||||
free(malloc(1)); // ensure heap is initialised
|
||||
stack_low = current_stackptr();
|
||||
memcheck_update_stackptr();
|
||||
for (p=(uint32_t *)(stack_low-1); p>(uint32_t *)__brkval; p--) {
|
||||
*p = SENTINAL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
this returns the real amount of free memory by looking for
|
||||
overwrites of the stack sentinal values
|
||||
*/
|
||||
unsigned memcheck_available_memory(void)
|
||||
{
|
||||
memcheck_update_stackptr();
|
||||
while (*stack_low != SENTINAL && stack_low > (const uint32_t *)__brkval) {
|
||||
stack_low--;
|
||||
}
|
||||
return (unsigned)(stack_low) - __brkval;
|
||||
}
|
6
libraries/memcheck/memcheck.h
Normal file
6
libraries/memcheck/memcheck.h
Normal file
@ -0,0 +1,6 @@
|
||||
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
||||
|
||||
unsigned memcheck_available_memory(void);
|
||||
void memcheck_init(void);
|
||||
void memcheck_update_stackptr(void);
|
||||
|
Loading…
Reference in New Issue
Block a user