le_mem.h
Go to the documentation of this file.
10 * malloc, free, strdup, calloc, realloc, etc. can result in performance degradation and out-of-memory13 * This is due to fragmentation of the heap. The degraded performance and exhausted memory result from indirect interactions37 * starts-up, and use them until your process terminates. It's up to the OS to clean-up the memory38 * pools, along with everything else your process is using, when your process terminates. (Although,39 * if you find yourself really needing to delete pools, @ref mem_sub_pools could offer you a solution.)48 * The following sections describe these, beginning with the most basic usage and working up to more54 * Before allocating memory from a pool, the pool must be created using le_mem_CreatePool(), passing55 * it the name of the pool and the size of the objects to be allocated from that pool. This returns60 * one function with three parameters) to make it virtually impossible to accidentally get the parameters88 * reference that it was given. This allows the xx_pt_ProcessStart() function to be re-implemented102 * For a discussion on how to pick the number of objects to have in your pools, see @ref mem_pool_sizes.108 * - @c le_mem_AssertAlloc() - Log an error and take down the process if there are no free blocks in110 * - @c le_mem_ForceAlloc() - If there are no free blocks in the pool, log a warning and automatically141 * Also, each object knows which pool it came from, so the code that releases the object doesn't have to care.159 * - Whenever someone calls le_mem_AddRef() on an object, its reference count is incremented by 1.161 * - When its reference count reaches zero, it's destroyed (i.e., its memory is released back into the pool.)166 * - increment its reference count and pass a pointer to the object to another function (or thread, data structure, etc.).168 * - release the object without having to worry about when the other function is finished with it.182 * Destructors are a powerful feature of C++. Anyone who has any non-trivial experience with C++ has187 * In Legato, it's possible to call @c le_mem_SetDestructor() to attach a function to a memory pool191 * the destructor returns, the object will be fully destroyed, and its memory will be released back226 * PointDestructor() for each object in the pointList, and the "Destroying point..." message will255 * The memory system also supports two different forms of diagnostics. Both are enabled by setting258 * The first of these options is @ref MEM_TRACE. When you enable @ref MEM_TRACE every pool is given261 * For instance, the configTree node pool is called, "configTree.nodePool". So to enable a trace of268 * The second diagnostic build flag is @ref MEM_POOLS. When @ref MEM_POOLS is disabled, the pools289 * Although memory pools are @a thread-safe, they are not @a async-safe. This means that memory pools291 * by a normal thread. To be safe, <b> don't call any memory pool functions from within a signal handler. </b>295 * between threads, so it's easy to forget to synchronize calls to @c le_mem_Release() with other code304 * Ideally, the pools should be fully allocated to their maximum sizes at start-up so there aren't310 * Choosing the right size for your pools correctly at start-up is easy to do if there is a maximum313 * carrier that will never carry more than 24 calls at a time, then it's obvious that you need to330 * of client. It can have multiple clients at the same time, but it doesn't know how many clients331 * or what their resource needs will be until the clients register with it at start-up. We'd want332 * those clients to be as decoupled from each other as possible (i.e., we want the clients know as little335 * report their own needs to the service-provider. Also, we don't want each client to have to wait337 * the services offered by the service-provider. That would add more complexity to the interactions351 * a problem because we can't @a shrink pools or delete pools when clients go away. This is where356 * Essentially, a Sub-Pool is a memory pool that gets its blocks from another pool (the super-pool).360 * dynamically pop into existence and later disappear again. When a client attaches to the service366 * so what's the point of having a sub-pool, when all of the resources could just be allocated from374 * A visual side effect of using sub-pools is that their blocks will count as being in use by the378 * and the number of objects to move to the sub-pool, and it returns a reference to the new sub-pool.380 * To delete a sub-pool, call @c le_mem_DeleteSubPool(). Do not try to use it to delete a pool that381 * was created using le_mem_CreatePool(). It's only for sub-pools created using le_mem_CreateSubPool().382 * Also, it's @b not okay to delete a sub-pool while there are still blocks allocated from it, or430 * super-pool, the initial number of objects in the sub-pool, and size of an object in the sub-pool454 //--------------------------------------------------------------------------------------------------462 //--------------------------------------------------------------------------------------------------473 //--------------------------------------------------------------------------------------------------477 * @param objPtr Pointer to the object where reference count has reached zero. After the destructor485 //--------------------------------------------------------------------------------------------------487 (494 //--------------------------------------------------------------------------------------------------498 * @note This should not be used directly. To create a memory pool use either le_mem_CreatePool()501 //--------------------------------------------------------------------------------------------------508 // These members go before LE_CONFIG_MEM_POOLS so numAllocations will always be aligned, even on539 //--------------------------------------------------------------------------------------------------544 //--------------------------------------------------------------------------------------------------548 //--------------------------------------------------------------------------------------------------552 //--------------------------------------------------------------------------------------------------565 //----------------------------------------------------------------------------------------------569 //----------------------------------------------------------------------------------------------577 //----------------------------------------------------------------------------------------------581 //----------------------------------------------------------------------------------------------593 //----------------------------------------------------------------------------------------------597 //----------------------------------------------------------------------------------------------610 //----------------------------------------------------------------------------------------------614 //----------------------------------------------------------------------------------------------629 //--------------------------------------------------------------------------------------------------634 //--------------------------------------------------------------------------------------------------649 //--------------------------------------------------------------------------------------------------655 //--------------------------------------------------------------------------------------------------664 //--------------------------------------------------------------------------------------------------675 //--------------------------------------------------------------------------------------------------677 (686 //--------------------------------------------------------------------------------------------------692 //--------------------------------------------------------------------------------------------------699 //--------------------------------------------------------------------------------------------------710 //--------------------------------------------------------------------------------------------------722 //--------------------------------------------------------------------------------------------------728 //--------------------------------------------------------------------------------------------------735 //--------------------------------------------------------------------------------------------------746 //--------------------------------------------------------------------------------------------------749 //--------------------------------------------------------------------------------------------------758 * @param numBlocks Default number of blocks. This can be overriden in components using the "pools"766 //--------------------------------------------------------------------------------------------------776 //--------------------------------------------------------------------------------------------------778 * Declare variables for a static memory pool, must specify which object section the variable goes788 * @param numBlocks Default number of blocks. This can be overridden in components using the "pools"791 * @param sectionName __attribute__((section("section"))) will be added to the pool declaration so the798 //--------------------------------------------------------------------------------------------------809 //--------------------------------------------------------------------------------------------------814 * @param numBlocks Default number of blocks. This can be overriden in components using the "pools"824 //--------------------------------------------------------------------------------------------------839 //--------------------------------------------------------------------------------------------------848 //--------------------------------------------------------------------------------------------------858 //----------------------------------------------------------------------------------------------866 //----------------------------------------------------------------------------------------------888 //----------------------------------------------------------------------------------------------898 //----------------------------------------------------------------------------------------------919 //----------------------------------------------------------------------------------------------929 //----------------------------------------------------------------------------------------------950 //----------------------------------------------------------------------------------------------958 //----------------------------------------------------------------------------------------------982 //----------------------------------------------------------------------------------------------992 //----------------------------------------------------------------------------------------------1015 //----------------------------------------------------------------------------------------------1025 //----------------------------------------------------------------------------------------------1046 //--------------------------------------------------------------------------------------------------1049 * (force or assert). Forced allocation will expand into the heap if the configured pool size is1050 * exceeded, while assert allocation will abort the program with an error if the pool cannot satisfy1057 //--------------------------------------------------------------------------------------------------1066 //--------------------------------------------------------------------------------------------------1068 * Attempts to allocate a variably-sized object from a pool using the configured allocation failure1069 * behaviour (force or assert). Forced allocation will expand into the heap if the configured pool1070 * size is exceeded, while assert allocation will abort the program with an error if the pool cannot1078 //--------------------------------------------------------------------------------------------------1087 //--------------------------------------------------------------------------------------------------1097 //--------------------------------------------------------------------------------------------------1107 //----------------------------------------------------------------------------------------------1121 //----------------------------------------------------------------------------------------------1143 //----------------------------------------------------------------------------------------------1152 //----------------------------------------------------------------------------------------------1172 //--------------------------------------------------------------------------------------------------1179 //--------------------------------------------------------------------------------------------------1185 //----------------------------------------------------------------------------------------------1198 //----------------------------------------------------------------------------------------------1205 //--------------------------------------------------------------------------------------------------1214 //--------------------------------------------------------------------------------------------------1222 //--------------------------------------------------------------------------------------------------1229 //--------------------------------------------------------------------------------------------------1237 //--------------------------------------------------------------------------------------------------1244 //--------------------------------------------------------------------------------------------------1251 //--------------------------------------------------------------------------------------------------1256 * "myComponent", then the full pool name returned by this function would be "myComponent.myPool".1262 //--------------------------------------------------------------------------------------------------1271 //--------------------------------------------------------------------------------------------------1279 //--------------------------------------------------------------------------------------------------1286 //--------------------------------------------------------------------------------------------------1294 //--------------------------------------------------------------------------------------------------1301 //--------------------------------------------------------------------------------------------------1308 //--------------------------------------------------------------------------------------------------1315 //--------------------------------------------------------------------------------------------------1317 * Fetches the total size of the object including all the memory overhead in a given pool (in bytes).1322 //--------------------------------------------------------------------------------------------------1330 //--------------------------------------------------------------------------------------------------1336 //--------------------------------------------------------------------------------------------------1343 //--------------------------------------------------------------------------------------------------1350 //--------------------------------------------------------------------------------------------------1352 (1359 //--------------------------------------------------------------------------------------------------1366 //--------------------------------------------------------------------------------------------------1378 //--------------------------------------------------------------------------------------------------1384 //--------------------------------------------------------------------------------------------------1394 //--------------------------------------------------------------------------------------------------1403 //--------------------------------------------------------------------------------------------------1405 (1415 //--------------------------------------------------------------------------------------------------1421 //--------------------------------------------------------------------------------------------------1428 //--------------------------------------------------------------------------------------------------1437 //--------------------------------------------------------------------------------------------------1452 //--------------------------------------------------------------------------------------------------1458 //--------------------------------------------------------------------------------------------------1469 //--------------------------------------------------------------------------------------------------1478 //--------------------------------------------------------------------------------------------------1480 (1489 return _le_mem_CreateReducedPool(superPool, STRINGIZE(LE_COMPONENT_NAME), name, numObjects, objSize);1492 //--------------------------------------------------------------------------------------------------1498 //--------------------------------------------------------------------------------------------------1507 //--------------------------------------------------------------------------------------------------1516 //--------------------------------------------------------------------------------------------------1532 //--------------------------------------------------------------------------------------------------1541 //--------------------------------------------------------------------------------------------------1548 //--------------------------------------------------------------------------------------------------1557 //--------------------------------------------------------------------------------------------------1565 //--------------------------------------------------------------------------------------------------1574 //--------------------------------------------------------------------------------------------------1582 //--------------------------------------------------------------------------------------------------1584 * Duplicate a UTF-8 string. The space for the duplicate string will be allocated from the provided1590 //--------------------------------------------------------------------------------------------------size_t numFreeNumber of free objects currently available in this pool.Definition: le_mem.h:559void * le_mem_TryAlloc(le_mem_PoolRef_t pool)void * le_mem_TryVarAlloc(le_mem_PoolRef_t pool, size_t size)le_mem_PoolRef_t le_mem_ExpandPool(le_mem_PoolRef_t pool, size_t numObjects)void * le_mem_AssertAlloc(le_mem_PoolRef_t pool)void le_mem_GetStats(le_mem_PoolRef_t pool, le_mem_PoolStats_t *statsPtr)void le_mem_Resume(void)size_t numBlocksInUseNumber of currently allocated blocks.Definition: le_mem.h:555void le_mem_SetDestructor(le_mem_PoolRef_t pool, le_mem_Destructor_t destructor)void * le_mem_AssertVarAlloc(le_mem_PoolRef_t pool, size_t size)uint64_t numAllocsNumber of times an object has been allocated from this pool.Definition: le_mem.h:558size_t maxNumBlocksUsedMaximum number of allocated blocks at any one time.Definition: le_mem.h:513size_t maxNumBlocksUsedMaximum number of allocated blocks at any one time.Definition: le_mem.h:556void * le_mem_ForceVarAlloc(le_mem_PoolRef_t pool, size_t size)void le_mem_ResetStats(le_mem_PoolRef_t pool)size_t numOverflowsNumber of times le_mem_ForceAlloc() had to expand the pool.Definition: le_mem.h:557static le_mem_PoolRef_t le_mem_CreateReducedPool(le_mem_PoolRef_t superPool, const char *name, size_t numObjects, size_t objSize)Definition: le_mem.h:1480void le_mem_SetNumObjsToForce(le_mem_PoolRef_t pool, size_t numObjects)size_t numOverflowsNumber of times le_mem_ForceAlloc() had to expand pool.Definition: le_mem.h:510char * le_mem_StrDup(le_mem_PoolRef_t poolRef, const char *srcStr)void le_mem_DeleteSubPool(le_mem_PoolRef_t subPool)le_result_t le_mem_GetName(le_mem_PoolRef_t pool, char *namePtr, size_t bufSize)bool le_mem_IsSubPool(le_mem_PoolRef_t pool)void le_mem_Release(void *objPtr)Definition: le_mem.h:553size_t blockSizeNumber of bytes in a block, including all overhead.Definition: le_mem.h:520size_t le_mem_GetObjectCount(le_mem_PoolRef_t pool)Definition: le_mem.h:502Definition: le_singlyLinkedList.h:201size_t le_mem_GetObjectFullSize(le_mem_PoolRef_t pool)le_mem_Destructor_t destructorThe destructor for objects in this pool.Definition: le_mem.h:531size_t le_mem_GetBlockSize(void *objPtr)static le_mem_PoolRef_t le_mem_CreatePool(const char *name, size_t objSize)Definition: le_mem.h:677size_t le_mem_GetObjectSize(le_mem_PoolRef_t pool)Definition: le_doublyLinkedList.h:200le_dls_Link_t poolLinkThis pool's link in the list of memory pools.Definition: le_mem.h:504static le_mem_PoolRef_t le_mem_CreateSubPool(le_mem_PoolRef_t superPool, const char *name, size_t numObjects)Definition: le_mem.h:1405void le_mem_AddRef(void *objPtr)void * le_mem_ForceAlloc(le_mem_PoolRef_t pool)size_t userDataSizeSize of the object requested by the client in bytes.Definition: le_mem.h:519void le_mem_Hibernate(void **freeStartPtr, void **freeEndPtr)size_t numBlocksInUseNumber of currently allocated blocks.Definition: le_mem.h:523size_t le_mem_GetRefCount(void *objPtr)