2020-02-19 04:06:01 +00:00
|
|
|
#pragma once
|
2020-02-02 17:59:00 +00:00
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
#include "PointerQueue.h"
|
2020-02-02 17:59:00 +00:00
|
|
|
|
2020-06-12 18:53:59 +00:00
|
|
|
template <class T> class Allocator
|
2020-02-19 04:06:01 +00:00
|
|
|
{
|
|
|
|
|
2020-03-19 02:15:51 +00:00
|
|
|
public:
|
2020-06-12 18:53:59 +00:00
|
|
|
virtual ~Allocator() {}
|
2020-02-02 17:59:00 +00:00
|
|
|
|
2020-02-06 16:49:33 +00:00
|
|
|
/// Return a queable object which has been prefilled with zeros. Panic if no buffer is available
|
2020-04-17 16:48:54 +00:00
|
|
|
/// Note: this method is safe to call from regular OR ISR code
|
2020-02-19 04:06:01 +00:00
|
|
|
T *allocZeroed()
|
|
|
|
{
|
2020-02-06 16:49:33 +00:00
|
|
|
T *p = allocZeroed(0);
|
2020-02-19 04:06:01 +00:00
|
|
|
|
2020-02-06 16:49:33 +00:00
|
|
|
assert(p); // FIXME panic instead
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-03-19 02:15:51 +00:00
|
|
|
/// Return a queable object which has been prefilled with zeros - allow timeout to wait for available buffers (you probably
|
2020-04-17 16:48:54 +00:00
|
|
|
/// don't want this version).
|
2020-02-19 04:06:01 +00:00
|
|
|
T *allocZeroed(TickType_t maxWait)
|
|
|
|
{
|
2020-06-12 18:53:59 +00:00
|
|
|
T *p = alloc(maxWait);
|
2020-02-19 04:06:01 +00:00
|
|
|
|
|
|
|
if (p)
|
2020-02-02 20:45:32 +00:00
|
|
|
memset(p, 0, sizeof(T));
|
|
|
|
return p;
|
|
|
|
}
|
2020-02-02 17:59:00 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
/// Return a queable object which is a copy of some other object
|
2020-02-19 04:06:01 +00:00
|
|
|
T *allocCopy(const T &src, TickType_t maxWait = portMAX_DELAY)
|
|
|
|
{
|
2020-06-12 18:53:59 +00:00
|
|
|
T *p = alloc(maxWait);
|
|
|
|
assert(p);
|
2020-02-19 04:06:01 +00:00
|
|
|
|
|
|
|
if (p)
|
2020-02-03 18:00:53 +00:00
|
|
|
*p = src;
|
2020-02-02 17:59:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a buffer for use by others
|
2020-06-12 18:53:59 +00:00
|
|
|
virtual void release(T *p) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Alloc some storage
|
|
|
|
virtual T *alloc(TickType_t maxWait) = 0;
|
|
|
|
};
|
|
|
|
|
2020-06-12 19:11:18 +00: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 19:11:18 +00:00
|
|
|
{
|
|
|
|
assert(p);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-06-13 15:27:25 +00:00
|
|
|
// Alloc some storage
|
2022-01-24 17:24:40 +00:00
|
|
|
virtual T *alloc(TickType_t maxWait) override
|
2020-06-13 15:27:25 +00:00
|
|
|
{
|
|
|
|
T *p = (T *)malloc(sizeof(T));
|
|
|
|
assert(p);
|
|
|
|
return p;
|
|
|
|
}
|
2020-06-12 19:11:18 +00:00
|
|
|
};
|