Closed andy31415 closed 3 years ago
Additional recommendations:
BitMapObjectPool
already exists as a templated type to implement pools. Should we just use that everywhere?After auditing our code, we have three memory management implementations in current CHIP SDK.
Heap memory allocation APIs The interface is defined in support/CHIPMem.h, which provides standard C dynamic memory management like APIs, such as MemoryAlloc/MemoryCalloc/MemoryRealloc/MemoryFree.
Its implementation is based on either the system C dynamic memory management or our private implementations in PrivateHeap.h/PrivateHeap.cpp.
Memory pool class BitMapObjectPool The interface is defined in support/Pool.h. This is actually not a typical pool implementation which provides similar APIs to allows dynamic memory allocation comparable to malloc or C++'s operator new based on pre-defined fixed blocks with different sizes. Such as you can not request a memory block from the pool with arbitrary size.
It is a smarter C++ implementation of static array. You need to specify the list of object of the same type, and all of the memory managed by the pool is allocated only once (during construction of the pool object)
Memory pool class SystemObject This interface is defined in system/SystemObject.h, this pool implementation provides similar functions as BitMapObjectPool, but does not call the constructor as you add the element to the pool and destructor as you remove the element from the pool. It does not use operator New to initialize the new object added to the pool, so it does not have the meta overhead come with it.
In summary, the original Pool implementation based on pre-configured fixed-size blocks in support/CHIPMem-Simple.cpp has been replaced by private heap implementation in support/PrivateHeadp.cpp, and BitMapObjectPool is a C++ implementation of static array and SystemObject is a C implementation of static array.
Don't forget System::ObjectPool
Problem
Existing code memory allocation story is not very uniform:
Downside of these strategies:
Pools always need to be over-provisioned for the worst case, making each pool enerally waste a bit of RAM and this adds up the more memory poools we have
Proposed Solution