2020-02-18 20:06:01 -08:00
|
|
|
#pragma once
|
2020-02-02 09:59:00 -08:00
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <assert.h>
|
2024-12-19 03:47:46 -08:00
|
|
|
#include <functional>
|
|
|
|
|
#include <memory>
|
2020-02-02 09:59:00 -08:00
|
|
|
|
2020-02-02 12:45:32 -08:00
|
|
|
#include "PointerQueue.h"
|
2025-09-12 15:49:56 -05:00
|
|
|
#include "configuration.h" // For LOG_WARN, LOG_DEBUG, LOG_HEAP
|
2020-02-02 09:59:00 -08:00
|
|
|
|
2020-06-12 11:53:59 -07:00
|
|
|
template <class T> class Allocator
|
2020-02-18 20:06:01 -08:00
|
|
|
{
|
|
|
|
|
|
2020-03-18 19:15:51 -07:00
|
|
|
public:
|
2024-12-19 03:47:46 -08:00
|
|
|
Allocator() : deleter([this](T *p) { this->release(p); }) {}
|
2020-06-12 11:53:59 -07:00
|
|
|
virtual ~Allocator() {}
|
2020-02-02 09:59:00 -08:00
|
|
|
|
2025-09-12 15:49:56 -05:00
|
|
|
/// Return a queable object which has been prefilled with zeros. Return nullptr if no buffer is available
|
2020-04-17 09:48:54 -07:00
|
|
|
/// Note: this method is safe to call from regular OR ISR code
|
2020-02-18 20:06:01 -08:00
|
|
|
T *allocZeroed()
|
|
|
|
|
{
|
2020-02-06 08:49:33 -08:00
|
|
|
T *p = allocZeroed(0);
|
2025-09-12 15:49:56 -05:00
|
|
|
if (!p) {
|
|
|
|
|
LOG_WARN("Failed to allocate zeroed memory");
|
|
|
|
|
}
|
2020-02-06 08:49:33 -08:00
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 19:15:51 -07:00
|
|
|
/// Return a queable object which has been prefilled with zeros - allow timeout to wait for available buffers (you probably
|
2020-04-17 09:48:54 -07:00
|
|
|
/// don't want this version).
|
2020-02-18 20:06:01 -08:00
|
|
|
T *allocZeroed(TickType_t maxWait)
|
|
|
|
|
{
|
2020-06-12 11:53:59 -07:00
|
|
|
T *p = alloc(maxWait);
|
2020-02-18 20:06:01 -08:00
|
|
|
|
|
|
|
|
if (p)
|
2020-02-02 12:45:32 -08:00
|
|
|
memset(p, 0, sizeof(T));
|
|
|
|
|
return p;
|
|
|
|
|
}
|
2020-02-02 09:59:00 -08:00
|
|
|
|
2020-02-02 12:45:32 -08:00
|
|
|
/// Return a queable object which is a copy of some other object
|
2020-02-18 20:06:01 -08:00
|
|
|
T *allocCopy(const T &src, TickType_t maxWait = portMAX_DELAY)
|
|
|
|
|
{
|
2020-06-12 11:53:59 -07:00
|
|
|
T *p = alloc(maxWait);
|
2025-09-12 15:49:56 -05:00
|
|
|
if (!p) {
|
|
|
|
|
LOG_WARN("Failed to allocate memory for copy");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-02-18 20:06:01 -08:00
|
|
|
|
2025-09-12 15:49:56 -05:00
|
|
|
*p = src;
|
2020-02-02 09:59:00 -08:00
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 03:47:46 -08:00
|
|
|
/// Variations of the above methods that return std::unique_ptr instead of raw pointers.
|
|
|
|
|
using UniqueAllocation = std::unique_ptr<T, const std::function<void(T *)> &>;
|
|
|
|
|
/// Return a queable object which has been prefilled with zeros.
|
|
|
|
|
/// std::unique_ptr wrapped variant of allocZeroed().
|
|
|
|
|
UniqueAllocation allocUniqueZeroed() { return UniqueAllocation(allocZeroed(), deleter); }
|
|
|
|
|
/// Return a queable object which has been prefilled with zeros - allow timeout to wait for available buffers (you probably
|
|
|
|
|
/// don't want this version).
|
|
|
|
|
/// std::unique_ptr wrapped variant of allocZeroed(TickType_t maxWait).
|
|
|
|
|
UniqueAllocation allocUniqueZeroed(TickType_t maxWait) { return UniqueAllocation(allocZeroed(maxWait), deleter); }
|
|
|
|
|
/// Return a queable object which is a copy of some other object
|
|
|
|
|
/// std::unique_ptr wrapped variant of allocCopy(const T &src, TickType_t maxWait).
|
|
|
|
|
UniqueAllocation allocUniqueCopy(const T &src, TickType_t maxWait = portMAX_DELAY)
|
|
|
|
|
{
|
|
|
|
|
return UniqueAllocation(allocCopy(src, maxWait), deleter);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 09:59:00 -08:00
|
|
|
/// Return a buffer for use by others
|
2020-06-12 11:53:59 -07:00
|
|
|
virtual void release(T *p) = 0;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// Alloc some storage
|
|
|
|
|
virtual T *alloc(TickType_t maxWait) = 0;
|
2024-12-19 03:47:46 -08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// std::unique_ptr Deleter function; calls release().
|
|
|
|
|
const std::function<void(T *)> deleter;
|
2020-06-12 11:53:59 -07:00
|
|
|
};
|
|
|
|
|
|
2020-06-12 12:11:18 -07:00
|
|
|
/**
|
|
|
|
|
* An allocator that just uses regular free/malloc
|
|
|
|
|
*/
|
|
|
|
|
template <class T> class MemoryDynamic : public Allocator<T>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/// Return a buffer for use by others
|
2022-01-24 17:24:40 +00:00
|
|
|
virtual void release(T *p) override
|
2020-06-12 12:11:18 -07:00
|
|
|
{
|
2025-09-12 15:49:56 -05:00
|
|
|
if (p == nullptr)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-09-09 10:29:07 -05:00
|
|
|
LOG_HEAP("Freeing 0x%x", p);
|
|
|
|
|
|
2020-06-12 12:11:18 -07:00
|
|
|
free(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2020-06-13 08:27:25 -07:00
|
|
|
// Alloc some storage
|
2022-01-24 17:24:40 +00:00
|
|
|
virtual T *alloc(TickType_t maxWait) override
|
2020-06-13 08:27:25 -07:00
|
|
|
{
|
|
|
|
|
T *p = (T *)malloc(sizeof(T));
|
|
|
|
|
assert(p);
|
|
|
|
|
return p;
|
|
|
|
|
}
|
2020-06-12 12:11:18 -07:00
|
|
|
};
|
2025-09-12 15:49:56 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A static memory pool that uses a fixed buffer instead of heap allocation
|
|
|
|
|
*/
|
|
|
|
|
template <class T, int MaxSize> class MemoryPool : public Allocator<T>
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
T pool[MaxSize];
|
|
|
|
|
bool used[MaxSize];
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
MemoryPool()
|
|
|
|
|
{
|
|
|
|
|
// Initialize the used array to false (all slots free)
|
|
|
|
|
for (int i = 0; i < MaxSize; i++) {
|
|
|
|
|
used[i] = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Return a buffer for use by others
|
|
|
|
|
virtual void release(T *p) override
|
|
|
|
|
{
|
|
|
|
|
if (!p) {
|
|
|
|
|
LOG_DEBUG("Failed to release memory, pointer is null");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the index of this pointer in our pool
|
|
|
|
|
int index = p - pool;
|
|
|
|
|
if (index >= 0 && index < MaxSize) {
|
|
|
|
|
assert(used[index]); // Should be marked as used
|
|
|
|
|
used[index] = false;
|
|
|
|
|
LOG_HEAP("Released static pool item %d at 0x%x", index, p);
|
|
|
|
|
} else {
|
|
|
|
|
LOG_WARN("Pointer 0x%x not from our pool!", p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// Alloc some storage from our static pool
|
|
|
|
|
virtual T *alloc(TickType_t maxWait) override
|
|
|
|
|
{
|
|
|
|
|
// Find first free slot
|
|
|
|
|
for (int i = 0; i < MaxSize; i++) {
|
|
|
|
|
if (!used[i]) {
|
|
|
|
|
used[i] = true;
|
|
|
|
|
LOG_HEAP("Allocated static pool item %d at 0x%x", i, &pool[i]);
|
|
|
|
|
return &pool[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No free slots available - return nullptr instead of asserting
|
|
|
|
|
LOG_WARN("No free slots available in static memory pool!");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
};
|