Store index of slot in the pool instead of a pointer or a distance

This commit is contained in:
Benoit Blanchon
2023-06-14 11:57:31 +02:00
parent 068c40d6ed
commit c4e5051a7a
44 changed files with 343 additions and 310 deletions

View File

@ -4,8 +4,7 @@ static_assert(ARDUINOJSON_ENABLE_PROGMEM == 1, "ARDUINOJSON_ENABLE_PROGMEM");
static_assert(ARDUINOJSON_USE_LONG_LONG == 0, "ARDUINOJSON_USE_LONG_LONG");
static_assert(ARDUINOJSON_SLOT_OFFSET_SIZE == 1,
"ARDUINOJSON_SLOT_OFFSET_SIZE");
static_assert(ARDUINOJSON_SLOT_ID_SIZE == 1, "ARDUINOJSON_SLOT_ID_SIZE");
static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");

View File

@ -2,8 +2,7 @@
static_assert(ARDUINOJSON_USE_LONG_LONG == 1, "ARDUINOJSON_USE_LONG_LONG");
static_assert(ARDUINOJSON_SLOT_OFFSET_SIZE == 2,
"ARDUINOJSON_SLOT_OFFSET_SIZE");
static_assert(ARDUINOJSON_SLOT_ID_SIZE == 2, "ARDUINOJSON_SLOT_ID_SIZE");
static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");

View File

@ -2,14 +2,13 @@
static_assert(ARDUINOJSON_USE_LONG_LONG == 1, "ARDUINOJSON_USE_LONG_LONG");
static_assert(ARDUINOJSON_SLOT_OFFSET_SIZE == 4,
"ARDUINOJSON_SLOT_OFFSET_SIZE");
static_assert(ARDUINOJSON_SLOT_ID_SIZE == 4, "ARDUINOJSON_SLOT_ID_SIZE");
static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
static_assert(sizeof(ArduinoJson::detail::VariantSlot) == 32,
static_assert(sizeof(ArduinoJson::detail::VariantSlot) == 24,
"sizeof(VariantSlot)");
int main() {}

View File

@ -2,8 +2,7 @@
static_assert(ARDUINOJSON_USE_LONG_LONG == 1, "ARDUINOJSON_USE_LONG_LONG");
static_assert(ARDUINOJSON_SLOT_OFFSET_SIZE == 2,
"ARDUINOJSON_SLOT_OFFSET_SIZE");
static_assert(ARDUINOJSON_SLOT_ID_SIZE == 2, "ARDUINOJSON_SLOT_ID_SIZE");
static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");

View File

@ -31,7 +31,8 @@ TEST_CASE("JsonDocument assignment") {
}
SECTION("Copy assignment reallocates when capacity is smaller") {
JsonDocument doc1(4096, &spyingAllocator);
const size_t capacity = 100 * sizeof(ArduinoJson::detail::VariantSlot);
JsonDocument doc1(capacity, &spyingAllocator);
deserializeJson(doc1, "[{\"hello\":\"world\"}]");
JsonDocument doc2(sizeofArray(1), &spyingAllocator);
spyingAllocator.clearLog();
@ -41,14 +42,15 @@ TEST_CASE("JsonDocument assignment") {
REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Deallocate(sizeofArray(1))
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Allocate(capacity)
<< AllocatorLog::Allocate(sizeofString(5)) // hello
<< AllocatorLog::Allocate(sizeofString(5)) // world
);
}
SECTION("Copy assignment reallocates when capacity is larger") {
JsonDocument doc1(1024, &spyingAllocator);
const size_t capacity1 = 100 * sizeof(ArduinoJson::detail::VariantSlot);
JsonDocument doc1(capacity1, &spyingAllocator);
deserializeJson(doc1, "{\"hello\":\"world\"}");
JsonDocument doc2(4096, &spyingAllocator);
spyingAllocator.clearLog();
@ -58,7 +60,7 @@ TEST_CASE("JsonDocument assignment") {
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Deallocate(4096)
<< AllocatorLog::Allocate(1024)
<< AllocatorLog::Allocate(capacity1)
<< AllocatorLog::Allocate(sizeofString(5)) // hello
<< AllocatorLog::Allocate(sizeofString(5)) // world
);

View File

@ -22,8 +22,9 @@ TEST_CASE("JsonDocument constructor") {
}
SECTION("JsonDocument(const JsonDocument&)") {
const size_t capacity = 100 * sizeof(ArduinoJson::detail::VariantSlot);
{
JsonDocument doc1(4096, &spyingAllocator);
JsonDocument doc1(capacity, &spyingAllocator);
doc1.set(std::string("The size of this string is 32!!"));
JsonDocument doc2(doc1);
@ -32,14 +33,14 @@ TEST_CASE("JsonDocument constructor") {
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
}
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
AllocatorLog() << AllocatorLog::Allocate(capacity)
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Allocate(4096)
<< AllocatorLog::Allocate(capacity)
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Deallocate(sizeofString(31))
<< AllocatorLog::Deallocate(4096)
<< AllocatorLog::Deallocate(capacity)
<< AllocatorLog::Deallocate(sizeofString(31))
<< AllocatorLog::Deallocate(4096));
<< AllocatorLog::Deallocate(capacity));
}
SECTION("JsonDocument(JsonDocument&&)") {

View File

@ -13,9 +13,10 @@ using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonDocument::garbageCollect()") {
const size_t capacity = 100 * sizeof(ArduinoJson::detail::VariantSlot);
ControllableAllocator controllableAllocator;
SpyingAllocator spyingAllocator(&controllableAllocator);
JsonDocument doc(4096, &spyingAllocator);
JsonDocument doc(capacity, &spyingAllocator);
SECTION("when allocation succeeds") {
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
@ -29,10 +30,10 @@ TEST_CASE("JsonDocument::garbageCollect()") {
REQUIRE(doc.memoryUsage() == sizeofObject(1) + sizeofString(7));
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(4096)
AllocatorLog() << AllocatorLog::Allocate(capacity)
<< AllocatorLog::Allocate(sizeofString(7))
<< AllocatorLog::Deallocate(sizeofString(7))
<< AllocatorLog::Deallocate(4096));
<< AllocatorLog::Deallocate(capacity));
}
SECTION("when allocation fails") {
@ -49,7 +50,7 @@ TEST_CASE("JsonDocument::garbageCollect()") {
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::AllocateFail(4096)
AllocatorLog() << AllocatorLog::AllocateFail(capacity)
<< AllocatorLog::AllocateFail(sizeofString(7)));
}
}

