2020-02-23 18:49:37 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
2020-02-23 21:54:40 +00:00
|
|
|
#define POOL_SIZE 16384
|
2020-02-23 18:49:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An allocator (and placement new operator) that allocates storage from a fixed sized buffer.
|
|
|
|
* It will panic if that buffer fills up.
|
|
|
|
* If you are _sure_ no outstanding references to blocks in this buffer still exist, you can call
|
|
|
|
* reset() to start from scratch.
|
2023-01-21 13:34:29 +00:00
|
|
|
*
|
2020-02-23 18:49:37 +00:00
|
|
|
* Currently the only usecase for this class is the ESP32 bluetooth stack, where once we've called deinit(false)
|
|
|
|
* we are sure all those bluetooth objects no longer exist, and we'll need to recreate them when we restart bluetooth
|
|
|
|
*/
|
|
|
|
class SimpleAllocator
|
|
|
|
{
|
2022-01-24 07:00:14 +00:00
|
|
|
uint8_t bytes[POOL_SIZE] = {};
|
2020-02-23 18:49:37 +00:00
|
|
|
|
2022-01-24 07:00:14 +00:00
|
|
|
uint32_t nextFree = 0;
|
2020-02-23 18:49:37 +00:00
|
|
|
|
2023-01-21 13:34:29 +00:00
|
|
|
public:
|
2020-02-23 18:49:37 +00:00
|
|
|
SimpleAllocator();
|
|
|
|
|
|
|
|
void *alloc(size_t size);
|
|
|
|
|
|
|
|
/** If you are _sure_ no outstanding references to blocks in this buffer still exist, you can call
|
2023-01-21 13:34:29 +00:00
|
|
|
* reset() to start from scratch.
|
|
|
|
* */
|
2020-02-23 18:49:37 +00:00
|
|
|
void reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
void *operator new(size_t size, SimpleAllocator &p);
|
|
|
|
|
2020-02-23 21:20:46 +00:00
|
|
|
/**
|
|
|
|
* Temporarily makes the specified Allocator be used for _all_ allocations. Useful when calling library routines
|
|
|
|
* that don't know about pools
|
|
|
|
*/
|
2023-01-21 13:34:29 +00:00
|
|
|
class AllocatorScope
|
|
|
|
{
|
|
|
|
public:
|
2022-01-24 19:58:07 +00:00
|
|
|
explicit AllocatorScope(SimpleAllocator &a);
|
2020-02-23 21:20:46 +00:00
|
|
|
~AllocatorScope();
|
|
|
|
};
|