Skip to content

Commit e858570

Browse files
committed
Test: change SpyingAllocator into a decorator
1 parent 0643c2e commit e858570

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

extras/tests/Helpers/Allocators.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,30 @@ class AllocatorLog {
8484

8585
class SpyingAllocator : public ArduinoJson::Allocator {
8686
public:
87+
SpyingAllocator(
88+
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
89+
: _upstream(upstream) {}
8790
virtual ~SpyingAllocator() {}
8891

8992
void* allocate(size_t n) override {
9093
_log << AllocatorLog::Allocate(n);
9194
auto block = reinterpret_cast<AllocatedBlock*>(
92-
malloc(sizeof(AllocatedBlock) + n - 1));
95+
_upstream->allocate(sizeof(AllocatedBlock) + n - 1));
9396
block->size = n;
9497
return block->payload;
9598
}
9699

97100
void deallocate(void* p) override {
98101
auto block = AllocatedBlock::fromPayload(p);
99102
_log << AllocatorLog::Deallocate(block->size);
100-
free(block);
103+
_upstream->deallocate(block);
101104
}
102105

103106
void* reallocate(void* p, size_t n) override {
104107
auto block = AllocatedBlock::fromPayload(p);
105108
_log << AllocatorLog::Reallocate(block->size, n);
106109
block = reinterpret_cast<AllocatedBlock*>(
107-
realloc(block, sizeof(AllocatedBlock) + n - 1));
110+
_upstream->reallocate(block, sizeof(AllocatedBlock) + n - 1));
108111
block->size = n;
109112
return block->payload;
110113
}
@@ -128,6 +131,7 @@ class SpyingAllocator : public ArduinoJson::Allocator {
128131
};
129132

130133
AllocatorLog _log;
134+
Allocator* _upstream;
131135
};
132136

133137
class ControllableAllocator : public ArduinoJson::Allocator {

0 commit comments

Comments
 (0)