From d95a3bd19a92ae1cd93545e40930ab574a9388b4 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 7 Apr 2023 18:28:41 +0200 Subject: [PATCH] Tests: add TimebombAllocator --- extras/tests/Helpers/Allocators.hpp | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/extras/tests/Helpers/Allocators.hpp b/extras/tests/Helpers/Allocators.hpp index cbbc3e22..ab4bbea1 100644 --- a/extras/tests/Helpers/Allocators.hpp +++ b/extras/tests/Helpers/Allocators.hpp @@ -177,3 +177,38 @@ class ControllableAllocator : public ArduinoJson::Allocator { bool _enabled; Allocator* _upstream; }; + +class TimebombAllocator : public ArduinoJson::Allocator { + public: + TimebombAllocator( + size_t initialCountdown, + Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance()) + : _countdown(initialCountdown), _upstream(upstream) {} + virtual ~TimebombAllocator() {} + + void* allocate(size_t n) override { + if (!_countdown) + return nullptr; + _countdown--; + return _upstream->allocate(n); + } + + void deallocate(void* p) override { + _upstream->deallocate(p); + } + + void* reallocate(void* ptr, size_t n) override { + if (!_countdown) + return nullptr; + _countdown--; + return _upstream->reallocate(ptr, n); + } + + void setCountdown(size_t value) { + _countdown = value; + } + + private: + size_t _countdown = 0; + Allocator* _upstream; +};