@geastack/engine 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ui/tree_state.cpp CHANGED
@@ -4,8 +4,11 @@
4
4
  #include <algorithm>
5
5
  #include <cstddef>
6
6
  #include <cstring>
7
+ #include <cstdlib>
7
8
  #include <deque>
8
9
  #include <new>
10
+ #include <memory>
11
+ #include <vector>
9
12
  #include <utility>
10
13
 
11
14
  // TreeState is a single large struct (kMaxNodes-scaled per-node arrays: nodes,
@@ -20,7 +23,187 @@
20
23
  #include "esp_heap_caps.h"
21
24
  #endif
22
25
 
23
- namespace gea::embedded::ui {
26
+ namespace gea::embedded::ui
27
+ {
28
+
29
+ namespace
30
+ {
31
+ std::size_t g_nodeTextStorageBytes = 0;
32
+ template <class T> struct NodeTextAllocator {
33
+ using value_type = T;
34
+ NodeTextAllocator() = default;
35
+ template <class U> NodeTextAllocator(const NodeTextAllocator<U> &) {}
36
+ T *allocate(std::size_t n)
37
+ {
38
+ #if defined(ESP_PLATFORM)
39
+ // Match TreeState placement even when the board sends small ordinary
40
+ // allocations to SRAM. Node text must not steal the audio heap.
41
+ T *result = static_cast<T *>(heap_caps_malloc(n * sizeof(T), MALLOC_CAP_SPIRAM));
42
+ if (!result) result = std::allocator<T>{}.allocate(n);
43
+ #else
44
+ T *result = std::allocator<T>{}.allocate(n);
45
+ #endif
46
+ g_nodeTextStorageBytes += n * sizeof(T);
47
+ return result;
48
+ }
49
+ void deallocate(T *ptr, std::size_t n)
50
+ {
51
+ g_nodeTextStorageBytes -= n * sizeof(T);
52
+ #if defined(ESP_PLATFORM)
53
+ // ESP-IDF new/delete and capability allocations use the same heap.
54
+ heap_caps_free(ptr);
55
+ #else
56
+ std::allocator<T>{}.deallocate(ptr, n);
57
+ #endif
58
+ }
59
+ template <class U> bool operator==(const NodeTextAllocator<U> &) const
60
+ {
61
+ return true;
62
+ }
63
+ template <class U> bool operator!=(const NodeTextAllocator<U> &) const
64
+ {
65
+ return false;
66
+ }
67
+ };
68
+ union NodeTextEntry {
69
+ // A free entry has no live string, so its reuse link occupies the same
70
+ // bytes instead of adding a word to every nonempty label.
71
+ std::string text;
72
+ std::uint32_t nextFree;
73
+ NodeTextEntry() : nextFree(UINT32_MAX) {}
74
+ ~NodeTextEntry() {} // Live strings are destroyed when their owner clears.
75
+ };
76
+ struct NodeTextPool {
77
+ // Fixed small pages avoid libc++ deque's 4 KiB minimum block for a UI
78
+ // containing only a handful of labels. Page addresses never move.
79
+ static constexpr std::size_t kPageEntries = 16;
80
+ struct PageDeleter {
81
+ void operator()(NodeTextEntry *page) const {
82
+ for (std::size_t i = 0; i < kPageEntries; ++i) page[i].~NodeTextEntry();
83
+ NodeTextAllocator<NodeTextEntry>{}.deallocate(page, kPageEntries);
84
+ }
85
+ };
86
+ std::vector<NodeTextEntry *, NodeTextAllocator<NodeTextEntry *>> pages;
87
+ std::size_t size = 0;
88
+ std::uint32_t freeHead = UINT32_MAX;
89
+ NodeTextEntry &at(std::size_t index)
90
+ {
91
+ return pages[index / kPageEntries][index % kPageEntries];
92
+ }
93
+ void append()
94
+ {
95
+ if (size == pages.size() * kPageEntries) {
96
+ std::unique_ptr<NodeTextEntry[], PageDeleter> page(NodeTextAllocator<NodeTextEntry>{}.allocate(kPageEntries));
97
+ for (std::size_t i = 0; i < kPageEntries; ++i) new (&page[i]) NodeTextEntry;
98
+ pages.push_back(page.get());
99
+ page.release();
100
+ }
101
+ ++size;
102
+ }
103
+ };
104
+ NodeTextPool *g_nodeTextPool = nullptr;
105
+ const std::string g_emptyNodeText;
106
+ NodeTextPool &nodeTextPool()
107
+ {
108
+ if (!g_nodeTextPool)
109
+ g_nodeTextPool = new (NodeTextAllocator<NodeTextPool>{}.allocate(1)) NodeTextPool;
110
+ return *g_nodeTextPool;
111
+ }
112
+ } // namespace
113
+
114
+ std::size_t NodeText::storageBytes()
115
+ {
116
+ return g_nodeTextStorageBytes;
117
+ }
118
+
119
+ StorageUsage NodeText::storageUsage()
120
+ {
121
+ StorageUsage usage;
122
+ usage.staticBytes = sizeof(g_nodeTextStorageBytes) + sizeof(g_nodeTextPool) + sizeof(g_emptyNodeText);
123
+ usage.addString(g_emptyNodeText);
124
+ if (!g_nodeTextPool) return usage;
125
+ const auto &pool = *g_nodeTextPool;
126
+ usage.addAllocation(g_nodeTextPool, sizeof(NodeTextPool));
127
+ usage.addVector(pool.pages);
128
+ for (const auto *page : pool.pages)
129
+ usage.addAllocation(page, NodeTextPool::kPageEntries * sizeof(NodeTextEntry));
130
+ for (std::size_t index = 0; index < pool.size; ++index) {
131
+ bool free = false;
132
+ // Census only: walking the free chain avoids allocating a temporary
133
+ // live bitmap, and never reads a destroyed union member as a string.
134
+ for (auto handle = pool.freeHead; handle != UINT32_MAX; handle = g_nodeTextPool->at(handle).nextFree)
135
+ if (handle == index) { free = true; break; }
136
+ if (!free) usage.addString(g_nodeTextPool->at(index).text);
137
+ }
138
+ return usage;
139
+ }
140
+
141
+ NodeText::NodeText(const NodeText &other)
142
+ {
143
+ if (!other.empty())
144
+ assign(other.c_str());
145
+ }
146
+ NodeText::NodeText(NodeText &&other) noexcept : handle_(other.handle_)
147
+ {
148
+ other.handle_ = kEmpty;
149
+ }
150
+ NodeText::~NodeText() { clear(); }
151
+ NodeText &NodeText::operator=(const NodeText &other)
152
+ {
153
+ if (this != &other) {
154
+ if (other.empty())
155
+ clear();
156
+ else
157
+ assign(other.c_str());
158
+ }
159
+ return *this;
160
+ }
161
+ NodeText &NodeText::operator=(NodeText &&other) noexcept
162
+ {
163
+ if (this != &other) {
164
+ clear();
165
+ handle_ = other.handle_;
166
+ other.handle_ = kEmpty;
167
+ }
168
+ return *this;
169
+ }
170
+ const std::string &NodeText::str() const
171
+ {
172
+ return empty() ? g_emptyNodeText : nodeTextPool().at(handle_).text;
173
+ }
174
+ void NodeText::clear()
175
+ {
176
+ if (empty())
177
+ return;
178
+ auto &pool = nodeTextPool();
179
+ auto &entry = pool.at(handle_);
180
+ // Release long text allocations on removal, rather than retaining the
181
+ // largest label ever seen in every reusable slot.
182
+ entry.text.~basic_string();
183
+ entry.nextFree = pool.freeHead;
184
+ pool.freeHead = handle_;
185
+ handle_ = kEmpty;
186
+ }
187
+ void NodeText::assign(const char *text)
188
+ {
189
+ if (!text || !*text) {
190
+ clear();
191
+ return;
192
+ }
193
+ auto &pool = nodeTextPool();
194
+ if (empty()) {
195
+ if (pool.freeHead == kEmpty) {
196
+ const auto index = pool.size;
197
+ pool.append();
198
+ handle_ = static_cast<std::uint32_t>(index);
199
+ } else {
200
+ handle_ = pool.freeHead;
201
+ pool.freeHead = pool.at(handle_).nextFree;
202
+ }
203
+ new (&pool.at(handle_).text) std::string;
204
+ }
205
+ pool.at(handle_).text.assign(text);
206
+ }
24
207
 
25
208
  namespace {
26
209
 
@@ -38,6 +221,7 @@ bool validToken(const std::string &token)
38
221
  return true;
39
222
  }
40
223
 
224
+ #if GEA_UI_CLASS_OVERFLOW
41
225
  std::deque<std::vector<CssAtomId>> &classListOverflowLists()
42
226
  {
43
227
  static std::deque<std::vector<CssAtomId>> lists;
@@ -89,6 +273,9 @@ void releaseClassListOverflow(NodeClassList &list)
89
273
  classListOverflowFreeList().push_back(list.overflowHandle);
90
274
  list.overflowHandle = NodeClassList::kNoOverflow;
91
275
  }
276
+ #else
277
+ void releaseClassListOverflow(NodeClassList &) {}
278
+ #endif
92
279
 
93
280
  bool appendClassAtom(NodeClassList &list, CssAtomId atom)
94
281
  {
@@ -97,11 +284,16 @@ bool appendClassAtom(NodeClassList &list, CssAtomId atom)
97
284
  list.inlineTokens[list.count++] = atom;
98
285
  return true;
99
286
  }
287
+ #if GEA_UI_CLASS_OVERFLOW
100
288
  auto *overflow = ensureClassListOverflow(list);
101
289
  if (!overflow || list.overflowHandle == NodeClassList::kNoOverflow) return false;
102
290
  overflow->push_back(atom);
103
291
  ++list.count;
104
292
  return true;
293
+ #else
294
+ // A violated source proof must fail visibly, never truncate a class list.
295
+ std::abort();
296
+ #endif
105
297
  }
106
298
 
107
299
  void eraseClassAtomAt(NodeClassList &list, std::size_t index)
@@ -111,6 +303,7 @@ void eraseClassAtomAt(NodeClassList &list, std::size_t index)
111
303
  const std::size_t inlineEnd = std::min<std::size_t>(list.count, NodeClassList::kInlineTokenCount);
112
304
  for (std::size_t i = index + 1; i < inlineEnd; ++i)
113
305
  list.inlineTokens[i - 1] = list.inlineTokens[i];
306
+ #if GEA_UI_CLASS_OVERFLOW
114
307
  if (list.count > NodeClassList::kInlineTokenCount) {
115
308
  auto *overflow = classListOverflow(list.overflowHandle);
116
309
  if (overflow && !overflow->empty()) {
@@ -118,12 +311,15 @@ void eraseClassAtomAt(NodeClassList &list, std::size_t index)
118
311
  overflow->erase(overflow->begin());
119
312
  if (overflow->empty()) releaseClassListOverflow(list);
120
313
  }
121
- } else if (list.count > 0) {
314
+ } else
315
+ #endif
316
+ if (list.count > 0) {
122
317
  list.inlineTokens[list.count - 1] = kInvalidCssAtom;
123
318
  }
124
319
  --list.count;
125
320
  return;
126
321
  }
