Skip to content

Commit d95a3bd

Browse files
committed
Tests: add TimebombAllocator
1 parent 22e4f21 commit d95a3bd

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

extras/tests/Helpers/Allocators.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,38 @@ class ControllableAllocator : public ArduinoJson::Allocator {
177177
bool _enabled;
178178
Allocator* _upstream;
179179
};
180+
181+
class TimebombAllocator : public ArduinoJson::Allocator {
182+
public:
183+
TimebombAllocator(
184+
size_t initialCountdown,
185+
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
186+
: _countdown(initialCountdown), _upstream(upstream) {}
187+
virtual ~TimebombAllocator() {}
188+
189+
void* allocate(size_t n) override {
190+
if (!_countdown)
191+
return nullptr;
192+
_countdown--;
193+
return _upstream->allocate(n);
194+
}
195+
196+
void deallocate(void* p) override {
197+
_upstream->deallocate(p);
198+
}
199+
200+
void* reallocate(void* ptr, size_t n) override {
201+
if (!_countdown)
202+
return nullptr;
203+
_countdown--;
204+
return _upstream->reallocate(ptr, n);
205+
}
206+
207+
void setCountdown(size_t value) {
208+
_countdown = value;
209+
}
210+
211+
private:
212+
size_t _countdown = 0;
213+
Allocator* _upstream;
214+
};

0 commit comments

Comments
 (0)