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

freememory() available more generally

fixed some mem diags
This commit is contained in:
Gregor Baues 2021-06-04 13:46:21 +02:00
parent 083c73ebc3
commit c30f1c7cbe
6 changed files with 39 additions and 100 deletions

View File

@ -33,7 +33,8 @@
#include "MQTTBrokers.h" #include "MQTTBrokers.h"
#include "DCCTimer.h" #include "DCCTimer.h"
#include "CommandDistributor.h" #include "CommandDistributor.h"
#include "MemoryFree.h" // #include "MemoryFree.h"
#include "freeMemory.h"
MQTTInterface *MQTTInterface::singleton = NULL; MQTTInterface *MQTTInterface::singleton = NULL;

View File

@ -37,7 +37,8 @@
#include "DCCEXParser.h" #include "DCCEXParser.h"
#include "Queue.h" #include "Queue.h"
#include "ObjectPool.h" #include "ObjectPool.h"
#include "MemoryFree.h" // #include "MemoryFree.h"
#include "freeMemory.h"
#define MAXPAYLOAD 64 // max length of a payload recieved #define MAXPAYLOAD 64 // max length of a payload recieved
#define MAXDOMAINLENGTH 32 // domain name length for the broker e.g. test.mosquitto.org #define MAXDOMAINLENGTH 32 // domain name length for the broker e.g. test.mosquitto.org

View File

@ -1,57 +0,0 @@
#if (ARDUINO >= 100)
#include <Arduino.h>
// #include <Transport/MQTT/DccMQTT.h>
// #include <Diag/Telemetry/DccTelemetry.h>
#else
#include <WProgram.h>
#endif
extern unsigned int __heap_start;
extern void *__brkval;
/*
* The free list structure as maintained by the
* avr-libc memory allocation routines.
*/
struct __freelist
{
size_t sz;
struct __freelist *nx;
};
/* The head of the free list structure */
extern struct __freelist *__flp;
#include "MemoryFree.h"
/* Calculates the size of the free list */
int freeListSize()
{
struct __freelist* current;
int total = 0;
for (current = __flp; current; current = current->nx)
{
total += 2; /* Add two bytes for the memory block's header */
total += (int) current->sz;
}
return total;
}
int freeMemory()
{
int free_memory;
if ((int)__brkval == 0)
{
free_memory = ((int)&free_memory) - ((int)&__heap_start);
}
else
{
free_memory = ((int)&free_memory) - ((int)__brkval);
free_memory += freeListSize();
}
// report free memory over MQTT - to be replaced by Telemetry
// DccTelemetry::free_memory(free_memory);
return free_memory;
}

View File

@ -1,14 +0,0 @@
#ifndef MEMORY_FREE_H
#define MEMORY_FREE_H
#ifdef __cplusplus
extern "C" {
#endif
int freeMemory();
#ifdef __cplusplus
}
#endif
#endif

View File

@ -31,11 +31,13 @@ extern char *__malloc_heap_start;
#error Unsupported board type #error Unsupported board type
#endif #endif
static volatile int minimum_free_memory = __INT_MAX__; static volatile int minimum_free_memory = __INT_MAX__;
#if !defined(__IMXRT1062__) #if !defined(__IMXRT1062__)
static inline int freeMemory() {
inline int freeMemory()
// static inline int freeMemory()
{
char top; char top;
#if defined(__arm__) #if defined(__arm__)
return &top - reinterpret_cast<char *>(sbrk(0)); return &top - reinterpret_cast<char *>(sbrk(0));
@ -47,7 +49,8 @@ static inline int freeMemory() {
} }
// Return low memory value. // Return low memory value.
int minimumFreeMemory() { int minimumFreeMemory()
{
byte sreg_save = SREG; byte sreg_save = SREG;
noInterrupts(); // Disable interrupts noInterrupts(); // Disable interrupts
int retval = minimum_free_memory; int retval = minimum_free_memory;
@ -71,7 +74,8 @@ int minimumFreeMemory() {
#endif #endif
#endif #endif
static inline int freeMemory() { static inline int freeMemory()
{
extern unsigned long _ebss; extern unsigned long _ebss;
extern unsigned long _sdata; extern unsigned long _sdata;
extern unsigned long _estack; extern unsigned long _estack;
@ -84,7 +88,8 @@ static inline int freeMemory() {
} }
// Return low memory value. // Return low memory value.
int minimumFreeMemory() { int minimumFreeMemory()
{
//byte sreg_save = SREG; //byte sreg_save = SREG;
//noInterrupts(); // Disable interrupts //noInterrupts(); // Disable interrupts
int retval = minimum_free_memory; int retval = minimum_free_memory;
@ -93,7 +98,6 @@ int minimumFreeMemory() {
} }
#endif #endif
// Update low ram level. Allow for extra bytes to be specified // Update low ram level. Allow for extra bytes to be specified
// by estimation or inspection, that may be used by other // by estimation or inspection, that may be used by other
// called subroutines. Must be called with interrupts disabled. // called subroutines. Must be called with interrupts disabled.
@ -103,9 +107,11 @@ int minimumFreeMemory() {
// So even if all of the heap is freed, the reported minimum free // So even if all of the heap is freed, the reported minimum free
// memory will not increase. // memory will not increase.
// //
void updateMinimumFreeMemory(unsigned char extraBytes) { void updateMinimumFreeMemory(unsigned char extraBytes)
{
int spare = freeMemory() - extraBytes; int spare = freeMemory() - extraBytes;
if (spare < 0) spare = 0; if (spare < 0)
if (spare < minimum_free_memory) minimum_free_memory = spare; spare = 0;
if (spare < minimum_free_memory)
minimum_free_memory = spare;
} }

View File

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