forked from bblanchon/ArduinoJson
Test: change SpyingAllocator
into a decorator
This commit is contained in:
@ -84,12 +84,15 @@ class AllocatorLog {
|
|||||||
|
|
||||||
class SpyingAllocator : public ArduinoJson::Allocator {
|
class SpyingAllocator : public ArduinoJson::Allocator {
|
||||||
public:
|
public:
|
||||||
|
SpyingAllocator(
|
||||||
|
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
||||||
|
: _upstream(upstream) {}
|
||||||
virtual ~SpyingAllocator() {}
|
virtual ~SpyingAllocator() {}
|
||||||
|
|
||||||
void* allocate(size_t n) override {
|
void* allocate(size_t n) override {
|
||||||
_log << AllocatorLog::Allocate(n);
|
_log << AllocatorLog::Allocate(n);
|
||||||
auto block = reinterpret_cast<AllocatedBlock*>(
|
auto block = reinterpret_cast<AllocatedBlock*>(
|
||||||
malloc(sizeof(AllocatedBlock) + n - 1));
|
_upstream->allocate(sizeof(AllocatedBlock) + n - 1));
|
||||||
block->size = n;
|
block->size = n;
|
||||||
return block->payload;
|
return block->payload;
|
||||||
}
|
}
|
||||||
@ -97,14 +100,14 @@ class SpyingAllocator : public ArduinoJson::Allocator {
|
|||||||
void deallocate(void* p) override {
|
void deallocate(void* p) override {
|
||||||
auto block = AllocatedBlock::fromPayload(p);
|
auto block = AllocatedBlock::fromPayload(p);
|
||||||
_log << AllocatorLog::Deallocate(block->size);
|
_log << AllocatorLog::Deallocate(block->size);
|
||||||
free(block);
|
_upstream->deallocate(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
_log << AllocatorLog::Reallocate(block->size, n);
|
||||||
block = reinterpret_cast<AllocatedBlock*>(
|
block = reinterpret_cast<AllocatedBlock*>(
|
||||||
realloc(block, sizeof(AllocatedBlock) + n - 1));
|
_upstream->reallocate(block, sizeof(AllocatedBlock) + n - 1));
|
||||||
block->size = n;
|
block->size = n;
|
||||||
return block->payload;
|
return block->payload;
|
||||||
}
|
}
|
||||||
@ -128,6 +131,7 @@ class SpyingAllocator : public ArduinoJson::Allocator {
|
|||||||
};
|
};
|
||||||
|
|
||||||
AllocatorLog _log;
|
AllocatorLog _log;
|
||||||
|
Allocator* _upstream;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ControllableAllocator : public ArduinoJson::Allocator {
|
class ControllableAllocator : public ArduinoJson::Allocator {
|
||||||
|
Reference in New Issue
Block a user