View File

@ -2,6 +2,8 @@
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#define ARDUINOJSON_SLOT_ID_SIZE 4 // required to reach 65536 elements
#include <ArduinoJson.h>
#include <catch.hpp>
@ -55,6 +57,7 @@ TEST_CASE("serialize MsgPack array") {
const char* nil = 0;
for (int i = 0; i < 65536; i++)
array.add(nil);
REQUIRE(array.size() == 65536);
check(array,
std::string("\xDD\x00\x01\x00\x00", 5) + std::string(65536, '\xc0'));

View File

@ -11,13 +11,13 @@
using namespace ArduinoJson::detail;
TEST_CASE("ResourceManager::allocVariant()") {
TEST_CASE("ResourceManager::allocSlot()") {
SECTION("Returns different pointer") {
ResourceManager resources(4096);
VariantSlot* s1 = resources.allocVariant();
VariantSlot* s1 = resources.allocSlot();
REQUIRE(s1 != 0);
VariantSlot* s2 = resources.allocVariant();
VariantSlot* s2 = resources.allocSlot();
REQUIRE(s2 != 0);
REQUIRE(s1 != s2);
@ -26,27 +26,33 @@ TEST_CASE("ResourceManager::allocVariant()") {
SECTION("Returns aligned pointers") {
ResourceManager resources(4096);
REQUIRE(isAligned(resources.allocVariant()));
REQUIRE(isAligned(resources.allocVariant()));
REQUIRE(isAligned(resources.allocSlot().operator VariantSlot*()));
REQUIRE(isAligned(resources.allocSlot().operator VariantSlot*()));
}
SECTION("Returns zero if capacity is 0") {
ResourceManager resources(0);
REQUIRE(resources.allocVariant() == 0);
auto variant = resources.allocSlot();
REQUIRE(variant.id() == NULL_SLOT);
REQUIRE(static_cast<VariantSlot*>(variant) == nullptr);
}
SECTION("Returns zero if buffer is null") {
ResourceManager resources(4096, FailingAllocator::instance());
REQUIRE(resources.allocVariant() == 0);
auto variant = resources.allocSlot();
REQUIRE(variant.id() == NULL_SLOT);
REQUIRE(static_cast<VariantSlot*>(variant) == nullptr);
}
SECTION("Returns zero if capacity is insufficient") {
ResourceManager resources(sizeof(VariantSlot));
resources.allocVariant();
resources.allocSlot();
REQUIRE(resources.allocVariant() == 0);
auto variant = resources.allocSlot();
REQUIRE(variant.id() == NULL_SLOT);
REQUIRE(static_cast<VariantSlot*>(variant) == nullptr);
}
}

View File

@ -15,7 +15,7 @@ TEST_CASE("ResourceManager::clear()") {
ResourceManager resources(poolCapacity);
SECTION("Discards allocated variants") {
resources.allocVariant();
resources.allocSlot();
resources.clear();
REQUIRE(resources.size() == 0);

View File

@ -9,7 +9,7 @@
using namespace ArduinoJson::detail;
TEST_CASE("ResourceManager::capacity()") {
const size_t capacity = 64;
const size_t capacity = 4 * sizeof(VariantSlot);
ResourceManager resources(capacity);
REQUIRE(capacity == resources.capacity());
}
@ -25,10 +25,10 @@ TEST_CASE("ResourceManager::size()") {
const size_t variantCount = resources.capacity() / sizeof(VariantSlot);
for (size_t i = 0; i < variantCount; i++)
resources.allocVariant();
resources.allocSlot();
size_t size = resources.size();
resources.allocVariant();
resources.allocSlot();
REQUIRE(size == resources.size());
}