forked from bblanchon/ArduinoJson
Test: Support failed allocations in SpyingAllocator
This commit is contained in:
@ -31,33 +31,38 @@ struct FailingAllocator : ArduinoJson::Allocator {
|
|||||||
|
|
||||||
class AllocatorLog {
|
class AllocatorLog {
|
||||||
public:
|
public:
|
||||||
struct Allocate {
|
static std::string Allocate(size_t s) {
|
||||||
Allocate(size_t s) : size(s) {}
|
char buffer[32];
|
||||||
size_t size;
|
sprintf(buffer, "allocate(%zu)", s);
|
||||||
};
|
return buffer;
|
||||||
|
|
||||||
struct Reallocate {
|
|
||||||
Reallocate(size_t s1, size_t s2) : oldSize(s1), newSize(s2) {}
|
|
||||||
size_t oldSize, newSize;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Deallocate {
|
|
||||||
Deallocate(size_t s) : size(s) {}
|
|
||||||
size_t size;
|
|
||||||
};
|
|
||||||
|
|
||||||
AllocatorLog& operator<<(const Allocate& a) {
|
|
||||||
_log << "allocate(" << a.size << ")\n";
|
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AllocatorLog& operator<<(const Deallocate& d) {
|
static std::string AllocateFail(size_t s) {
|
||||||
_log << "deallocate(" << d.size << ")\n";
|
char buffer[32];
|
||||||
return *this;
|
sprintf(buffer, "allocate(%zu) -> nullptr", s);
|
||||||
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
AllocatorLog& operator<<(const Reallocate& a) {
|
static std::string Reallocate(size_t s1, size_t s2) {
|
||||||
_log << "reallocate(" << a.oldSize << ", " << a.newSize << ")\n";
|
char buffer[32];
|
||||||
|
sprintf(buffer, "reallocate(%zu, %zu)", s1, s2);
|
||||||
|
return buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::string ReallocateFail(size_t s1, size_t s2) {
|
||||||
|
char buffer[32];
|
||||||
|
sprintf(buffer, "reallocate(%zu, %zu) -> nullptr", s1, s2);
|
||||||
|
return buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::string Deallocate(size_t s) {
|
||||||
|
char buffer[32];
|
||||||
|
sprintf(buffer, "deallocate(%zu)", s);
|
||||||
|
return buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
AllocatorLog& operator<<(const std::string& s) {
|
||||||
|
_log << s << "\n";
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,11 +95,16 @@ class SpyingAllocator : public ArduinoJson::Allocator {
|
|||||||
virtual ~SpyingAllocator() {}
|
virtual ~SpyingAllocator() {}
|
||||||
|
|
||||||
void* allocate(size_t n) override {
|
void* allocate(size_t n) override {
|
||||||
_log << AllocatorLog::Allocate(n);
|
|
||||||
auto block = reinterpret_cast<AllocatedBlock*>(
|
auto block = reinterpret_cast<AllocatedBlock*>(
|
||||||
_upstream->allocate(sizeof(AllocatedBlock) + n - 1));
|
_upstream->allocate(sizeof(AllocatedBlock) + n - 1));
|
||||||
|
if (block) {
|
||||||
|
_log << AllocatorLog::Allocate(n);
|
||||||
block->size = n;
|
block->size = n;
|
||||||
return block->payload;
|
return block->payload;
|
||||||
|
} else {
|
||||||
|
_log << AllocatorLog::AllocateFail(n);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocate(void* p) override {
|
void deallocate(void* p) override {
|
||||||
@ -105,11 +115,17 @@ class SpyingAllocator : public ArduinoJson::Allocator {
|
|||||||
|
|
||||||
void* reallocate(void* p, size_t n) override {
|
void* reallocate(void* p, size_t n) override {
|
||||||
auto block = AllocatedBlock::fromPayload(p);
|
auto block = AllocatedBlock::fromPayload(p);
|
||||||
_log << AllocatorLog::Reallocate(block->size, n);
|
auto oldSize = block->size;
|
||||||
block = reinterpret_cast<AllocatedBlock*>(
|
block = reinterpret_cast<AllocatedBlock*>(
|
||||||
_upstream->reallocate(block, sizeof(AllocatedBlock) + n - 1));
|
_upstream->reallocate(block, sizeof(AllocatedBlock) + n - 1));
|
||||||
|
if (block) {
|
||||||
|
_log << AllocatorLog::Reallocate(oldSize, n);
|
||||||
block->size = n;
|
block->size = n;
|
||||||
return block->payload;
|
return block->payload;
|
||||||
|
} else {
|
||||||
|
_log << AllocatorLog::ReallocateFail(oldSize, n);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const AllocatorLog& log() const {
|
const AllocatorLog& log() const {
|
||||||
|
Reference in New Issue
Block a user