2023-03-20 12:28:34 +01:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
|
|
|
// Copyright © 2014-2023, Benoit BLANCHON
|
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <ArduinoJson/Memory/Allocator.hpp>
|
|
|
|
|
2023-03-31 14:36:24 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
2023-03-20 12:28:34 +01:00
|
|
|
struct FailingAllocator : ArduinoJson::Allocator {
|
|
|
|
static FailingAllocator* instance() {
|
|
|
|
static FailingAllocator allocator;
|
|
|
|
return &allocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FailingAllocator() = default;
|
|
|
|
~FailingAllocator() = default;
|
|
|
|
|
|
|
|
void* allocate(size_t) override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(void*) override {}
|
|
|
|
|
|
|
|
void* reallocate(void*, size_t) override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
2023-03-31 14:36:24 +02:00
|
|
|
|
2023-04-01 10:34:37 +02:00
|
|
|
class AllocatorLog {
|
|
|
|
public:
|
|
|
|
struct Allocate {
|
|
|
|
Allocate(size_t s) : size(s) {}
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Reallocate {
|
2023-04-01 10:59:34 +02:00
|
|
|
Reallocate(size_t s1, size_t s2) : oldSize(s1), newSize(s2) {}
|
|
|
|
size_t oldSize, newSize;
|
2023-04-01 10:34:37 +02:00
|
|
|
};
|
|
|
|
|
2023-04-01 10:59:34 +02:00
|
|
|
struct Deallocate {
|
|
|
|
Deallocate(size_t s) : size(s) {}
|
|
|
|
size_t size;
|
|
|
|
};
|
2023-04-01 10:34:37 +02:00
|
|
|
|
|
|
|
AllocatorLog& operator<<(const Allocate& a) {
|
|
|
|
_log << "allocate(" << a.size << ")\n";
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-04-01 10:59:34 +02:00
|
|
|
AllocatorLog& operator<<(const Deallocate& d) {
|
|
|
|
_log << "deallocate(" << d.size << ")\n";
|
2023-04-01 10:34:37 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocatorLog& operator<<(const Reallocate& a) {
|
2023-04-01 10:59:34 +02:00
|
|
|
_log << "reallocate(" << a.oldSize << ", " << a.newSize << ")\n";
|
2023-04-01 10:34:37 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str() const {
|
|
|
|
auto s = _log.str();
|
|
|
|
if (s.empty())
|
|
|
|
return "(empty)";
|
|
|
|
s.pop_back(); // remove the trailing '\n'
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const AllocatorLog& other) const {
|
|
|
|
return str() == other.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& os, const AllocatorLog& log) {
|
|
|
|
os << log.str();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::ostringstream _log;
|
|
|
|
};
|
|
|
|
|
2023-03-31 14:36:24 +02:00
|
|
|
class SpyingAllocator : public ArduinoJson::Allocator {
|
|
|
|
public:
|
2023-04-02 16:51:20 +02:00
|
|
|
SpyingAllocator(
|
|
|
|
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
|
|
|
: _upstream(upstream) {}
|
2023-03-31 14:36:24 +02:00
|
|
|
virtual ~SpyingAllocator() {}
|
|
|
|
|
|
|
|
void* allocate(size_t n) override {
|
2023-04-01 10:34:37 +02:00
|
|
|
_log << AllocatorLog::Allocate(n);
|
2023-04-01 10:59:34 +02:00
|
|
|
auto block = reinterpret_cast<AllocatedBlock*>(
|
2023-04-02 16:51:20 +02:00
|
|
|
_upstream->allocate(sizeof(AllocatedBlock) + n - 1));
|
2023-04-01 10:59:34 +02:00
|
|
|
block->size = n;
|
|
|
|
return block->payload;
|
2023-03-31 14:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(void* p) override {
|
2023-04-01 10:59:34 +02:00
|
|
|
auto block = AllocatedBlock::fromPayload(p);
|
|
|
|
_log << AllocatorLog::Deallocate(block->size);
|
2023-04-02 16:51:20 +02:00
|
|
|
_upstream->deallocate(block);
|
2023-03-31 14:36:24 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 10:59:34 +02:00
|
|
|
void* reallocate(void* p, size_t n) override {
|
|
|
|
auto block = AllocatedBlock::fromPayload(p);
|
|
|
|
_log << AllocatorLog::Reallocate(block->size, n);
|
|
|
|
block = reinterpret_cast<AllocatedBlock*>(
|
2023-04-02 16:51:20 +02:00
|
|
|
_upstream->reallocate(block, sizeof(AllocatedBlock) + n - 1));
|
2023-04-01 10:59:34 +02:00
|
|
|
block->size = n;
|
|
|
|
return block->payload;
|
2023-03-31 14:36:24 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 10:34:37 +02:00
|
|
|
const AllocatorLog& log() const {
|
|
|
|
return _log;
|
2023-03-31 14:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-04-01 10:59:34 +02:00
|
|
|
struct AllocatedBlock {
|
|
|
|
size_t size;
|
|
|
|
char payload[1];
|
|
|
|
|
|
|
|
static AllocatedBlock* fromPayload(void* p) {
|
|
|
|
return reinterpret_cast<AllocatedBlock*>(
|
|
|
|
// Cast to void* to silence "cast increases required alignment of
|
|
|
|
// target type [-Werror=cast-align]"
|
|
|
|
reinterpret_cast<void*>(reinterpret_cast<char*>(p) -
|
|
|
|
offsetof(AllocatedBlock, payload)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-01 10:34:37 +02:00
|
|
|
AllocatorLog _log;
|
2023-04-02 16:51:20 +02:00
|
|
|
Allocator* _upstream;
|
2023-03-31 14:36:24 +02:00
|
|
|
};
|
2023-04-01 15:06:24 +02:00
|
|
|
|
|
|
|
class ControllableAllocator : public ArduinoJson::Allocator {
|
|
|
|
public:
|
|
|
|
ControllableAllocator() : _enabled(true) {}
|
|
|
|
virtual ~ControllableAllocator() {}
|
|
|
|
|
|
|
|
void* allocate(size_t n) override {
|
|
|
|
return _enabled ? malloc(n) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(void* p) override {
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* reallocate(void* ptr, size_t n) override {
|
|
|
|
return realloc(ptr, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable() {
|
|
|
|
_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool _enabled;
|
|
|
|
};
|