Skip to content

Commit 94aacf8

Browse files
committed
Replace extension with eight-byte values
1 parent bd2dccd commit 94aacf8

File tree

6 files changed

+77
-71
lines changed

6 files changed

+77
-71
lines changed

src/ArduinoJson/Configuration.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,9 @@
274274
#endif
275275

276276
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_DOUBLE
277-
# define ARDUINOJSON_USE_EXTENSIONS 1
277+
# define ARDUINOJSON_USE_8_BYTE_VALUES 1
278278
#else
279-
# define ARDUINOJSON_USE_EXTENSIONS 0
279+
# define ARDUINOJSON_USE_8_BYTE_VALUES 0
280280
#endif
281281

282282
#if defined(nullptr)

src/ArduinoJson/Memory/ResourceManager.hpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ class VariantData;
1818
class VariantWithId;
1919

2020
class ResourceManager {
21-
union SlotData {
22-
VariantData variant;
23-
#if ARDUINOJSON_USE_EXTENSIONS
24-
VariantExtension extension;
25-
#endif
26-
};
21+
using SlotData = VariantData; // TODO: remove SlotData?
2722

2823
public:
2924
constexpr static size_t slotSize = sizeof(SlotData);
@@ -44,6 +39,7 @@ class ResourceManager {
4439
swap(a.stringPool_, b.stringPool_);
4540
swap(a.variantPools_, b.variantPools_);
4641
swap(a.staticStringsPools_, b.staticStringsPools_);
42+
swap(a.lgValuePools_, b.lgValuePools_);
4743
swap_(a.allocator_, b.allocator_);
4844
swap_(a.overflowed_, b.overflowed_);
4945
}
@@ -64,10 +60,10 @@ class ResourceManager {
6460
void freeVariant(Slot<VariantData> slot);
6561
VariantData* getVariant(SlotId id) const;
6662

67-
#if ARDUINOJSON_USE_EXTENSIONS
68-
Slot<VariantExtension> allocExtension();
69-
void freeExtension(SlotId slot);
70-
VariantExtension* getExtension(SlotId id) const;
63+
#if ARDUINOJSON_USE_8_BYTE_VALUES
64+
Slot<EightByteValue> allocEightByte();
65+
void freeEightByte(SlotId slot);
66+
EightByteValue* getEightByte(SlotId id) const;
7167
#endif
7268

7369
template <typename TAdaptedString>
@@ -136,11 +132,17 @@ class ResourceManager {
136132
variantPools_.clear(allocator_);
137133
stringPool_.clear(allocator_);
138134
staticStringsPools_.clear(allocator_);
135+
#if ARDUINOJSON_USE_8_BYTE_VALUES
136+
lgValuePools_.clear(allocator_);
137+
#endif
139138
}
140139

141140
void shrinkToFit() {
142141
variantPools_.shrinkToFit(allocator_);
143142
staticStringsPools_.shrinkToFit(allocator_);
143+
#if ARDUINOJSON_USE_8_BYTE_VALUES
144+
lgValuePools_.shrinkToFit(allocator_);
145+
#endif
144146
}
145147

146148
private:
@@ -149,6 +151,9 @@ class ResourceManager {
149151
StringPool stringPool_;
150152
MemoryPoolList<SlotData> variantPools_;
151153
MemoryPoolList<const char*> staticStringsPools_;
154+
#if ARDUINOJSON_USE_8_BYTE_VALUES
155+
MemoryPoolList<EightByteValue> eightBytePools_;
156+
#endif
152157
};
153158

154159
ARDUINOJSON_END_PRIVATE_NAMESPACE

src/ArduinoJson/Memory/ResourceManagerImpl.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,23 @@ inline VariantData* ResourceManager::getVariant(SlotId id) const {
2929
return reinterpret_cast<VariantData*>(variantPools_.getSlot(id));
3030
}
3131

32-
#if ARDUINOJSON_USE_EXTENSIONS
33-
inline Slot<VariantExtension> ResourceManager::allocExtension() {
34-
auto p = variantPools_.allocSlot(allocator_);
35-
if (!p) {
32+
#if ARDUINOJSON_USE_8_BYTE_VALUES
33+
inline Slot<EightByteValue> ResourceManager::allocEightByte() {
34+
auto slot = eightBytePools_.allocSlot(allocator_);
35+
if (!slot) {
3636
overflowed_ = true;
3737
return {};
3838
}
39-
return {&p->extension, p.id()};
39+
return slot;
4040
}
4141

42-
inline void ResourceManager::freeExtension(SlotId id) {
43-
auto p = getExtension(id);
44-
variantPools_.freeSlot({reinterpret_cast<SlotData*>(p), id});
42+
inline void ResourceManager::freeEightByte(SlotId id) {
43+
auto p = getEightByte(id);
44+
eightBytePools_.freeSlot({p, id});
4545
}
4646

47-
inline VariantExtension* ResourceManager::getExtension(SlotId id) const {
48-
return &variantPools_.getSlot(id)->extension;
47+
inline EightByteValue* ResourceManager::getEightByte(SlotId id) const {
48+
return eightBytePools_.getSlot(id).ptr();
4949
}
5050
#endif
5151

src/ArduinoJson/Variant/VariantContent.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
1616
enum class VariantTypeBits : uint8_t {
1717
OwnedStringBit = 0x01, // 0000 0001
1818
NumberBit = 0x08, // 0000 1000
19-
#if ARDUINOJSON_USE_EXTENSIONS
20-
ExtensionBit = 0x10, // 0001 0000
21-
#endif
19+
EightByteBit = 0x10, // 0001 0000
2220
CollectionMask = 0x60,
2321
};
2422

@@ -64,8 +62,8 @@ union VariantContent {
6462
char asTinyString[tinyStringMaxLength + 1];
6563
};
6664

67-
#if ARDUINOJSON_USE_EXTENSIONS
68-
union VariantExtension {
65+
#if ARDUINOJSON_USE_8_BYTE_VALUES
66+
union EightByteValue {
6967
# if ARDUINOJSON_USE_LONG_LONG
7068
uint64_t asUint64;
7169
int64_t asInt64;
@@ -74,6 +72,9 @@ union VariantExtension {
7472
double asDouble;
7573
# endif
7674
};
75+
76+
static_assert(sizeof(EightByteValue) == 8,
77+
"sizeof(EightByteValue) must be 8 bytes");
7778
#endif
7879

7980
ARDUINOJSON_END_PRIVATE_NAMESPACE

src/ArduinoJson/Variant/VariantData.hpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class VariantData {
5353
template <typename TVisitor>
5454
typename TVisitor::result_type accept(
5555
TVisitor& visit, const ResourceManager* resources) const {
56-
#if ARDUINOJSON_USE_EXTENSIONS
57-
auto extension = getExtension(resources);
56+
#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES
57+
auto eightByteValue = getEightByte(resources);
5858
#else
5959
(void)resources; // silence warning
6060
#endif
@@ -64,7 +64,7 @@ class VariantData {
6464

6565
#if ARDUINOJSON_USE_DOUBLE
6666
case VariantType::Double:
67-
return visit.visit(extension->asDouble);
67+
return visit.visit(eightByteValue->asDouble);
6868
#endif
6969

7070
case VariantType::Array:
@@ -95,10 +95,10 @@ class VariantData {
9595

9696
#if ARDUINOJSON_USE_LONG_LONG
9797
case VariantType::Int64:
98-
return visit.visit(extension->asInt64);
98+
return visit.visit(eightByteValue->asInt64);
9999

100100
case VariantType::Uint64:
101-
return visit.visit(extension->asUint64);
101+
return visit.visit(eightByteValue->asUint64);
102102
#endif
103103

104104
case VariantType::Boolean:
@@ -145,8 +145,8 @@ class VariantData {
145145
}
146146

147147
bool asBoolean(const ResourceManager* resources) const {
148-
#if ARDUINOJSON_USE_EXTENSIONS
149-
auto extension = getExtension(resources);
148+
#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES
149+
auto eightByteValue = getEightByte(resources);
150150
#else
151151
(void)resources; // silence warning
152152
#endif
@@ -160,14 +160,14 @@ class VariantData {
160160
return content_.asFloat != 0;
161161
#if ARDUINOJSON_USE_DOUBLE
162162
case VariantType::Double:
163-
return extension->asDouble != 0;
163+
return eightByteValue->asDouble != 0;
164164
#endif
165165
case VariantType::Null:
166166
return false;
167167
#if ARDUINOJSON_USE_LONG_LONG
168168
case VariantType::Uint64:
169169
case VariantType::Int64:
170-
return extension->asUint64 != 0;
170+
return eightByteValue->asUint64 != 0;
171171
#endif
172172
default:
173173
return true;
@@ -193,8 +193,8 @@ class VariantData {
193193
template <typename T>
194194
T asFloat(const ResourceManager* resources) const {
195195
static_assert(is_floating_point<T>::value, "T must be a floating point");
196-
#if ARDUINOJSON_USE_EXTENSIONS
197-
auto extension = getExtension(resources);
196+
#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES
197+
auto eightByteValue = getEightByte(resources);
198198
#else
199199
(void)resources; // silence warning
200200
#endif
@@ -208,9 +208,9 @@ class VariantData {
208208
return static_cast<T>(content_.asInt32);
209209
#if ARDUINOJSON_USE_LONG_LONG
210210
case VariantType::Uint64:
211-
return static_cast<T>(extension->asUint64);
211+
return static_cast<T>(eightByteValue->asUint64);
212212
case VariantType::Int64:
213-
return static_cast<T>(extension->asInt64);
213+
return static_cast<T>(eightByteValue->asInt64);
214214
#endif
215215
case VariantType::TinyString:
216216
str = content_.asTinyString;
@@ -225,7 +225,7 @@ class VariantData {
225225
return static_cast<T>(content_.asFloat);
226226
#if ARDUINOJSON_USE_DOUBLE
227227
case VariantType::Double:
228-
return static_cast<T>(extension->asDouble);
228+
return static_cast<T>(eightByteValue->asDouble);
229229
#endif
230230
default:
231231
return 0.0;
@@ -238,8 +238,8 @@ class VariantData {
238238
template <typename T>
239239
T asIntegral(const ResourceManager* resources) const {
240240
static_assert(is_integral<T>::value, "T must be an integral type");
241-
#if ARDUINOJSON_USE_EXTENSIONS
242-
auto extension = getExtension(resources);
241+
#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES
242+
auto eightByteValue = getEightByte(resources);
243243
#else
244244
(void)resources; // silence warning
245245
#endif
@@ -253,9 +253,9 @@ class VariantData {
253253
return convertNumber<T>(content_.asInt32);
254254
#if ARDUINOJSON_USE_LONG_LONG
255255
case VariantType::Uint64:
256-
return convertNumber<T>(extension->asUint64);
256+
return convertNumber<T>(eightByteValue->asUint64);
257257
case VariantType::Int64:
258-
return convertNumber<T>(extension->asInt64);
258+
return convertNumber<T>(eightByteValue->asInt64);
259259
#endif
260260
case VariantType::TinyString:
261261
str = content_.asTinyString;
@@ -270,7 +270,7 @@ class VariantData {
270270
return convertNumber<T>(content_.asFloat);
271271
#if ARDUINOJSON_USE_DOUBLE
272272
case VariantType::Double:
273-
return convertNumber<T>(extension->asDouble);
273+
return convertNumber<T>(eightByteValue->asDouble);
274274
#endif
275275
default:
276276
return 0;
@@ -314,8 +314,8 @@ class VariantData {
314314
}
315315
}
316316

317-
#if ARDUINOJSON_USE_EXTENSIONS
318-
const VariantExtension* getExtension(const ResourceManager* resources) const;
317+
#if ARDUINOJSON_USE_8_BYTE_VALUES
318+
const EightByteValue* getEightByte(const ResourceManager* resources) const;
319319
#endif
320320

321321
VariantData* getElement(size_t index,
@@ -378,7 +378,7 @@ class VariantData {
378378
template <typename T>
379379
bool isInteger(const ResourceManager* resources) const {
380380
#if ARDUINOJSON_USE_LONG_LONG
381-
auto extension = getExtension(resources);
381+
auto eightByteValue = getEightByte(resources);
382382
#else
383383
(void)resources; // silence warning
384384
#endif
@@ -391,10 +391,10 @@ class VariantData {
391391

392392
#if ARDUINOJSON_USE_LONG_LONG
393393
case VariantType::Uint64:
394-
return canConvertNumber<T>(extension->asUint64);
394+
return canConvertNumber<T>(eightByteValue->asUint64);
395395

396396
case VariantType::Int64:
397-
return canConvertNumber<T>(extension->asInt64);
397+
return canConvertNumber<T>(eightByteValue->asInt64);
398398
#endif
399399

400400
default:

src/ArduinoJson/Variant/VariantImpl.hpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ inline void VariantData::clear(ResourceManager* resources) {
6161
if (type_ & VariantTypeBits::OwnedStringBit)
6262
resources->dereferenceString(content_.asOwnedString->data);
6363

64-
#if ARDUINOJSON_USE_EXTENSIONS
65-
if (type_ & VariantTypeBits::ExtensionBit)
66-
resources->freeExtension(content_.asSlotId);
64+
#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES
65+
if (type_ & VariantTypeBits::EightByteBit)
66+
resources->freeEightByte(content_.asSlotId);
6767
#endif
6868

6969
auto collection = asCollection();
@@ -73,12 +73,12 @@ inline void VariantData::clear(ResourceManager* resources) {
7373
type_ = VariantType::Null;
7474
}
7575

76-
#if ARDUINOJSON_USE_EXTENSIONS
77-
inline const VariantExtension* VariantData::getExtension(
76+
#if ARDUINOJSON_USE_EIGHT_BYTE_VALUES
77+
inline const EightByteValue* VariantData::getEightByteValue(
7878
const ResourceManager* resources) const {
79-
return type_ & VariantTypeBits::ExtensionBit
80-
? resources->getExtension(content_.asSlotId)
81-
: nullptr;
79+
return type_ & VariantTypeBits::EightByteBit
80+
? resources->getEightByteValue(content_.asSlotId)
81+
: 0;
8282
}
8383
#endif
8484

@@ -101,12 +101,12 @@ enable_if_t<sizeof(T) == 8, bool> VariantData::setFloat(
101101
type_ = VariantType::Float;
102102
content_.asFloat = valueAsFloat;
103103
} else {
104-
auto extension = resources->allocExtension();
105-
if (!extension)
104+
auto slot = resources->allocEightByte();
105+
if (!slot)
106106
return false;
107107
type_ = VariantType::Double;
108-
content_.asSlotId = extension.id();
109-
extension->asDouble = value;
108+
content_.asSlotId = slot.id();
109+
slot->asDouble = value;
110110
}
111111
#else
112112
type_ = VariantType::Float;
@@ -127,12 +127,12 @@ enable_if_t<is_signed<T>::value, bool> VariantData::setInteger(
127127
}
128128
#if ARDUINOJSON_USE_LONG_LONG
129129
else {
130-
auto extension = resources->allocExtension();
131-
if (!extension)
130+
auto slot = resources->allocEightByte();
131+
if (!slot)
132132
return false;
133133
type_ = VariantType::Int64;
134-
content_.asSlotId = extension.id();
135-
extension->asInt64 = value;
134+
content_.asSlotId = slot.id();
135+
slot->asInt64 = value;
136136
}
137137
#endif
138138
return true;
@@ -150,12 +150,12 @@ enable_if_t<is_unsigned<T>::value, bool> VariantData::setInteger(
150150
}
151151
#if ARDUINOJSON_USE_LONG_LONG
152152
else {
153-
auto extension = resources->allocExtension();
154-
if (!extension)
153+
auto slot = resources->allocEightByte();
154+
if (!slot)
155155
return false;
156156
type_ = VariantType::Uint64;
157-
content_.asSlotId = extension.id();
158-
extension->asUint64 = value;
157+
content_.asSlotId = slot.id();
158+
slot->asUint64 = value;
159159
}
160160
#endif
161161
return true;

0 commit comments

Comments
 (0)