Added JsonDocument::overflowed() (closes #1358)

This commit is contained in:
Benoit Blanchon
2020-09-05 10:54:46 +02:00
parent 6d2ad4539f
commit 8d37939086
13 changed files with 155 additions and 64 deletions

View File

@@ -29,7 +29,8 @@ class MemoryPool {
: _begin(buf),
_left(buf),
_right(buf ? buf + capa : 0),
_end(buf ? buf + capa : 0) {
_end(buf ? buf + capa : 0),
_overflowed(false) {
ARDUINOJSON_ASSERT(isAligned(_begin));
ARDUINOJSON_ASSERT(isAligned(_right));
ARDUINOJSON_ASSERT(isAligned(_end));
@@ -48,6 +49,10 @@ class MemoryPool {
return size_t(_left - _begin + _end - _right);
}
bool overflowed() const {
return _overflowed;
}
VariantSlot* allocVariant() {
return allocRight<VariantSlot>();
}
@@ -91,9 +96,14 @@ class MemoryPool {
return str;
}
void markAsOverflowed() {
_overflowed = true;
}
void clear() {
_left = _begin;
_right = _end;
_overflowed = false;
}
bool canAlloc(size_t bytes) const {
@@ -171,8 +181,10 @@ class MemoryPool {
#endif
char* allocString(size_t n) {
if (!canAlloc(n))
if (!canAlloc(n)) {
_overflowed = true;
return 0;
}
char* s = _left;
_left += n;
checkInvariants();
@@ -185,13 +197,16 @@ class MemoryPool {
}
void* allocRight(size_t bytes) {
if (!canAlloc(bytes))
if (!canAlloc(bytes)) {
_overflowed = true;
return 0;
}
_right -= bytes;
return _right;
}
char *_begin, *_left, *_right, *_end;
bool _overflowed;
};
} // namespace ARDUINOJSON_NAMESPACE