From c2e756d942b136fa3349fa85706227ff10fc9741 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 31 May 2025 16:31:31 +0200 Subject: [PATCH] ResourceManagerTests pass --- src/ArduinoJson/Configuration.hpp | 4 ++-- src/ArduinoJson/Memory/ResourceManager.hpp | 14 +++++++------- src/ArduinoJson/Memory/ResourceManagerImpl.hpp | 11 ++++++----- src/ArduinoJson/Variant/VariantContent.hpp | 2 +- src/ArduinoJson/Variant/VariantData.hpp | 10 +++++----- src/ArduinoJson/Variant/VariantImpl.hpp | 8 ++++---- 6 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/ArduinoJson/Configuration.hpp b/src/ArduinoJson/Configuration.hpp index 54c01615..38d78f46 100644 --- a/src/ArduinoJson/Configuration.hpp +++ b/src/ArduinoJson/Configuration.hpp @@ -274,9 +274,9 @@ #endif #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_DOUBLE -# define ARDUINOJSON_USE_8_BYTE_VALUES 1 +# define ARDUINOJSON_USE_8_BYTE_POOL 1 #else -# define ARDUINOJSON_USE_8_BYTE_VALUES 0 +# define ARDUINOJSON_USE_8_BYTE_POOL 0 #endif #if defined(nullptr) diff --git a/src/ArduinoJson/Memory/ResourceManager.hpp b/src/ArduinoJson/Memory/ResourceManager.hpp index d405415c..cfbec45a 100644 --- a/src/ArduinoJson/Memory/ResourceManager.hpp +++ b/src/ArduinoJson/Memory/ResourceManager.hpp @@ -39,7 +39,7 @@ class ResourceManager { swap(a.stringPool_, b.stringPool_); swap(a.variantPools_, b.variantPools_); swap(a.staticStringsPools_, b.staticStringsPools_); - swap(a.lgValuePools_, b.lgValuePools_); + swap(a.eightBytePools_, b.eightBytePools_); swap_(a.allocator_, b.allocator_); swap_(a.overflowed_, b.overflowed_); } @@ -60,7 +60,7 @@ class ResourceManager { void freeVariant(Slot slot); VariantData* getVariant(SlotId id) const; -#if ARDUINOJSON_USE_8_BYTE_VALUES +#if ARDUINOJSON_USE_8_BYTE_POOL Slot allocEightByte(); void freeEightByte(SlotId slot); EightByteValue* getEightByte(SlotId id) const; @@ -132,16 +132,16 @@ class ResourceManager { variantPools_.clear(allocator_); stringPool_.clear(allocator_); staticStringsPools_.clear(allocator_); -#if ARDUINOJSON_USE_8_BYTE_VALUES - lgValuePools_.clear(allocator_); +#if ARDUINOJSON_USE_8_BYTE_POOL + eightBytePools_.clear(allocator_); #endif } void shrinkToFit() { variantPools_.shrinkToFit(allocator_); staticStringsPools_.shrinkToFit(allocator_); -#if ARDUINOJSON_USE_8_BYTE_VALUES - lgValuePools_.shrinkToFit(allocator_); +#if ARDUINOJSON_USE_8_BYTE_POOL + eightBytePools_.shrinkToFit(allocator_); #endif } @@ -151,7 +151,7 @@ class ResourceManager { StringPool stringPool_; MemoryPoolList variantPools_; MemoryPoolList staticStringsPools_; -#if ARDUINOJSON_USE_8_BYTE_VALUES +#if ARDUINOJSON_USE_8_BYTE_POOL MemoryPoolList eightBytePools_; #endif }; diff --git a/src/ArduinoJson/Memory/ResourceManagerImpl.hpp b/src/ArduinoJson/Memory/ResourceManagerImpl.hpp index 8cbc571e..d046770b 100644 --- a/src/ArduinoJson/Memory/ResourceManagerImpl.hpp +++ b/src/ArduinoJson/Memory/ResourceManagerImpl.hpp @@ -12,12 +12,13 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE inline Slot ResourceManager::allocVariant() { - auto p = variantPools_.allocSlot(allocator_); - if (!p) { + auto slot = variantPools_.allocSlot(allocator_); + if (!slot) { overflowed_ = true; return {}; } - return {new (&p->variant) VariantData, p.id()}; + new (slot.ptr()) VariantData(); + return slot; } inline void ResourceManager::freeVariant(Slot variant) { @@ -29,7 +30,7 @@ inline VariantData* ResourceManager::getVariant(SlotId id) const { return reinterpret_cast(variantPools_.getSlot(id)); } -#if ARDUINOJSON_USE_8_BYTE_VALUES +#if ARDUINOJSON_USE_8_BYTE_POOL inline Slot ResourceManager::allocEightByte() { auto slot = eightBytePools_.allocSlot(allocator_); if (!slot) { @@ -45,7 +46,7 @@ inline void ResourceManager::freeEightByte(SlotId id) { } inline EightByteValue* ResourceManager::getEightByte(SlotId id) const { - return eightBytePools_.getSlot(id).ptr(); + return eightBytePools_.getSlot(id); } #endif diff --git a/src/ArduinoJson/Variant/VariantContent.hpp b/src/ArduinoJson/Variant/VariantContent.hpp index 74842984..b9570a5b 100644 --- a/src/ArduinoJson/Variant/VariantContent.hpp +++ b/src/ArduinoJson/Variant/VariantContent.hpp @@ -62,7 +62,7 @@ union VariantContent { char asTinyString[tinyStringMaxLength + 1]; }; -#if ARDUINOJSON_USE_8_BYTE_VALUES +#if ARDUINOJSON_USE_8_BYTE_POOL union EightByteValue { # if ARDUINOJSON_USE_LONG_LONG uint64_t asUint64; diff --git a/src/ArduinoJson/Variant/VariantData.hpp b/src/ArduinoJson/Variant/VariantData.hpp index 0311fb3c..71f8ad6f 100644 --- a/src/ArduinoJson/Variant/VariantData.hpp +++ b/src/ArduinoJson/Variant/VariantData.hpp @@ -53,7 +53,7 @@ class VariantData { template typename TVisitor::result_type accept( TVisitor& visit, const ResourceManager* resources) const { -#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES +#if ARDUINOJSON_USE_8_BYTE_POOL auto eightByteValue = getEightByte(resources); #else (void)resources; // silence warning @@ -145,7 +145,7 @@ class VariantData { } bool asBoolean(const ResourceManager* resources) const { -#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES +#if ARDUINOJSON_USE_8_BYTE_POOL auto eightByteValue = getEightByte(resources); #else (void)resources; // silence warning @@ -193,7 +193,7 @@ class VariantData { template T asFloat(const ResourceManager* resources) const { static_assert(is_floating_point::value, "T must be a floating point"); -#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES +#if ARDUINOJSON_USE_8_BYTE_POOL auto eightByteValue = getEightByte(resources); #else (void)resources; // silence warning @@ -238,7 +238,7 @@ class VariantData { template T asIntegral(const ResourceManager* resources) const { static_assert(is_integral::value, "T must be an integral type"); -#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES +#if ARDUINOJSON_USE_8_BYTE_POOL auto eightByteValue = getEightByte(resources); #else (void)resources; // silence warning @@ -314,7 +314,7 @@ class VariantData { } } -#if ARDUINOJSON_USE_8_BYTE_VALUES +#if ARDUINOJSON_USE_8_BYTE_POOL const EightByteValue* getEightByte(const ResourceManager* resources) const; #endif diff --git a/src/ArduinoJson/Variant/VariantImpl.hpp b/src/ArduinoJson/Variant/VariantImpl.hpp index b1780547..7004cd00 100644 --- a/src/ArduinoJson/Variant/VariantImpl.hpp +++ b/src/ArduinoJson/Variant/VariantImpl.hpp @@ -61,7 +61,7 @@ inline void VariantData::clear(ResourceManager* resources) { if (type_ & VariantTypeBits::OwnedStringBit) resources->dereferenceString(content_.asOwnedString->data); -#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES +#if ARDUINOJSON_USE_8_BYTE_POOL if (type_ & VariantTypeBits::EightByteBit) resources->freeEightByte(content_.asSlotId); #endif @@ -73,11 +73,11 @@ inline void VariantData::clear(ResourceManager* resources) { type_ = VariantType::Null; } -#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES -inline const EightByteValue* VariantData::getEightByteValue( +#if ARDUINOJSON_USE_8_BYTE_POOL +inline const EightByteValue* VariantData::getEightByte( const ResourceManager* resources) const { return type_ & VariantTypeBits::EightByteBit - ? resources->getEightByteValue(content_.asSlotId) + ? resources->getEightByte(content_.asSlotId) : 0; } #endif