322
+ #if GEA_UI_CLASS_OVERFLOW
127
323
  auto *overflow = classListOverflow(list.overflowHandle);
128
324
  if (!overflow) return;
129
325
  const std::size_t overflowIndex = index - NodeClassList::kInlineTokenCount;
@@ -131,6 +327,7 @@ void eraseClassAtomAt(NodeClassList &list, std::size_t index)
131
327
  overflow->erase(overflow->begin() + static_cast<std::ptrdiff_t>(overflowIndex));
132
328
  --list.count;
133
329
  if (overflow->empty()) releaseClassListOverflow(list);
330
+ #endif
134
331
  }
135
332
 
136
333
  bool classListsEqual(const NodeClassList &a, const NodeClassList &b)
@@ -186,9 +383,11 @@ NodeClassList::NodeClassList(NodeClassList &&other) noexcept
186
383
  {
187
384
  for (std::uint8_t i = 0; i < kInlineTokenCount; ++i)
188
385
  inlineTokens[i] = other.inlineTokens[i];
386
+ #if GEA_UI_CLASS_OVERFLOW
189
387
  overflowHandle = other.overflowHandle;
190
- count = other.count;
191
388
  other.overflowHandle = kNoOverflow;
389
+ #endif
390
+ count = other.count;
192
391
  other.count = 0;
193
392
  for (std::uint8_t i = 0; i < kInlineTokenCount; ++i)
194
393
  other.inlineTokens[i] = kInvalidCssAtom;
@@ -200,9 +399,11 @@ NodeClassList &NodeClassList::operator=(NodeClassList &&other) noexcept
200
399
  clear();
201
400
  for (std::uint8_t i = 0; i < kInlineTokenCount; ++i)
202
401
  inlineTokens[i] = other.inlineTokens[i];
402
+ #if GEA_UI_CLASS_OVERFLOW
203
403
  overflowHandle = other.overflowHandle;
204
- count = other.count;
205
404
  other.overflowHandle = kNoOverflow;
405
+ #endif
406
+ count = other.count;
206
407
  other.count = 0;
207
408
  for (std::uint8_t i = 0; i < kInlineTokenCount; ++i)
208
409
  other.inlineTokens[i] = kInvalidCssAtom;
@@ -289,11 +490,15 @@ CssAtomId NodeClassList::at(std::size_t index) const
289
490
  {
290
491
  if (index >= count) return kInvalidCssAtom;
291
492
  if (index < kInlineTokenCount) return inlineTokens[index];
493
+ #if GEA_UI_CLASS_OVERFLOW
292
494
  const auto *overflow = classListOverflowConst(overflowHandle);
293
495
  if (!overflow) return kInvalidCssAtom;
294
496
  const std::size_t overflowIndex = index - kInlineTokenCount;
295
497
  if (overflowIndex >= overflow->size()) return kInvalidCssAtom;
296
498
  return (*overflow)[overflowIndex];
499
+ #else
500
+ return kInvalidCssAtom;
501
+ #endif
297
502
  }
298
503
 
299
504
  std::string NodeClassList::value() const
@@ -307,138 +512,136 @@ std::string NodeClassList::value() const
307
512
  return out;
308
513
  }
