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; 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 #endif

View File

@ -1,4 +1,5 @@
#include "PacketHistory.h" #include "PacketHistory.h"
#include "MemoryPool.h"
#include "configuration.h" #include "configuration.h"
#include "mesh-pb-constants.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 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 #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 if (size < 4 || size > PACKETHISTORY_MAX) { // Copilot suggested - makes sense
LOG_WARN("Packet History - Invalid size %d, using default %d", size, PACKETHISTORY_MAX); LOG_WARN("Packet History - Invalid size %d, using default %d", size, PACKETHISTORY_MAX);
@ -25,23 +27,45 @@ PacketHistory::PacketHistory(uint32_t size) : recentPacketsCapacity(0), recentPa
// Allocate memory for the recent packets array // Allocate memory for the recent packets array
recentPacketsCapacity = size; recentPacketsCapacity = size;
recentPackets = new PacketRecord[recentPacketsCapacity]; if (has_psram()) {
if (!recentPackets) { // No logging here, console/log probably uninitialized yet. // Prefer PSRAM so the large history pool stays out of internal RAM on ESP32-S3 builds.
LOG_ERROR("Packet History - Memory allocation failed for size=%d entries / %d Bytes", size, recentPackets = psramAllocArray<PacketRecord>(recentPacketsCapacity);
sizeof(PacketRecord) * recentPacketsCapacity); if (recentPackets) {
recentPacketsCapacity = 0; // mark allocation fail memset(recentPackets, 0, sizeof(PacketRecord) * recentPacketsCapacity);
return; // return early recentPacketsInPsram = true;
} else {
LOG_WARN("Packet History - PSRAM allocation failed, falling back to DRAM");
}
} }
// Initialize the recent packets array to zero if (!recentPackets) {
memset(recentPackets, 0, sizeof(PacketRecord) * recentPacketsCapacity); // 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,
sizeof(PacketRecord) * recentPacketsCapacity);
recentPacketsCapacity = 0; // mark allocation fail
return; // return early
}
// Initialize the recent packets array to zero
memset(recentPackets, 0, sizeof(PacketRecord) * recentPacketsCapacity);
}
} }
PacketHistory::~PacketHistory() PacketHistory::~PacketHistory()
{ {
recentPacketsCapacity = 0; if (recentPackets) {
delete[] recentPackets; // Release via the allocator that produced the buffer.
if (recentPacketsInPsram)
psramFreeArray(recentPackets);
else
delete[] recentPackets;
}
recentPackets = NULL; recentPackets = NULL;
recentPacketsCapacity = 0;
recentPacketsInPsram = false;
} }
/** Update recentPackets and return true if we have already seen this packet */ /** Update recentPackets and return true if we have already seen this packet */
@ -458,4 +482,4 @@ inline uint8_t PacketHistory::getOurTxHopLimit(PacketRecord &r)
inline void PacketHistory::setOurTxHopLimit(PacketRecord &r, uint8_t hopLimit) inline void PacketHistory::setOurTxHopLimit(PacketRecord &r, uint8_t hopLimit)
{ {
r.hop_limit = (r.hop_limit & ~HOP_LIMIT_OUR_TX_MASK) | ((hopLimit << HOP_LIMIT_OUR_TX_SHIFT) & HOP_LIMIT_OUR_TX_MASK); r.hop_limit = (r.hop_limit & ~HOP_LIMIT_OUR_TX_MASK) | ((hopLimit << HOP_LIMIT_OUR_TX_SHIFT) & HOP_LIMIT_OUR_TX_MASK);
} }

View File

@ -27,6 +27,7 @@ class PacketHistory
uint32_t recentPacketsCapacity = uint32_t recentPacketsCapacity =
0; // Can be set in constructor, no need to recompile. Used to allocate memory for mx_recentPackets. 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. 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. /** Find a packet record in history.
* @param sender NodeNum * @param sender NodeNum