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:
|
2023-04-03 08:52:38 +02:00
|
|
|
static std::string Allocate(size_t s) {
|
|
|
|
char buffer[32];
|
|
|
|
sprintf(buffer, "allocate(%zu)", s);
|
|
|
|
return buffer;
|
|
|
|
}
|
2023-04-01 10:34:37 +02:00
|
|
|
|
2023-04-03 08:52:38 +02:00
|
|
|
static std::string AllocateFail(size_t s) {
|
|
|
|
char buffer[32];
|
|
|
|
sprintf(buffer, "allocate(%zu) -> nullptr", s);
|
|
|
|
return buffer;
|
|
|
|
}
|
2023-04-01 10:34:37 +02:00
|
|
|
|
2023-04-03 08:52:38 +02:00
|
|
|
static std::string Reallocate(size_t s1, size_t s2) {
|
|
|
|
char buffer[32];
|
|
|
|
sprintf(buffer, "reallocate(%zu, %zu)", s1, s2);
|
|
|
|
return buffer;
|
2023-04-01 10:59:34 +02:00
|
|
|
};
|
2023-04-01 10:34:37 +02:00
|
|
|
|
2023-04-03 08:52:38 +02:00
|
|
|
static std::string ReallocateFail(size_t s1, size_t s2) {
|
|
|
|
char buffer[32];
|
|
|
|
sprintf(buffer, "reallocate(%zu, %zu) -> nullptr", s1, s2);
|
|
|
|
return buffer;
|
|
|
|
};
|
2023-04-01 10:34:37 +02:00
|
|
|
|
2023-04-03 08:52:38 +02:00
|
|
|
static std::string Deallocate(size_t s) {
|
|
|
|
char buffer[32];
|
|
|
|
sprintf(buffer, "deallocate(%zu)", s);
|
|
|
|
return buffer;
|
|
|
|
};
|
2023-04-01 10:34:37 +02:00
|
|
|
|
2023-04-03 08:52:38 +02:00
|
|
|
AllocatorLog& operator<<(const std::string& s) {
|
2023-04-21 18:53:16 +02:00
|
|
|
log_ << s << "\n";
|
2023-04-01 10:34:37 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str() const {
|
2023-04-21 18:53:16 +02:00
|
|
|
auto s = log_.str();
|
2023-04-01 10:34:37 +02:00
|
|
|
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:
|
2023-04-21 18:53:16 +02:00
|
|
|
std::ostringstream log_;
|
2023-04-01 10:34:37 +02:00
|
|
|
};
|
|
|
|
|
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())
|
2023-04-21 18:53:16 +02:00
|
|
|
: upstream_(upstream) {}
|
2023-03-31 14:36:24 +02:00
|
|
|
virtual ~SpyingAllocator() {}
|
|
|
|
|
|
|
|
void* allocate(size_t n) override {
|
2023-04-01 10:59:34 +02:00
|
|
|
auto block = reinterpret_cast<AllocatedBlock*>(
|
2023-04-21 18:53:16 +02:00
|
|
|
upstream_->allocate(sizeof(AllocatedBlock) + n - 1));
|
2023-04-03 08:52:38 +02:00
|
|
|
if (block) {
|
2023-04-21 18:53:16 +02:00
|
|
|
log_ << AllocatorLog::Allocate(n);
|
2023-04-03 08:52:38 +02:00
|
|
|
block->size = n;
|
|
|
|
return block->payload;
|
|
|
|
} else {
|
2023-04-21 18:53:16 +02:00
|
|
|
log_ << AllocatorLog::AllocateFail(n);
|
2023-04-03 08:52:38 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
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);
|
2023-04-21 18:53:16 +02:00
|
|
|
log_ << AllocatorLog::Deallocate(block->size);
|
|
|
|
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);
|
2023-04-03 08:52:38 +02:00
|
|
|
auto oldSize = block->size;
|
2023-04-01 10:59:34 +02:00
|
|
|
block = reinterpret_cast<AllocatedBlock*>(
|
2023-04-21 18:53:16 +02:00
|
|
|
upstream_->reallocate(block, sizeof(AllocatedBlock) + n - 1));
|
2023-04-03 08:52:38 +02:00
|
|
|
if (block) {
|
2023-04-21 18:53:16 +02:00
|
|
|
log_ << AllocatorLog::Reallocate(oldSize, n);
|
2023-04-07 18:29:54 +02:00
|
|
|
ARDUINOJSON_ASSERT(block->size == oldSize);
|
2023-04-03 08:52:38 +02:00
|
|
|
block->size = n;
|
|
|
|
return block->payload;
|
|
|
|
} else {
|
2023-04-21 18:53:16 +02:00
|
|
|
log_ << AllocatorLog::ReallocateFail(oldSize, n);
|
2023-04-03 08:52:38 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-03-31 14:36:24 +02:00
|
|
|
}
|
|
|
|
|
2023-04-07 18:29:54 +02:00
|
|
|
void clearLog() {
|
2023-04-21 18:53:16 +02:00
|
|
|
log_ = AllocatorLog();
|
2023-04-07 18:29:54 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 10:34:37 +02:00
|
|
|
const AllocatorLog& log() const {
|
2023-04-21 18:53:16 +02:00
|
|
|
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-21 18:53:16 +02:00
|
|
|
AllocatorLog log_;
|
|
|
|
Allocator* upstream_;
|
2023-03-31 14:36:24 +02:00
|
|
|
};
|
2023-04-01 15:06:24 +02:00
|
|
|
|
|
|
|
class ControllableAllocator : public ArduinoJson::Allocator {
|
|
|
|
public:
|
2023-04-02 16:55:02 +02:00
|
|
|
ControllableAllocator(
|
|
|
|
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
2023-04-21 18:53:16 +02:00
|
|
|
: enabled_(true), upstream_(upstream) {}
|
2023-04-01 15:06:24 +02:00
|
|
|
virtual ~ControllableAllocator() {}
|
|
|
|
|
|
|
|
void* allocate(size_t n) override {
|
2023-04-21 18:53:16 +02:00
|
|
|
return enabled_ ? upstream_->allocate(n) : 0;
|
2023-04-01 15:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(void* p) override {
|
2023-04-21 18:53:16 +02:00
|
|
|
upstream_->deallocate(p);
|
2023-04-01 15:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void* reallocate(void* ptr, size_t n) override {
|
2023-04-21 18:53:16 +02:00
|
|
|
return enabled_ ? upstream_->reallocate(ptr, n) : 0;
|
2023-04-01 15:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void disable() {
|
2023-04-21 18:53:16 +02:00
|
|
|
enabled_ = false;
|
2023-04-01 15:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-04-21 18:53:16 +02:00
|
|
|
bool enabled_;
|
|
|
|
Allocator* upstream_;
|
2023-04-01 15:06:24 +02:00
|
|
|
};
|
2023-04-07 18:28:41 +02:00
|
|
|
|
|
|
|
class TimebombAllocator : public ArduinoJson::Allocator {
|
|
|
|
public:
|
|
|
|
TimebombAllocator(
|
|
|
|
size_t initialCountdown,
|
|
|
|
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
2023-04-21 18:53:16 +02:00
|
|
|
: countdown_(initialCountdown), upstream_(upstream) {}
|
2023-04-07 18:28:41 +02:00
|
|
|
virtual ~TimebombAllocator() {}
|
|
|
|
|
|
|
|
void* allocate(size_t n) override {
|
2023-04-21 18:53:16 +02:00
|
|
|
if (!countdown_)
|
2023-04-07 18:28:41 +02:00
|
|
|
return nullptr;
|
2023-04-21 18:53:16 +02:00
|
|
|
countdown_--;
|
|
|
|
return upstream_->allocate(n);
|
2023-04-07 18:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(void* p) override {
|
2023-04-21 18:53:16 +02:00
|
|
|
upstream_->deallocate(p);
|
2023-04-07 18:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void* reallocate(void* ptr, size_t n) override {
|
2023-04-21 18:53:16 +02:00
|
|
|
if (!countdown_)
|
2023-04-07 18:28:41 +02:00
|
|
|
return nullptr;
|
2023-04-21 18:53:16 +02:00
|
|
|
countdown_--;
|
|
|
|
return upstream_->reallocate(ptr, n);
|
2023-04-07 18:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void setCountdown(size_t value) {
|
2023-04-21 18:53:16 +02:00
|
|
|
countdown_ = value;
|
2023-04-07 18:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-04-21 18:53:16 +02:00
|
|
|
size_t countdown_ = 0;
|
|
|
|
Allocator* upstream_;
|
2023-04-07 18:28:41 +02:00
|
|
|
};
|