309
514
 
515
+ // Empty override stores own nothing. One allocation contains both metadata
516
+ // and values; computed styles remain ordinary aligned fields in Node.
517
+ struct NodeStyleOverrideStore::Block {
518
+ // Entries are unique by Property; geometric growth fits this bound.
519
+ static_assert(static_cast<unsigned>(Property::Count) <= 32768);
520
+ std::uint16_t count = 0, capacity = 0;
521
+ // Only CSS px overrides allocate unit metadata. Raw numeric styles keep
522
+ // their compact entries and do not lose precision by reverse-scaling ints.
523
+ std::unique_ptr<std::vector<std::pair<Property, float>>> cssPixels;
524
+ NodeStyleOverride *values() { return reinterpret_cast<NodeStyleOverride *>(this + 1); }
525
+ const NodeStyleOverride *values() const { return reinterpret_cast<const NodeStyleOverride *>(this + 1); }
526
+ };
310
527
  namespace {
311
-
312
- NodeStyleOverride &styleOverrideAt(NodeStyleOverrideStore &store, std::size_t index)
313
- {
314
- return store.spilled ? store.spillValues[index] : store.inlineValues[index];
315
- }
316
-
317
528
  void appendStyleOverride(NodeStyleOverrideStore &store, NodeStyleOverride entry)
318
529
  {
319
- if (!store.spilled && store.inlineCount < NodeStyleOverrideStore::kInlineCount) {
320
- store.inlineValues[store.inlineCount++] = entry;
321
- return;
322
- }
323
- if (!store.spilled) {
324
- store.spillCapacity = NodeStyleOverrideStore::kInlineCount * 2;
325
- store.spillValues = new NodeStyleOverride[store.spillCapacity];
326
- for (std::size_t i = 0; i < store.inlineCount; ++i)
327
- store.spillValues[i] = store.inlineValues[i];
328
- store.spillCount = store.inlineCount;
329
- store.spilled = true;
330
- }
331
- if (store.spillCount >= store.spillCapacity) {
332
- const std::size_t nextCapacity = store.spillCapacity ? store.spillCapacity * 2 : NodeStyleOverrideStore::kInlineCount * 2;
333
- auto *next = new NodeStyleOverride[nextCapacity];
334
- for (std::size_t i = 0; i < store.spillCount; ++i)
335
- next[i] = store.spillValues[i];
336
- delete[] store.spillValues;
337
- store.spillValues = next;
338
- store.spillCapacity = nextCapacity;
530
+ auto *old = store.block;
531
+ if (!old || old->count == old->capacity) {
532
+ // Small inline styles commonly have three entries. Grow 2 -> 3 -> 4
533
+ // before doubling, so they do not retain an unused fourth entry.
534
+ const std::size_t capacity = old ? (old->capacity < 4 ? old->capacity + 1 : old->capacity * 2) : 2;
535
+ auto *next = new (::operator new(sizeof(NodeStyleOverrideStore::Block) + capacity * sizeof(NodeStyleOverride))) NodeStyleOverrideStore::Block;
536
+ next->capacity = static_cast<std::uint16_t>(capacity);
537
+ if (old) {
538
+ next->count = old->count;
539
+ next->cssPixels = std::move(old->cssPixels);
540
+ for (std::size_t i = 0; i < old->count; ++i) new (&next->values()[i]) NodeStyleOverride(old->values()[i]);
541
+ old->~Block();
542
+ ::operator delete(old);
543
+ }
544
+ store.block = next;
339
545
  }
340
- store.spillValues[store.spillCount++] = entry;
546
+ new (&store.block->values()[store.block->count++]) NodeStyleOverride(entry);
547
+ }
341
548
  }
342
-
343
- } // namespace
344
-
345
549
  NodeStyleOverrideStore::NodeStyleOverrideStore(const NodeStyleOverrideStore &other)
