forked from bblanchon/ArduinoJson
Removed the automatic expansion of DynamicJsonDocument
This commit is contained in:
@ -7,10 +7,12 @@
|
||||
#include "ArduinoJson/Namespace.hpp"
|
||||
|
||||
#include "ArduinoJson/JsonArray.hpp"
|
||||
#include "ArduinoJson/JsonDocument.hpp"
|
||||
#include "ArduinoJson/JsonObject.hpp"
|
||||
#include "ArduinoJson/JsonVariant.hpp"
|
||||
|
||||
#include "ArduinoJson/DynamicJsonDocument.hpp"
|
||||
#include "ArduinoJson/StaticJsonDocument.hpp"
|
||||
|
||||
#include "ArduinoJson/Data/VariantAsImpl.hpp"
|
||||
#include "ArduinoJson/JsonArrayImpl.hpp"
|
||||
#include "ArduinoJson/JsonArraySubscript.hpp"
|
||||
|
49
src/ArduinoJson/DynamicJsonDocument.hpp
Normal file
49
src/ArduinoJson/DynamicJsonDocument.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonDocument.hpp"
|
||||
|
||||
#include <stdlib.h> // malloc, free
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class DynamicJsonDocument : public JsonDocument {
|
||||
public:
|
||||
DynamicJsonDocument(size_t capa = ARDUINOJSON_DEFAULT_POOL_SIZE)
|
||||
: JsonDocument(alloc(capa), addPadding(capa)) {}
|
||||
|
||||
DynamicJsonDocument(const DynamicJsonDocument& src)
|
||||
: JsonDocument(alloc(src.memoryUsage()), addPadding(src.memoryUsage())) {
|
||||
copy(src);
|
||||
}
|
||||
|
||||
DynamicJsonDocument(const JsonDocument& src)
|
||||
: JsonDocument(alloc(src.memoryUsage()), addPadding(src.memoryUsage())) {
|
||||
copy(src);
|
||||
}
|
||||
|
||||
~DynamicJsonDocument() {
|
||||
free(memoryPool().buffer());
|
||||
}
|
||||
|
||||
DynamicJsonDocument& operator=(const DynamicJsonDocument& src) {
|
||||
copy(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DynamicJsonDocument& operator=(const JsonDocument& src) {
|
||||
copy(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
static char* alloc(size_t capa) {
|
||||
return reinterpret_cast<char*>(malloc(addPadding(capa)));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -6,18 +6,14 @@
|
||||
|
||||
#include "Data/JsonVariantTo.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "Memory/DynamicMemoryPool.hpp"
|
||||
#include "Memory/StaticMemoryPool.hpp"
|
||||
#include "Memory/MemoryPool.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TMemoryPool>
|
||||
class JsonDocument : public Visitable {
|
||||
public:
|
||||
uint8_t nestingLimit;
|
||||
|
||||
JsonDocument() : nestingLimit(ARDUINOJSON_DEFAULT_NESTING_LIMIT) {}
|
||||
|
||||
template <typename Visitor>
|
||||
void accept(Visitor& visitor) const {
|
||||
return getVariant().accept(visitor);
|
||||
@ -47,7 +43,12 @@ class JsonDocument : public Visitable {
|
||||
return _memoryPool.size();
|
||||
}
|
||||
|
||||
TMemoryPool& memoryPool() {
|
||||
size_t capacity() const {
|
||||
return _memoryPool.capacity();
|
||||
}
|
||||
|
||||
// for internal use only
|
||||
MemoryPool& memoryPool() {
|
||||
return _memoryPool;
|
||||
}
|
||||
|
||||
@ -58,10 +59,13 @@ class JsonDocument : public Visitable {
|
||||
}
|
||||
|
||||
protected:
|
||||
template <typename T>
|
||||
void copy(const JsonDocument<T>& src) {
|
||||
JsonDocument(char* buf, size_t capa)
|
||||
: nestingLimit(ARDUINOJSON_DEFAULT_NESTING_LIMIT),
|
||||
_memoryPool(buf, capa) {}
|
||||
|
||||
void copy(const JsonDocument& src) {
|
||||
nestingLimit = src.nestingLimit;
|
||||
to<JsonVariant>().set(src.template as<JsonVariant>());
|
||||
to<JsonVariant>().set(src.as<JsonVariant>());
|
||||
}
|
||||
|
||||
private:
|
||||
@ -73,59 +77,8 @@ class JsonDocument : public Visitable {
|
||||
return JsonVariantConst(&_rootData);
|
||||
}
|
||||
|
||||
TMemoryPool _memoryPool;
|
||||
MemoryPool _memoryPool;
|
||||
JsonVariantData _rootData;
|
||||
};
|
||||
|
||||
class DynamicJsonDocument : public JsonDocument<DynamicMemoryPool> {
|
||||
public:
|
||||
DynamicJsonDocument() {}
|
||||
DynamicJsonDocument(size_t capacity) {
|
||||
memoryPool().reserve(capacity);
|
||||
}
|
||||
|
||||
DynamicJsonDocument(const DynamicJsonDocument& src) {
|
||||
memoryPool().reserve(src.memoryUsage());
|
||||
copy(src);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DynamicJsonDocument(const JsonDocument<T>& src) {
|
||||
memoryPool().reserve(src.memoryUsage());
|
||||
copy(src);
|
||||
}
|
||||
|
||||
DynamicJsonDocument& operator=(const DynamicJsonDocument& src) {
|
||||
copy(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DynamicJsonDocument& operator=(const JsonDocument<T>& src) {
|
||||
copy(src);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
template <size_t CAPACITY>
|
||||
class StaticJsonDocument : public JsonDocument<StaticMemoryPool<CAPACITY> > {
|
||||
public:
|
||||
StaticJsonDocument() {}
|
||||
|
||||
template <typename T>
|
||||
StaticJsonDocument(const JsonDocument<T>& src) {
|
||||
this->copy(src);
|
||||
}
|
||||
|
||||
StaticMemoryPoolBase& memoryPool() {
|
||||
return JsonDocument<StaticMemoryPool<CAPACITY> >::memoryPool();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
StaticJsonDocument operator=(const JsonDocument<T>& src) {
|
||||
this->copy(src);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -1,187 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Strings/StringInMemoryPool.hpp"
|
||||
#include "Alignment.hpp"
|
||||
#include "MemoryPool.hpp"
|
||||
#include "StaticMemoryPool.hpp"
|
||||
|
||||
#include <stdlib.h> // malloc, free
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic push
|
||||
#endif
|
||||
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
||||
#endif
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
class DefaultAllocator {
|
||||
public:
|
||||
void* allocate(size_t size) {
|
||||
return malloc(size);
|
||||
}
|
||||
void deallocate(void* pointer) {
|
||||
free(pointer);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TAllocator>
|
||||
class DynamicMemoryPoolBase : public MemoryPool {
|
||||
class Block : public StaticMemoryPoolBase {
|
||||
public:
|
||||
Block(char* buf, size_t sz, Block* nxt)
|
||||
: StaticMemoryPoolBase(buf, sz), next(nxt) {}
|
||||
Block* next;
|
||||
};
|
||||
|
||||
public:
|
||||
enum { EmptyBlockSize = sizeof(Block) };
|
||||
|
||||
DynamicMemoryPoolBase(size_t initialSize = ARDUINOJSON_DEFAULT_POOL_SIZE)
|
||||
: _head(NULL), _nextBlockCapacity(initialSize) {}
|
||||
|
||||
~DynamicMemoryPoolBase() {
|
||||
clear();
|
||||
}
|
||||
|
||||
void reserve(size_t capacity) {
|
||||
_nextBlockCapacity = capacity;
|
||||
}
|
||||
|
||||
virtual size_t size() const {
|
||||
size_t sum = 0;
|
||||
for (Block* b = _head; b; b = b->next) {
|
||||
sum += b->size();
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
virtual VariantSlot* allocVariant() {
|
||||
for (Block* b = _head; b; b = b->next) {
|
||||
VariantSlot* s = b->allocVariant();
|
||||
if (s) return s;
|
||||
}
|
||||
|
||||
if (!addNewBlock(sizeof(VariantSlot))) return 0;
|
||||
|
||||
return _head->allocVariant();
|
||||
}
|
||||
|
||||
virtual void freeVariant(VariantSlot* slot) {
|
||||
for (Block* b = _head; b; b = b->next) {
|
||||
if (b->owns(slot)) {
|
||||
b->freeVariant(slot);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual void freeString(StringSlot* slot) {
|
||||
for (Block* b = _head; b; b = b->next) {
|
||||
if (b->owns(slot)) {
|
||||
b->freeString(slot);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual StringSlot* allocFrozenString(size_t n) {
|
||||
for (Block* b = _head; b; b = b->next) {
|
||||
StringSlot* s = b->allocFrozenString(n);
|
||||
if (s) return s;
|
||||
}
|
||||
|
||||
if (!addNewBlock(sizeof(StringSlot) + n)) return 0;
|
||||
|
||||
return _head->allocFrozenString(n);
|
||||
}
|
||||
|
||||
virtual StringSlot* allocExpandableString() {
|
||||
for (Block* b = _head; b; b = b->next) {
|
||||
StringSlot* s = b->allocExpandableString();
|
||||
if (s) return s;
|
||||
}
|
||||
|
||||
if (!addNewBlock(sizeof(StringSlot))) return 0;
|
||||
|
||||
return _head->allocExpandableString();
|
||||
}
|
||||
|
||||
virtual StringSlot* expandString(StringSlot* oldSlot) {
|
||||
if (!addNewBlock(sizeof(StringSlot) + oldSlot->size)) return 0;
|
||||
|
||||
StringSlot* newSlot = _head->allocExpandableString();
|
||||
|
||||
ARDUINOJSON_ASSERT(newSlot->size > oldSlot->size);
|
||||
memcpy(newSlot->value, oldSlot->value, oldSlot->size);
|
||||
freeString(oldSlot);
|
||||
|
||||
return newSlot;
|
||||
}
|
||||
|
||||
virtual void freezeString(StringSlot* slot, size_t newSize) {
|
||||
for (Block* b = _head; b; b = b->next) {
|
||||
if (b->owns(slot)) {
|
||||
b->freezeString(slot, newSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Resets the memoryPool.
|
||||
// USE WITH CAUTION: this invalidates all previously allocated data
|
||||
void clear() {
|
||||
Block* currentBlock = _head;
|
||||
while (currentBlock != NULL) {
|
||||
_nextBlockCapacity = currentBlock->capacity();
|
||||
Block* nextBlock = currentBlock->next;
|
||||
_allocator.deallocate(currentBlock);
|
||||
currentBlock = nextBlock;
|
||||
}
|
||||
_head = 0;
|
||||
}
|
||||
|
||||
size_t blockCount() const {
|
||||
size_t sum = 0;
|
||||
for (Block* b = _head; b; b = b->next) sum++;
|
||||
return sum;
|
||||
}
|
||||
|
||||
private:
|
||||
bool addNewBlock(size_t minCapacity) {
|
||||
size_t capacity = _nextBlockCapacity;
|
||||
if (minCapacity > capacity) capacity = minCapacity;
|
||||
capacity = addPadding(capacity);
|
||||
size_t bytes = sizeof(Block) + capacity;
|
||||
char* p = reinterpret_cast<char*>(_allocator.allocate(bytes));
|
||||
if (!p) return false;
|
||||
Block* block = new (p) Block(p + sizeof(Block), capacity, _head);
|
||||
_nextBlockCapacity = capacity * 2;
|
||||
_head = block;
|
||||
return true;
|
||||
}
|
||||
|
||||
TAllocator _allocator;
|
||||
Block* _head;
|
||||
size_t _nextBlockCapacity;
|
||||
};
|
||||
|
||||
// Implements a MemoryPool with dynamic memory allocation.
|
||||
// You are strongly encouraged to consider using StaticMemoryPool which is much
|
||||
// more suitable for embedded systems.
|
||||
typedef DynamicMemoryPoolBase<DefaultAllocator> DynamicMemoryPool;
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
#endif
|
@ -4,31 +4,203 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h> // for size_t
|
||||
|
||||
#include "../Polyfills/attributes.hpp"
|
||||
#include "../Polyfills/assert.hpp"
|
||||
#include "../Polyfills/mpl/max.hpp"
|
||||
#include "../Strings/StringInMemoryPool.hpp"
|
||||
#include "Alignment.hpp"
|
||||
#include "MemoryPool.hpp"
|
||||
#include "SlotList.hpp"
|
||||
#include "StringSlot.hpp"
|
||||
#include "VariantSlot.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
// _begin _end
|
||||
// v v
|
||||
// +-------------+--------------+-----------+
|
||||
// | strings... | (free) | ...slots |
|
||||
// +-------------+--------------+-----------+
|
||||
// ^ ^
|
||||
// _left _right
|
||||
|
||||
class MemoryPool {
|
||||
class UpdateStringSlotAddress {
|
||||
public:
|
||||
UpdateStringSlotAddress(const char* address, size_t offset)
|
||||
: _address(address), _offset(offset) {}
|
||||
|
||||
void operator()(StringSlot* slot) const {
|
||||
ARDUINOJSON_ASSERT(slot != NULL);
|
||||
if (slot->value > _address) slot->value -= _offset;
|
||||
}
|
||||
|
||||
private:
|
||||
const char* _address;
|
||||
size_t _offset;
|
||||
};
|
||||
|
||||
public:
|
||||
virtual StringSlot *allocExpandableString() = 0;
|
||||
virtual StringSlot *allocFrozenString(size_t) = 0;
|
||||
virtual StringSlot *expandString(StringSlot *) = 0;
|
||||
virtual void freezeString(StringSlot *, size_t) = 0;
|
||||
virtual void freeString(StringSlot *) = 0;
|
||||
MemoryPool(char* buf, size_t capa)
|
||||
: _begin(buf),
|
||||
_left(buf),
|
||||
_right(buf ? buf + capa : 0),
|
||||
_end(buf ? buf + capa : 0) {
|
||||
ARDUINOJSON_ASSERT(isAligned(_begin));
|
||||
ARDUINOJSON_ASSERT(isAligned(_right));
|
||||
ARDUINOJSON_ASSERT(isAligned(_end));
|
||||
}
|
||||
|
||||
virtual VariantSlot *allocVariant() = 0;
|
||||
virtual void freeVariant(VariantSlot *) = 0;
|
||||
void* buffer() {
|
||||
return _begin;
|
||||
}
|
||||
|
||||
virtual size_t size() const = 0;
|
||||
// Gets the capacity of the memoryPool in bytes
|
||||
size_t capacity() const {
|
||||
return size_t(_end - _begin);
|
||||
}
|
||||
|
||||
protected:
|
||||
// CAUTION: NO VIRTUAL DESTRUCTOR!
|
||||
// If we add a virtual constructor the Arduino compiler will add malloc()
|
||||
// and free() to the binary, adding 706 useless bytes.
|
||||
~MemoryPool() {}
|
||||
size_t size() const {
|
||||
return allocated_bytes() - _freeVariants.size() - _freeStrings.size();
|
||||
}
|
||||
|
||||
VariantSlot* allocVariant() {
|
||||
VariantSlot* s = _freeVariants.pop();
|
||||
if (s) return s;
|
||||
return s ? s : allocRight<VariantSlot>();
|
||||
}
|
||||
|
||||
void freeVariant(VariantSlot* slot) {
|
||||
freeVariantSlot(slot);
|
||||
compactRightSide();
|
||||
}
|
||||
|
||||
void freeString(StringSlot* slot) {
|
||||
freeStringSlot(slot);
|
||||
compactLeftSide(slot->value, slot->size);
|
||||
compactRightSide();
|
||||
}
|
||||
|
||||
StringSlot* allocFrozenString(size_t n) {
|
||||
StringSlot* s = allocStringSlot();
|
||||
if (!s) return 0;
|
||||
if (!canAlloc(n)) return 0;
|
||||
|
||||
s->value = _left;
|
||||
s->size = n;
|
||||
_left += n;
|
||||
_usedString.push(s);
|
||||
checkInvariants();
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
StringSlot* allocExpandableString() {
|
||||
StringSlot* s = allocStringSlot();
|
||||
if (!s) return 0;
|
||||
|
||||
s->value = _left;
|
||||
s->size = size_t(_right - _left);
|
||||
_usedString.push(s);
|
||||
_left = _right;
|
||||
|
||||
checkInvariants();
|
||||
return s;
|
||||
}
|
||||
|
||||
void freezeString(StringSlot* slot, size_t newSize) {
|
||||
_left -= (slot->size - newSize);
|
||||
slot->size = newSize;
|
||||
checkInvariants();
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_left = _begin;
|
||||
_right = _end;
|
||||
_freeVariants.clear();
|
||||
_freeStrings.clear();
|
||||
_usedString.clear();
|
||||
}
|
||||
|
||||
bool canAlloc(size_t bytes) const {
|
||||
return _left + bytes <= _right;
|
||||
}
|
||||
|
||||
bool owns(void* p) const {
|
||||
return _begin <= p && p < _end;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T* allocRight() {
|
||||
return reinterpret_cast<T*>(allocRight(sizeof(T)));
|
||||
}
|
||||
|
||||
char* allocRight(size_t bytes) {
|
||||
if (!canAlloc(bytes)) return 0;
|
||||
_right -= bytes;
|
||||
return _right;
|
||||
}
|
||||
|
||||
// Workaround for missing placement new
|
||||
void* operator new(size_t, void* p) {
|
||||
return p;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t allocated_bytes() const {
|
||||
return size_t(_left - _begin + _end - _right);
|
||||
}
|
||||
|
||||
StringSlot* allocStringSlot() {
|
||||
StringSlot* s = _freeStrings.pop();
|
||||
if (s) return s;
|
||||
return allocRight<StringSlot>();
|
||||
}
|
||||
|
||||
void freeVariantSlot(VariantSlot* slot) {
|
||||
_freeVariants.push(slot);
|
||||
}
|
||||
|
||||
void freeStringSlot(StringSlot* slot) {
|
||||
_usedString.remove(slot);
|
||||
_freeStrings.push(slot);
|
||||
}
|
||||
|
||||
void compactLeftSide(char* holeAddress, size_t holeSize) {
|
||||
ARDUINOJSON_ASSERT(holeAddress >= _begin);
|
||||
ARDUINOJSON_ASSERT(holeAddress + holeSize <= _left);
|
||||
char* holeEnd = holeAddress + holeSize;
|
||||
memmove(holeAddress, // where the hole begun
|
||||
holeEnd, // where the hole ended
|
||||
size_t(_left - holeEnd)); // everything after the hole
|
||||
_left -= holeSize;
|
||||
_usedString.forEach(UpdateStringSlotAddress(holeAddress, holeSize));
|
||||
checkInvariants();
|
||||
}
|
||||
|
||||
void compactRightSide() {
|
||||
loop:
|
||||
if (_freeStrings.remove(_right)) {
|
||||
_right += sizeof(StringSlot);
|
||||
goto loop;
|
||||
}
|
||||
if (_freeVariants.remove(_right)) {
|
||||
_right += sizeof(VariantSlot);
|
||||
goto loop;
|
||||
}
|
||||
checkInvariants();
|
||||
}
|
||||
|
||||
void checkInvariants() {
|
||||
ARDUINOJSON_ASSERT(_begin <= _left);
|
||||
ARDUINOJSON_ASSERT(_left <= _right);
|
||||
ARDUINOJSON_ASSERT(_right <= _end);
|
||||
ARDUINOJSON_ASSERT(isAligned(_right));
|
||||
}
|
||||
|
||||
char *_begin, *_left, *_right, *_end;
|
||||
SlotList<VariantSlot> _freeVariants;
|
||||
SlotList<StringSlot> _freeStrings;
|
||||
SlotList<StringSlot> _usedString;
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -1,236 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Polyfills/assert.hpp"
|
||||
#include "../Polyfills/mpl/max.hpp"
|
||||
#include "../Strings/StringInMemoryPool.hpp"
|
||||
#include "Alignment.hpp"
|
||||
#include "MemoryPool.hpp"
|
||||
#include "SlotList.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
// _begin _end
|
||||
// v v
|
||||
// +-------------+--------------+-----------+
|
||||
// | strings... | (free) | ...slots |
|
||||
// +-------------+--------------+-----------+
|
||||
// ^ ^
|
||||
// _left _right
|
||||
|
||||
class StaticMemoryPoolBase : public MemoryPool {
|
||||
class UpdateStringSlotAddress {
|
||||
public:
|
||||
UpdateStringSlotAddress(const char* address, size_t offset)
|
||||
: _address(address), _offset(offset) {}
|
||||
|
||||
void operator()(StringSlot* slot) const {
|
||||
ARDUINOJSON_ASSERT(slot != NULL);
|
||||
if (slot->value > _address) slot->value -= _offset;
|
||||
}
|
||||
|
||||
private:
|
||||
const char* _address;
|
||||
size_t _offset;
|
||||
};
|
||||
|
||||
public:
|
||||
// Gets the capacity of the memoryPool in bytes
|
||||
size_t capacity() const {
|
||||
return size_t(_end - _begin);
|
||||
}
|
||||
|
||||
virtual size_t size() const {
|
||||
return allocated_bytes() - _freeVariants.size() - _freeStrings.size();
|
||||
}
|
||||
|
||||
virtual VariantSlot* allocVariant() {
|
||||
VariantSlot* s = _freeVariants.pop();
|
||||
if (s) return s;
|
||||
return s ? s : allocRight<VariantSlot>();
|
||||
}
|
||||
|
||||
virtual void freeVariant(VariantSlot* slot) {
|
||||
freeVariantSlot(slot);
|
||||
compactRightSide();
|
||||
}
|
||||
|
||||
virtual void freeString(StringSlot* slot) {
|
||||
freeStringSlot(slot);
|
||||
compactLeftSide(slot->value, slot->size);
|
||||
compactRightSide();
|
||||
}
|
||||
|
||||
virtual StringSlot* allocFrozenString(size_t n) {
|
||||
StringSlot* s = allocStringSlot();
|
||||
if (!s) return 0;
|
||||
if (!canAlloc(n)) return 0;
|
||||
|
||||
s->value = _left;
|
||||
s->size = n;
|
||||
_left += n;
|
||||
_usedString.push(s);
|
||||
checkInvariants();
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
virtual StringSlot* allocExpandableString() {
|
||||
StringSlot* s = allocStringSlot();
|
||||
if (!s) return 0;
|
||||
|
||||
s->value = _left;
|
||||
s->size = size_t(_right - _left);
|
||||
_usedString.push(s);
|
||||
_left = _right;
|
||||
|
||||
checkInvariants();
|
||||
return s;
|
||||
}
|
||||
|
||||
virtual StringSlot* expandString(StringSlot*) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual void freezeString(StringSlot* slot, size_t newSize) {
|
||||
_left -= (slot->size - newSize);
|
||||
slot->size = newSize;
|
||||
checkInvariants();
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_left = _begin;
|
||||
_right = _end;
|
||||
_freeVariants.clear();
|
||||
_freeStrings.clear();
|
||||
_usedString.clear();
|
||||
}
|
||||
|
||||
bool canAlloc(size_t bytes) const {
|
||||
return _left + bytes <= _right;
|
||||
}
|
||||
|
||||
bool owns(void* p) const {
|
||||
return _begin <= p && p < _end;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T* allocRight() {
|
||||
return reinterpret_cast<T*>(allocRight(sizeof(T)));
|
||||
}
|
||||
|
||||
char* allocRight(size_t bytes) {
|
||||
if (!canAlloc(bytes)) return 0;
|
||||
_right -= bytes;
|
||||
return _right;
|
||||
}
|
||||
|
||||
// Workaround for missing placement new
|
||||
void* operator new(size_t, void* p) {
|
||||
return p;
|
||||
}
|
||||
|
||||
protected:
|
||||
StaticMemoryPoolBase(char* buffer, size_t capa)
|
||||
: _begin(buffer),
|
||||
_left(buffer),
|
||||
_right(buffer + capa),
|
||||
_end(buffer + capa) {}
|
||||
|
||||
~StaticMemoryPoolBase() {}
|
||||
|
||||
// Gets the current usage of the memoryPool in bytes
|
||||
size_t allocated_bytes() const {
|
||||
return size_t(_left - _begin + _end - _right);
|
||||
}
|
||||
|
||||
private:
|
||||
StringSlot* allocStringSlot() {
|
||||
StringSlot* s = _freeStrings.pop();
|
||||
if (s) return s;
|
||||
return allocRight<StringSlot>();
|
||||
}
|
||||
|
||||
void freeVariantSlot(VariantSlot* slot) {
|
||||
_freeVariants.push(slot);
|
||||
}
|
||||
|
||||
void freeStringSlot(StringSlot* slot) {
|
||||
_usedString.remove(slot);
|
||||
_freeStrings.push(slot);
|
||||
}
|
||||
|
||||
void compactLeftSide(char* holeAddress, size_t holeSize) {
|
||||
ARDUINOJSON_ASSERT(holeAddress >= _begin);
|
||||
ARDUINOJSON_ASSERT(holeAddress + holeSize <= _left);
|
||||
char* holeEnd = holeAddress + holeSize;
|
||||
memmove(holeAddress, // where the hole begun
|
||||
holeEnd, // where the hole ended
|
||||
size_t(_left - holeEnd)); // everything after the hole
|
||||
_left -= holeSize;
|
||||
_usedString.forEach(UpdateStringSlotAddress(holeAddress, holeSize));
|
||||
checkInvariants();
|
||||
}
|
||||
|
||||
void compactRightSide() {
|
||||
loop:
|
||||
if (_freeStrings.remove(_right)) {
|
||||
_right += sizeof(StringSlot);
|
||||
goto loop;
|
||||
}
|
||||
if (_freeVariants.remove(_right)) {
|
||||
_right += sizeof(VariantSlot);
|
||||
goto loop;
|
||||
}
|
||||
checkInvariants();
|
||||
}
|
||||
|
||||
void checkInvariants() {
|
||||
ARDUINOJSON_ASSERT(_begin <= _left);
|
||||
ARDUINOJSON_ASSERT(_left <= _right);
|
||||
ARDUINOJSON_ASSERT(_right <= _end);
|
||||
}
|
||||
|
||||
char *_begin, *_left, *_right, *_end;
|
||||
SlotList<VariantSlot> _freeVariants;
|
||||
SlotList<StringSlot> _freeStrings;
|
||||
SlotList<StringSlot> _usedString;
|
||||
}; // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic push
|
||||
#endif
|
||||
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
||||
#endif
|
||||
|
||||
// Implements a MemoryPool with fixed memory allocation.
|
||||
// The template paramenter CAPACITY specifies the capacity of the memoryPool in
|
||||
// bytes.
|
||||
template <size_t CAPACITY>
|
||||
class StaticMemoryPool : public StaticMemoryPoolBase {
|
||||
static const size_t ACTUAL_CAPACITY =
|
||||
AddPadding<Max<1, CAPACITY>::value>::value;
|
||||
|
||||
public:
|
||||
explicit StaticMemoryPool()
|
||||
: StaticMemoryPoolBase(_buffer, ACTUAL_CAPACITY) {}
|
||||
|
||||
private:
|
||||
char _buffer[ACTUAL_CAPACITY];
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
#endif
|
@ -29,8 +29,8 @@ class StringBuilder {
|
||||
if (!_slot) return;
|
||||
|
||||
if (_size >= _slot->size) {
|
||||
_slot = _parent->expandString(_slot);
|
||||
if (!_slot) return;
|
||||
_slot = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
_slot->value[_size++] = c;
|
||||
|
33
src/ArduinoJson/StaticJsonDocument.hpp
Normal file
33
src/ArduinoJson/StaticJsonDocument.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonDocument.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <size_t CAPACITY>
|
||||
class StaticJsonDocument : public JsonDocument {
|
||||
static const size_t ACTUAL_CAPACITY =
|
||||
AddPadding<Max<1, CAPACITY>::value>::value;
|
||||
|
||||
public:
|
||||
StaticJsonDocument() : JsonDocument(_buffer, ACTUAL_CAPACITY) {}
|
||||
|
||||
StaticJsonDocument(const JsonDocument& src)
|
||||
: JsonDocument(_buffer, ACTUAL_CAPACITY) {
|
||||
copy(src);
|
||||
}
|
||||
|
||||
StaticJsonDocument operator=(const JsonDocument& src) {
|
||||
copy(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
char _buffer[ACTUAL_CAPACITY];
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
Reference in New Issue
Block a user