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

This commit is contained in:
Benoit Blanchon
2018-11-09 17:27:32 +01:00
parent e842838a23
commit f375459d53
68 changed files with 1504 additions and 740 deletions

View File

@@ -13,12 +13,12 @@ class ArduinoString {
ArduinoString(const ::String& str) : _str(&str) {}
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
StringSlot* save(TMemoryPool* memoryPool) const {
if (isNull()) return NULL;
size_t n = _str->length() + 1;
void* dup = memoryPool->alloc(n);
if (dup != NULL) memcpy(dup, _str->c_str(), n);
return static_cast<const char*>(dup);
StringSlot* slot = memoryPool->allocFrozenString(n);
if (slot) memcpy(slot->value, _str->c_str(), n);
return slot;
}
bool isNull() const {

View File

@@ -22,11 +22,11 @@ class FixedSizeFlashString {
}
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
StringSlot* save(TMemoryPool* memoryPool) const {
if (!_str) return NULL;
void* dup = memoryPool->alloc(_size);
if (dup != NULL) memcpy_P(dup, (const char*)_str, _size);
return static_cast<const char*>(dup);
StringSlot* slot = memoryPool->allocFrozenString(_size);
if (!slot) memcpy_P(slot->value, (const char*)_str, _size);
return slot;
}
size_t size() const {

View File

@@ -23,12 +23,11 @@ class FixedSizeRamString {
}
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
StringSlot* save(TMemoryPool* memoryPool) const {
if (!_str) return NULL;
void* dup = memoryPool->alloc(_size);
if (!dup) return NULL;
memcpy(dup, _str, _size);
return static_cast<const char*>(dup);
StringSlot* slot = memoryPool->allocFrozenString(_size);
if (slot) memcpy(slot->value, _str, _size);
return slot;
}
size_t size() const {

View File

@@ -13,11 +13,11 @@ class StlString {
StlString(const std::string& str) : _str(&str) {}
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
StringSlot* save(TMemoryPool* memoryPool) const {
size_t n = _str->length() + 1;
void* dup = memoryPool->alloc(n);
if (dup != NULL) memcpy(dup, _str->c_str(), n);
return static_cast<const char*>(dup);
StringSlot* slot = memoryPool->allocFrozenString(n);
if (slot) memcpy(slot->value, _str->c_str(), n);
return slot;
}
bool isNull() const {

View File

@@ -4,13 +4,45 @@
#pragma once
#include "ZeroTerminatedRamStringConst.hpp"
#include <string.h>
#include "../Memory/StringSlot.hpp"
namespace ARDUINOJSON_NAMESPACE {
class StringInMemoryPool : public ZeroTerminatedRamStringConst {
class StringInMemoryPool {
public:
StringInMemoryPool(const char* str = 0) : ZeroTerminatedRamStringConst(str) {}
StringInMemoryPool(StringSlot* s = 0) : _slot(s) {}
bool equals(const char* expected) const {
if (!_slot) return expected == 0;
const char* actual = _slot->value;
if (actual == expected) return true;
return strcmp(actual, expected) == 0;
}
bool isNull() const {
return !_slot;
}
template <typename TMemoryPool>
StringSlot* save(TMemoryPool*) const {
return _slot;
}
size_t size() const {
return _slot->size;
}
StringSlot* slot() const {
return _slot;
}
const char* c_str() const {
return _slot->value;
}
protected:
StringSlot* _slot;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -21,12 +21,12 @@ class ZeroTerminatedFlashString {
}
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
StringSlot* save(TMemoryPool* memoryPool) const {
if (!_str) return NULL;
size_t n = size() + 1; // copy the terminator
void* dup = memoryPool->alloc(n);
if (dup != NULL) memcpy_P(dup, (const char*)_str, n);
return static_cast<const char*>(dup);
StringSlot* slot = memoryPool->allocFrozenString(n);
if (slot) memcpy_P(slot->value, reinterpret_cast<const char*>(_str), n);
return slot;
}
size_t size() const {

View File

@@ -14,13 +14,12 @@ class ZeroTerminatedRamString : public ZeroTerminatedRamStringConst {
: ZeroTerminatedRamStringConst(str) {}
template <typename TMemoryPool>
const char* save(TMemoryPool* memoryPool) const {
StringSlot* save(TMemoryPool* memoryPool) const {
if (!_str) return NULL;
size_t n = size() + 1;
void* dup = memoryPool->alloc(n);
if (!dup) return NULL;
memcpy(dup, _str, n);
return static_cast<const char*>(dup);
StringSlot* slot = memoryPool->allocFrozenString(n);
if (slot) memcpy(slot->value, _str, n);
return slot;
}
};

View File

@@ -11,7 +11,7 @@ namespace ARDUINOJSON_NAMESPACE {
class ZeroTerminatedRamStringConst {
public:
ZeroTerminatedRamStringConst(const char* str) : _str(str) {}
ZeroTerminatedRamStringConst(const char* str = 0) : _str(str) {}
bool equals(const char* expected) const {
const char* actual = _str;
@@ -23,15 +23,19 @@ class ZeroTerminatedRamStringConst {
return !_str;
}
template <typename TMemoryPool>
const char* save(TMemoryPool*) const {
return _str;
}
// template <typename TMemoryPool>
// const char* save(TMemoryPool*) const {
// return _str;
// }
size_t size() const {
return strlen(_str);
}
const char* c_str() const {
return _str;
}
protected:
const char* _str;
};