Replace sizeof(VariantData) with sizeof(SlotData)

This commit is contained in:
Benoit Blanchon
2024-09-10 18:26:58 +02:00
parent d92eee8736
commit a1809d0f31
8 changed files with 11 additions and 12 deletions

View File

@ -10,8 +10,7 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
static_assert(ARDUINOJSON_USE_DOUBLE == 0, "ARDUINOJSON_USE_DOUBLE"); static_assert(ARDUINOJSON_USE_DOUBLE == 0, "ARDUINOJSON_USE_DOUBLE");
static_assert(sizeof(ArduinoJson::detail::VariantData) == 6, static_assert(ArduinoJson::detail::ResourceManager::slotSize == 6, "slot size");
"sizeof(VariantData)");
void setup() {} void setup() {}
void loop() {} void loop() {}

View File

@ -8,8 +8,7 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE"); static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
static_assert(sizeof(ArduinoJson::detail::VariantData) == 8, static_assert(ArduinoJson::detail::ResourceManager::slotSize == 8, "slot size");
"sizeof(VariantData)");
void setup() {} void setup() {}
void loop() {} void loop() {}

View File

@ -8,7 +8,7 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE"); static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
static_assert(sizeof(ArduinoJson::detail::VariantData) == 16, static_assert(ArduinoJson::detail::ResourceManager::slotSize == 16,
"sizeof(VariantData)"); "slot size");
int main() {} int main() {}

View File

@ -8,7 +8,6 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE"); static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
static_assert(sizeof(ArduinoJson::detail::VariantData) == 8, static_assert(ArduinoJson::detail::ResourceManager::slotSize == 8, "slot size");
"sizeof(VariantData)");
int main() {} int main() {}

View File

@ -28,7 +28,7 @@ TEST_CASE("ResourceManager::shrinkToFit()") {
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() ==
AllocatorLog{ AllocatorLog{
Allocate(sizeofPool()), Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeof(VariantData)), Reallocate(sizeofPool(), sizeofPool(1)),
}); });
} }
@ -49,7 +49,7 @@ TEST_CASE("ResourceManager::shrinkToFit()") {
REQUIRE(spyingAllocator.log() == REQUIRE(spyingAllocator.log() ==
AllocatorLog{ AllocatorLog{
Reallocate(sizeofPool(), sizeof(VariantData)), Reallocate(sizeofPool(), sizeofPool(1)),
Reallocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2), Reallocate(sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT * 2),
sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT + 1)), sizeofPoolList(ARDUINOJSON_INITIAL_POOL_COUNT + 1)),
}); });

View File

@ -73,7 +73,7 @@ inline bool ArrayData::addValue(T&& value, ResourceManager* resources) {
// Returns the size (in bytes) of an array with n elements. // Returns the size (in bytes) of an array with n elements.
constexpr size_t sizeofArray(size_t n) { constexpr size_t sizeofArray(size_t n) {
return n * sizeof(VariantData); return n * ResourceManager::slotSize;
} }
ARDUINOJSON_END_PRIVATE_NAMESPACE ARDUINOJSON_END_PRIVATE_NAMESPACE

View File

@ -26,6 +26,8 @@ class ResourceManager {
}; };
public: public:
constexpr static size_t slotSize = sizeof(SlotData);
ResourceManager(Allocator* allocator = DefaultAllocator::instance()) ResourceManager(Allocator* allocator = DefaultAllocator::instance())
: allocator_(allocator), overflowed_(false) {} : allocator_(allocator), overflowed_(false) {}

View File

@ -70,7 +70,7 @@ inline VariantData* ObjectData::addMember(TAdaptedString key,
// Returns the size (in bytes) of an object with n members. // Returns the size (in bytes) of an object with n members.
constexpr size_t sizeofObject(size_t n) { constexpr size_t sizeofObject(size_t n) {
return 2 * n * sizeof(VariantData); return 2 * n * ResourceManager::slotSize;
} }
ARDUINOJSON_END_PRIVATE_NAMESPACE ARDUINOJSON_END_PRIVATE_NAMESPACE