mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-02 00:38:26 +01:00
Added BasicJsonDocument::shrinkToFit()
This commit is contained in:
@@ -21,6 +21,12 @@ inline size_t addPadding(size_t bytes) {
|
||||
return (bytes + mask) & ~mask;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T *addPadding(T *p) {
|
||||
size_t address = addPadding(reinterpret_cast<size_t>(p));
|
||||
return reinterpret_cast<T *>(address);
|
||||
}
|
||||
|
||||
template <size_t bytes>
|
||||
struct AddPadding {
|
||||
static const size_t mask = sizeof(void *) - 1;
|
||||
|
||||
@@ -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>();
|
||||
|
||||
Reference in New Issue
Block a user