Use only one byte for the string length on 8-bit platforms

This commit is contained in:
Benoit Blanchon
2023-10-09 14:53:16 +02:00
parent d20e64187b
commit 7e6b89d21f
2 changed files with 15 additions and 5 deletions

View File

@ -122,6 +122,15 @@
# endif
#endif
// Number of bytes to store the length of a string
#ifndef ARDUINOJSON_STRING_LENGTH_SIZE
# if ARDUINOJSON_SIZEOF_POINTER <= 2
# define ARDUINOJSON_STRING_LENGTH_SIZE 1 // up to 255 characters
# else
# define ARDUINOJSON_STRING_LENGTH_SIZE 2 // up to 65535 characters
# endif
#endif
#ifdef ARDUINO
// Enable support for Arduino's String class

View File

@ -11,7 +11,6 @@
#include <ArduinoJson/Polyfills/limits.hpp>
#include <stddef.h> // offsetof
#include <stdint.h> // uint16_t
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
@ -20,12 +19,14 @@ struct StringNode {
// (there can never be more references than slots)
using references_type = uint_t<ARDUINOJSON_SLOT_ID_SIZE * 8>::type;
using length_type = uint_t<ARDUINOJSON_STRING_LENGTH_SIZE * 8>::type;
struct StringNode* next;
uint16_t length;
length_type length;
references_type references;
char data[1];
static constexpr size_t maxLength = numeric_limits<uint16_t>::highest();
static constexpr size_t maxLength = numeric_limits<length_type>::highest();
static constexpr size_t sizeForLength(size_t n) {
return n + 1 + offsetof(StringNode, data);
@ -37,7 +38,7 @@ struct StringNode {
auto node = reinterpret_cast<StringNode*>(
allocator->allocate(sizeForLength(length)));
if (node) {
node->length = uint16_t(length);
node->length = length_type(length);
node->references = 1;
}
return node;
@ -53,7 +54,7 @@ struct StringNode {
else
newNode = nullptr;
if (newNode)
newNode->length = uint16_t(length);
newNode->length = length_type(length);
else
allocator->deallocate(node);
return newNode;