diff --git a/extras/tests/CMakeLists.txt b/extras/tests/CMakeLists.txt index c62401f9..fdab9b61 100644 --- a/extras/tests/CMakeLists.txt +++ b/extras/tests/CMakeLists.txt @@ -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) diff --git a/extras/tests/JsonDeserializer/array.cpp b/extras/tests/JsonDeserializer/array.cpp index 6ed08774..dd4ced02 100644 --- a/extras/tests/JsonDeserializer/array.cpp +++ b/extras/tests/JsonDeserializer/array.cpp @@ -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") { diff --git a/extras/tests/MemoryPool/allocVariant.cpp b/extras/tests/MemoryPool/allocVariant.cpp deleted file mode 100644 index da44965a..00000000 --- a/extras/tests/MemoryPool/allocVariant.cpp +++ /dev/null @@ -1,51 +0,0 @@ -// ArduinoJson - https://arduinojson.org -// Copyright © 2014-2023, Benoit BLANCHON -// MIT License - -#include -#include -#include - -#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); - } -} diff --git a/extras/tests/MemoryPool/saveString.cpp b/extras/tests/MemoryPool/saveString.cpp deleted file mode 100644 index a78a2470..00000000 --- a/extras/tests/MemoryPool/saveString.cpp +++ /dev/null @@ -1,68 +0,0 @@ -// ArduinoJson - https://arduinojson.org -// Copyright © 2014-2023, Benoit BLANCHON -// MIT License - -#include -#include -#include - -#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); - } -} diff --git a/extras/tests/MemoryPool/size.cpp b/extras/tests/MemoryPool/size.cpp deleted file mode 100644 index 78fcda85..00000000 --- a/extras/tests/MemoryPool/size.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// ArduinoJson - https://arduinojson.org -// Copyright © 2014-2023, Benoit BLANCHON -// MIT License - -#include -#include -#include - -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()); - } -} diff --git a/extras/tests/Misc/Utf8.cpp b/extras/tests/Misc/Utf8.cpp index 35936e31..f845bc5b 100644 --- a/extras/tests/Misc/Utf8.cpp +++ b/extras/tests/Misc/Utf8.cpp @@ -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); diff --git a/extras/tests/MemoryPool/CMakeLists.txt b/extras/tests/ResourceManager/CMakeLists.txt similarity index 63% rename from extras/tests/MemoryPool/CMakeLists.txt rename to extras/tests/ResourceManager/CMakeLists.txt index 7c5e8c98..214d5b82 100644 --- a/extras/tests/MemoryPool/CMakeLists.txt +++ b/extras/tests/ResourceManager/CMakeLists.txt @@ -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" ) diff --git a/extras/tests/MemoryPool/StringBuilder.cpp b/extras/tests/ResourceManager/StringBuilder.cpp similarity index 73% rename from extras/tests/MemoryPool/StringBuilder.cpp rename to extras/tests/ResourceManager/StringBuilder.cpp index 34568462..74712b28 100644 --- a/extras/tests/MemoryPool/StringBuilder.cpp +++ b/extras/tests/ResourceManager/StringBuilder.cpp @@ -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)); } } diff --git a/extras/tests/ResourceManager/allocVariant.cpp b/extras/tests/ResourceManager/allocVariant.cpp new file mode 100644 index 00000000..4cb88d6a --- /dev/null +++ b/extras/tests/ResourceManager/allocVariant.cpp @@ -0,0 +1,51 @@ +// ArduinoJson - https://arduinojson.org +// Copyright © 2014-2023, Benoit BLANCHON +// MIT License + +#include +#include +#include + +#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); + } +} diff --git a/extras/tests/MemoryPool/clear.cpp b/extras/tests/ResourceManager/clear.cpp similarity index 50% rename from extras/tests/MemoryPool/clear.cpp rename to extras/tests/ResourceManager/clear.cpp index ffe00777..8c7a1200 100644 --- a/extras/tests/MemoryPool/clear.cpp +++ b/extras/tests/ResourceManager/clear.cpp @@ -2,7 +2,7 @@ // Copyright © 2014-2023, Benoit BLANCHON // MIT License -#include +#include #include #include #include @@ -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); } } diff --git a/extras/tests/ResourceManager/saveString.cpp b/extras/tests/ResourceManager/saveString.cpp new file mode 100644 index 00000000..ff599512 --- /dev/null +++ b/extras/tests/ResourceManager/saveString.cpp @@ -0,0 +1,69 @@ +// ArduinoJson - https://arduinojson.org +// Copyright © 2014-2023, Benoit BLANCHON +// MIT License + +#include +#include +#include + +#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); + } +} diff --git a/extras/tests/ResourceManager/size.cpp b/extras/tests/ResourceManager/size.cpp new file mode 100644 index 00000000..da3a9903 --- /dev/null +++ b/extras/tests/ResourceManager/size.cpp @@ -0,0 +1,35 @@ +// ArduinoJson - https://arduinojson.org +// Copyright © 2014-2023, Benoit BLANCHON +// MIT License + +#include +#include +#include + +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()); + } +} diff --git a/src/ArduinoJson/Array/ElementProxy.hpp b/src/ArduinoJson/Array/ElementProxy.hpp index d6e9aa5d..6f862aa6 100644 --- a/src/ArduinoJson/Array/ElementProxy.hpp +++ b/src/ArduinoJson/Array/ElementProxy.hpp @@ -40,8 +40,8 @@ class ElementProxy : public VariantRefBase>, } private: - FORCE_INLINE MemoryPool* getPool() const { - return VariantAttorney::getPool(upstream_); + FORCE_INLINE ResourceManager* getResourceManager() const { + return VariantAttorney::getResourceManager(upstream_); } FORCE_INLINE VariantData* getData() const { @@ -49,8 +49,9 @@ class ElementProxy : public VariantRefBase>, } FORCE_INLINE VariantData* getOrCreateData() const { - return variantGetOrAddElement(VariantAttorney::getOrCreateData(upstream_), - index_, VariantAttorney::getPool(upstream_)); + return variantGetOrAddElement( + VariantAttorney::getOrCreateData(upstream_), index_, + VariantAttorney::getResourceManager(upstream_)); } TUpstream upstream_; diff --git a/src/ArduinoJson/Array/JsonArray.hpp b/src/ArduinoJson/Array/JsonArray.hpp index 541c8e7b..3d8df21c 100644 --- a/src/ArduinoJson/Array/JsonArray.hpp +++ b/src/ArduinoJson/Array/JsonArray.hpp @@ -21,17 +21,19 @@ class JsonArray : public detail::VariantOperators { typedef JsonArrayIterator iterator; // Constructs an unbound reference. - FORCE_INLINE JsonArray() : data_(0), pool_(0) {} + FORCE_INLINE JsonArray() : data_(0), resources_(0) {} // INTERNAL USE ONLY - FORCE_INLINE JsonArray(detail::MemoryPool* pool, detail::CollectionData* data) - : data_(data), pool_(pool) {} + FORCE_INLINE JsonArray(detail::ResourceManager* resources, + detail::CollectionData* data) + : data_(data), resources_(resources) {} // Returns a JsonVariant pointing to the array. // https://arduinojson.org/v6/api/jsonvariant/ operator JsonVariant() { void* data = data_; // prevent warning cast-align - return JsonVariant(pool_, reinterpret_cast(data)); + return JsonVariant(resources_, + reinterpret_cast(data)); } // Returns a read-only reference to the array. @@ -44,7 +46,7 @@ class JsonArray : public detail::VariantOperators { // Returns a reference to the new element. // https://arduinojson.org/v6/api/jsonarray/add/ JsonVariant add() const { - return JsonVariant(pool_, collectionAddElement(data_, pool_)); + return JsonVariant(resources_, collectionAddElement(data_, resources_)); } // Appends a value to the array. @@ -66,7 +68,7 @@ class JsonArray : public detail::VariantOperators { FORCE_INLINE iterator begin() const { if (!data_) return iterator(); - return iterator(pool_, data_->head()); + return iterator(resources_, data_->head()); } // Returns an iterator following the last element of the array. @@ -78,7 +80,7 @@ class JsonArray : public detail::VariantOperators { // Copies an array. // https://arduinojson.org/v6/api/jsonarray/set/ FORCE_INLINE bool set(JsonArrayConst src) const { - return collectionCopy(data_, src.data_, pool_); + return collectionCopy(data_, src.data_, resources_); } // Compares the content of two arrays. @@ -90,21 +92,21 @@ class JsonArray : public detail::VariantOperators { // ⚠️ Doesn't release the memory associated with the removed element. // https://arduinojson.org/v6/api/jsonarray/remove/ FORCE_INLINE void remove(iterator it) const { - collectionRemove(data_, it.slot_, pool_); + collectionRemove(data_, it.slot_, resources_); } // Removes the element at the specified index. // ⚠️ Doesn't release the memory associated with the removed element. // https://arduinojson.org/v6/api/jsonarray/remove/ FORCE_INLINE void remove(size_t index) const { - collectionRemoveElement(data_, index, pool_); + collectionRemoveElement(data_, index, resources_); } // Removes all the elements of the array. // ⚠️ Doesn't release the memory associated with the removed elements. // https://arduinojson.org/v6/api/jsonarray/clear/ void clear() const { - collectionClear(data_, pool_); + collectionClear(data_, resources_); } // Gets or sets the element at the specified index. @@ -158,8 +160,8 @@ class JsonArray : public detail::VariantOperators { } private: - detail::MemoryPool* getPool() const { - return pool_; + detail::ResourceManager* getResourceManager() const { + return resources_; } detail::VariantData* getData() const { @@ -171,19 +173,19 @@ class JsonArray : public detail::VariantOperators { } detail::CollectionData* data_; - detail::MemoryPool* pool_; + detail::ResourceManager* resources_; }; template <> struct Converter : private detail::VariantAttorney { static void toJson(JsonVariantConst src, JsonVariant dst) { - variantCopyFrom(getData(dst), getData(src), getPool(dst)); + variantCopyFrom(getData(dst), getData(src), getResourceManager(dst)); } static JsonArray fromJson(JsonVariant src) { auto data = getData(src); - auto pool = getPool(src); - return JsonArray(pool, data != 0 ? data->asArray() : 0); + auto resources = getResourceManager(src); + return JsonArray(resources, data != 0 ? data->asArray() : 0); } static detail::InvalidConversion fromJson( @@ -207,7 +209,8 @@ template template inline typename enable_if::value, JsonArray>::type VariantRefBase::to() const { - return JsonArray(getPool(), variantToArray(getOrCreateData(), getPool())); + return JsonArray(getResourceManager(), + variantToArray(getOrCreateData(), getResourceManager())); } ARDUINOJSON_END_PRIVATE_NAMESPACE diff --git a/src/ArduinoJson/Array/JsonArrayConst.hpp b/src/ArduinoJson/Array/JsonArrayConst.hpp index ad951f1a..e6f177f4 100644 --- a/src/ArduinoJson/Array/JsonArrayConst.hpp +++ b/src/ArduinoJson/Array/JsonArrayConst.hpp @@ -118,7 +118,7 @@ class JsonArrayConst : public detail::VariantOperators { template <> struct Converter : private detail::VariantAttorney { static void toJson(JsonVariantConst src, JsonVariant dst) { - variantCopyFrom(getData(dst), getData(src), getPool(dst)); + variantCopyFrom(getData(dst), getData(src), getResourceManager(dst)); } static JsonArrayConst fromJson(JsonVariantConst src) { diff --git a/src/ArduinoJson/Array/JsonArrayIterator.hpp b/src/ArduinoJson/Array/JsonArrayIterator.hpp index d9048b28..5566deeb 100644 --- a/src/ArduinoJson/Array/JsonArrayIterator.hpp +++ b/src/ArduinoJson/Array/JsonArrayIterator.hpp @@ -11,8 +11,8 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE class VariantPtr { public: - VariantPtr(detail::MemoryPool* pool, detail::VariantData* data) - : variant_(pool, data) {} + VariantPtr(detail::ResourceManager* resources, detail::VariantData* data) + : variant_(resources, data) {} JsonVariant* operator->() { return &variant_; @@ -31,15 +31,15 @@ class JsonArrayIterator { public: JsonArrayIterator() : slot_(0) {} - explicit JsonArrayIterator(detail::MemoryPool* pool, + explicit JsonArrayIterator(detail::ResourceManager* resources, detail::VariantSlot* slot) - : pool_(pool), slot_(slot) {} + : resources_(resources), slot_(slot) {} JsonVariant operator*() const { - return JsonVariant(pool_, slot_->data()); + return JsonVariant(resources_, slot_->data()); } VariantPtr operator->() { - return VariantPtr(pool_, slot_->data()); + return VariantPtr(resources_, slot_->data()); } bool operator==(const JsonArrayIterator& other) const { @@ -61,7 +61,7 @@ class JsonArrayIterator { } private: - detail::MemoryPool* pool_; + detail::ResourceManager* resources_; detail::VariantSlot* slot_; }; diff --git a/src/ArduinoJson/Collection/CollectionFunctions.hpp b/src/ArduinoJson/Collection/CollectionFunctions.hpp index 6af00dd7..d23f3f49 100644 --- a/src/ArduinoJson/Collection/CollectionFunctions.hpp +++ b/src/ArduinoJson/Collection/CollectionFunctions.hpp @@ -9,10 +9,10 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE inline VariantData* collectionAddElement(CollectionData* array, - MemoryPool* pool) { + ResourceManager* resources) { if (!array) return nullptr; - auto slot = new (pool) VariantSlot(); + auto slot = new (resources) VariantSlot(); if (!slot) return nullptr; array->add(slot); @@ -21,16 +21,16 @@ inline VariantData* collectionAddElement(CollectionData* array, template inline VariantData* collectionAddMember(CollectionData* obj, TAdaptedString key, - MemoryPool* pool) { + ResourceManager* resources) { ARDUINOJSON_ASSERT(!key.isNull()); ARDUINOJSON_ASSERT(obj != nullptr); - auto slot = new (pool) VariantSlot(); + auto slot = new (resources) VariantSlot(); if (!slot) return nullptr; if (key.isLinked()) slot->setKey(key.data()); else { - auto storedKey = pool->saveString(key); + auto storedKey = resources->saveString(key); if (!storedKey) return nullptr; slot->setKey(storedKey); @@ -39,31 +39,31 @@ inline VariantData* collectionAddMember(CollectionData* obj, TAdaptedString key, return slot->data(); } -inline void collectionClear(CollectionData* c, MemoryPool* pool) { +inline void collectionClear(CollectionData* c, ResourceManager* resources) { if (!c) return; for (auto slot = c->head(); slot; slot = slot->next()) - slotRelease(slot, pool); + slotRelease(slot, resources); c->clear(); } inline bool collectionCopy(CollectionData* dst, const CollectionData* src, - MemoryPool* pool) { + ResourceManager* resources) { if (!dst || !src) return false; - collectionClear(dst, pool); + collectionClear(dst, resources); for (VariantSlot* s = src->head(); s; s = s->next()) { VariantData* var; if (s->key() != 0) { JsonString key(s->key(), s->ownsKey() ? JsonString::Copied : JsonString::Linked); - var = collectionAddMember(dst, adaptString(key), pool); + var = collectionAddMember(dst, adaptString(key), resources); } else { - var = collectionAddElement(dst, pool); + var = collectionAddElement(dst, resources); } - if (!variantCopyFrom(var, s->data(), pool)) + if (!variantCopyFrom(var, s->data(), resources)) return false; } return true; @@ -78,26 +78,26 @@ inline VariantData* collectionGetMember(const CollectionData* obj, } inline void collectionRemove(CollectionData* data, VariantSlot* slot, - MemoryPool* pool) { + ResourceManager* resources) { if (!data || !slot) return; data->remove(slot); - slotRelease(slot, pool); + slotRelease(slot, resources); } inline void collectionRemoveElement(CollectionData* array, size_t index, - MemoryPool* pool) { + ResourceManager* resources) { if (!array) return; - collectionRemove(array, array->get(index), pool); + collectionRemove(array, array->get(index), resources); } template inline void collectionRemoveMember(CollectionData* obj, TAdaptedString key, - MemoryPool* pool) { + ResourceManager* resources) { if (!obj) return; - collectionRemove(obj, obj->get(key), pool); + collectionRemove(obj, obj->get(key), resources); } ARDUINOJSON_END_PRIVATE_NAMESPACE diff --git a/src/ArduinoJson/Deserialization/deserialize.hpp b/src/ArduinoJson/Deserialization/deserialize.hpp index 6dfb9d09..3f6b4f32 100644 --- a/src/ArduinoJson/Deserialization/deserialize.hpp +++ b/src/ArduinoJson/Deserialization/deserialize.hpp @@ -23,9 +23,10 @@ struct first_or_void { }; template