Skip to content

Commit

Permalink
VariantSlot stores a StringNode* for owned keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jul 7, 2023
1 parent 04d7ec3 commit 324b1e5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/ArduinoJson/Memory/ResourceManager.hpp
Expand Up @@ -114,7 +114,7 @@ class ResourceManager {
StringNode::destroy(node, allocator_);
}

void dereferenceString(const char* s) {
void dereferenceString(StringNode* s) {
stringPool_.dereference(s, allocator_);
}

Expand Down
22 changes: 12 additions & 10 deletions src/ArduinoJson/Memory/StringPool.hpp
Expand Up @@ -82,21 +82,23 @@ class StringPool {
return nullptr;
}

void dereference(const char* s, Allocator* allocator) {
void dereference(StringNode* s, Allocator* allocator) {
ARDUINOJSON_ASSERT(s != nullptr);
ARDUINOJSON_ASSERT(s->references > 0);
if (--s->references > 0)
return;
StringNode* prev = nullptr;
for (auto node = strings_; node; node = node->next) {
if (node->data == s) {
if (--node->references == 0) {
if (prev)
prev->next = node->next;
else
strings_ = node->next;
StringNode::destroy(node, allocator);
}
return;
if (node == s) {
if (prev)
prev->next = node->next;
else
strings_ = node->next;
break;
}
prev = node;
}
StringNode::destroy(s, allocator);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoJson/Variant/SlotFunctions.hpp
Expand Up @@ -20,7 +20,7 @@ inline size_t slotSize(const VariantSlot* var) {

inline void VariantSlot::release(ResourceManager* resources) {
if (flags_ & OWNED_KEY_BIT)
resources->dereferenceString(key_);
resources->dereferenceString(ownedKey_);
data()->setNull(resources);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoJson/Variant/VariantData.hpp
Expand Up @@ -545,7 +545,7 @@ class VariantData {
private:
void release(ResourceManager* resources) {
if (flags_ & OWNED_VALUE_BIT)
resources->dereferenceString(content_.asOwnedString->data);
resources->dereferenceString(content_.asOwnedString);

auto collection = asCollection();
if (collection)
Expand Down
16 changes: 11 additions & 5 deletions src/ArduinoJson/Variant/VariantSlot.hpp
Expand Up @@ -23,7 +23,10 @@ class VariantSlot {
VariantContent content_;
uint8_t flags_;
VariantSlotDiff next_;
const char* key_;
union {
StringNode* ownedKey_;
const char* linkedKey_;
};

public:
// Placement new
Expand All @@ -33,7 +36,7 @@ class VariantSlot {

static void operator delete(void*, void*) noexcept {}

VariantSlot() : flags_(0), next_(0), key_(0) {}
VariantSlot() : flags_(0), next_(0), linkedKey_(0) {}

void release(ResourceManager* resources);

Expand Down Expand Up @@ -73,17 +76,20 @@ class VariantSlot {
void setKey(const char* k) {
ARDUINOJSON_ASSERT(k);
flags_ &= VALUE_MASK;
key_ = k;
linkedKey_ = k;
}

void setKey(StringNode* k) {
ARDUINOJSON_ASSERT(k);
flags_ |= OWNED_KEY_BIT;
key_ = k->data;
ownedKey_ = k;
}

const char* key() const {
return key_;
if (flags_ & OWNED_KEY_BIT)
return ownedKey_->data;
else
return linkedKey_;
}

bool ownsKey() const {
Expand Down

0 comments on commit 324b1e5

Please sign in to comment.