mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-02 00:38:26 +01:00
Use singly-linked list to reduce memory usage
This commit is contained in:
@@ -15,20 +15,17 @@ inline VariantData* arrayAdd(ArrayData* arr, MemoryPool* pool) {
|
||||
VariantSlot* slot = pool->allocVariant();
|
||||
if (!slot) return 0;
|
||||
|
||||
slot->next = 0;
|
||||
slot->value.type = JSON_NULL;
|
||||
slot->value.keyIsOwned = false;
|
||||
slot->init();
|
||||
|
||||
if (arr->tail) {
|
||||
slot->attachTo(arr->tail);
|
||||
arr->tail = slot;
|
||||
} else {
|
||||
slot->prev = 0;
|
||||
arr->head = slot;
|
||||
arr->tail = slot;
|
||||
}
|
||||
|
||||
return &slot->value;
|
||||
return slot->getData();
|
||||
}
|
||||
|
||||
inline VariantSlot* arrayGetSlot(const ArrayData* arr, size_t index) {
|
||||
@@ -38,20 +35,20 @@ inline VariantSlot* arrayGetSlot(const ArrayData* arr, size_t index) {
|
||||
|
||||
inline VariantData* arrayGet(const ArrayData* arr, size_t index) {
|
||||
VariantSlot* slot = arrayGetSlot(arr, index);
|
||||
return slot ? &slot->value : 0;
|
||||
return slot ? slot->getData() : 0;
|
||||
}
|
||||
|
||||
inline void arrayRemove(ArrayData* arr, VariantSlot* slot) {
|
||||
if (!arr || !slot) return;
|
||||
|
||||
if (slot->prev)
|
||||
slot->getPrev()->setNext(slot->getNext());
|
||||
VariantSlot* prev = slot->getPrev(arr->head);
|
||||
VariantSlot* next = slot->getNext();
|
||||
|
||||
if (prev)
|
||||
prev->setNext(next);
|
||||
else
|
||||
arr->head = slot->getNext();
|
||||
if (slot->next)
|
||||
slot->getNext()->setPrev(slot->getPrev());
|
||||
else
|
||||
arr->tail = slot->getPrev();
|
||||
arr->head = next;
|
||||
if (!next) arr->tail = prev;
|
||||
}
|
||||
|
||||
inline void arrayRemove(ArrayData* arr, size_t index) {
|
||||
@@ -70,7 +67,7 @@ inline bool arrayCopy(ArrayData* dst, const ArrayData* src, MemoryPool* pool) {
|
||||
if (!dst || !src) return false;
|
||||
arrayClear(dst);
|
||||
for (VariantSlot* s = src->head; s; s = s->getNext()) {
|
||||
if (!variantCopy(arrayAdd(dst, pool), &s->value, pool)) return false;
|
||||
if (!variantCopy(arrayAdd(dst, pool), s->getData(), pool)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -85,7 +82,7 @@ inline bool arrayEquals(const ArrayData* a1, const ArrayData* a2) {
|
||||
for (;;) {
|
||||
if (s1 == s2) return true;
|
||||
if (!s1 || !s2) return false;
|
||||
if (!variantEquals(&s1->value, &s2->value)) return false;
|
||||
if (!variantEquals(s1->getData(), s2->getData())) return false;
|
||||
s1 = s1->getNext();
|
||||
s2 = s2->getNext();
|
||||
}
|
||||
|
||||
@@ -33,10 +33,10 @@ class ArrayIterator {
|
||||
: _memoryPool(memoryPool), _slot(slot) {}
|
||||
|
||||
VariantRef operator*() const {
|
||||
return VariantRef(_memoryPool, &_slot->value);
|
||||
return VariantRef(_memoryPool, _slot->getData());
|
||||
}
|
||||
VariantPtr operator->() {
|
||||
return VariantPtr(_memoryPool, &_slot->value);
|
||||
return VariantPtr(_memoryPool, _slot->getData());
|
||||
}
|
||||
|
||||
bool operator==(const ArrayIterator &other) const {
|
||||
@@ -88,10 +88,10 @@ class ArrayConstRefIterator {
|
||||
explicit ArrayConstRefIterator(const VariantSlot *slot) : _slot(slot) {}
|
||||
|
||||
VariantConstRef operator*() const {
|
||||
return VariantConstRef(&_slot->value);
|
||||
return VariantConstRef(_slot->getData());
|
||||
}
|
||||
VariantConstPtr operator->() {
|
||||
return VariantConstPtr(&_slot->value);
|
||||
return VariantConstPtr(_slot->getData());
|
||||
}
|
||||
|
||||
bool operator==(const ArrayConstRefIterator &other) const {
|
||||
|
||||
Reference in New Issue
Block a user