Skip to content

Commit 9049891

Browse files
kpschoedelpull[bot]
authored andcommitted
Add heap allocation to lib/support/Pool.h (#11698)
#### Problem We have too many pool allocators. Previous PRs (#11428, #11487) transitionally use `BitMapObjectPool` where previously `System::ObjectPool` had been used, but this lost the ability to configure heap allocation. #### Change overview - Add a heap allocator (from #9590) - Add allocation selection (from #11371) - Use this for `System::Timer` (complementing #11487) - Factor out common code. - Use a heap-allocated list to track heap-allocated objects. - More unit tests. Co-authored-by: Zang MingJie <zealot0630@gmail.com> Co-authored-by: C Freeman <cecille@google.com> A future PR will use this for Inet pools (complementing #11428); that is not done here because it would conflict with other Inet changes under way. #### Testing Added heap versions of unit tests in TestPool. (A future PR will add `System::Object`-style statistics and re-unify most of these tests.) CI should show `.bss` decreases corresponding to increases in #11487.
1 parent 4770e6f commit 9049891

File tree

5 files changed

+564
-72
lines changed

5 files changed

+564
-72
lines changed

src/lib/support/Pool.cpp

+64-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
namespace chip {
2525

26+
namespace internal {
27+
2628
StaticAllocatorBitmap::StaticAllocatorBitmap(void * storage, std::atomic<tBitChunkType> * usage, size_t capacity,
2729
size_t elementSize) :
2830
StaticAllocatorBase(capacity),
@@ -46,7 +48,7 @@ void * StaticAllocatorBitmap::Allocate()
4648
{
4749
if (usage.compare_exchange_strong(value, value | (kBit1 << offset)))
4850
{
49-
mAllocated++;
51+
IncreaseUsage();
5052
return At(word * kBitChunkSize + offset);
5153
}
5254
else
@@ -70,7 +72,7 @@ void StaticAllocatorBitmap::Deallocate(void * element)
7072

7173
auto value = mUsage[word].fetch_and(~(kBit1 << offset));
7274
nlASSERT((value & (kBit1 << offset)) != 0); // assert fail when free an unused slot
73-
mAllocated--;
75+
DecreaseUsage();
7476
}
7577

7678
size_t StaticAllocatorBitmap::IndexOf(void * element)
@@ -83,7 +85,7 @@ size_t StaticAllocatorBitmap::IndexOf(void * element)
8385
return index;
8486
}
8587

86-
bool StaticAllocatorBitmap::ForEachActiveObjectInner(void * context, Lambda lambda)
88+
bool StaticAllocatorBitmap::ForEachActiveObjectInner(void * context, bool lambda(void * context, void * object))
8789
{
8890
for (size_t word = 0; word * kBitChunkSize < Capacity(); ++word)
8991
{
@@ -101,4 +103,63 @@ bool StaticAllocatorBitmap::ForEachActiveObjectInner(void * context, Lambda lamb
101103
return true;
102104
}
103105

106+
#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
107+
108+
HeapObjectListNode * HeapObjectList::FindNode(void * object) const
109+
{
110+
for (HeapObjectListNode * p = mNext; p != this; p = p->mNext)
111+
{
112+
if (p->mObject == object)
113+
{
114+
return p;
115+
}
116+
}
117+
return nullptr;
118+
}
119+
120+
using Lambda = bool (*)(void *, void *);
121+
bool HeapObjectList::ForEachNode(void * context, bool lambda(void * context, void * object))
122+
{
123+
++mIterationDepth;
124+
bool result = true;
125+
bool anyReleased = false;
126+
HeapObjectListNode * p = mNext;
127+
while (p != this)
128+
{
129+
if (p->mObject != nullptr)
130+
{
131+
if (!lambda(context, p->mObject))
132+
{
133+
result = false;
134+
break;
135+
}
136+
}
137+
if (p->mObject == nullptr)
138+
{
139+
anyReleased = true;
140+
}
141+
p = p->mNext;
142+
}
143+
--mIterationDepth;
144+
if (mIterationDepth == 0 && anyReleased)
145+
{
146+
// Remove nodes for released objects.
147+
p = mNext;
148+
while (p != this)
149+
{
150+
HeapObjectListNode * next = p->mNext;
151+
if (p->mObject == nullptr)
152+
{
153+
p->Remove();
154+
delete p;
155+
}
156+
p = next;
157+
}
158+
}
159+
return result;
160+
}
161+
162+
#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
163+
164+
} // namespace internal
104165
} // namespace chip

0 commit comments

Comments
 (0)