Adding PacketRecord to PSRAM as well as part of this change.

This commit is contained in:
Clive Blackledge 2025-10-03 00:56:50 -07:00 committed by Tom Fifield
parent 05c05accb8
commit 66110877e0
3 changed files with 60 additions and 12 deletions

View File

@ -228,4 +228,27 @@ template <class T, int MaxSize> class PsramMemoryPool : public Allocator<T>
return nullptr;
}
};
// Utility helpers for PSRAM-backed array allocations on ESP32 targets.
template <typename T> inline T *psramAllocArray(size_t count)
{
return static_cast<T *>(heap_caps_malloc(sizeof(T) * count, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
}
template <typename T> inline void psramFreeArray(T *ptr)
{
if (ptr)
heap_caps_free(ptr);
}
#else
template <typename T> inline T *psramAllocArray(size_t count)
{
(void)count;
return nullptr;
}
template <typename T> inline void psramFreeArray(T *ptr)
{
(void)ptr;
}
#endif

View File

@ -1,4 +1,5 @@
#include "PacketHistory.h"
#include "MemoryPool.h"
#include "configuration.h"
#include "mesh-pb-constants.h"
@ -16,7 +17,8 @@
#define VERBOSE_PACKET_HISTORY 0 // Set to 1 for verbose logging, 2 for heavy debugging
#define PACKET_HISTORY_TRACE_AGING 1 // Set to 1 to enable logging of the age of re/used history slots
PacketHistory::PacketHistory(uint32_t size) : recentPacketsCapacity(0), recentPackets(NULL) // Initialize members
PacketHistory::PacketHistory(uint32_t size)
: recentPacketsCapacity(0), recentPackets(NULL), recentPacketsInPsram(false) // Initialize members
{
if (size < 4 || size > PACKETHISTORY_MAX) { // Copilot suggested - makes sense
LOG_WARN("Packet History - Invalid size %d, using default %d", size, PACKETHISTORY_MAX);
@ -25,6 +27,19 @@ PacketHistory::PacketHistory(uint32_t size) : recentPacketsCapacity(0), recentPa
// Allocate memory for the recent packets array
recentPacketsCapacity = size;
if (has_psram()) {
// Prefer PSRAM so the large history pool stays out of internal RAM on ESP32-S3 builds.
recentPackets = psramAllocArray<PacketRecord>(recentPacketsCapacity);
if (recentPackets) {
memset(recentPackets, 0, sizeof(PacketRecord) * recentPacketsCapacity);
recentPacketsInPsram = true;
} else {
LOG_WARN("Packet History - PSRAM allocation failed, falling back to DRAM");
}
}
if (!recentPackets) {
// Fall back to DRAM if PSRAM is unavailable or exhausted.
recentPackets = new PacketRecord[recentPacketsCapacity];
if (!recentPackets) { // No logging here, console/log probably uninitialized yet.
LOG_ERROR("Packet History - Memory allocation failed for size=%d entries / %d Bytes", size,
@ -36,12 +51,21 @@ PacketHistory::PacketHistory(uint32_t size) : recentPacketsCapacity(0), recentPa
// Initialize the recent packets array to zero
memset(recentPackets, 0, sizeof(PacketRecord) * recentPacketsCapacity);
}
}
PacketHistory::~PacketHistory()
{
recentPacketsCapacity = 0;
if (recentPackets) {
// Release via the allocator that produced the buffer.
if (recentPacketsInPsram)
psramFreeArray(recentPackets);
else
delete[] recentPackets;
}
recentPackets = NULL;
recentPacketsCapacity = 0;
recentPacketsInPsram = false;
}
/** Update recentPackets and return true if we have already seen this packet */

View File

@ -27,6 +27,7 @@ class PacketHistory
uint32_t recentPacketsCapacity =
0; // Can be set in constructor, no need to recompile. Used to allocate memory for mx_recentPackets.
PacketRecord *recentPackets = NULL; // Simple and fixed in size. Debloat.
bool recentPacketsInPsram = false; // Remember backing store so we free via the matching allocator.
/** Find a packet record in history.
* @param sender NodeNum