From c711fe592db35e57c9273bdd2f1bdc60f609edf6 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Wed, 28 Oct 2020 09:23:26 +0100 Subject: [PATCH] Allowed more than 32767 values in non-embedded mode (fixes #1414) --- CHANGELOG.md | 1 + src/ArduinoJson/Configuration.hpp | 17 ++++++++++++++ src/ArduinoJson/Polyfills/integer.hpp | 30 +++++++++++++++++++++++++ src/ArduinoJson/Variant/VariantSlot.hpp | 9 ++++---- 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 src/ArduinoJson/Polyfills/integer.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 98254812..15e20f43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ HEAD * Fixed error `ambiguous overload for 'operator|'` (issue #1411) * Fixed `operator|(MemberProxy, JsonObject)` (issue #1415) +* Allowed more than 32767 values in non-embedded mode (issue #1414) v6.17.0 (2020-10-19) ------- diff --git a/src/ArduinoJson/Configuration.hpp b/src/ArduinoJson/Configuration.hpp index 42ba498e..32a6f339 100644 --- a/src/ArduinoJson/Configuration.hpp +++ b/src/ArduinoJson/Configuration.hpp @@ -83,6 +83,18 @@ #define ARDUINOJSON_DEFAULT_NESTING_LIMIT 10 #endif +// Number of bits to store the pointer to next node +// (saves RAM but limits the number of values in a document) +#ifndef ARDUINOJSON_SLOT_OFFSET_SIZE +#if defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 2 +// Address space == 16-bit => max 127 values +#define ARDUINOJSON_SLOT_OFFSET_SIZE 1 +#else +// Address space > 16-bit => max 32767 values +#define ARDUINOJSON_SLOT_OFFSET_SIZE 2 +#endif +#endif + #else // ARDUINOJSON_EMBEDDED_MODE // On a computer we have plenty of memory so we can use doubles @@ -114,6 +126,11 @@ #define ARDUINOJSON_DEFAULT_NESTING_LIMIT 50 #endif +// Number of bits to store the pointer to next node +#ifndef ARDUINOJSON_SLOT_OFFSET_SIZE +#define ARDUINOJSON_SLOT_OFFSET_SIZE 4 +#endif + #endif // ARDUINOJSON_EMBEDDED_MODE #ifdef ARDUINO diff --git a/src/ArduinoJson/Polyfills/integer.hpp b/src/ArduinoJson/Polyfills/integer.hpp new file mode 100644 index 00000000..8dfebb09 --- /dev/null +++ b/src/ArduinoJson/Polyfills/integer.hpp @@ -0,0 +1,30 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2020 +// MIT License + +#pragma once + +#include // int8_t, int16_t + +#include + +namespace ARDUINOJSON_NAMESPACE { + +template +struct int_t; + +template <> +struct int_t<8> { + typedef int8_t type; +}; + +template <> +struct int_t<16> { + typedef int16_t type; +}; + +template <> +struct int_t<32> { + typedef int32_t type; +}; +} // namespace ARDUINOJSON_NAMESPACE diff --git a/src/ArduinoJson/Variant/VariantSlot.hpp b/src/ArduinoJson/Variant/VariantSlot.hpp index 9a62954d..38494f3b 100644 --- a/src/ArduinoJson/Variant/VariantSlot.hpp +++ b/src/ArduinoJson/Variant/VariantSlot.hpp @@ -4,8 +4,7 @@ #pragma once -#include // int8_t, int16_t - +#include #include #include #include @@ -13,7 +12,7 @@ namespace ARDUINOJSON_NAMESPACE { -typedef conditional::type VariantSlotDiff; +typedef int_t::type VariantSlotDiff; class VariantSlot { // CAUTION: same layout as VariantData @@ -63,9 +62,9 @@ class VariantSlot { void setNext(VariantSlot* slot) { ARDUINOJSON_ASSERT(!slot || slot - this >= - numeric_limits::lowest()); + numeric_limits::lowest()); ARDUINOJSON_ASSERT(!slot || slot - this <= - numeric_limits::highest()); + numeric_limits::highest()); _next = VariantSlotDiff(slot ? slot - this : 0); }