[pool] extract PoolBase and add ConfigPool (#12596)

This commit updates the object pool implementation to support dynamically
configured pool sizes without penalizing the memory footprint of
existing static usages.

A new `PoolBase` class is introduced to encapsulate the core linked-list
management logic (`Allocate()` and `Free()`), relying on a shared
`mFreeList`.

The existing `Pool` class is updated to inherit from `PoolBase`. It
retains its statically allocated `mPool` array, ensuring zero code-size
or RAM penalty for current users of fixed-size pools.

A new `ConfigPool` class is added, also inheriting from `PoolBase`.
This class allows a pool to be initialized at run-time with an
externally provided memory buffer (`mEntryArray`) and size
(`mNumEntries`). This enables future use cases where memory allocation
for pools can be provided dynamically by the system integrator.

This commit also includes unit tests for `ConfigPool` to verify its
allocation, deallocation, and initialization behavior.
This commit is contained in:
Abtin Keshavarzian
2026-03-04 00:40:13 -06:00
committed by GitHub
parent e5169ea810
commit 01d75f730f
2 changed files with 148 additions and 41 deletions
+26 -1
View File
@@ -61,6 +61,7 @@ private:
constexpr uint16_t kPoolSize = 11;
typedef Pool<Entry, kPoolSize> EntryPool;
typedef ConfigPool<Entry> ConfigEntryPool;
static Entry sNonPoolEntry;
@@ -79,7 +80,15 @@ void VerifyEntry(EntryPool &aPool, const Entry &aEntry, bool aInitWithInstance)
VerifyOrQuit(aEntry.IsInitializedWithInstance() == aInitWithInstance, "Pool did not correctly Init() entry");
}
void TestPool(EntryPool &aPool, bool aInitWithInstance)
void VerifyEntry(ConfigEntryPool &aPool, const Entry &aEntry, bool aInitWithInstance)
{
OT_UNUSED_VARIABLE(aInitWithInstance);
VerifyOrQuit(aPool.IsPoolEntry(aEntry));
VerifyOrQuit(!aPool.IsPoolEntry(sNonPoolEntry), "Pool::IsPoolEntry() succeeded for non-pool entry");
}
template <typename PoolType> void TestPool(PoolType &aPool, bool aInitWithInstance = false)
{
Entry *entries[kPoolSize];
@@ -121,17 +130,33 @@ void TestPool(void)
EntryPool pool1;
EntryPool pool2(*instance);
printf("TestPool(/* aInitWithInstance */ false)\n");
TestPool(pool1, /* aInitWithInstance */ false);
printf("TestPool(/* aInitWithInstance */ true)\n");
TestPool(pool2, /* aInitWithInstance */ true);
testFreeInstance(instance);
}
void TestConfigPool(void)
{
ConfigEntryPool pool;
Entry entries[kPoolSize];
printf("TestConfigPool()\n");
SuccessOrQuit(pool.Init(entries, kPoolSize));
TestPool(pool);
VerifyOrQuit(pool.Init(entries, kPoolSize) != kErrorNone);
}
} // namespace ot
int main(void)
{
ot::TestPool();
ot::TestConfigPool();
printf("All tests passed\n");
return 0;
}