346
550
  {
347
- for (std::size_t i = 0, n = other.size(); i < n; ++i)
348
- appendStyleOverride(*this, other.at(i));
551
+ for (std::size_t i = 0; i < other.size(); ++i) appendStyleOverride(*this, other.at(i));
552
+ if (other.block && other.block->cssPixels)
553
+ block->cssPixels = std::make_unique<std::vector<std::pair<Property, float>>>(*other.block->cssPixels);
349
554
  }
350
-
351
555
  NodeStyleOverrideStore &NodeStyleOverrideStore::operator=(const NodeStyleOverrideStore &other)
352
556
  {
353
- if (this == &other) return *this;
354
- clear();
355
- for (std::size_t i = 0, n = other.size(); i < n; ++i)
356
- appendStyleOverride(*this, other.at(i));
557
+ if (this != &other) {
558
+ NodeStyleOverrideStore copy(other);
559
+ *this = std::move(copy);
560
+ }
357
561
  return *this;
358
562
  }
359
-
360
- NodeStyleOverrideStore::NodeStyleOverrideStore(NodeStyleOverrideStore &&other) noexcept
563
+ NodeStyleOverrideStore::NodeStyleOverrideStore(NodeStyleOverrideStore &&other) noexcept : block(other.block)
361
564
  {
362
- for (std::uint8_t i = 0; i < kInlineCount; ++i)
363
- inlineValues[i] = other.inlineValues[i];
364
- spillValues = other.spillValues;
365
- spillCount = other.spillCount;
366
- spillCapacity = other.spillCapacity;
367
- inlineCount = other.inlineCount;
368
- spilled = other.spilled;
369
- other.spillValues = nullptr;
370
- other.spillCount = 0;
371
- other.spillCapacity = 0;
372
- other.inlineCount = 0;
373
- other.spilled = false;
565
+ other.block = nullptr;
374
566
  }
