Rename MemoryPool to ResourceManager

This commit is contained in:
Benoit Blanchon
2023-06-17 16:10:56 +02:00
parent 2a663db3c7
commit 4871380060
36 changed files with 473 additions and 453 deletions

View File

@ -20,7 +20,7 @@ add_subdirectory(JsonDocument)
add_subdirectory(JsonObject)
add_subdirectory(JsonSerializer)
add_subdirectory(JsonVariant)
add_subdirectory(MemoryPool)
add_subdirectory(ResourceManager)
add_subdirectory(Misc)
add_subdirectory(MixedConfiguration)
add_subdirectory(MsgPackDeserializer)

View File

@ -308,8 +308,6 @@ TEST_CASE("deserialize JSON array under memory constraints") {
deserializeJson(doc, " [ \"1234567\" ] ");
REQUIRE(sizeofArray(1) + sizeofString(7) == doc.memoryUsage());
// note: we use a string of 8 bytes to be sure that the MemoryPool
// will not insert bytes to enforce alignement
}
SECTION("Should clear the JsonArray") {

View File

@ -1,51 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Variant/VariantSlot.hpp>
#include <catch.hpp>
#include "Allocators.hpp"
using namespace ArduinoJson::detail;
TEST_CASE("new (pool) VariantSlot()") {
SECTION("Returns different pointer") {
MemoryPool pool(4096);
VariantSlot* s1 = new (&pool) VariantSlot();
REQUIRE(s1 != 0);
VariantSlot* s2 = new (&pool) VariantSlot();
REQUIRE(s2 != 0);
REQUIRE(s1 != s2);
}
SECTION("Returns aligned pointers") {
MemoryPool pool(4096);
REQUIRE(isAligned(new (&pool) VariantSlot()));
REQUIRE(isAligned(new (&pool) VariantSlot()));
}
SECTION("Returns zero if capacity is 0") {
MemoryPool pool(0);
REQUIRE(new (&pool) VariantSlot() == 0);
}
SECTION("Returns zero if buffer is null") {
MemoryPool pool(4096, FailingAllocator::instance());
REQUIRE(new (&pool) VariantSlot() == 0);
}
SECTION("Returns zero if capacity is insufficient") {
MemoryPool pool(sizeof(VariantSlot));
new (&pool) VariantSlot();
REQUIRE(new (&pool) VariantSlot() == 0);
}
}

View File

@ -1,68 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Strings/StringAdapters.hpp>
#include <catch.hpp>
#include "Allocators.hpp"
using namespace ArduinoJson::detail;
static StringNode* saveString(MemoryPool& pool, const char* s) {
return pool.saveString(adaptString(s));
}
static StringNode* saveString(MemoryPool& pool, const char* s, size_t n) {
return pool.saveString(adaptString(s, n));
}
TEST_CASE("MemoryPool::saveString()") {
MemoryPool pool(32);
SECTION("Duplicates different strings") {
auto a = saveString(pool, "hello");
auto b = saveString(pool, "world");
REQUIRE(a->data != b->data);
REQUIRE(a->length == 5);
REQUIRE(b->length == 5);
REQUIRE(a->references == 1);
REQUIRE(b->references == 1);
REQUIRE(pool.size() == 2 * sizeofString(5));
}
SECTION("Deduplicates identical strings") {
auto a = saveString(pool, "hello");
auto b = saveString(pool, "hello");
REQUIRE(a == b);
REQUIRE(a->length == 5);
REQUIRE(a->references == 2);
REQUIRE(pool.size() == sizeofString(5));
}
SECTION("Deduplicates identical strings that contain NUL") {
auto a = saveString(pool, "hello\0world", 11);
auto b = saveString(pool, "hello\0world", 11);
REQUIRE(a == b);
REQUIRE(a->length == 11);
REQUIRE(a->references == 2);
REQUIRE(pool.size() == sizeofString(11));
}
SECTION("Don't stop on first NUL") {
auto a = saveString(pool, "hello");
auto b = saveString(pool, "hello\0world", 11);
REQUIRE(a != b);
REQUIRE(a->length == 5);
REQUIRE(b->length == 11);
REQUIRE(a->references == 1);
REQUIRE(b->references == 1);
REQUIRE(pool.size() == sizeofString(5) + sizeofString(11));
}
SECTION("Returns NULL when allocation fails") {
MemoryPool pool2(32, FailingAllocator::instance());
REQUIRE(saveString(pool2, "a") == nullptr);
}
}

View File

@ -1,35 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Variant/VariantSlot.hpp>
#include <catch.hpp>
using namespace ArduinoJson::detail;
TEST_CASE("MemoryPool::capacity()") {
const size_t capacity = 64;
MemoryPool pool(capacity);
REQUIRE(capacity == pool.capacity());
}
TEST_CASE("MemoryPool::size()") {
MemoryPool pool(4096);
SECTION("Initial size is 0") {
REQUIRE(0 == pool.size());
}
SECTION("Doesn't grow when memory pool is full") {
const size_t variantCount = pool.capacity() / sizeof(VariantSlot);
for (size_t i = 0; i < variantCount; i++)
new (&pool) VariantSlot();
size_t size = pool.size();
new (&pool) VariantSlot();
REQUIRE(size == pool.size());
}
}

View File

@ -10,8 +10,8 @@
using namespace ArduinoJson::detail;
static void testCodepoint(uint32_t codepoint, std::string expected) {
MemoryPool pool(4096);
StringBuilder str(&pool);
ResourceManager resources(4096);
StringBuilder str(&resources);
str.startString();
CAPTURE(codepoint);

View File

@ -2,7 +2,7 @@
# Copyright © 2014-2023, Benoit BLANCHON
# MIT License
add_executable(MemoryPoolTests
add_executable(ResourceManagerTests
allocVariant.cpp
clear.cpp
saveString.cpp
@ -10,9 +10,9 @@ add_executable(MemoryPoolTests
StringBuilder.cpp
)
add_test(MemoryPool MemoryPoolTests)
add_test(ResourceManager ResourceManagerTests)
set_tests_properties(MemoryPool
set_tests_properties(ResourceManager
PROPERTIES
LABELS "Catch"
)

View File

@ -12,16 +12,16 @@ using namespace ArduinoJson::detail;
TEST_CASE("StringBuilder") {
ControllableAllocator controllableAllocator;
SpyingAllocator spyingAllocator(&controllableAllocator);
MemoryPool pool(0, &spyingAllocator);
ResourceManager resources(0, &spyingAllocator);
SECTION("Empty string") {
StringBuilder str(&pool);
StringBuilder str(&resources);
str.startString();
str.save();
REQUIRE(pool.size() == sizeofString(0));
REQUIRE(pool.overflowed() == false);
REQUIRE(resources.size() == sizeofString(0));
REQUIRE(resources.overflowed() == false);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
@ -29,20 +29,20 @@ TEST_CASE("StringBuilder") {
}
SECTION("Short string fits in first allocation") {
StringBuilder str(&pool);
StringBuilder str(&resources);
str.startString();
str.append("hello");
REQUIRE(str.isValid() == true);
REQUIRE(str.str() == "hello");
REQUIRE(pool.overflowed() == false);
REQUIRE(resources.overflowed() == false);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31)));
}
SECTION("Long string needs reallocation") {
StringBuilder str(&pool);
StringBuilder str(&resources);
str.startString();
str.append(
@ -53,7 +53,7 @@ TEST_CASE("StringBuilder") {
REQUIRE(str.str() ==
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
"eiusmod tempor incididunt ut labore et dolore magna aliqua.");
REQUIRE(pool.overflowed() == false);
REQUIRE(resources.overflowed() == false);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
@ -63,7 +63,7 @@ TEST_CASE("StringBuilder") {
}
SECTION("Realloc fails") {
StringBuilder str(&pool);
StringBuilder str(&resources);
str.startString();
controllableAllocator.disable();
@ -77,62 +77,62 @@ TEST_CASE("StringBuilder") {
sizeofString(63))
<< AllocatorLog::Deallocate(sizeofString(31)));
REQUIRE(str.isValid() == false);
REQUIRE(pool.overflowed() == true);
REQUIRE(resources.overflowed() == true);
}
SECTION("Initial allocation fails") {
StringBuilder str(&pool);
StringBuilder str(&resources);
controllableAllocator.disable();
str.startString();
REQUIRE(str.isValid() == false);
REQUIRE(pool.overflowed() == true);
REQUIRE(resources.overflowed() == true);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
}
}
static StringNode* addStringToPool(MemoryPool& pool, const char* s) {
StringBuilder str(&pool);
static StringNode* addStringToPool(ResourceManager& resources, const char* s) {
StringBuilder str(&resources);
str.startString();
str.append(s);
return str.save();
}
TEST_CASE("StringBuilder::save() deduplicates strings") {
MemoryPool pool(4096);
ResourceManager resources(4096);
SECTION("Basic") {
auto s1 = addStringToPool(pool, "hello");
auto s2 = addStringToPool(pool, "world");
auto s3 = addStringToPool(pool, "hello");
auto s1 = addStringToPool(resources, "hello");
auto s2 = addStringToPool(resources, "world");
auto s3 = addStringToPool(resources, "hello");
REQUIRE(s1 == s3);
REQUIRE(s2 != s3);
REQUIRE(s1->references == 2);
REQUIRE(s2->references == 1);
REQUIRE(s3->references == 2);
REQUIRE(pool.size() == 2 * sizeofString(5));
REQUIRE(resources.size() == 2 * sizeofString(5));
}
SECTION("Requires terminator") {
auto s1 = addStringToPool(pool, "hello world");
auto s2 = addStringToPool(pool, "hello");
auto s1 = addStringToPool(resources, "hello world");
auto s2 = addStringToPool(resources, "hello");
REQUIRE(s2 != s1);
REQUIRE(s1->references == 1);
REQUIRE(s2->references == 1);
REQUIRE(pool.size() == sizeofString(11) + sizeofString(5));
REQUIRE(resources.size() == sizeofString(11) + sizeofString(5));
}
SECTION("Don't overrun") {
auto s1 = addStringToPool(pool, "hello world");
auto s2 = addStringToPool(pool, "wor");
auto s1 = addStringToPool(resources, "hello world");
auto s2 = addStringToPool(resources, "wor");
REQUIRE(s2 != s1);
REQUIRE(s1->references == 1);
REQUIRE(s2->references == 1);
REQUIRE(pool.size() == sizeofString(11) + sizeofString(3));
REQUIRE(resources.size() == sizeofString(11) + sizeofString(3));
}
}

View File

@ -0,0 +1,51 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/ResourceManager.hpp>
#include <ArduinoJson/Variant/VariantSlot.hpp>
#include <catch.hpp>
#include "Allocators.hpp"
using namespace ArduinoJson::detail;
TEST_CASE("new (resources) VariantSlot()") {
SECTION("Returns different pointer") {
ResourceManager resources(4096);
VariantSlot* s1 = new (&resources) VariantSlot();
REQUIRE(s1 != 0);
VariantSlot* s2 = new (&resources) VariantSlot();
REQUIRE(s2 != 0);
REQUIRE(s1 != s2);
}
SECTION("Returns aligned pointers") {
ResourceManager resources(4096);
REQUIRE(isAligned(new (&resources) VariantSlot()));
REQUIRE(isAligned(new (&resources) VariantSlot()));
}
SECTION("Returns zero if capacity is 0") {
ResourceManager resources(0);
REQUIRE(new (&resources) VariantSlot() == 0);
}
SECTION("Returns zero if buffer is null") {
ResourceManager resources(4096, FailingAllocator::instance());
REQUIRE(new (&resources) VariantSlot() == 0);
}
SECTION("Returns zero if capacity is insufficient") {
ResourceManager resources(sizeof(VariantSlot));
new (&resources) VariantSlot();
REQUIRE(new (&resources) VariantSlot() == 0);
}
}

View File

@ -2,7 +2,7 @@
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Memory/ResourceManager.hpp>
#include <ArduinoJson/Strings/StringAdapters.hpp>
#include <ArduinoJson/Variant/VariantSlot.hpp>
#include <catch.hpp>
@ -11,22 +11,22 @@ using namespace ArduinoJson::detail;
static const size_t poolCapacity = 512;
TEST_CASE("MemoryPool::clear()") {
MemoryPool pool(poolCapacity);
TEST_CASE("ResourceManager::clear()") {
ResourceManager resources(poolCapacity);
SECTION("Discards allocated variants") {
new (&pool) VariantSlot();
new (&resources) VariantSlot();
pool.clear();
REQUIRE(pool.size() == 0);
resources.clear();
REQUIRE(resources.size() == 0);
}
SECTION("Discards allocated strings") {
pool.saveString(adaptString("123456789"));
REQUIRE(pool.size() == sizeofString(9));
resources.saveString(adaptString("123456789"));
REQUIRE(resources.size() == sizeofString(9));
pool.clear();
resources.clear();
REQUIRE(pool.size() == 0);
REQUIRE(resources.size() == 0);
}
}

View File

@ -0,0 +1,69 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/ResourceManager.hpp>
#include <ArduinoJson/Strings/StringAdapters.hpp>
#include <catch.hpp>
#include "Allocators.hpp"
using namespace ArduinoJson::detail;
static StringNode* saveString(ResourceManager& resources, const char* s) {
return resources.saveString(adaptString(s));
}
static StringNode* saveString(ResourceManager& resources, const char* s,
size_t n) {
return resources.saveString(adaptString(s, n));
}
TEST_CASE("ResourceManager::saveString()") {
ResourceManager resources(32);
SECTION("Duplicates different strings") {
auto a = saveString(resources, "hello");
auto b = saveString(resources, "world");
REQUIRE(a->data != b->data);
REQUIRE(a->length == 5);
REQUIRE(b->length == 5);
REQUIRE(a->references == 1);
REQUIRE(b->references == 1);
REQUIRE(resources.size() == 2 * sizeofString(5));
}
SECTION("Deduplicates identical strings") {
auto a = saveString(resources, "hello");
auto b = saveString(resources, "hello");
REQUIRE(a == b);
REQUIRE(a->length == 5);
REQUIRE(a->references == 2);
REQUIRE(resources.size() == sizeofString(5));
}
SECTION("Deduplicates identical strings that contain NUL") {
auto a = saveString(resources, "hello\0world", 11);
auto b = saveString(resources, "hello\0world", 11);
REQUIRE(a == b);
REQUIRE(a->length == 11);
REQUIRE(a->references == 2);
REQUIRE(resources.size() == sizeofString(11));
}
SECTION("Don't stop on first NUL") {
auto a = saveString(resources, "hello");
auto b = saveString(resources, "hello\0world", 11);
REQUIRE(a != b);
REQUIRE(a->length == 5);
REQUIRE(b->length == 11);
REQUIRE(a->references == 1);
REQUIRE(b->references == 1);
REQUIRE(resources.size() == sizeofString(5) + sizeofString(11));
}
SECTION("Returns NULL when allocation fails") {
ResourceManager pool2(32, FailingAllocator::instance());
REQUIRE(saveString(pool2, "a") == nullptr);
}
}

View File

@ -0,0 +1,35 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Memory/ResourceManager.hpp>
#include <ArduinoJson/Variant/VariantSlot.hpp>
#include <catch.hpp>
using namespace ArduinoJson::detail;
TEST_CASE("ResourceManager::capacity()") {
const size_t capacity = 64;
ResourceManager resources(capacity);
REQUIRE(capacity == resources.capacity());
}
TEST_CASE("ResourceManager::size()") {
ResourceManager resources(4096);
SECTION("Initial size is 0") {
REQUIRE(0 == resources.size());
}
SECTION("Doesn't grow when memory pool is full") {
const size_t variantCount = resources.capacity() / sizeof(VariantSlot);
for (size_t i = 0; i < variantCount; i++)
new (&resources) VariantSlot();
size_t size = resources.size();
new (&resources) VariantSlot();
REQUIRE(size == resources.size());
}
}