JsonArray::remove() and JsonObject::remove() now release the memory of the variant

This commit is contained in:
Benoit Blanchon
2018-10-19 19:40:21 +02:00
parent ae089dcff7
commit d8d939660b
15 changed files with 174 additions and 37 deletions

View File

@@ -13,10 +13,11 @@ namespace ARDUINOJSON_NAMESPACE {
inline JsonVariantData* arrayAdd(JsonArrayData* arr, MemoryPool* pool) {
if (!arr) return 0;
Slot* slot = new (pool) Slot();
Slot* slot = pool->allocSlot();
if (!slot) return 0;
slot->next = 0;
slot->value.type = JSON_NULL;
if (arr->tail) {
slot->prev = arr->tail;
@@ -41,7 +42,7 @@ inline JsonVariantData* arrayGet(const JsonArrayData* arr, size_t index) {
return slot ? &slot->value : 0;
}
inline void arrayRemove(JsonArrayData* arr, Slot* slot) {
inline void arrayRemove(JsonArrayData* arr, Slot* slot, MemoryPool* pool) {
if (!arr || !slot) return;
if (slot->prev)
@@ -52,10 +53,12 @@ inline void arrayRemove(JsonArrayData* arr, Slot* slot) {
slot->next->prev = slot->prev;
else
arr->tail = slot->prev;
slotFree(slot, pool);
}
inline void arrayRemove(JsonArrayData* arr, size_t index) {
arrayRemove(arr, arrayGetSlot(arr, index));
inline void arrayRemove(JsonArrayData* arr, size_t index, MemoryPool* pool) {
arrayRemove(arr, arrayGetSlot(arr, index), pool);
}
inline void arrayClear(JsonArrayData* arr) {

View File

@@ -4,6 +4,7 @@
#pragma once
#include "../Memory/MemoryPool.hpp"
#include "JsonVariantData.hpp"
#include "SlotFunctions.hpp"
@@ -28,10 +29,11 @@ inline bool objectContainsKey(const JsonObjectData* obj, const TKey& key) {
template <typename TKey>
inline JsonVariantData* objectAdd(JsonObjectData* obj, TKey key,
MemoryPool* pool) {
Slot* slot = new (pool) Slot();
Slot* slot = pool->allocSlot();
if (!slot) return 0;
slot->next = 0;
slot->value.type = JSON_NULL;
if (obj->tail) {
slot->prev = obj->tail;
@@ -74,7 +76,7 @@ inline void objectClear(JsonObjectData* obj) {
obj->tail = 0;
}
inline void objectRemove(JsonObjectData* obj, Slot* slot) {
inline void objectRemove(JsonObjectData* obj, Slot* slot, MemoryPool* pool) {
if (!obj) return;
if (!slot) return;
if (slot->prev)
@@ -85,6 +87,8 @@ inline void objectRemove(JsonObjectData* obj, Slot* slot) {
slot->next->prev = slot->prev;
else
obj->tail = slot->prev;
slotFree(slot, pool);
}
inline size_t objectSize(const JsonObjectData* obj) {

View File

@@ -4,12 +4,11 @@
#pragma once
#include "../Memory/AllocableInMemoryPool.hpp"
#include "JsonVariantData.hpp"
namespace ARDUINOJSON_NAMESPACE {
struct Slot : AllocableInMemoryPool {
struct Slot {
JsonVariantData value;
struct Slot* next;
struct Slot* prev;

View File

@@ -4,6 +4,7 @@
#pragma once
#include "../Memory/MemoryPool.hpp"
#include "../Strings/StringTypes.hpp"
#include "JsonVariantData.hpp"
#include "Slot.hpp"
@@ -56,4 +57,15 @@ inline size_t slotSize(const Slot* slot) {
}
return n;
}
inline void slotFree(Slot* slot, MemoryPool* pool) {
const JsonVariantData& v = slot->value;
if (v.type == JSON_ARRAY || v.type == JSON_OBJECT) {
for (Slot* s = v.content.asObject.head; s; s = s->next) {
slotFree(s, pool);
}
}
pool->freeSlot(slot);
}
} // namespace ARDUINOJSON_NAMESPACE