375
-
376
567
  NodeStyleOverrideStore &NodeStyleOverrideStore::operator=(NodeStyleOverrideStore &&other) noexcept
377
568
  {
378
- if (this == &other) return *this;
379
- delete[] spillValues;
380
- for (std::uint8_t i = 0; i < kInlineCount; ++i)
381
- inlineValues[i] = other.inlineValues[i];
382
- spillValues = other.spillValues;
383
- spillCount = other.spillCount;
384
- spillCapacity = other.spillCapacity;
385
- inlineCount = other.inlineCount;
386
- spilled = other.spilled;
387
- other.spillValues = nullptr;
388
- other.spillCount = 0;
389
- other.spillCapacity = 0;
390
- other.inlineCount = 0;
391
- other.spilled = false;
569
+ if (this != &other) {
570
+ clear();
571
+ block = other.block;
572
+ other.block = nullptr;
573
+ }
392
574
  return *this;
393
575
  }
394
-
395
- NodeStyleOverrideStore::~NodeStyleOverrideStore()
576
+ NodeStyleOverrideStore::~NodeStyleOverrideStore() { clear(); }
577
+ void NodeStyleOverrideStore::clear()
396
578
  {
397
- delete[] spillValues;
579
+ if (!block) return;
580
+ block->~Block();
581
+ ::operator delete(block);
582
+ block = nullptr;
398
583
  }
