• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bblanchon / ArduinoJson / 5533332671

pending completion
5533332671

push

github

bblanchon
Store index of slot in the pool instead of a pointer or a distance

189 of 189 new or added lines in 29 files covered. (100.0%)

3378 of 3392 relevant lines covered (99.59%)

7695.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.7
/src/ArduinoJson/Collection/CollectionImpl.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2023, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Collection/CollectionData.hpp>
8
#include <ArduinoJson/Memory/Alignment.hpp>
9
#include <ArduinoJson/Strings/StringAdapters.hpp>
10
#include <ArduinoJson/Variant/VariantCompare.hpp>
11
#include <ArduinoJson/Variant/VariantData.hpp>
12

13
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
14

15
inline const char* CollectionIterator::key() const {
12,585✔
16
  ARDUINOJSON_ASSERT(slot_ != nullptr);
17
  return slot_->key();
12,585✔
18
}
19

20
inline void CollectionIterator::setKey(const char* s) {
710✔
21
  ARDUINOJSON_ASSERT(slot_ != nullptr);
22
  ARDUINOJSON_ASSERT(s != nullptr);
23
  return slot_->setKey(s);
710✔
24
}
25

26
inline void CollectionIterator::setKey(StringNode* s) {
1,389✔
27
  ARDUINOJSON_ASSERT(slot_ != nullptr);
28
  ARDUINOJSON_ASSERT(s != nullptr);
29
  return slot_->setKey(s);
1,389✔
30
}
31

32
inline bool CollectionIterator::ownsKey() const {
475✔
33
  ARDUINOJSON_ASSERT(slot_ != nullptr);
34
  return slot_->ownsKey();
475✔
35
}
36

37
inline void CollectionIterator::next(const ResourceManager* resources) {
275,489✔
38
  ARDUINOJSON_ASSERT(slot_ != nullptr);
39
  auto nextId = slot_->next();
275,489✔
40
  if (nextId)
275,489✔
41
    slot_ = resources->getSlot(nextId);
271,663✔
42
  else
43
    slot_ = nullptr;
3,826✔
44
}
275,489✔
45

46
inline CollectionData::iterator CollectionData::addSlot(
70,726✔
47
    ResourceManager* resources) {
48
  auto slot = resources->allocSlot();
70,726✔
49
  if (!slot)
70,726✔
50
    return nullptr;
23✔
51
  if (tail_) {
70,703✔
52
    auto tail = resources->getSlot(tail_);
68,525✔
53
    tail->setNext(slot.id());
68,525✔
54
    tail_ = slot.id();
68,525✔
55
  } else {
56
    head_ = slot.id();
2,178✔
57
    tail_ = slot.id();
2,178✔
58
  }
59
  return iterator(slot);
70,703✔
60
}
61

62
inline void CollectionData::clear(ResourceManager* resources) {
54✔
63
  for (auto it = createIterator(resources); !it.done(); it.next(resources))
71✔
64
    releaseSlot(it.slot_, resources);
17✔
65
  head_ = 0;
54✔
66
  tail_ = 0;
54✔
67
}
54✔
68

69
inline SlotWithId CollectionData::getPreviousSlot(
40✔
70
    VariantSlot* target, const ResourceManager* resources) const {
71
  auto prev = SlotWithId(nullptr, 0);
40✔
72
  auto currentId = head_;
40✔
73
  while (currentId) {
67✔
74
    auto currentSlot = resources->getSlot(currentId);
67✔
75
    if (currentSlot == target)
67✔
76
      return prev;
40✔
77
    prev = SlotWithId(currentSlot, currentId);
27✔
78
    currentId = currentSlot->next();
27✔
79
  }
80
  return SlotWithId(nullptr, 0);
×
81
}
82

83
inline void CollectionData::remove(iterator it, ResourceManager* resources) {
42✔
84
  if (it.done())
42✔
85
    return;
2✔
86
  auto curr = it.slot_;
40✔
87
  auto prev = getPreviousSlot(curr, resources);
40✔
88
  auto next = curr->next();
40✔
89
  if (prev)
40✔
90
    prev->setNext(next);
23✔
91
  else
92
    head_ = next;
17✔
93
  if (!next)
40✔
94
    tail_ = prev.id();
19✔
95
  releaseSlot(curr, resources);
40✔
96
}
97

98
inline size_t CollectionData::memoryUsage(
20✔
99
    const ResourceManager* resources) const {
100
  size_t total = 0;
20✔
101
  for (auto it = createIterator(resources); !it.done(); it.next(resources)) {
38✔
102
    total += sizeof(VariantSlot) + it->memoryUsage(resources);
18✔
103
    if (it.ownsKey())
18✔
104
      total += sizeofString(strlen(it.key()));
1✔
105
  }
106
  return total;
20✔
107
}
108

109
inline size_t CollectionData::nesting(const ResourceManager* resources) const {
16✔
110
  size_t maxChildNesting = 0;
16✔
111
  for (auto it = createIterator(resources); !it.done(); it.next(resources)) {
22✔
112
    size_t childNesting = it->nesting(resources);
6✔
113
    if (childNesting > maxChildNesting)
6✔
114
      maxChildNesting = childNesting;
4✔
115
  }
116
  return maxChildNesting + 1;
16✔
117
}
118

119
inline size_t CollectionData::size(const ResourceManager* resources) const {
358✔
120
  size_t count = 0;
358✔
121
  for (auto it = createIterator(resources); !it.done(); it.next(resources))
132,051✔
122
    count++;
131,693✔
123
  return count;
358✔
124
}
125

126
inline void CollectionData::releaseSlot(VariantSlot* slot,
57✔
127
                                        ResourceManager* resources) {
128
  ARDUINOJSON_ASSERT(slot != nullptr);
129
  if (slot->ownsKey())
57✔
130
    resources->dereferenceString(slot->key());
7✔
131
  slot->data()->setNull(resources);
57✔
132
}
57✔
133

134
ARDUINOJSON_END_PRIVATE_NAMESPACE
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc