From 163dd270e89214cd4203ee3157cc054f8cc0a377 Mon Sep 17 00:00:00 2001 From: Neil McKechnie Date: Tue, 9 Mar 2021 22:43:41 +0000 Subject: [PATCH] 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. --- CommandStation-EX.ino | 2 +- DCCEXParser.cpp | 2 +- freeMemory.cpp | 23 ++++++++++++++--------- freeMemory.h | 4 ++-- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino index 14b0057..4954223 100644 --- a/CommandStation-EX.ino +++ b/CommandStation-EX.ino @@ -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; diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index e0496cc..2556cc2 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -720,7 +720,7 @@ bool DCCEXParser::parseD(Print *stream, int params, int p[]) return true; case HASH_KEYWORD_RAM: // - StringFormatter::send(stream, F("\nFree memory=%d\n"), freeMemory()); + StringFormatter::send(stream, F("\nFree memory=%d\n"), minimumFreeMemory()); break; case HASH_KEYWORD_ACK: // diff --git a/freeMemory.cpp b/freeMemory.cpp index fd99d5b..f8d2a0b 100644 --- a/freeMemory.cpp +++ b/freeMemory.cpp @@ -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(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; } diff --git a/freeMemory.h b/freeMemory.h index bf93189..3d1fe40 100644 --- a/freeMemory.h +++ b/freeMemory.h @@ -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