Files
firmware/src/platform/esp32/SimpleAllocator.cpp

66 lines
1.2 KiB
C++
Raw Normal View History

2023-01-21 14:34:29 +01:00
#include "SimpleAllocator.h"
2020-02-23 10:49:37 -08:00
#include "assert.h"
2022-12-30 10:56:57 -06:00
#include "configuration.h"
2023-01-21 14:34:29 +01:00
SimpleAllocator::SimpleAllocator()
{
reset();
}
2020-02-23 10:49:37 -08:00
void *SimpleAllocator::alloc(size_t size)
{
assert(nextFree + size <= sizeof(bytes));
void *res = &bytes[nextFree];
nextFree += size;
LOG_DEBUG("Total simple allocs %u\n", nextFree);
2020-02-23 10:49:37 -08:00
return res;
}
2023-01-21 14:34:29 +01:00
void SimpleAllocator::reset()
{
nextFree = 0;
}
2020-02-23 10:49:37 -08:00
void *operator new(size_t size, SimpleAllocator &p)
{
return p.alloc(size);
}
#if 0
// This was a dumb idea, turn off for now
SimpleAllocator *activeAllocator;
AllocatorScope::AllocatorScope(SimpleAllocator &a)
{
assert(!activeAllocator);
activeAllocator = &a;
}
AllocatorScope::~AllocatorScope()
{
assert(activeAllocator);
activeAllocator = NULL;
}
/// Global new/delete, uses a simple allocator if it is in scope
void *operator new(size_t sz) throw(std::bad_alloc)
{
void *mem = activeAllocator ? activeAllocator->alloc(sz) : malloc(sz);
if (mem)
return mem;
else
throw std::bad_alloc();
}
void operator delete(void *ptr) throw()
{
if (activeAllocator)
2024-03-21 20:45:48 -05:00
LOG_WARN("Leaking an active allocator object\n"); // We don't properly handle this yet
else
free(ptr);
}
#endif