1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-26 17:46:14 +01:00

Memory monitoring updates

Split update from read value;
Inhibit interrupts when reading (normally done from loop code);
Don't inhibit interrupts when updating (normally done from interupt code);
Make freeMemory() local and ask for inline code generation.
This commit is contained in:
Neil McKechnie 2021-03-09 22:43:41 +00:00
parent 0880507d89
commit 163dd270e8
4 changed files with 18 additions and 13 deletions

View File

@ -126,7 +126,7 @@ void loop()
// Report any decrease in memory (will automatically trigger on first call)
static int ramLowWatermark = 32767; // replaced on first loop
int freeNow = updateMinimumFreeMemory();
int freeNow = minimumFreeMemory();
if (freeNow < ramLowWatermark)
{
ramLowWatermark = freeNow;

View File

@ -720,7 +720,7 @@ bool DCCEXParser::parseD(Print *stream, int params, int p[])
return true;
case HASH_KEYWORD_RAM: // <D RAM>
StringFormatter::send(stream, F("\nFree memory=%d\n"), freeMemory());
StringFormatter::send(stream, F("\nFree memory=%d\n"), minimumFreeMemory());
break;
case HASH_KEYWORD_ACK: // <D ACK ON/OFF> <D ACK [LIMIT|MIN|MAX] Value>

View File

@ -35,26 +35,31 @@ extern char *__malloc_heap_start;
static volatile int minimum_free_memory = 32767;
int freeMemory() {
static inline int freeMemory() {
char top;
#if defined(__arm__)
return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(__AVR__)
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#else
#error bailed out alredy above
#error bailed out already above
#endif
}
// Update low ram level. Allow for extra bytes to be specified
// by estimation or inspection, that may be used by other
// called subroutines.
int updateMinimumFreeMemory(unsigned char extraBytes) {
byte sreg_save = SREG;
noInterrupts();
// called subroutines. Must be called with interrupts disabled.
//
void updateMinimumFreeMemory(unsigned char extraBytes) {
int spare = freeMemory()-extraBytes;
if (spare < minimum_free_memory) minimum_free_memory = spare;
int returnValue = minimum_free_memory;
SREG = sreg_save;
return returnValue;
}
// Return low memory value.
int minimumFreeMemory() {
byte sreg_save = SREG;
noInterrupts(); // Disable interrupts
int retval = minimum_free_memory;
SREG = sreg_save; // Restore interrupt state
return retval;
}

View File

@ -20,6 +20,6 @@
#ifndef freeMemory_h
#define freeMemory_h
int freeMemory();
int updateMinimumFreeMemory(unsigned char extraBytes=0);
void updateMinimumFreeMemory(unsigned char extraBytes=0);
int minimumFreeMemory();
#endif