Test: include deallocated size in allocator's log

This commit is contained in:
Benoit Blanchon
2023-04-01 10:59:34 +02:00
parent 2eb726b744
commit 57810af2ac
2 changed files with 50 additions and 26 deletions

View File

@ -37,24 +37,27 @@ class AllocatorLog {
}; };
struct Reallocate { struct Reallocate {
Reallocate(size_t s) : size(s) {} Reallocate(size_t s1, size_t s2) : oldSize(s1), newSize(s2) {}
size_t size; size_t oldSize, newSize;
}; };
struct Deallocate {}; struct Deallocate {
Deallocate(size_t s) : size(s) {}
size_t size;
};
AllocatorLog& operator<<(const Allocate& a) { AllocatorLog& operator<<(const Allocate& a) {
_log << "allocate(" << a.size << ")\n"; _log << "allocate(" << a.size << ")\n";
return *this; return *this;
} }
AllocatorLog& operator<<(const Deallocate&) { AllocatorLog& operator<<(const Deallocate& d) {
_log << "deallocate()\n"; _log << "deallocate(" << d.size << ")\n";
return *this; return *this;
} }
AllocatorLog& operator<<(const Reallocate& a) { AllocatorLog& operator<<(const Reallocate& a) {
_log << "reallocate(" << a.size << ")\n"; _log << "reallocate(" << a.oldSize << ", " << a.newSize << ")\n";
return *this; return *this;
} }
@ -85,17 +88,25 @@ class SpyingAllocator : public ArduinoJson::Allocator {
void* allocate(size_t n) override { void* allocate(size_t n) override {
_log << AllocatorLog::Allocate(n); _log << AllocatorLog::Allocate(n);
return malloc(n); auto block = reinterpret_cast<AllocatedBlock*>(
malloc(sizeof(AllocatedBlock) + n - 1));
block->size = n;
return block->payload;
} }
void deallocate(void* p) override { void deallocate(void* p) override {
_log << AllocatorLog::Deallocate(); auto block = AllocatedBlock::fromPayload(p);
free(p); _log << AllocatorLog::Deallocate(block->size);
free(block);
} }
void* reallocate(void* ptr, size_t n) override { void* reallocate(void* p, size_t n) override {
_log << AllocatorLog::Reallocate(n); auto block = AllocatedBlock::fromPayload(p);
return realloc(ptr, n); _log << AllocatorLog::Reallocate(block->size, n);
block = reinterpret_cast<AllocatedBlock*>(
realloc(block, sizeof(AllocatedBlock) + n - 1));
block->size = n;
return block->payload;
} }
const AllocatorLog& log() const { const AllocatorLog& log() const {
@ -103,5 +114,18 @@ class SpyingAllocator : public ArduinoJson::Allocator {
} }
private: private:
struct AllocatedBlock {
size_t size;
char payload[1];
static AllocatedBlock* fromPayload(void* p) {
return reinterpret_cast<AllocatedBlock*>(
// Cast to void* to silence "cast increases required alignment of
// target type [-Werror=cast-align]"
reinterpret_cast<void*>(reinterpret_cast<char*>(p) -
offsetof(AllocatedBlock, payload)));
}
};
AllocatorLog _log; AllocatorLog _log;
}; };

View File

@ -44,7 +44,7 @@ TEST_CASE("JsonDocument's allocator") {
{ JsonDocument doc(4096, &spyingAllocator); } { JsonDocument doc(4096, &spyingAllocator); }
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(4096)
<< AllocatorLog::Deallocate()); << AllocatorLog::Deallocate(4096));
} }
SECTION("Copy construct") { SECTION("Copy construct") {
@ -61,8 +61,8 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(4096)
<< AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(4096)
<< AllocatorLog::Deallocate() << AllocatorLog::Deallocate(4096)
<< AllocatorLog::Deallocate()); << AllocatorLog::Deallocate(4096));
} }
SECTION("Move construct") { SECTION("Move construct") {
@ -79,7 +79,7 @@ TEST_CASE("JsonDocument's allocator") {
} }
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(4096)
<< AllocatorLog::Deallocate()); << AllocatorLog::Deallocate(4096));
} }
SECTION("Copy assign larger") { SECTION("Copy assign larger") {
@ -97,10 +97,10 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(4096)
<< AllocatorLog::Allocate(8) << AllocatorLog::Allocate(8)
<< AllocatorLog::Deallocate() << AllocatorLog::Deallocate(8)
<< AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(4096)
<< AllocatorLog::Deallocate() << AllocatorLog::Deallocate(4096)
<< AllocatorLog::Deallocate()); << AllocatorLog::Deallocate(4096));
} }
SECTION("Copy assign smaller") { SECTION("Copy assign smaller") {
@ -118,10 +118,10 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(1024) << AllocatorLog::Allocate(1024)
<< AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(4096)
<< AllocatorLog::Deallocate() << AllocatorLog::Deallocate(4096)
<< AllocatorLog::Allocate(1024) << AllocatorLog::Allocate(1024)
<< AllocatorLog::Deallocate() << AllocatorLog::Deallocate(1024)
<< AllocatorLog::Deallocate()); << AllocatorLog::Deallocate(1024));
} }
SECTION("Copy assign same size") { SECTION("Copy assign same size") {
@ -139,8 +139,8 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(1024) << AllocatorLog::Allocate(1024)
<< AllocatorLog::Allocate(1024) << AllocatorLog::Allocate(1024)
<< AllocatorLog::Deallocate() << AllocatorLog::Deallocate(1024)
<< AllocatorLog::Deallocate()); << AllocatorLog::Deallocate(1024));
} }
SECTION("Move assign") { SECTION("Move assign") {
@ -159,8 +159,8 @@ TEST_CASE("JsonDocument's allocator") {
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(4096) << AllocatorLog::Allocate(4096)
<< AllocatorLog::Allocate(8) << AllocatorLog::Allocate(8)
<< AllocatorLog::Deallocate() << AllocatorLog::Deallocate(8)
<< AllocatorLog::Deallocate()); << AllocatorLog::Deallocate(4096));
} }
SECTION("garbageCollect()") { SECTION("garbageCollect()") {