Rename VariantPool to MemoryPool

This commit is contained in:
Benoit Blanchon
2024-08-25 11:44:38 +02:00
parent d3721cb122
commit f2894552f2
14 changed files with 47 additions and 45 deletions

View File

@ -5,8 +5,8 @@
#pragma once #pragma once
#include <ArduinoJson/Memory/Allocator.hpp> #include <ArduinoJson/Memory/Allocator.hpp>
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Memory/StringBuilder.hpp> #include <ArduinoJson/Memory/StringBuilder.hpp>
#include <ArduinoJson/Memory/VariantPool.hpp>
#include <sstream> #include <sstream>
@ -265,12 +265,12 @@ class TimebombAllocator : public ArduinoJson::Allocator {
} // namespace } // namespace
inline size_t sizeofPoolList(size_t n = ARDUINOJSON_INITIAL_POOL_COUNT) { 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( inline size_t sizeofPool(
ArduinoJson::detail::SlotCount n = ARDUINOJSON_POOL_CAPACITY) { 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) { inline size_t sizeofStringBuffer(size_t iteration = 1) {

View File

@ -2,8 +2,8 @@
// Copyright © 2014-2024, Benoit BLANCHON // Copyright © 2014-2024, Benoit BLANCHON
// MIT License // MIT License
#include <ArduinoJson/Memory/MemoryPoolImpl.hpp>
#include <ArduinoJson/Memory/StringBuilder.hpp> #include <ArduinoJson/Memory/StringBuilder.hpp>
#include <ArduinoJson/Memory/VariantPoolImpl.hpp>
#include <catch.hpp> #include <catch.hpp>
#include "Allocators.hpp" #include "Allocators.hpp"

View File

@ -2,9 +2,9 @@
// Copyright © 2014-2024, Benoit BLANCHON // Copyright © 2014-2024, Benoit BLANCHON
// MIT License // MIT License
#include <ArduinoJson/Memory/MemoryPoolImpl.hpp>
#include <ArduinoJson/Memory/ResourceManager.hpp> #include <ArduinoJson/Memory/ResourceManager.hpp>
#include <ArduinoJson/Memory/ResourceManagerImpl.hpp> #include <ArduinoJson/Memory/ResourceManagerImpl.hpp>
#include <ArduinoJson/Memory/VariantPoolImpl.hpp>
#include <ArduinoJson/Strings/StringAdapters.hpp> #include <ArduinoJson/Strings/StringAdapters.hpp>
#include <catch.hpp> #include <catch.hpp>

View File

@ -2,8 +2,8 @@
// Copyright © 2014-2024, Benoit BLANCHON // Copyright © 2014-2024, Benoit BLANCHON
// MIT License // MIT License
#include <ArduinoJson/Memory/MemoryPoolImpl.hpp>
#include <ArduinoJson/Memory/ResourceManager.hpp> #include <ArduinoJson/Memory/ResourceManager.hpp>
#include <ArduinoJson/Memory/VariantPoolImpl.hpp>
#include <ArduinoJson/Strings/StringAdapters.hpp> #include <ArduinoJson/Strings/StringAdapters.hpp>
#include <catch.hpp> #include <catch.hpp>

View File

@ -2,9 +2,9 @@
// Copyright © 2014-2024, Benoit BLANCHON // Copyright © 2014-2024, Benoit BLANCHON
// MIT License // MIT License
#include <ArduinoJson/Memory/MemoryPoolImpl.hpp>
#include <ArduinoJson/Memory/ResourceManager.hpp> #include <ArduinoJson/Memory/ResourceManager.hpp>
#include <ArduinoJson/Memory/ResourceManagerImpl.hpp> #include <ArduinoJson/Memory/ResourceManagerImpl.hpp>
#include <ArduinoJson/Memory/VariantPoolImpl.hpp>
#include <catch.hpp> #include <catch.hpp>
#include "Allocators.hpp" #include "Allocators.hpp"

View File

@ -2,9 +2,9 @@
// Copyright © 2014-2024, Benoit BLANCHON // Copyright © 2014-2024, Benoit BLANCHON
// MIT License // MIT License
#include <ArduinoJson/Memory/MemoryPoolImpl.hpp>
#include <ArduinoJson/Memory/ResourceManager.hpp> #include <ArduinoJson/Memory/ResourceManager.hpp>
#include <ArduinoJson/Memory/ResourceManagerImpl.hpp> #include <ArduinoJson/Memory/ResourceManagerImpl.hpp>
#include <ArduinoJson/Memory/VariantPoolImpl.hpp>
#include <catch.hpp> #include <catch.hpp>
#include "Allocators.hpp" #include "Allocators.hpp"

View File

@ -3,9 +3,9 @@
// MIT License // MIT License
#include <ArduinoJson/Memory/Alignment.hpp> #include <ArduinoJson/Memory/Alignment.hpp>
#include <ArduinoJson/Memory/MemoryPoolImpl.hpp>
#include <ArduinoJson/Memory/ResourceManager.hpp> #include <ArduinoJson/Memory/ResourceManager.hpp>
#include <ArduinoJson/Memory/ResourceManagerImpl.hpp> #include <ArduinoJson/Memory/ResourceManagerImpl.hpp>
#include <ArduinoJson/Memory/VariantPoolImpl.hpp>
#include <catch.hpp> #include <catch.hpp>
#include "Allocators.hpp" #include "Allocators.hpp"

View File

@ -36,8 +36,8 @@
#include "ArduinoJson/Array/ElementProxy.hpp" #include "ArduinoJson/Array/ElementProxy.hpp"
#include "ArduinoJson/Array/Utilities.hpp" #include "ArduinoJson/Array/Utilities.hpp"
#include "ArduinoJson/Collection/CollectionImpl.hpp" #include "ArduinoJson/Collection/CollectionImpl.hpp"
#include "ArduinoJson/Memory/MemoryPoolImpl.hpp"
#include "ArduinoJson/Memory/ResourceManagerImpl.hpp" #include "ArduinoJson/Memory/ResourceManagerImpl.hpp"
#include "ArduinoJson/Memory/VariantPoolImpl.hpp"
#include "ArduinoJson/Object/MemberProxy.hpp" #include "ArduinoJson/Object/MemberProxy.hpp"
#include "ArduinoJson/Object/ObjectImpl.hpp" #include "ArduinoJson/Object/ObjectImpl.hpp"
#include "ArduinoJson/Variant/ConverterImpl.hpp" #include "ArduinoJson/Variant/ConverterImpl.hpp"

View File

@ -4,7 +4,7 @@
#pragma once #pragma once
#include <ArduinoJson/Memory/ResourceManager.hpp> #include <ArduinoJson/Memory/Allocator.hpp>
#include <ArduinoJson/Polyfills/assert.hpp> #include <ArduinoJson/Polyfills/assert.hpp>
#include <ArduinoJson/Polyfills/integer.hpp> #include <ArduinoJson/Polyfills/integer.hpp>
@ -44,7 +44,7 @@ class SlotWithId {
SlotId id_; SlotId id_;
}; };
class VariantPool { class MemoryPool {
public: public:
void create(SlotCount cap, Allocator* allocator); void create(SlotCount cap, Allocator* allocator);
void destroy(Allocator* allocator); void destroy(Allocator* allocator);

View File

@ -4,12 +4,12 @@
#pragma once #pragma once
#include <ArduinoJson/Memory/VariantPool.hpp> #include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Variant/VariantSlot.hpp> #include <ArduinoJson/Variant/VariantSlot.hpp>
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
inline void VariantPool::create(SlotCount cap, Allocator* allocator) { inline void MemoryPool::create(SlotCount cap, Allocator* allocator) {
ARDUINOJSON_ASSERT(cap > 0); ARDUINOJSON_ASSERT(cap > 0);
slots_ = slots_ =
reinterpret_cast<VariantSlot*>(allocator->allocate(slotsToBytes(cap))); reinterpret_cast<VariantSlot*>(allocator->allocate(slotsToBytes(cap)));
@ -17,7 +17,7 @@ inline void VariantPool::create(SlotCount cap, Allocator* allocator) {
usage_ = 0; usage_ = 0;
} }
inline void VariantPool::destroy(Allocator* allocator) { inline void MemoryPool::destroy(Allocator* allocator) {
if (slots_) if (slots_)
allocator->deallocate(slots_); allocator->deallocate(slots_);
slots_ = nullptr; slots_ = nullptr;
@ -25,7 +25,7 @@ inline void VariantPool::destroy(Allocator* allocator) {
usage_ = 0; usage_ = 0;
} }
inline void VariantPool::shrinkToFit(Allocator* allocator) { inline void MemoryPool::shrinkToFit(Allocator* allocator) {
auto newSlots = reinterpret_cast<VariantSlot*>( auto newSlots = reinterpret_cast<VariantSlot*>(
allocator->reallocate(slots_, slotsToBytes(usage_))); allocator->reallocate(slots_, slotsToBytes(usage_)));
if (newSlots) { if (newSlots) {
@ -34,7 +34,7 @@ inline void VariantPool::shrinkToFit(Allocator* allocator) {
} }
} }
inline SlotWithId VariantPool::allocSlot() { inline SlotWithId MemoryPool::allocSlot() {
if (!slots_) if (!slots_)
return {}; return {};
if (usage_ >= capacity_) if (usage_ >= capacity_)
@ -44,28 +44,28 @@ inline SlotWithId VariantPool::allocSlot() {
return {slot, SlotId(index)}; return {slot, SlotId(index)};
} }
inline VariantSlot* VariantPool::getSlot(SlotId id) const { inline VariantSlot* MemoryPool::getSlot(SlotId id) const {
ARDUINOJSON_ASSERT(id < usage_); ARDUINOJSON_ASSERT(id < usage_);
return &slots_[id]; return &slots_[id];
} }
inline SlotCount VariantPool::usage() const { inline SlotCount MemoryPool::usage() const {
return usage_; return usage_;
} }
inline void VariantPool::clear() { inline void MemoryPool::clear() {
usage_ = 0; usage_ = 0;
} }
inline SlotCount VariantPool::bytesToSlots(size_t n) { inline SlotCount MemoryPool::bytesToSlots(size_t n) {
return static_cast<SlotCount>(n / sizeof(VariantSlot)); return static_cast<SlotCount>(n / sizeof(VariantSlot));
} }
inline size_t VariantPool::slotsToBytes(SlotCount n) { inline size_t MemoryPool::slotsToBytes(SlotCount n) {
return n * sizeof(VariantSlot); return n * sizeof(VariantSlot);
} }
inline SlotWithId VariantPoolList::allocFromFreeList() { inline SlotWithId MemoryPoolList::allocFromFreeList() {
ARDUINOJSON_ASSERT(freeList_ != NULL_SLOT); ARDUINOJSON_ASSERT(freeList_ != NULL_SLOT);
auto id = freeList_; auto id = freeList_;
auto slot = getSlot(freeList_); auto slot = getSlot(freeList_);
@ -73,7 +73,7 @@ inline SlotWithId VariantPoolList::allocFromFreeList() {
return {slot, id}; return {slot, id};
} }
inline void VariantPoolList::freeSlot(SlotWithId slot) { inline void MemoryPoolList::freeSlot(SlotWithId slot) {
slot->free.next = freeList_; slot->free.next = freeList_;
freeList_ = slot.id(); freeList_ = slot.id();
} }

View File

@ -4,22 +4,25 @@
#pragma once #pragma once
#include <ArduinoJson/Memory/VariantPool.hpp> #include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Polyfills/assert.hpp> #include <ArduinoJson/Polyfills/assert.hpp>
#include <ArduinoJson/Polyfills/utility.hpp>
#include <string.h> // memcpy
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
using PoolCount = SlotId; using PoolCount = SlotId;
class VariantPoolList { class MemoryPoolList {
public: public:
VariantPoolList() = default; MemoryPoolList() = default;
~VariantPoolList() { ~MemoryPoolList() {
ARDUINOJSON_ASSERT(count_ == 0); 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 aUsedPreallocated = a.pools_ == a.preallocatedPools_;
bool bUsedPreallocated = b.pools_ == b.preallocatedPools_; bool bUsedPreallocated = b.pools_ == b.preallocatedPools_;
@ -50,7 +53,7 @@ class VariantPoolList {
swap_(a.freeList_, b.freeList_); swap_(a.freeList_, b.freeList_);
} }
VariantPoolList& operator=(VariantPoolList&& src) { MemoryPoolList& operator=(MemoryPoolList&& src) {
ARDUINOJSON_ASSERT(count_ == 0); ARDUINOJSON_ASSERT(count_ == 0);
if (src.pools_ == src.preallocatedPools_) { if (src.pools_ == src.preallocatedPools_) {
memcpy(preallocatedPools_, src.preallocatedPools_, memcpy(preallocatedPools_, src.preallocatedPools_,
@ -122,8 +125,8 @@ class VariantPoolList {
if (count_ > 0) if (count_ > 0)
pools_[count_ - 1].shrinkToFit(allocator); pools_[count_ - 1].shrinkToFit(allocator);
if (pools_ != preallocatedPools_ && count_ != capacity_) { if (pools_ != preallocatedPools_ && count_ != capacity_) {
pools_ = static_cast<VariantPool*>( pools_ = static_cast<MemoryPool*>(
allocator->reallocate(pools_, count_ * sizeof(VariantPool))); allocator->reallocate(pools_, count_ * sizeof(MemoryPool)));
ARDUINOJSON_ASSERT(pools_ != nullptr); // realloc to smaller can't fail ARDUINOJSON_ASSERT(pools_ != nullptr); // realloc to smaller can't fail
capacity_ = count_; capacity_ = count_;
} }
@ -142,7 +145,7 @@ class VariantPoolList {
SlotId(poolIndex * ARDUINOJSON_POOL_CAPACITY + slot.id())}; SlotId(poolIndex * ARDUINOJSON_POOL_CAPACITY + slot.id())};
} }
VariantPool* addPool(Allocator* allocator) { MemoryPool* addPool(Allocator* allocator) {
if (count_ == capacity_ && !increaseCapacity(allocator)) if (count_ == capacity_ && !increaseCapacity(allocator))
return nullptr; return nullptr;
auto pool = &pools_[count_++]; auto pool = &pools_[count_++];
@ -160,24 +163,24 @@ class VariantPoolList {
auto newCapacity = PoolCount(capacity_ * 2); auto newCapacity = PoolCount(capacity_ * 2);
if (pools_ == preallocatedPools_) { if (pools_ == preallocatedPools_) {
newPools = allocator->allocate(newCapacity * sizeof(VariantPool)); newPools = allocator->allocate(newCapacity * sizeof(MemoryPool));
if (!newPools) if (!newPools)
return false; return false;
memcpy(newPools, preallocatedPools_, sizeof(preallocatedPools_)); memcpy(newPools, preallocatedPools_, sizeof(preallocatedPools_));
} else { } else {
newPools = newPools =
allocator->reallocate(pools_, newCapacity * sizeof(VariantPool)); allocator->reallocate(pools_, newCapacity * sizeof(MemoryPool));
if (!newPools) if (!newPools)
return false; return false;
} }
pools_ = static_cast<VariantPool*>(newPools); pools_ = static_cast<MemoryPool*>(newPools);
capacity_ = newCapacity; capacity_ = newCapacity;
return true; return true;
} }
VariantPool preallocatedPools_[ARDUINOJSON_INITIAL_POOL_COUNT]; MemoryPool preallocatedPools_[ARDUINOJSON_INITIAL_POOL_COUNT];
VariantPool* pools_ = preallocatedPools_; MemoryPool* pools_ = preallocatedPools_;
PoolCount count_ = 0; PoolCount count_ = 0;
PoolCount capacity_ = ARDUINOJSON_INITIAL_POOL_COUNT; PoolCount capacity_ = ARDUINOJSON_INITIAL_POOL_COUNT;
SlotId freeList_ = NULL_SLOT; SlotId freeList_ = NULL_SLOT;

View File

@ -5,8 +5,8 @@
#pragma once #pragma once
#include <ArduinoJson/Memory/Allocator.hpp> #include <ArduinoJson/Memory/Allocator.hpp>
#include <ArduinoJson/Memory/MemoryPoolList.hpp>
#include <ArduinoJson/Memory/StringPool.hpp> #include <ArduinoJson/Memory/StringPool.hpp>
#include <ArduinoJson/Memory/VariantPoolList.hpp>
#include <ArduinoJson/Polyfills/assert.hpp> #include <ArduinoJson/Polyfills/assert.hpp>
#include <ArduinoJson/Polyfills/utility.hpp> #include <ArduinoJson/Polyfills/utility.hpp>
#include <ArduinoJson/Strings/StringAdapters.hpp> #include <ArduinoJson/Strings/StringAdapters.hpp>
@ -14,7 +14,7 @@
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
union VariantSlot; union VariantSlot;
class VariantPool; class MemoryPool;
class VariantData; class VariantData;
class VariantWithId; class VariantWithId;
@ -43,8 +43,7 @@ class ResourceManager {
} }
size_t size() const { size_t size() const {
return VariantPool::slotsToBytes(variantPools_.usage()) + return MemoryPool::slotsToBytes(variantPools_.usage()) + stringPool_.size();
stringPool_.size();
} }
bool overflowed() const { bool overflowed() const {
@ -114,7 +113,7 @@ class ResourceManager {
Allocator* allocator_; Allocator* allocator_;
bool overflowed_; bool overflowed_;
StringPool stringPool_; StringPool stringPool_;
VariantPoolList variantPools_; MemoryPoolList variantPools_;
}; };
ARDUINOJSON_END_PRIVATE_NAMESPACE ARDUINOJSON_END_PRIVATE_NAMESPACE

View File

@ -13,7 +13,7 @@
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
union VariantSlot; union VariantSlot;
class VariantPool; class MemoryPool;
class StringPool { class StringPool {
public: public:

View File

@ -4,8 +4,8 @@
#pragma once #pragma once
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Memory/StringNode.hpp> #include <ArduinoJson/Memory/StringNode.hpp>
#include <ArduinoJson/Memory/VariantPool.hpp>
#include <ArduinoJson/Misc/SerializedValue.hpp> #include <ArduinoJson/Misc/SerializedValue.hpp>
#include <ArduinoJson/Numbers/convertNumber.hpp> #include <ArduinoJson/Numbers/convertNumber.hpp>
#include <ArduinoJson/Strings/JsonString.hpp> #include <ArduinoJson/Strings/JsonString.hpp>