7 #include <ArduinoJson/Memory/Alignment.hpp>
8 #include <ArduinoJson/Polyfills/assert.hpp>
9 #include <ArduinoJson/Polyfills/mpl/max.hpp>
10 #include <ArduinoJson/Variant/VariantSlot.hpp>
14 #define JSON_STRING_SIZE(SIZE) (SIZE + 1)
16 namespace ARDUINOJSON_NAMESPACE {
31 _right(buf ? buf + capa : 0),
32 _end(buf ? buf + capa : 0),
34 ARDUINOJSON_ASSERT(isAligned(_begin));
35 ARDUINOJSON_ASSERT(isAligned(_right));
36 ARDUINOJSON_ASSERT(isAligned(_end));
44 size_t capacity()
const {
45 return size_t(_end - _begin);
49 return size_t(_left - _begin + _end - _right);
52 bool overflowed()
const {
57 return allocRight<VariantSlot>();
60 template <
typename TAdaptedString>
61 const char* saveString(
const TAdaptedString& str) {
65 #if ARDUINOJSON_ENABLE_STRING_DEDUPLICATION
66 const char* existingCopy = findString(str.begin());
71 size_t n = str.size();
73 char* newCopy = allocString(n + 1);
75 str.copyTo(newCopy, n);
81 void getFreeZone(
char** zoneStart,
size_t* zoneSize)
const {
83 *zoneSize = size_t(_right - _left);
86 const char* saveStringFromFreeZone(
size_t len) {
87 #if ARDUINOJSON_ENABLE_STRING_DEDUPLICATION
88 const char* dup = findString(_left);
93 const char* str = _left;
99 void markAsOverflowed() {
109 bool canAlloc(
size_t bytes)
const {
110 return _left + bytes <= _right;
113 bool owns(
void* p)
const {
114 return _begin <= p && p < _end;
118 void*
operator new(size_t,
void* p) {
134 char* new_right = addPadding(_left);
135 if (new_right >= _right)
138 size_t right_size =
static_cast<size_t>(_end - _right);
139 memmove(new_right, _right, right_size);
141 ptrdiff_t bytes_reclaimed = _right - new_right;
143 _end = new_right + right_size;
144 return bytes_reclaimed;
149 void movePointers(ptrdiff_t offset) {
157 void checkInvariants() {
158 ARDUINOJSON_ASSERT(_begin <= _left);
159 ARDUINOJSON_ASSERT(_left <= _right);
160 ARDUINOJSON_ASSERT(_right <= _end);
161 ARDUINOJSON_ASSERT(isAligned(_right));
164 #if ARDUINOJSON_ENABLE_STRING_DEDUPLICATION
165 template <
typename TIterator>
166 const char* findString(TIterator str) {
167 for (
char* next = _begin; next < _left; ++next) {
171 for (TIterator it = str; *it == *next; ++it) {
177 while (*next) ++next;
183 char* allocString(
size_t n) {
194 template <
typename T>
196 return reinterpret_cast<T*
>(allocRight(
sizeof(T)));
199 void* allocRight(
size_t bytes) {
200 if (!canAlloc(bytes)) {
208 char *_begin, *_left, *_right, *_end;