Skip to content

Commit acd465b

Browse files
committed
Test: change ControllableAllocator into a decorator
1 parent e858570 commit acd465b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

extras/tests/Helpers/Allocators.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,21 @@ class SpyingAllocator : public ArduinoJson::Allocator {
136136

137137
class ControllableAllocator : public ArduinoJson::Allocator {
138138
public:
139-
ControllableAllocator() : _enabled(true) {}
139+
ControllableAllocator(
140+
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
141+
: _enabled(true), _upstream(upstream) {}
140142
virtual ~ControllableAllocator() {}
141143

142144
void* allocate(size_t n) override {
143-
return _enabled ? malloc(n) : 0;
145+
return _enabled ? _upstream->allocate(n) : 0;
144146
}
145147

146148
void deallocate(void* p) override {
147-
free(p);
149+
_upstream->deallocate(p);
148150
}
149151

150152
void* reallocate(void* ptr, size_t n) override {
151-
return realloc(ptr, n);
153+
return _upstream->reallocate(ptr, n);
152154
}
153155

154156
void disable() {
@@ -157,4 +159,5 @@ class ControllableAllocator : public ArduinoJson::Allocator {
157159

158160
private:
159161
bool _enabled;
162+
Allocator* _upstream;
160163
};

0 commit comments

Comments
 (0)