Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add heap allocation to lib/support/Pool.h #11698

Merged
merged 6 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions src/lib/support/Pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace chip {

namespace internal {

StaticAllocatorBitmap::StaticAllocatorBitmap(void * storage, std::atomic<tBitChunkType> * usage, size_t capacity,
size_t elementSize) :
StaticAllocatorBase(capacity),
Expand All @@ -46,7 +48,7 @@ void * StaticAllocatorBitmap::Allocate()
{
if (usage.compare_exchange_strong(value, value | (kBit1 << offset)))
{
mAllocated++;
IncreaseUsage();
return At(word * kBitChunkSize + offset);
}
else
Expand All @@ -70,7 +72,7 @@ void StaticAllocatorBitmap::Deallocate(void * element)

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

size_t StaticAllocatorBitmap::IndexOf(void * element)
Expand All @@ -83,7 +85,7 @@ size_t StaticAllocatorBitmap::IndexOf(void * element)
return index;
}

bool StaticAllocatorBitmap::ForEachActiveObjectInner(void * context, Lambda lambda)
bool StaticAllocatorBitmap::ForEachActiveObjectInner(void * context, bool lambda(void * context, void * object))
{
for (size_t word = 0; word * kBitChunkSize < Capacity(); ++word)
{
Expand All @@ -101,4 +103,61 @@ bool StaticAllocatorBitmap::ForEachActiveObjectInner(void * context, Lambda lamb
return true;
}

#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP

HeapObjectListNode * HeapObjectList::FindNode(void * object) const
{
for (HeapObjectListNode * p = mNext; p != this; p = p->mNext)
{
if (p->mObject == object)
{
return p;
}
}
return nullptr;
}

using Lambda = bool (*)(void *, void *);
bool HeapObjectList::ForEachNode(void * context, bool lambda(void * context, void * object))
{
bool result = true;
bool anyReleased = false;
HeapObjectListNode * p = mNext;
while (p != this)
{
if (p->mObject != nullptr)
{
if (!lambda(context, p->mObject))
{
result = false;
break;
}
}
if (p->mObject == nullptr)
{
anyReleased = true;
}
p = p->mNext;
}
if (anyReleased)
{
// Remove nodes for released objects.
p = mNext;
while (p != this)
{
HeapObjectListNode * next = p->mNext;
if (p->mObject == nullptr)
{
p->Remove();
delete p;
}
p = next;
}
}
return result;
}

#endif // CHIP_SYSTEM_CONFIG_POOL_USE_HEAP

} // namespace internal
} // namespace chip
Loading