Restored the monotonic allocator

This commit is contained in:
Benoit Blanchon
2018-11-30 14:27:33 +01:00
parent 637f7a5bfa
commit 45f4e5ac20
23 changed files with 35 additions and 609 deletions

View File

@@ -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