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

bblanchon / ArduinoJson / 13505335486

24 Feb 2025 06:37PM UTC coverage: 99.227%. Remained the same
13505335486

push

github

bblanchon
Store static string in a dedicated pool

32 of 35 new or added lines in 7 files covered. (91.43%)

3 existing lines in 1 file now uncovered.

3979 of 4010 relevant lines covered (99.23%)

10910.69 hits per line

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

98.48
/src/ArduinoJson/Memory/ResourceManager.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2025, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Memory/Allocator.hpp>
8
#include <ArduinoJson/Memory/MemoryPoolList.hpp>
9
#include <ArduinoJson/Memory/StringPool.hpp>
10
#include <ArduinoJson/Polyfills/assert.hpp>
11
#include <ArduinoJson/Polyfills/utility.hpp>
12
#include <ArduinoJson/Strings/StringAdapters.hpp>
13
#include <ArduinoJson/Variant/VariantData.hpp>
14

15
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
16

17
class VariantData;
18
class VariantWithId;
19

20
class ResourceManager {
21
  union SlotData {
22
    VariantData variant;
23
#if ARDUINOJSON_USE_EXTENSIONS
24
    VariantExtension extension;
25
#endif
26
  };
27

28
 public:
29
  constexpr static size_t slotSize = sizeof(SlotData);
30

31
  ResourceManager(Allocator* allocator = DefaultAllocator::instance())
2,096✔
32
      : allocator_(allocator), overflowed_(false) {}
2,096✔
33

34
  ~ResourceManager() {
2,096✔
35
    stringPool_.clear(allocator_);
2,096✔
36
    variantPools_.clear(allocator_);
2,096✔
37
    staticStringsPools_.clear(allocator_);
2,096✔
38
  }
2,096✔
39

40
  ResourceManager(const ResourceManager&) = delete;
41
  ResourceManager& operator=(const ResourceManager& src) = delete;
42

43
  friend void swap(ResourceManager& a, ResourceManager& b) {
11✔
44
    swap(a.stringPool_, b.stringPool_);
11✔
45
    swap(a.variantPools_, b.variantPools_);
11✔
46
    swap(a.staticStringsPools_, b.staticStringsPools_);
11✔
47
    swap_(a.allocator_, b.allocator_);
11✔
48
    swap_(a.overflowed_, b.overflowed_);
11✔
49
  }
11✔
50

51
  Allocator* allocator() const {
7✔
52
    return allocator_;
7✔
53
  }
54

55
  size_t size() const {
14✔
56
    return variantPools_.size() + stringPool_.size();
14✔
57
  }
58

59
  bool overflowed() const {
66,376✔
60
    return overflowed_;
66,376✔
61
  }
62

63
  Slot<VariantData> allocVariant();
64
  void freeVariant(Slot<VariantData> slot);
65
  VariantData* getVariant(SlotId id) const;
66

67
#if ARDUINOJSON_USE_EXTENSIONS
68
  Slot<VariantExtension> allocExtension();
69
  void freeExtension(SlotId slot);
70
  VariantExtension* getExtension(SlotId id) const;
71
#endif
72

73
  template <typename TAdaptedString>
74
  StringNode* saveString(TAdaptedString str) {
375✔
75
    if (str.isNull())
375✔
76
      return 0;
2✔
77

78
    auto node = stringPool_.add(str, allocator_);
373✔
79
    if (!node)
373✔
80
      overflowed_ = true;
15✔
81

82
    return node;
373✔
83
  }
84

85
  void saveString(StringNode* node) {
1,443✔
86
    stringPool_.add(node);
1,443✔
87
  }
1,443✔
88

89
  template <typename TAdaptedString>
90
  StringNode* getString(const TAdaptedString& str) const {
1,746✔
91
    return stringPool_.get(str);
1,746✔
92
  }
93

94
  StringNode* createString(size_t length) {
1,679✔
95
    auto node = StringNode::create(length, allocator_);
1,679✔
96
    if (!node)
1,679✔
97
      overflowed_ = true;
32✔
98
    return node;
1,679✔
99
  }
100

101
  StringNode* resizeString(StringNode* node, size_t length) {
1,323✔
102
    node = StringNode::resize(node, length, allocator_);
1,323✔
103
    if (!node)
1,323✔
104
      overflowed_ = true;
6✔
105
    return node;
1,323✔
106
  }
107

108
  void destroyString(StringNode* node) {
198✔
109
    StringNode::destroy(node, allocator_);
198✔
110
  }
198✔
111

112
  void dereferenceString(const char* s) {
26✔
113
    stringPool_.dereference(s, allocator_);
26✔
114
  }
26✔
115

116
  SlotId saveStaticString(const char* s) {
1,157✔
117
    auto slot = staticStringsPools_.allocSlot(allocator_);
1,157✔
118
    if (!slot)
1,157✔
NEW
119
      return NULL_SLOT;
×
120
    *slot = s;
1,157✔
121
    return slot.id();
1,157✔
122
  }
123

124
  const char* getStaticString(SlotId id) const {
5,822✔
125
    return *staticStringsPools_.getSlot(id);
5,822✔
126
  }
127

128
  void clear() {
1,945✔
129
    variantPools_.clear(allocator_);
1,945✔
130
    overflowed_ = false;
1,945✔
131
    stringPool_.clear(allocator_);
1,945✔
132
    staticStringsPools_.clear(allocator_);
1,945✔
133
  }
1,945✔
134

135
  void shrinkToFit() {
1,349✔
136
    variantPools_.shrinkToFit(allocator_);
1,349✔
137
    staticStringsPools_.shrinkToFit(allocator_);
1,349✔
138
  }
1,349✔
139

140
 private:
141
  Allocator* allocator_;
142
  bool overflowed_;
143
  StringPool stringPool_;
144
  MemoryPoolList<SlotData> variantPools_;
145
  MemoryPoolList<const char*> staticStringsPools_;
146
};
147

148
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