diff --git a/extras/tests/Helpers/Allocators.hpp b/extras/tests/Helpers/Allocators.hpp index 095d726a..fd6e7b21 100644 --- a/extras/tests/Helpers/Allocators.hpp +++ b/extras/tests/Helpers/Allocators.hpp @@ -5,8 +5,8 @@ #pragma once #include +#include #include -#include #include @@ -265,12 +265,12 @@ class TimebombAllocator : public ArduinoJson::Allocator { } // namespace inline size_t sizeofPoolList(size_t n = ARDUINOJSON_INITIAL_POOL_COUNT) { - return sizeof(ArduinoJson::detail::VariantPool) * n; + return sizeof(ArduinoJson::detail::MemoryPool) * n; } inline size_t sizeofPool( ArduinoJson::detail::SlotCount n = ARDUINOJSON_POOL_CAPACITY) { - return ArduinoJson::detail::VariantPool::slotsToBytes(n); + return ArduinoJson::detail::MemoryPool::slotsToBytes(n); } inline size_t sizeofStringBuffer(size_t iteration = 1) { diff --git a/extras/tests/ResourceManager/StringBuilder.cpp b/extras/tests/ResourceManager/StringBuilder.cpp index f8398587..00905e3d 100644 --- a/extras/tests/ResourceManager/StringBuilder.cpp +++ b/extras/tests/ResourceManager/StringBuilder.cpp @@ -2,8 +2,8 @@ // Copyright © 2014-2024, Benoit BLANCHON // MIT License +#include #include -#include #include #include "Allocators.hpp" diff --git a/extras/tests/ResourceManager/clear.cpp b/extras/tests/ResourceManager/clear.cpp index 94b9baf4..a444e69c 100644 --- a/extras/tests/ResourceManager/clear.cpp +++ b/extras/tests/ResourceManager/clear.cpp @@ -2,9 +2,9 @@ // Copyright © 2014-2024, Benoit BLANCHON // MIT License +#include #include #include -#include #include #include diff --git a/extras/tests/ResourceManager/saveString.cpp b/extras/tests/ResourceManager/saveString.cpp index f19ac197..a054bdef 100644 --- a/extras/tests/ResourceManager/saveString.cpp +++ b/extras/tests/ResourceManager/saveString.cpp @@ -2,8 +2,8 @@ // Copyright © 2014-2024, Benoit BLANCHON // MIT License +#include #include -#include #include #include diff --git a/extras/tests/ResourceManager/shrinkToFit.cpp b/extras/tests/ResourceManager/shrinkToFit.cpp index b7562ca5..812e2005 100644 --- a/extras/tests/ResourceManager/shrinkToFit.cpp +++ b/extras/tests/ResourceManager/shrinkToFit.cpp @@ -2,9 +2,9 @@ // Copyright © 2014-2024, Benoit BLANCHON // MIT License +#include #include #include -#include #include #include "Allocators.hpp" diff --git a/extras/tests/ResourceManager/size.cpp b/extras/tests/ResourceManager/size.cpp index ff637b61..e24ecdca 100644 --- a/extras/tests/ResourceManager/size.cpp +++ b/extras/tests/ResourceManager/size.cpp @@ -2,9 +2,9 @@ // Copyright © 2014-2024, Benoit BLANCHON // MIT License +#include #include #include -#include #include #include "Allocators.hpp" diff --git a/extras/tests/ResourceManager/swap.cpp b/extras/tests/ResourceManager/swap.cpp index eb511660..b52e05aa 100644 --- a/extras/tests/ResourceManager/swap.cpp +++ b/extras/tests/ResourceManager/swap.cpp @@ -3,9 +3,9 @@ // MIT License #include +#include #include #include -#include #include #include "Allocators.hpp" diff --git a/src/ArduinoJson.hpp b/src/ArduinoJson.hpp index 085507a0..e61a20d3 100644 --- a/src/ArduinoJson.hpp +++ b/src/ArduinoJson.hpp @@ -36,8 +36,8 @@ #include "ArduinoJson/Array/ElementProxy.hpp" #include "ArduinoJson/Array/Utilities.hpp" #include "ArduinoJson/Collection/CollectionImpl.hpp" +#include "ArduinoJson/Memory/MemoryPoolImpl.hpp" #include "ArduinoJson/Memory/ResourceManagerImpl.hpp" -#include "ArduinoJson/Memory/VariantPoolImpl.hpp" #include "ArduinoJson/Object/MemberProxy.hpp" #include "ArduinoJson/Object/ObjectImpl.hpp" #include "ArduinoJson/Variant/ConverterImpl.hpp" diff --git a/src/ArduinoJson/Memory/VariantPool.hpp b/src/ArduinoJson/Memory/MemoryPool.hpp similarity index 95% rename from src/ArduinoJson/Memory/VariantPool.hpp rename to src/ArduinoJson/Memory/MemoryPool.hpp index 92d468ff..23a95d85 100644 --- a/src/ArduinoJson/Memory/VariantPool.hpp +++ b/src/ArduinoJson/Memory/MemoryPool.hpp @@ -4,7 +4,7 @@ #pragma once -#include +#include #include #include @@ -44,7 +44,7 @@ class SlotWithId { SlotId id_; }; -class VariantPool { +class MemoryPool { public: void create(SlotCount cap, Allocator* allocator); void destroy(Allocator* allocator); diff --git a/src/ArduinoJson/Memory/VariantPoolImpl.hpp b/src/ArduinoJson/Memory/MemoryPoolImpl.hpp similarity index 66% rename from src/ArduinoJson/Memory/VariantPoolImpl.hpp rename to src/ArduinoJson/Memory/MemoryPoolImpl.hpp index 833eb627..2046aa55 100644 --- a/src/ArduinoJson/Memory/VariantPoolImpl.hpp +++ b/src/ArduinoJson/Memory/MemoryPoolImpl.hpp @@ -4,12 +4,12 @@ #pragma once -#include +#include #include ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE -inline void VariantPool::create(SlotCount cap, Allocator* allocator) { +inline void MemoryPool::create(SlotCount cap, Allocator* allocator) { ARDUINOJSON_ASSERT(cap > 0); slots_ = reinterpret_cast(allocator->allocate(slotsToBytes(cap))); @@ -17,7 +17,7 @@ inline void VariantPool::create(SlotCount cap, Allocator* allocator) { usage_ = 0; } -inline void VariantPool::destroy(Allocator* allocator) { +inline void MemoryPool::destroy(Allocator* allocator) { if (slots_) allocator->deallocate(slots_); slots_ = nullptr; @@ -25,7 +25,7 @@ inline void VariantPool::destroy(Allocator* allocator) { usage_ = 0; } -inline void VariantPool::shrinkToFit(Allocator* allocator) { +inline void MemoryPool::shrinkToFit(Allocator* allocator) { auto newSlots = reinterpret_cast( allocator->reallocate(slots_, slotsToBytes(usage_))); if (newSlots) { @@ -34,7 +34,7 @@ inline void VariantPool::shrinkToFit(Allocator* allocator) { } } -inline SlotWithId VariantPool::allocSlot() { +inline SlotWithId MemoryPool::allocSlot() { if (!slots_) return {}; if (usage_ >= capacity_) @@ -44,28 +44,28 @@ inline SlotWithId VariantPool::allocSlot() { return {slot, SlotId(index)}; } -inline VariantSlot* VariantPool::getSlot(SlotId id) const { +inline VariantSlot* MemoryPool::getSlot(SlotId id) const { ARDUINOJSON_ASSERT(id < usage_); return &slots_[id]; } -inline SlotCount VariantPool::usage() const { +inline SlotCount MemoryPool::usage() const { return usage_; } -inline void VariantPool::clear() { +inline void MemoryPool::clear() { usage_ = 0; } -inline SlotCount VariantPool::bytesToSlots(size_t n) { +inline SlotCount MemoryPool::bytesToSlots(size_t n) { return static_cast(n / sizeof(VariantSlot)); } -inline size_t VariantPool::slotsToBytes(SlotCount n) { +inline size_t MemoryPool::slotsToBytes(SlotCount n) { return n * sizeof(VariantSlot); } -inline SlotWithId VariantPoolList::allocFromFreeList() { +inline SlotWithId MemoryPoolList::allocFromFreeList() { ARDUINOJSON_ASSERT(freeList_ != NULL_SLOT); auto id = freeList_; auto slot = getSlot(freeList_); @@ -73,7 +73,7 @@ inline SlotWithId VariantPoolList::allocFromFreeList() { return {slot, id}; } -inline void VariantPoolList::freeSlot(SlotWithId slot) { +inline void MemoryPoolList::freeSlot(SlotWithId slot) { slot->free.next = freeList_; freeList_ = slot.id(); } diff --git a/src/ArduinoJson/Memory/VariantPoolList.hpp b/src/ArduinoJson/Memory/MemoryPoolList.hpp similarity index 86% rename from src/ArduinoJson/Memory/VariantPoolList.hpp rename to src/ArduinoJson/Memory/MemoryPoolList.hpp index db2db329..b87b047f 100644 --- a/src/ArduinoJson/Memory/VariantPoolList.hpp +++ b/src/ArduinoJson/Memory/MemoryPoolList.hpp @@ -4,22 +4,25 @@ #pragma once -#include +#include #include +#include + +#include // memcpy ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE using PoolCount = SlotId; -class VariantPoolList { +class MemoryPoolList { public: - VariantPoolList() = default; + MemoryPoolList() = default; - ~VariantPoolList() { + ~MemoryPoolList() { ARDUINOJSON_ASSERT(count_ == 0); } - friend void swap(VariantPoolList& a, VariantPoolList& b) { + friend void swap(MemoryPoolList& a, MemoryPoolList& b) { bool aUsedPreallocated = a.pools_ == a.preallocatedPools_; bool bUsedPreallocated = b.pools_ == b.preallocatedPools_; @@ -50,7 +53,7 @@ class VariantPoolList { swap_(a.freeList_, b.freeList_); } - VariantPoolList& operator=(VariantPoolList&& src) { + MemoryPoolList& operator=(MemoryPoolList&& src) { ARDUINOJSON_ASSERT(count_ == 0); if (src.pools_ == src.preallocatedPools_) { memcpy(preallocatedPools_, src.preallocatedPools_, @@ -122,8 +125,8 @@ class VariantPoolList { if (count_ > 0) pools_[count_ - 1].shrinkToFit(allocator); if (pools_ != preallocatedPools_ && count_ != capacity_) { - pools_ = static_cast( - allocator->reallocate(pools_, count_ * sizeof(VariantPool))); + pools_ = static_cast( + allocator->reallocate(pools_, count_ * sizeof(MemoryPool))); ARDUINOJSON_ASSERT(pools_ != nullptr); // realloc to smaller can't fail capacity_ = count_; } @@ -142,7 +145,7 @@ class VariantPoolList { SlotId(poolIndex * ARDUINOJSON_POOL_CAPACITY + slot.id())}; } - VariantPool* addPool(Allocator* allocator) { + MemoryPool* addPool(Allocator* allocator) { if (count_ == capacity_ && !increaseCapacity(allocator)) return nullptr; auto pool = &pools_[count_++]; @@ -160,24 +163,24 @@ class VariantPoolList { auto newCapacity = PoolCount(capacity_ * 2); if (pools_ == preallocatedPools_) { - newPools = allocator->allocate(newCapacity * sizeof(VariantPool)); + newPools = allocator->allocate(newCapacity * sizeof(MemoryPool)); if (!newPools) return false; memcpy(newPools, preallocatedPools_, sizeof(preallocatedPools_)); } else { newPools = - allocator->reallocate(pools_, newCapacity * sizeof(VariantPool)); + allocator->reallocate(pools_, newCapacity * sizeof(MemoryPool)); if (!newPools) return false; } - pools_ = static_cast(newPools); + pools_ = static_cast(newPools); capacity_ = newCapacity; return true; } - VariantPool preallocatedPools_[ARDUINOJSON_INITIAL_POOL_COUNT]; - VariantPool* pools_ = preallocatedPools_; + MemoryPool preallocatedPools_[ARDUINOJSON_INITIAL_POOL_COUNT]; + MemoryPool* pools_ = preallocatedPools_; PoolCount count_ = 0; PoolCount capacity_ = ARDUINOJSON_INITIAL_POOL_COUNT; SlotId freeList_ = NULL_SLOT; diff --git a/src/ArduinoJson/Memory/ResourceManager.hpp b/src/ArduinoJson/Memory/ResourceManager.hpp index 51cbe134..28f97665 100644 --- a/src/ArduinoJson/Memory/ResourceManager.hpp +++ b/src/ArduinoJson/Memory/ResourceManager.hpp @@ -5,8 +5,8 @@ #pragma once #include +#include #include -#include #include #include #include @@ -14,7 +14,7 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE union VariantSlot; -class VariantPool; +class MemoryPool; class VariantData; class VariantWithId; @@ -43,8 +43,7 @@ class ResourceManager { } size_t size() const { - return VariantPool::slotsToBytes(variantPools_.usage()) + - stringPool_.size(); + return MemoryPool::slotsToBytes(variantPools_.usage()) + stringPool_.size(); } bool overflowed() const { @@ -114,7 +113,7 @@ class ResourceManager { Allocator* allocator_; bool overflowed_; StringPool stringPool_; - VariantPoolList variantPools_; + MemoryPoolList variantPools_; }; ARDUINOJSON_END_PRIVATE_NAMESPACE diff --git a/src/ArduinoJson/Memory/StringPool.hpp b/src/ArduinoJson/Memory/StringPool.hpp index 74ea710a..d2f754c8 100644 --- a/src/ArduinoJson/Memory/StringPool.hpp +++ b/src/ArduinoJson/Memory/StringPool.hpp @@ -13,7 +13,7 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE union VariantSlot; -class VariantPool; +class MemoryPool; class StringPool { public: diff --git a/src/ArduinoJson/Variant/VariantData.hpp b/src/ArduinoJson/Variant/VariantData.hpp index a808547e..87e37ecc 100644 --- a/src/ArduinoJson/Variant/VariantData.hpp +++ b/src/ArduinoJson/Variant/VariantData.hpp @@ -4,8 +4,8 @@ #pragma once +#include #include -#include #include #include #include