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() {
if (!data_)
return nullptr;
ARDUINOJSON_ASSERT(resources_ != nullptr);
auto slot = resources_->allocVariant();
auto slot = allocVariant();
if (!slot)
return nullptr;
CollectionImpl::appendOne(slot);
@ -60,13 +59,12 @@ template <typename T>
inline bool ArrayImpl::addValue(const T& value) {
if (!data_)
return false;
ARDUINOJSON_ASSERT(resources_ != nullptr);
auto slot = resources_->allocVariant();
auto slot = allocVariant();
if (!slot)
return false;
JsonVariant variant(slot.ptr(), resources_);
if (!variant.set(value)) {
resources_->freeVariant(slot);
freeVariant(slot);
return false;
}
CollectionImpl::appendOne(slot);

View File

@ -4,7 +4,6 @@
#pragma once
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Namespace.hpp>
#include <ArduinoJson/Polyfills/assert.hpp>
@ -114,6 +113,21 @@ class CollectionImpl {
void removeOne(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:
Slot<VariantData> getPreviousSlot(VariantData*) const;

View File

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

View File

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