project-chip / connectedhomeip

Matter (formerly Project CHIP) creates more connections between more objects, simplifying development for manufacturers and increasing compatibility for consumers, guided by the Connectivity Standards Alliance.
https://buildwithmatter.com
Apache License 2.0
7.43k stars 1.99k forks source link

Allow more control over Pool sizes #7716

Closed andy31415 closed 3 years ago

andy31415 commented 3 years ago

Problem

Existing code memory allocation story is not very uniform:

Downside of these strategies:

mrjerryjohns commented 3 years ago

Additional recommendations:

  1. BitMapObjectPool already exists as a templated type to implement pools. Should we just use that everywhere?
  2. Bias towards application-owned object lifetimes if possible.
  3. If that isn't possible, I'd highly recommend a model where the allocation algorithm when allocating out of a specific pool, degenerates to calling a 'AllocPoolObj' call that is provided by the application. This makes the code path regular and standard across constrained and unconstrained devices, with the latter actually returning a heap-allocated object as a result of that call.
yufengwangca commented 3 years ago

After auditing our code, we have three memory management implementations in current CHIP SDK.

  1. 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.

  2. 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)

  3. 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.

kpschoedel commented 3 years ago

Don't forget System::ObjectPool