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"
|
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
|
|
|
|
2020-02-06 08:49:33 -08:00
|
|
|
/// Return a queable object which has been prefilled with zeros. Panic 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);
|
2020-02-18 20:06:01 -08:00
|
|
|
|
2020-02-06 08:49:33 -08:00
|
|
|
assert(p); // FIXME panic instead
|
|
|
|
|
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);
|
|
|
|
|
assert(p);
|
2020-02-18 20:06:01 -08:00
|
|
|
|
|
|
|
|
if (p)
|
2020-02-03 10:00:53 -08: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
|
|
|
{
|
|
|
|
|
assert(p);
|
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
|
|
|
};
|