399
-
400
- void NodeStyleOverrideStore::clear()
584
+ std::size_t NodeStyleOverrideStore::size() const { return block ? block->count : 0; }
585
+ StorageUsage NodeStyleOverrideStore::storageUsage() const
401
586
  {
402
- if (spilled)
403
- spillCount = 0;
404
- else
405
- inlineCount = 0;
587
+ StorageUsage usage;
588
+ if (!block) return usage;
589
+ usage.addAllocation(block, sizeof(Block) + block->capacity * sizeof(NodeStyleOverride));
590
+ if (block->cssPixels) {
591
+ usage.addAllocation(block->cssPixels.get(), sizeof(*block->cssPixels));
592
+ usage.addVector(*block->cssPixels);
593
+ }
594
+ return usage;
406
595
  }
407
596
 
408
597
  void NodeStyleOverrideStore::set(Property property, int value)
409
598
  {
410
- for (std::size_t i = 0, n = size(); i < n; ++i) {
411
- auto &entry = styleOverrideAt(*this, i);
412
- if (entry.property != property) continue;
413
- entry.value = value;
414
- return;
599
+ for (std::size_t i = 0; i < size(); ++i) {
600
+ auto &entry = block->values()[i];
601
+ if (entry.property == property) {
602
+ entry.value = value;
603
+ if (block->cssPixels) {
604
+ auto &units = *block->cssPixels;
605
+ units.erase(std::remove_if(units.begin(), units.end(), [property](const auto &item) { return item.first == property; }), units.end());
606
+ }
607
+ return;
608
+ }
415
609
  }
416
610
  appendStyleOverride(*this, {property, value});
417
611
  }
418
-
612
+ void NodeStyleOverrideStore::setCssPixels(Property property, float value)
613
+ {
614
+ if (!block) return;
615
+ bool present = false;
616
+ for (std::size_t i = 0; i < size(); ++i) if (at(i).property == property) { present = true; break; }
617
+ if (!present) return;
618
+ if (!block->cssPixels) block->cssPixels = std::make_unique<std::vector<std::pair<Property, float>>>();
619
+ for (auto &entry : *block->cssPixels) if (entry.first == property) { entry.second = value; return; }
620
+ block->cssPixels->emplace_back(property, value);
621
+ }
622
+ bool NodeStyleOverrideStore::getCssPixels(Property property, float &value) const
623
+ {
624
+ if (!block || !block->cssPixels) return false;
625
+ for (const auto &entry : *block->cssPixels) if (entry.first == property) { value = entry.second; return true; }
626
+ return false;
627
+ }
419
628
  bool NodeStyleOverrideStore::remove(Property property)
420
629
  {
421
- for (std::size_t i = 0, n = size(); i < n; ++i) {
630
+ for (std::size_t i = 0; i < size(); ++i) {
422
631
  if (at(i).property != property) continue;
423
- if (spilled) {
424
- for (std::size_t j = i + 1; j < spillCount; ++j)
425
- spillValues[j - 1] = spillValues[j];
426
- --spillCount;
427
- } else {
428
- for (std::size_t j = i + 1; j < inlineCount; ++j)
429
- inlineValues[j - 1] = inlineValues[j];
430
- --inlineCount;
632
+ if (block->cssPixels) {
633
+ auto &units = *block->cssPixels;
634
+ units.erase(std::remove_if(units.begin(), units.end(), [property](const auto &item) { return item.first == property; }), units.end());
431
635
  }
636
+ for (std::size_t j = i + 1; j < block->count; ++j) block->values()[j - 1] = block->values()[j];
637
+ if (--block->count == 0) clear();
432
638
  return true;
433
639
  }
434
640
  return false;
435
641
  }
642
+ const NodeStyleOverride &NodeStyleOverrideStore::at(std::size_t index) const { return block->values()[index]; }
436
643
 
437
- const NodeStyleOverride &NodeStyleOverrideStore::at(std::size_t index) const
438
- {
439
- return spilled ? spillValues[index] : inlineValues[index];
440
- }
441
-
644
+ #if GEA_CSS_CUSTOM_PROPERTIES
442
645
  void NodeCustomPropertyStore::clear()
443
646
  {
444
647
  values.clear();
@@ -450,9 +653,16 @@ void NodeCustomPropertyStore::set(const std::string &name, const std::string &va
450
653
  set(internCssAtom(name), value);
451
654
  }
452
655
 
453
- CssAtomId customPropertyValueAtom(const std::string &value)
656
+ void setCustomPropertyStorage(NodeCustomProperty &entry, const std::string &value)
454
657
  {
455
- return value.empty() ? kInvalidCssAtom : internCssAtom(value);
658
+ const CssAtomId atom = value.empty() ? kInvalidCssAtom : internCssAtom(value);
659
+ // Form the replacement before releasing the old string: callers may pass
660
+ // the value returned by get(), including an uninterned overflow value.
661
+ auto storage = atom != kInvalidCssAtom || value.empty()
662
+ ? std::shared_ptr<const std::string>(std::shared_ptr<const std::string>{}, &cssAtomString(atom))
663
+ : std::make_shared<const std::string>(value);
664
+ entry.value = std::move(storage);
665
+ entry.valueAtom = atom;
456
666
  }
457
667
 
458
668
  void NodeCustomPropertyStore::set(CssAtomId nameId, const std::string &value)
@@ -460,15 +670,13 @@ void NodeCustomPropertyStore::set(CssAtomId nameId, const std::string &value)
460
670
  if (nameId == kInvalidCssAtom) return;
461
671
  for (auto &entry : values) {
462
672
  if (entry.nameId != nameId) continue;
463
- entry.value = value;
464
- entry.valueAtom = customPropertyValueAtom(value);
673
+ setCustomPropertyStorage(entry, value);
465
674
  entry.flags = 0;
466
675
  return;
467
676
  }
468
677
  NodeCustomProperty entry;
469
678
  entry.nameId = nameId;
470
- entry.value = value;
471
- entry.valueAtom = customPropertyValueAtom(value);
679
+ setCustomPropertyStorage(entry, value);
472
680
  values.push_back(std::move(entry));
473
681
  }
474
682
 
@@ -481,8 +689,7 @@ void NodeCustomPropertyStore::setColor(CssAtomId nameId,
481
689
  if (nameId == kInvalidCssAtom) return;
482
690
  for (auto &entry : values) {
483
691
  if (entry.nameId != nameId) continue;
484
- entry.value = value;
485
- entry.valueAtom = customPropertyValueAtom(value);
692
+ setCustomPropertyStorage(entry, value);
486
693
  entry.colorStyle = styleColor;
487
694
  entry.colorNative = nativeColor;
488
695
  entry.colorAlpha = alpha;
@@ -491,8 +698,7 @@ void NodeCustomPropertyStore::setColor(CssAtomId nameId,
491
698
  }
492
699
  NodeCustomProperty entry;
493
700
  entry.nameId = nameId;
494
- entry.value = value;
495
- entry.valueAtom = customPropertyValueAtom(value);
701
+ setCustomPropertyStorage(entry, value);
496
702
  entry.colorStyle = styleColor;
497
703
  entry.colorNative = nativeColor;
498
704
  entry.colorAlpha = alpha;
@@ -505,11 +711,11 @@ void NodeCustomPropertyStore::setLength(CssAtomId nameId,
505
711
  float lengthValue,
506
712
  std::uint8_t lengthUnit)
507
713
  {
714
+ #if GEA_CSS_CUSTOM_PROPERTY_LENGTHS
508
715
  if (nameId == kInvalidCssAtom) return;
509
716
  for (auto &entry : values) {
510
717
  if (entry.nameId != nameId) continue;
511
- entry.value = value;
512
- entry.valueAtom = customPropertyValueAtom(value);
718
+ setCustomPropertyStorage(entry, value);
513
719
  entry.lengthValue = lengthValue;
514
720
  entry.lengthUnit = lengthUnit;
515
721
  entry.flags = static_cast<std::uint8_t>((entry.flags & ~1u) | 2u);
@@ -517,12 +723,15 @@ void NodeCustomPropertyStore::setLength(CssAtomId nameId,
517
723
  }
518
724
  NodeCustomProperty entry;
519
725
  entry.nameId = nameId;
520
- entry.value = value;
521
- entry.valueAtom = customPropertyValueAtom(value);
726
+ setCustomPropertyStorage(entry, value);
522
727
  entry.lengthValue = lengthValue;
523
728
  entry.lengthUnit = lengthUnit;
524
729
  entry.flags = 2;
525
730
  values.push_back(std::move(entry));
731
+ #else
732
+ (void)lengthValue; (void)lengthUnit;
733
+ set(nameId, value);
734
+ #endif
526
735
  }
527
736
 
528
737
  const std::string *NodeCustomPropertyStore::get(const std::string &name) const
@@ -534,7 +743,7 @@ const std::string *NodeCustomPropertyStore::get(const std::string &name) const
534
743
  const std::string *NodeCustomPropertyStore::get(CssAtomId nameId) const
535
744
  {
536
745
  if (nameId == kInvalidCssAtom) return nullptr;
537
- if (const NodeCustomProperty *entry = getEntry(nameId)) return &entry->value;
746
+ if (const NodeCustomProperty *entry = getEntry(nameId)) return entry->value.get();
538
747
  return nullptr;
539
748
  }
540
749
 
@@ -546,6 +755,8 @@ const NodeCustomProperty *NodeCustomPropertyStore::getEntry(CssAtomId nameId) co
546
755
  return nullptr;
547
756
  }
548
757
 
758
+ #endif
759
+
549
760
  // File-scope lazy pointer rather than a function-local static. On this Xtensa
550
761
  // toolchain the static-local guard is NOT inlined, so a Meyers singleton pays a
551
762
  // __cxa_guard_acquire CALL on every access — and treeState() sits on the hottest
@@ -553,6 +764,14 @@ const NodeCustomProperty *NodeCustomPropertyStore::getEntry(CssAtomId nameId) co
553
764
  // (the mount task and the frame task never run concurrently), so the guard is
554
765
  // unnecessary; a zero-initialized pointer + null check is one load and a branch.
555
766
  static TreeState *g_treeState = nullptr;
767
+ StorageUsage treeStorageUsage()
768
+ {
769
+ StorageUsage usage;
770
+ usage.staticBytes = sizeof(g_treeState);
771
+ usage.addAllocation(g_treeState, sizeof(TreeState));
772
+ return usage;
773
+ }
774
+
556
775
 
557
776
  TreeState &treeState()
558
777
  {
@@ -571,4 +790,4 @@ TreeState &treeState()
571
790
  return *g_treeState;
572
791
  }
573
792
 
574
- } // namespace gea::embedded::ui
793
+ } // namespace gea::embedded::ui