1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-22 23:56:13 +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 "DCCTimer.h"
#include "CommandDistributor.h"
#include "MemoryFree.h"
// #include "MemoryFree.h"
#include "freeMemory.h"
MQTTInterface *MQTTInterface::singleton = NULL;

View File

@ -37,7 +37,8 @@
#include "DCCEXParser.h"
#include "Queue.h"
#include "ObjectPool.h"
#include "MemoryFree.h"
// #include "MemoryFree.h"
#include "freeMemory.h"
#define MAXPAYLOAD 64 // max length of a payload recieved
#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

@ -23,7 +23,7 @@
// thanks go to https://github.com/mpflaga/Arduino-MemoryFree
#if defined(__arm__)
extern "C" char* sbrk(int);
extern "C" char *sbrk(int);
#elif defined(__AVR__)
extern char *__brkval;
extern char *__malloc_heap_start;
@ -31,14 +31,16 @@ extern char *__malloc_heap_start;
#error Unsupported board type
#endif
static volatile int minimum_free_memory = __INT_MAX__;
#if !defined(__IMXRT1062__)
static inline int freeMemory() {
inline int freeMemory()
// static inline int freeMemory()
{
char top;
#if defined(__arm__)
return &top - reinterpret_cast<char*>(sbrk(0));
return &top - reinterpret_cast<char *>(sbrk(0));
#elif defined(__AVR__)
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#else
@ -47,7 +49,8 @@ static inline int freeMemory() {
}
// Return low memory value.
int minimumFreeMemory() {
int minimumFreeMemory()
{
byte sreg_save = SREG;
noInterrupts(); // Disable interrupts
int retval = minimum_free_memory;
@ -57,34 +60,36 @@ int minimumFreeMemory() {
#else
#if defined(ARDUINO_TEENSY40)
static const unsigned DTCM_START = 0x20000000UL;
static const unsigned OCRAM_START = 0x20200000UL;
static const unsigned OCRAM_SIZE = 512;
static const unsigned FLASH_SIZE = 1984;
static const unsigned DTCM_START = 0x20000000UL;
static const unsigned OCRAM_START = 0x20200000UL;
static const unsigned OCRAM_SIZE = 512;
static const unsigned FLASH_SIZE = 1984;
#elif defined(ARDUINO_TEENSY41)
static const unsigned DTCM_START = 0x20000000UL;
static const unsigned OCRAM_START = 0x20200000UL;
static const unsigned OCRAM_SIZE = 512;
static const unsigned FLASH_SIZE = 7936;
#if TEENSYDUINO>151
extern "C" uint8_t external_psram_size;
static const unsigned DTCM_START = 0x20000000UL;
static const unsigned OCRAM_START = 0x20200000UL;
static const unsigned OCRAM_SIZE = 512;
static const unsigned FLASH_SIZE = 7936;
#if TEENSYDUINO > 151
extern "C" uint8_t external_psram_size;
#endif
#endif
static inline int freeMemory() {
static inline int freeMemory()
{
extern unsigned long _ebss;
extern unsigned long _sdata;
extern unsigned long _estack;
const unsigned DTCM_START = 0x20000000UL;
unsigned dtcm = (unsigned)&_estack - DTCM_START;
unsigned stackinuse = (unsigned) &_estack - (unsigned) __builtin_frame_address(0);
unsigned stackinuse = (unsigned)&_estack - (unsigned)__builtin_frame_address(0);
unsigned varsinuse = (unsigned)&_ebss - (unsigned)&_sdata;
unsigned freemem = dtcm - (stackinuse + varsinuse);
return freemem;
}
// Return low memory value.
int minimumFreeMemory() {
int minimumFreeMemory()
{
//byte sreg_save = SREG;
//noInterrupts(); // Disable interrupts
int retval = minimum_free_memory;
@ -93,19 +98,20 @@ int minimumFreeMemory() {
}
#endif
// 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.
//
//
// Although __brkval may go up and down as heap memory is allocated
// and freed, this function records only the worst case encountered.
// 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.
//
void updateMinimumFreeMemory(unsigned char extraBytes) {
int spare = freeMemory()-extraBytes;
if (spare < 0) spare = 0;
if (spare < minimum_free_memory) minimum_free_memory = spare;
void updateMinimumFreeMemory(unsigned char extraBytes)
{
int spare = freeMemory() - extraBytes;
if (spare < 0)
spare = 0;
if (spare < minimum_free_memory)
minimum_free_memory = spare;
}

View File

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