Skip to content

Commit

Permalink
Test: include deallocated size in allocator's log
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Apr 1, 2023
1 parent 0e75737 commit b309466
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
45 changes: 33 additions & 12 deletions extras/tests/Helpers/Allocators.hpp
Expand Up @@ -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;
}

Expand Down Expand Up @@ -83,23 +86,41 @@ class SpyingAllocator : public ArduinoJson::Allocator {

void* allocate(size_t n) override {
_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 {
_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<AllocatedBlock*>(
realloc(block, sizeof(AllocatedBlock) + n - 1));
block->size = n;
return block->payload;
}

const AllocatorLog& log() const {
return _log;
}

private:
struct AllocatedBlock {
size_t size;
char payload[1];

static AllocatedBlock* fromPayload(void* p) {
return reinterpret_cast<AllocatedBlock*>(
reinterpret_cast<char*>(p) - offsetof(AllocatedBlock, payload));
}
};

AllocatorLog _log;
};
28 changes: 14 additions & 14 deletions extras/tests/JsonDocument/allocator.cpp
Expand Up @@ -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") {
Expand All @@ -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") {
Expand All @@ -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") {
Expand All @@ -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") {
Expand All @@ -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") {
Expand All @@ -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") {
Expand All @@ -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()") {
Expand Down

0 comments on commit b309466

Please sign in to comment.