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
|
|
|
|
|
|
|
/**
|
|
|
|
* A pool based allocator
|
2020-03-19 02:15:51 +00:00
|
|
|
*
|
2020-02-02 17:59:00 +00:00
|
|
|
* Eventually this routine will even be safe for ISR use...
|
|
|
|
*/
|
2020-03-19 02:15:51 +00:00
|
|
|
template <class T> class MemoryPool
|
2020-02-19 04:06:01 +00:00
|
|
|
{
|
2020-02-02 20:45:32 +00:00
|
|
|
PointerQueue<T> dead;
|
2020-02-02 17:59:00 +00:00
|
|
|
|
|
|
|
T *buf; // our large raw block of memory
|
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
size_t maxElements;
|
2020-02-19 04:06:01 +00:00
|
|
|
|
2020-03-19 02:15:51 +00:00
|
|
|
public:
|
2020-02-19 04:06:01 +00:00
|
|
|
MemoryPool(size_t _maxElements) : dead(_maxElements), maxElements(_maxElements)
|
|
|
|
{
|
2020-02-02 17:59:00 +00:00
|
|
|
buf = new T[maxElements];
|
2020-02-19 04:06:01 +00:00
|
|
|
|
2020-02-02 17:59:00 +00:00
|
|
|
// prefill dead
|
2020-02-19 04:06:01 +00:00
|
|
|
for (int i = 0; i < maxElements; i++)
|
2020-02-02 17:59:00 +00:00
|
|
|
release(&buf[i]);
|
|
|
|
}
|
|
|
|
|
2020-03-19 02:15:51 +00:00
|
|
|
~MemoryPool() { delete[] buf; }
|
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-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
|
|
|
|
/// don't want this version)
|
2020-02-19 04:06:01 +00:00
|
|
|
T *allocZeroed(TickType_t maxWait)
|
|
|
|
{
|
2020-02-02 20:45:32 +00:00
|
|
|
T *p = dead.dequeuePtr(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-02-02 20:45:32 +00:00
|
|
|
T *p = dead.dequeuePtr(maxWait);
|
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-02-19 04:06:01 +00:00
|
|
|
void release(T *p)
|
|
|
|
{
|
2020-03-16 00:52:01 +00:00
|
|
|
assert(dead.enqueue(p, 0));
|
2020-03-19 02:15:51 +00:00
|
|
|
assert(p >= buf &&
|
|
|
|
(p - buf) <
|
|
|
|
maxElements); // sanity check to make sure a programmer didn't free something that didn't come from this pool
|
2020-02-02 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 04:06:01 +00:00
|
|
|
/// Return a buffer from an ISR, if higherPriWoken is set to true you have some work to do ;-)
|
|
|
|
void releaseFromISR(T *p, BaseType_t *higherPriWoken)
|
|
|
|
{
|
2020-03-16 00:52:01 +00:00
|
|
|
assert(dead.enqueueFromISR(p, higherPriWoken));
|
2020-03-19 02:15:51 +00:00
|
|
|
assert(p >= buf &&
|
|
|
|
(p - buf) <
|
|
|
|
maxElements); // sanity check to make sure a programmer didn't free something that didn't come from this pool
|
2020-02-19 04:06:01 +00:00
|
|
|
}
|
|
|
|
};
|