Skip to content

Commit 29a1c57

Browse files
committed
Add a pool dedicated to 8-byte values (double/int64_t/uint64_t)
This new pool replaced the "extension" slot where a secondary variant slot was used to store 8-byte values.
1 parent 943f22d commit 29a1c57

File tree

17 files changed

+127
-113
lines changed

17 files changed

+127
-113
lines changed

extras/conf_test/avr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
1212

1313
static_assert(ARDUINOJSON_USE_DOUBLE == 0, "ARDUINOJSON_USE_DOUBLE");
1414

15-
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 6, "slot size");
15+
static_assert(sizeof(ArduinoJson::detail::VariantData) == 6, "slot size");
1616

1717
void setup() {}
1818
void loop() {}

extras/conf_test/esp8266.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
1010

1111
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
1212

13-
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 8, "slot size");
13+
static_assert(sizeof(ArduinoJson::detail::VariantData) == 8, "slot size");
1414

1515
void setup() {}
1616
void loop() {}

extras/conf_test/x64.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
1010

1111
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
1212

13-
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 16,
14-
"slot size");
13+
static_assert(sizeof(ArduinoJson::detail::VariantData) == 16, "slot size");
1514

1615
int main() {}

extras/conf_test/x86.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
1010

1111
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
1212

13-
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 8, "slot size");
13+
static_assert(sizeof(ArduinoJson::detail::VariantData) == 8, "slot size");
1414

1515
int main() {}

extras/tests/Helpers/Allocators.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ inline size_t sizeofPoolList(size_t n = ARDUINOJSON_INITIAL_POOL_COUNT) {
269269
return sizeof(MemoryPool<VariantData>) * n;
270270
}
271271

272+
template <typename T = ArduinoJson::detail::VariantData>
272273
inline size_t sizeofPool(
273274
ArduinoJson::detail::SlotCount n = ARDUINOJSON_POOL_CAPACITY) {
274-
using namespace ArduinoJson::detail;
275-
return MemoryPool<VariantData>::slotsToBytes(n);
275+
return ArduinoJson::detail::MemoryPool<T>::slotsToBytes(n);
276276
}
277277

