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 //--------------------------------------------------------------------------------------------------723 //--------------------------------------------------------------------------------------------------729 //--------------------------------------------------------------------------------------------------736 //--------------------------------------------------------------------------------------------------747 //--------------------------------------------------------------------------------------------------750 //--------------------------------------------------------------------------------------------------759 * @param numBlocks Default number of blocks. This can be overriden in components using the "pools"767 //--------------------------------------------------------------------------------------------------777 //--------------------------------------------------------------------------------------------------779 * Declare variables for a static memory pool, must specify which object section the variable goes789 * @param numBlocks Default number of blocks. This can be overridden in components using the "pools"792 * @param sectionName __attribute__((section("section"))) will be added to the pool declaration so the799 //--------------------------------------------------------------------------------------------------810 //--------------------------------------------------------------------------------------------------815 * @param numBlocks Default number of blocks. This can be overriden in components using the "pools"825 //--------------------------------------------------------------------------------------------------840 //--------------------------------------------------------------------------------------------------849 //--------------------------------------------------------------------------------------------------859 //----------------------------------------------------------------------------------------------867 //----------------------------------------------------------------------------------------------889 //----------------------------------------------------------------------------------------------899 //----------------------------------------------------------------------------------------------920 //----------------------------------------------------------------------------------------------930 //----------------------------------------------------------------------------------------------951 //----------------------------------------------------------------------------------------------959 //----------------------------------------------------------------------------------------------983 //----------------------------------------------------------------------------------------------993 //----------------------------------------------------------------------------------------------1016 //----------------------------------------------------------------------------------------------1026 //----------------------------------------------------------------------------------------------1047 //--------------------------------------------------------------------------------------------------1050 * (force or assert). Forced allocation will expand into the heap if the configured pool size is1051 * exceeded, while assert allocation will abort the program with an error if the pool cannot satisfy1058 //--------------------------------------------------------------------------------------------------1067 //--------------------------------------------------------------------------------------------------1069 * Attempts to allocate a variably-sized object from a pool using the configured allocation failure1070 * behaviour (force or assert). Forced allocation will expand into the heap if the configured pool1071 * size is exceeded, while assert allocation will abort the program with an error if the pool cannot1079 //--------------------------------------------------------------------------------------------------1088 //--------------------------------------------------------------------------------------------------1098 //--------------------------------------------------------------------------------------------------1108 //----------------------------------------------------------------------------------------------1122 //----------------------------------------------------------------------------------------------1144 //----------------------------------------------------------------------------------------------1153 //----------------------------------------------------------------------------------------------1173 //--------------------------------------------------------------------------------------------------1180 //--------------------------------------------------------------------------------------------------1186 //----------------------------------------------------------------------------------------------1199 //----------------------------------------------------------------------------------------------1206 //--------------------------------------------------------------------------------------------------1215 //--------------------------------------------------------------------------------------------------1223 //--------------------------------------------------------------------------------------------------1230 //--------------------------------------------------------------------------------------------------1238 //--------------------------------------------------------------------------------------------------1245 //--------------------------------------------------------------------------------------------------1252 //--------------------------------------------------------------------------------------------------1257 * "myComponent", then the full pool name returned by this function would be "myComponent.myPool".1263 //--------------------------------------------------------------------------------------------------1272 //--------------------------------------------------------------------------------------------------1280 //--------------------------------------------------------------------------------------------------1287 //--------------------------------------------------------------------------------------------------1295 //--------------------------------------------------------------------------------------------------1302 //--------------------------------------------------------------------------------------------------1309 //--------------------------------------------------------------------------------------------------1316 //--------------------------------------------------------------------------------------------------1318 * Fetches the total size of the object including all the memory overhead in a given pool (in bytes).1323 //--------------------------------------------------------------------------------------------------1331 //--------------------------------------------------------------------------------------------------1337 //--------------------------------------------------------------------------------------------------1344 //--------------------------------------------------------------------------------------------------1351 //--------------------------------------------------------------------------------------------------1353 (1360 //--------------------------------------------------------------------------------------------------1367 //--------------------------------------------------------------------------------------------------1380 //--------------------------------------------------------------------------------------------------1386 //--------------------------------------------------------------------------------------------------1396 //--------------------------------------------------------------------------------------------------1405 //--------------------------------------------------------------------------------------------------1407 (1417 //--------------------------------------------------------------------------------------------------1423 //--------------------------------------------------------------------------------------------------1430 //--------------------------------------------------------------------------------------------------1439 //--------------------------------------------------------------------------------------------------1455 //--------------------------------------------------------------------------------------------------1461 //--------------------------------------------------------------------------------------------------1472 //--------------------------------------------------------------------------------------------------1481 //--------------------------------------------------------------------------------------------------1483 (1492 return _le_mem_CreateReducedPool(superPool, STRINGIZE(LE_COMPONENT_NAME), name, numObjects, objSize);1495 //--------------------------------------------------------------------------------------------------1501 //--------------------------------------------------------------------------------------------------1510 //--------------------------------------------------------------------------------------------------1519 //--------------------------------------------------------------------------------------------------1536 //--------------------------------------------------------------------------------------------------1545 //--------------------------------------------------------------------------------------------------1552 //--------------------------------------------------------------------------------------------------1556 //--------------------------------------------------------------------------------------------------1564 //--------------------------------------------------------------------------------------------------1573 //--------------------------------------------------------------------------------------------------1581 //--------------------------------------------------------------------------------------------------1590 //--------------------------------------------------------------------------------------------------1598 //--------------------------------------------------------------------------------------------------1600 * Duplicate a UTF-8 string. The space for the duplicate string will be allocated from the provided1606 //--------------------------------------------------------------------------------------------------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:1483void 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)void le_mem_InitHibernation(void *startPtr, void *endPtr)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:1407void 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)