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 from375 * and the number of objects to move to the sub-pool, and it returns a reference to the new sub-pool.377 * To delete a sub-pool, call @c le_mem_DeleteSubPool(). Do not try to use it to delete a pool that378 * was created using le_mem_CreatePool(). It's only for sub-pools created using le_mem_CreateSubPool().379 * Also, it's @b not okay to delete a sub-pool while there are still blocks allocated from it, or427 * super-pool, the initial number of objects in the sub-pool, and size of an object in the sub-pool451 //--------------------------------------------------------------------------------------------------459 //--------------------------------------------------------------------------------------------------468 //--------------------------------------------------------------------------------------------------472 * @param objPtr Pointer to the object where reference count has reached zero. After the destructor480 //--------------------------------------------------------------------------------------------------482 (489 //--------------------------------------------------------------------------------------------------493 * @note This should not be used directly. To create a memory pool use either le_mem_CreatePool()496 //--------------------------------------------------------------------------------------------------503 // These members go before LE_CONFIG_MEM_POOLS so numAllocations will always be aligned, even on534 //--------------------------------------------------------------------------------------------------539 //--------------------------------------------------------------------------------------------------543 //--------------------------------------------------------------------------------------------------547 //--------------------------------------------------------------------------------------------------560 //----------------------------------------------------------------------------------------------564 //----------------------------------------------------------------------------------------------572 //----------------------------------------------------------------------------------------------576 //----------------------------------------------------------------------------------------------588 //----------------------------------------------------------------------------------------------592 //----------------------------------------------------------------------------------------------605 //----------------------------------------------------------------------------------------------609 //----------------------------------------------------------------------------------------------624 //--------------------------------------------------------------------------------------------------629 //--------------------------------------------------------------------------------------------------644 //--------------------------------------------------------------------------------------------------650 //--------------------------------------------------------------------------------------------------659 //--------------------------------------------------------------------------------------------------670 //--------------------------------------------------------------------------------------------------672 (681 //--------------------------------------------------------------------------------------------------687 //--------------------------------------------------------------------------------------------------694 //--------------------------------------------------------------------------------------------------705 //--------------------------------------------------------------------------------------------------717 //--------------------------------------------------------------------------------------------------723 //--------------------------------------------------------------------------------------------------730 //--------------------------------------------------------------------------------------------------741 //--------------------------------------------------------------------------------------------------744 //--------------------------------------------------------------------------------------------------753 * @param numBlocks Default number of blocks. This can be overriden in components using the "pools"761 //--------------------------------------------------------------------------------------------------771 //--------------------------------------------------------------------------------------------------773 * Declare variables for a static memory pool, must specify which object section the variable goes783 * @param numBlocks Default number of blocks. This can be overridden in components using the "pools"786 * @param sectionName __attribute__((section("section"))) will be added to the pool declaration so the793 //--------------------------------------------------------------------------------------------------804 //--------------------------------------------------------------------------------------------------809 * @param numBlocks Default number of blocks. This can be overriden in components using the "pools"819 //--------------------------------------------------------------------------------------------------834 //--------------------------------------------------------------------------------------------------843 //--------------------------------------------------------------------------------------------------853 //----------------------------------------------------------------------------------------------861 //----------------------------------------------------------------------------------------------883 //----------------------------------------------------------------------------------------------893 //----------------------------------------------------------------------------------------------914 //----------------------------------------------------------------------------------------------924 //----------------------------------------------------------------------------------------------945 //----------------------------------------------------------------------------------------------953 //----------------------------------------------------------------------------------------------977 //----------------------------------------------------------------------------------------------987 //----------------------------------------------------------------------------------------------1010 //----------------------------------------------------------------------------------------------1020 //----------------------------------------------------------------------------------------------1041 //--------------------------------------------------------------------------------------------------1044 * (force or assert). Forced allocation will expand into the heap if the configured pool size is1045 * exceeded, while assert allocation will abort the program with an error if the pool cannot satisfy1052 //--------------------------------------------------------------------------------------------------1061 //--------------------------------------------------------------------------------------------------1063 * Attempts to allocate a variably-sized object from a pool using the configured allocation failure1064 * behaviour (force or assert). Forced allocation will expand into the heap if the configured pool1065 * size is exceeded, while assert allocation will abort the program with an error if the pool cannot1073 //--------------------------------------------------------------------------------------------------1082 //--------------------------------------------------------------------------------------------------1092 //--------------------------------------------------------------------------------------------------1102 //----------------------------------------------------------------------------------------------1116 //----------------------------------------------------------------------------------------------1138 //----------------------------------------------------------------------------------------------1147 //----------------------------------------------------------------------------------------------1167 //--------------------------------------------------------------------------------------------------1174 //--------------------------------------------------------------------------------------------------1180 //----------------------------------------------------------------------------------------------1193 //----------------------------------------------------------------------------------------------1200 //--------------------------------------------------------------------------------------------------1209 //--------------------------------------------------------------------------------------------------1217 //--------------------------------------------------------------------------------------------------1224 //--------------------------------------------------------------------------------------------------1232 //--------------------------------------------------------------------------------------------------1239 //--------------------------------------------------------------------------------------------------1246 //--------------------------------------------------------------------------------------------------1251 * "myComponent", then the full pool name returned by this function would be "myComponent.myPool".1257 //--------------------------------------------------------------------------------------------------1266 //--------------------------------------------------------------------------------------------------1274 //--------------------------------------------------------------------------------------------------1281 //--------------------------------------------------------------------------------------------------1289 //--------------------------------------------------------------------------------------------------1296 //--------------------------------------------------------------------------------------------------1303 //--------------------------------------------------------------------------------------------------1310 //--------------------------------------------------------------------------------------------------1312 * Fetches the total size of the object including all the memory overhead in a given pool (in bytes).1317 //--------------------------------------------------------------------------------------------------1325 //--------------------------------------------------------------------------------------------------1331 //--------------------------------------------------------------------------------------------------1338 //--------------------------------------------------------------------------------------------------1345 //--------------------------------------------------------------------------------------------------1347 (1354 //--------------------------------------------------------------------------------------------------1361 //--------------------------------------------------------------------------------------------------1373 //--------------------------------------------------------------------------------------------------1379 //--------------------------------------------------------------------------------------------------1389 //--------------------------------------------------------------------------------------------------1398 //--------------------------------------------------------------------------------------------------1400 (1410 //--------------------------------------------------------------------------------------------------1416 //--------------------------------------------------------------------------------------------------1423 //--------------------------------------------------------------------------------------------------1432 //--------------------------------------------------------------------------------------------------1447 //--------------------------------------------------------------------------------------------------1453 //--------------------------------------------------------------------------------------------------1464 //--------------------------------------------------------------------------------------------------1473 //--------------------------------------------------------------------------------------------------1475 (1484 return _le_mem_CreateReducedPool(superPool, STRINGIZE(LE_COMPONENT_NAME), name, numObjects, objSize);1487 //--------------------------------------------------------------------------------------------------1493 //--------------------------------------------------------------------------------------------------1502 //--------------------------------------------------------------------------------------------------1511 //--------------------------------------------------------------------------------------------------1527 //--------------------------------------------------------------------------------------------------1536 //--------------------------------------------------------------------------------------------------1543 //--------------------------------------------------------------------------------------------------1552 //--------------------------------------------------------------------------------------------------1560 //--------------------------------------------------------------------------------------------------1569 //--------------------------------------------------------------------------------------------------1577 //--------------------------------------------------------------------------------------------------1579 * Duplicate a UTF-8 string. The space for the duplicate string will be allocated from the provided1585 //--------------------------------------------------------------------------------------------------size_t numFreeNumber of free objects currently available in this pool.Definition: le_mem.h:554void * 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:550void 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:553size_t maxNumBlocksUsedMaximum number of allocated blocks at any one time.Definition: le_mem.h:508size_t maxNumBlocksUsedMaximum number of allocated blocks at any one time.Definition: le_mem.h:551void * 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:552static 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:1475void 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:505char * 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:548size_t blockSizeNumber of bytes in a block, including all overhead.Definition: le_mem.h:515size_t le_mem_GetObjectCount(le_mem_PoolRef_t pool)Definition: le_mem.h:497Definition: 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:526size_t le_mem_GetBlockSize(void *objPtr)static le_mem_PoolRef_t le_mem_CreatePool(const char *name, size_t objSize)Definition: le_mem.h:672size_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:499static le_mem_PoolRef_t le_mem_CreateSubPool(le_mem_PoolRef_t superPool, const char *name, size_t numObjects)Definition: le_mem.h:1400void 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:514void le_mem_Hibernate(void **freeStartPtr, void **freeEndPtr)size_t numBlocksInUseNumber of currently allocated blocks.Definition: le_mem.h:518size_t le_mem_GetRefCount(void *objPtr)