mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-02 08:48:27 +01:00
Restored the monotonic allocator
This commit is contained in:
@@ -9,7 +9,6 @@
|
||||
#include "../Strings/StringInMemoryPool.hpp"
|
||||
#include "Alignment.hpp"
|
||||
#include "MemoryPool.hpp"
|
||||
#include "SlotList.hpp"
|
||||
#include "StringSlot.hpp"
|
||||
#include "VariantSlot.hpp"
|
||||
|
||||
@@ -60,24 +59,11 @@ class MemoryPool {
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return allocated_bytes() - _freeVariants.size() - _freeStrings.size();
|
||||
return size_t(_left - _begin + _end - _right);
|
||||
}
|
||||
|
||||
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();
|
||||
return allocRight<VariantSlot>();
|
||||
}
|
||||
|
||||
StringSlot* allocFrozenString(size_t n) {
|
||||
@@ -88,7 +74,6 @@ class MemoryPool {
|
||||
s->value = _left;
|
||||
s->size = n;
|
||||
_left += n;
|
||||
_usedString.push(s);
|
||||
checkInvariants();
|
||||
|
||||
return s;
|
||||
@@ -100,7 +85,6 @@ class MemoryPool {
|
||||
|
||||
s->value = _left;
|
||||
s->size = size_t(_right - _left);
|
||||
_usedString.push(s);
|
||||
_left = _right;
|
||||
|
||||
checkInvariants();
|
||||
@@ -116,9 +100,6 @@ class MemoryPool {
|
||||
void clear() {
|
||||
_left = _begin;
|
||||
_right = _end;
|
||||
_freeVariants.clear();
|
||||
_freeStrings.clear();
|
||||
_usedString.clear();
|
||||
}
|
||||
|
||||
bool canAlloc(size_t bytes) const {
|
||||
@@ -146,50 +127,10 @@ class MemoryPool {
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -198,9 +139,6 @@ class MemoryPool {
|
||||
}
|
||||
|
||||
char *_begin, *_left, *_right, *_end;
|
||||
SlotList<VariantSlot> _freeVariants;
|
||||
SlotList<StringSlot> _freeStrings;
|
||||
SlotList<StringSlot> _usedString;
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h> // for size_t
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TSlot>
|
||||
class SlotList {
|
||||
public:
|
||||
SlotList() : _head(0) {}
|
||||
|
||||
TSlot *pop() {
|
||||
TSlot *slot = _head;
|
||||
if (slot) _head = slot->next;
|
||||
return slot;
|
||||
}
|
||||
|
||||
void push(TSlot *slot) {
|
||||
slot->next = _head;
|
||||
_head = slot;
|
||||
}
|
||||
|
||||
bool remove(const TSlot *slot) {
|
||||
if (_head == slot) {
|
||||
_head = slot->next;
|
||||
return true;
|
||||
}
|
||||
|
||||
for (TSlot *s = _head; s; s = s->next) {
|
||||
if (s->next == slot) {
|
||||
s->next = slot->next;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool remove(const void *slot) {
|
||||
return remove(reinterpret_cast<const TSlot *>(slot));
|
||||
}
|
||||
|
||||
template <typename Functor>
|
||||
void forEach(const Functor &f) {
|
||||
for (TSlot *s = _head; s; s = s->next) {
|
||||
f(s);
|
||||
}
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
size_t sum = 0;
|
||||
for (TSlot *s = _head; s; s = s->next) sum += sizeof(TSlot);
|
||||
return sum;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_head = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
TSlot *_head;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
Reference in New Issue
Block a user