diff --git a/extras/tests/Helpers/Allocators.hpp b/extras/tests/Helpers/Allocators.hpp index a4b0793a2..6d35b3a9a 100644 --- a/extras/tests/Helpers/Allocators.hpp +++ b/extras/tests/Helpers/Allocators.hpp @@ -37,24 +37,27 @@ class AllocatorLog { }; struct Reallocate { - Reallocate(size_t s) : size(s) {} - size_t size; + Reallocate(size_t s1, size_t s2) : oldSize(s1), newSize(s2) {} + size_t oldSize, newSize; }; - struct Deallocate {}; + struct Deallocate { + Deallocate(size_t s) : size(s) {} + size_t size; + }; AllocatorLog& operator<<(const Allocate& a) { _log << "allocate(" << a.size << ")\n"; return *this; } - AllocatorLog& operator<<(const Deallocate&) { - _log << "deallocate()\n"; + AllocatorLog& operator<<(const Deallocate& d) { + _log << "deallocate(" << d.size << ")\n"; return *this; } AllocatorLog& operator<<(const Reallocate& a) { - _log << "reallocate(" << a.size << ")\n"; + _log << "reallocate(" << a.oldSize << ", " << a.newSize << ")\n"; return *this; } @@ -83,17 +86,25 @@ class SpyingAllocator : public ArduinoJson::Allocator { void* allocate(size_t n) override { _log << AllocatorLog::Allocate(n); - return malloc(n); + auto block = reinterpret_cast( + malloc(sizeof(AllocatedBlock) + n - 1)); + block->size = n; + return block->payload; } void deallocate(void* p) override { - _log << AllocatorLog::Deallocate(); - free(p); + auto block = AllocatedBlock::fromPayload(p); + _log << AllocatorLog::Deallocate(block->size); + free(block); } - void* reallocate(void* ptr, size_t n) override { - _log << AllocatorLog::Reallocate(n); - return realloc(ptr, n); + void* reallocate(void* p, size_t n) override { + auto block = AllocatedBlock::fromPayload(p); + _log << AllocatorLog::Reallocate(block->size, n); + block = reinterpret_cast( + realloc(block, sizeof(AllocatedBlock) + n - 1)); + block->size = n; + return block->payload; } const AllocatorLog& log() const { @@ -101,5 +112,15 @@ class SpyingAllocator : public ArduinoJson::Allocator { } private: + struct AllocatedBlock { + size_t size; + char payload[1]; + + static AllocatedBlock* fromPayload(void* p) { + return reinterpret_cast( + reinterpret_cast(p) - offsetof(AllocatedBlock, payload)); + } + }; + AllocatorLog _log; }; diff --git a/extras/tests/JsonDocument/allocator.cpp b/extras/tests/JsonDocument/allocator.cpp index 472b08307..62be1ce53 100644 --- a/extras/tests/JsonDocument/allocator.cpp +++ b/extras/tests/JsonDocument/allocator.cpp @@ -44,7 +44,7 @@ TEST_CASE("JsonDocument's allocator") { { JsonDocument doc(4096, &spyingAllocator); } REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(4096) - << AllocatorLog::Deallocate()); + << AllocatorLog::Deallocate(4096)); } SECTION("Copy construct") { @@ -61,8 +61,8 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(4096) - << AllocatorLog::Deallocate() - << AllocatorLog::Deallocate()); + << AllocatorLog::Deallocate(4096) + << AllocatorLog::Deallocate(4096)); } SECTION("Move construct") { @@ -79,7 +79,7 @@ TEST_CASE("JsonDocument's allocator") { } REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(4096) - << AllocatorLog::Deallocate()); + << AllocatorLog::Deallocate(4096)); } SECTION("Copy assign larger") { @@ -97,10 +97,10 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(8) - << AllocatorLog::Deallocate() + << AllocatorLog::Deallocate(8) << AllocatorLog::Allocate(4096) - << AllocatorLog::Deallocate() - << AllocatorLog::Deallocate()); + << AllocatorLog::Deallocate(4096) + << AllocatorLog::Deallocate(4096)); } SECTION("Copy assign smaller") { @@ -118,10 +118,10 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(1024) << AllocatorLog::Allocate(4096) - << AllocatorLog::Deallocate() + << AllocatorLog::Deallocate(4096) << AllocatorLog::Allocate(1024) - << AllocatorLog::Deallocate() - << AllocatorLog::Deallocate()); + << AllocatorLog::Deallocate(1024) + << AllocatorLog::Deallocate(1024)); } SECTION("Copy assign same size") { @@ -139,8 +139,8 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(1024) << AllocatorLog::Allocate(1024) - << AllocatorLog::Deallocate() - << AllocatorLog::Deallocate()); + << AllocatorLog::Deallocate(1024) + << AllocatorLog::Deallocate(1024)); } SECTION("Move assign") { @@ -159,8 +159,8 @@ TEST_CASE("JsonDocument's allocator") { REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(8) - << AllocatorLog::Deallocate() - << AllocatorLog::Deallocate()); + << AllocatorLog::Deallocate(8) + << AllocatorLog::Deallocate(4096)); } SECTION("garbageCollect()") {