CollectionImpl: add allocVariant(), getVariant(), freeVariant()

This commit is contained in:
Benoit Blanchon
2025-07-03 10:20:27 +02:00
parent 6ba4507252
commit 6f28ec0384
4 changed files with 31 additions and 25 deletions

View File

@ -22,8 +22,7 @@ inline ArrayImpl::iterator ArrayImpl::at(size_t index) const {
inline VariantData* ArrayImpl::addElement() { inline VariantData* ArrayImpl::addElement() {
if (!data_) if (!data_)
return nullptr; return nullptr;
ARDUINOJSON_ASSERT(resources_ != nullptr); auto slot = allocVariant();
auto slot = resources_->allocVariant();
if (!slot) if (!slot)
return nullptr; return nullptr;
CollectionImpl::appendOne(slot); CollectionImpl::appendOne(slot);
@ -60,13 +59,12 @@ template <typename T>
inline bool ArrayImpl::addValue(const T& value) { inline bool ArrayImpl::addValue(const T& value) {
if (!data_) if (!data_)
return false; return false;
ARDUINOJSON_ASSERT(resources_ != nullptr); auto slot = allocVariant();
auto slot = resources_->allocVariant();
if (!slot) if (!slot)
return false; return false;
JsonVariant variant(slot.ptr(), resources_); JsonVariant variant(slot.ptr(), resources_);
if (!variant.set(value)) { if (!variant.set(value)) {
resources_->freeVariant(slot); freeVariant(slot);
return false; return false;
} }
CollectionImpl::appendOne(slot); CollectionImpl::appendOne(slot);

View File

@ -4,7 +4,6 @@
#pragma once #pragma once
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Namespace.hpp> #include <ArduinoJson/Namespace.hpp>
#include <ArduinoJson/Polyfills/assert.hpp> #include <ArduinoJson/Polyfills/assert.hpp>
@ -114,6 +113,21 @@ class CollectionImpl {
void removeOne(iterator it); void removeOne(iterator it);
void removePair(iterator it); void removePair(iterator it);
VariantData* getVariant(SlotId id) const {
ARDUINOJSON_ASSERT(resources_ != nullptr);
return resources_->getVariant(id);
}
void freeVariant(Slot<VariantData> slot) {
ARDUINOJSON_ASSERT(resources_ != nullptr);
resources_->freeVariant(slot);
}
Slot<VariantData> allocVariant() {
ARDUINOJSON_ASSERT(resources_ != nullptr);
return resources_->allocVariant();
}
private: private:
Slot<VariantData> getPreviousSlot(VariantData*) const; Slot<VariantData> getPreviousSlot(VariantData*) const;

View File

@ -23,16 +23,14 @@ inline CollectionImpl::iterator CollectionImpl::createIterator() const {
if (!data_) if (!data_)
return iterator(); return iterator();
auto coll = getCollectionData(); auto coll = getCollectionData();
return iterator(resources_->getVariant(coll->head), coll->head); return iterator(getVariant(coll->head), coll->head);
} }
inline void CollectionImpl::appendOne(Slot<VariantData> slot) { inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
ARDUINOJSON_ASSERT(resources_ != nullptr);
auto coll = getCollectionData(); auto coll = getCollectionData();
if (coll->tail != NULL_SLOT) { if (coll->tail != NULL_SLOT) {
auto tail = resources_->getVariant(coll->tail); auto tail = getVariant(coll->tail);
tail->next = slot.id(); tail->next = slot.id();
coll->tail = slot.id(); coll->tail = slot.id();
} else { } else {
@ -43,14 +41,12 @@ inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
inline void CollectionImpl::appendPair(Slot<VariantData> key, inline void CollectionImpl::appendPair(Slot<VariantData> key,
Slot<VariantData> value) { Slot<VariantData> value) {
ARDUINOJSON_ASSERT(resources_ != nullptr);
auto coll = getCollectionData(); auto coll = getCollectionData();
key->next = value.id(); key->next = value.id();
if (coll->tail != NULL_SLOT) { if (coll->tail != NULL_SLOT) {
auto tail = resources_->getVariant(coll->tail); auto tail = getVariant(coll->tail);
tail->next = key.id(); tail->next = key.id();
coll->tail = value.id(); coll->tail = value.id();
} else { } else {
@ -68,9 +64,9 @@ inline void CollectionImpl::clear() {
auto next = coll->head; auto next = coll->head;
while (next != NULL_SLOT) { while (next != NULL_SLOT) {
auto currId = next; auto currId = next;
auto slot = resources_->getVariant(next); auto slot = getVariant(next);
next = slot->next; next = slot->next;
resources_->freeVariant({slot, currId}); freeVariant({slot, currId});
} }
coll->head = NULL_SLOT; coll->head = NULL_SLOT;
@ -83,7 +79,7 @@ inline Slot<VariantData> CollectionImpl::getPreviousSlot(
auto prev = Slot<VariantData>(); auto prev = Slot<VariantData>();
auto currentId = coll->head; auto currentId = coll->head;
while (currentId != NULL_SLOT) { while (currentId != NULL_SLOT) {
auto currentSlot = resources_->getVariant(currentId); auto currentSlot = getVariant(currentId);
if (currentSlot == target) if (currentSlot == target)
break; break;
prev = Slot<VariantData>(currentSlot, currentId); prev = Slot<VariantData>(currentSlot, currentId);
@ -105,7 +101,7 @@ inline void CollectionImpl::removeOne(iterator it) {
coll->head = next; coll->head = next;
if (next == NULL_SLOT) if (next == NULL_SLOT)
coll->tail = prev.id(); coll->tail = prev.id();
resources_->freeVariant({it.slot_, it.currentId_}); freeVariant({it.slot_, it.currentId_});
} }
inline void CollectionImpl::removePair(ObjectImpl::iterator it) { inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
@ -115,11 +111,11 @@ inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
auto keySlot = it.slot_; auto keySlot = it.slot_;
auto valueId = keySlot->next; auto valueId = keySlot->next;
auto valueSlot = resources_->getVariant(valueId); auto valueSlot = getVariant(valueId);
// remove value slot // remove value slot
keySlot->next = valueSlot->next; keySlot->next = valueSlot->next;
resources_->freeVariant({valueSlot, valueId}); freeVariant({valueSlot, valueId});
// remove key slot // remove key slot
removeOne(it); removeOne(it);

View File

@ -50,13 +50,12 @@ template <typename TAdaptedString>
inline VariantData* ObjectImpl::addMember(TAdaptedString key) { inline VariantData* ObjectImpl::addMember(TAdaptedString key) {
if (!data_) if (!data_)
return nullptr; return nullptr;
ARDUINOJSON_ASSERT(resources_ != nullptr);
auto keySlot = resources_->allocVariant(); auto keySlot = allocVariant();
if (!keySlot) if (!keySlot)
return nullptr; return nullptr;
auto valueSlot = resources_->allocVariant(); auto valueSlot = allocVariant();
if (!valueSlot) if (!valueSlot)
return nullptr; return nullptr;
@ -72,13 +71,12 @@ inline VariantData* ObjectImpl::addMember(TAdaptedString key) {
inline VariantData* ObjectImpl::addPair(VariantData** value) { inline VariantData* ObjectImpl::addPair(VariantData** value) {
if (!data_) if (!data_)
return nullptr; return nullptr;
ARDUINOJSON_ASSERT(resources_ != nullptr);
auto keySlot = resources_->allocVariant(); auto keySlot = allocVariant();
if (!keySlot) if (!keySlot)
return nullptr; return nullptr;
auto valueSlot = resources_->allocVariant(); auto valueSlot = allocVariant();
if (!valueSlot) if (!valueSlot)
return nullptr; return nullptr;
*value = valueSlot.ptr(); *value = valueSlot.ptr();