Skip to content

Commit a256ec7

Browse files
committed
RamString: use a bitfield to reduce size
1 parent 019e832 commit a256ec7

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/ArduinoJson/Strings/Adapters/RamString.hpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ struct IsChar
2020
class RamString {
2121
public:
2222
static const size_t typeSortKey = 2;
23+
#if ARDUINOJSON_SIZEOF_POINTER <= 2
24+
static constexpr size_t sizeMask = size_t(-1) >> 1;
25+
#else
26+
static constexpr size_t sizeMask = size_t(-1);
27+
#endif
2328

2429
RamString(const char* str, size_t sz, bool linked = false)
25-
: str_(str), size_(sz), linked_(linked) {}
30+
: str_(str), size_(sz & sizeMask), linked_(linked) {
31+
ARDUINOJSON_ASSERT(size_ == sz);
32+
}
2633

2734
bool isNull() const {
2835
return !str_;
@@ -48,8 +55,15 @@ class RamString {
4855

4956
protected:
5057
const char* str_;
58+
59+
#if ARDUINOJSON_SIZEOF_POINTER <= 2
60+
// Use a bitfield only on 8-bit microcontrollers
61+
size_t size_ : sizeof(size_t) * 8 - 1;
62+
bool linked_ : 1;
63+
#else
5164
size_t size_;
52-
bool linked_; // TODO: merge with size_
65+
bool linked_;
66+
#endif
5367
};
5468

5569
template <typename TChar>

0 commit comments

Comments
 (0)