Added BasicJsonDocument::shrinkToFit()

This commit is contained in:
Benoit Blanchon
2019-11-07 15:40:20 +01:00
parent 1b8107094f
commit 062c1c13b5
13 changed files with 270 additions and 19 deletions

View File

@@ -10,13 +10,15 @@
#include <ArduinoJson/Polyfills/mpl/max.hpp>
#include <ArduinoJson/Variant/VariantSlot.hpp>
#include <string.h> // memmove
namespace ARDUINOJSON_NAMESPACE {
// _begin _end
// v v
// +-------------+--------------+-----------+
// | strings... | (free) | ...slots |
// +-------------+--------------+-----------+
// _begin _end
// v v
// +-------------+--------------+--------------+
// | strings... | (free) | ...variants |
// +-------------+--------------+--------------+
// ^ ^
// _left _right
@@ -101,6 +103,39 @@ class MemoryPool {
return p;
}
// Squash the free space between strings and variants
//
// _begin _end
// v v
// +-------------+--------------+
// | strings... | ...variants |
// +-------------+--------------+
// ^
// _left _right
//
// This funcion is called before a realloc.
ptrdiff_t squash() {
char* new_right = addPadding(_left);
if (new_right >= _right) return 0;
size_t right_size = static_cast<size_t>(_end - _right);
memmove(new_right, _right, right_size);
ptrdiff_t bytes_reclaimed = _right - new_right;
_right = new_right;
_end = new_right + right_size;
return bytes_reclaimed;
}
// Move all pointers together
// This funcion is called after a realloc.
void movePointers(ptrdiff_t offset) {
_begin += offset;
_left += offset;
_right += offset;
_end += offset;
}
private:
StringSlot* allocStringSlot() {
return allocRight<StringSlot>();