278278
inline size_t sizeofStaticStringPool(

extras/tests/JsonArray/add.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "Allocators.hpp"
99
#include "Literals.hpp"
1010

11-
using ArduinoJson::detail::sizeofArray;
11+
using namespace ArduinoJson::detail;
1212

1313
TEST_CASE("JsonArray::add(T)") {
1414
SpyingAllocator spy;
@@ -33,7 +33,8 @@ TEST_CASE("JsonArray::add(T)") {
3333
REQUIRE(array[0].is<double>());
3434
REQUIRE_FALSE(array[0].is<bool>());
3535
REQUIRE(spy.log() == AllocatorLog{
36-
Allocate(sizeofPool()),
36+
Allocate(sizeofPool<VariantData>()),
37+
Allocate(sizeofPool<EightByteValue>()),
3738
});
3839
}
3940

extras/tests/JsonDeserializer/array.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "Allocators.hpp"
99

10-
using ArduinoJson::detail::sizeofArray;
10+
using namespace ArduinoJson::detail;
1111

1212
TEST_CASE("deserialize JSON array") {
1313
SpyingAllocator spy;
@@ -92,8 +92,12 @@ TEST_CASE("deserialize JSON array") {
9292
REQUIRE(arr[0].as<double>() == Approx(4.2123456));
9393
REQUIRE(arr[1] == -7E89);
9494
REQUIRE(spy.log() == AllocatorLog{
95-
Allocate(sizeofPool()),
96-
Reallocate(sizeofPool(), sizeofPool(4)),
95+
Allocate(sizeofPool<VariantData>()),
96+
Allocate(sizeofPool<EightByteValue>()),
97+
Reallocate(sizeofPool<VariantData>(),
98+
sizeofPool<VariantData>(2)),
99+
Reallocate(sizeofPool<EightByteValue>(),
100+
sizeofPool<EightByteValue>(2)),
97101
});
98102
}
99103

extras/tests/JsonDeserializer/errors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ TEST_CASE("deserializeJson() returns NoMemory if string length overflows") {
121121
}
122122
}
123123

124-
TEST_CASE("deserializeJson() returns NoMemory if extension allocation fails") {
124+
TEST_CASE("deserializeJson() returns NoMemory if 8-bit slot allocation fails") {
125125
JsonDocument doc(FailingAllocator::instance());
126126

127127
SECTION("uint32_t should pass") {

extras/tests/JsonVariant/set.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "Allocators.hpp"
99
#include "Literals.hpp"
1010

11-
using ArduinoJson::detail::sizeofObject;
11+
using namespace ArduinoJson::detail;
1212

1313
enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
1414

@@ -197,11 +197,11 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
197197
REQUIRE(result == true);
198198
REQUIRE(variant.is<double>() == true);
199199
REQUIRE(variant.as<double>() == 1.2);
200-
REQUIRE(spy.log() ==
201-
AllocatorLog{
202-
Allocate(sizeofPool()),
203-
Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
204-
});
200+
REQUIRE(spy.log() == AllocatorLog{
201+
Allocate(sizeofPool<EightByteValue>()),
202+
Reallocate(sizeofPool<EightByteValue>(),
203+
sizeofPool<EightByteValue>(1)),
204+
});
205205
}
206206

207207
SECTION("int32_t") {
@@ -220,11 +220,11 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
220220
REQUIRE(result == true);
221221
REQUIRE(variant.is<int64_t>() == true);
222222
REQUIRE(variant.as<int64_t>() == -2147483649LL);
223-
REQUIRE(spy.log() ==
224-
AllocatorLog{
225-
Allocate(sizeofPool()),
226-
Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
227-
});
223+
REQUIRE(spy.log() == AllocatorLog{
224+
Allocate(sizeofPool<EightByteValue>()),
225+
Reallocate(sizeofPool<EightByteValue>(),
226+
sizeofPool<EightByteValue>(1)),
227+
});
228228
}
229229

230230
SECTION("uint32_t") {
@@ -243,11 +243,11 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
243243
REQUIRE(result == true);
244244
REQUIRE(variant.is<uint64_t>() == true);
245245
REQUIRE(variant.as<uint64_t>() == 4294967296);
246-
REQUIRE(spy.log() ==
247-
AllocatorLog{
248-
Allocate(sizeofPool()),
249-
Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
250-
});
246+
REQUIRE(spy.log() == AllocatorLog{
247+
Allocate(sizeofPool<EightByteValue>()),
248+
Reallocate(sizeofPool<EightByteValue>(),
249+
sizeofPool<EightByteValue>(1)),
250+
});
251251
}
252252

253253
SECTION("JsonDocument") {
@@ -378,7 +378,7 @@ TEST_CASE("JsonVariant::set() releases the previous value") {
378378
}
379379

380380
SECTION("float") {
381-
v.set(1.2);
381+
v.set(1.2f);
382382
REQUIRE(spy.log() == AllocatorLog{
383383
Deallocate(sizeofString("world")),
384384
});
@@ -393,7 +393,7 @@ TEST_CASE("JsonVariant::set() releases the previous value") {
393393
}
394394
}
395395

396-
TEST_CASE("JsonVariant::set() reuses extension slot") {
396+
TEST_CASE("JsonVariant::set() reuses 8-bit slot") {
397397
SpyingAllocator spy;
398398
JsonDocument doc(&spy);
399399
JsonVariant variant = doc.to<JsonVariant>();

src/ArduinoJson/Array/ArrayImpl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ inline bool ArrayData::addValue(const T& value, ResourceManager* resources) {
7373

7474
// Returns the size (in bytes) of an array with n elements.
7575
constexpr size_t sizeofArray(size_t n) {
76-
return n * ResourceManager::slotSize;
76+
return n * sizeof(VariantData);
7777
}
7878

7979
ARDUINOJSON_END_PRIVATE_NAMESPACE

0 commit comments

Comments
 (0)