Lines Matching refs:pool

21  * \brief Memory pool management.
48 * Represent a page of memory allocate by a memory pool.
69 * \brief Memory pool.
71 * A pool of memory from which individual memory allocations can be made.
73 * but rather all of the memory allocated from a pool is freed when the pool
76 * The pools can be arranged into a hierarchy. If a pool with children is
78 * the pool itself.
82 * creating the root pool with the deMemPool_createFailingRoot() function.
92 deMemPool* firstChild; /*!< Pointer to first child pool in linked list. */
93 deMemPool* prevPool; /*!< Previous pool in parent's linked list. */
94 deMemPool* nextPool; /*!< Next pool in parent's linked list. */
106 int lastAllocatedIndex; /*!< Index of last allocated pool (rootPool only). */
165 * \brief Internal function for creating a new memory pool.
166 * \param parent Parent pool (may be null).
167 * \return The created memory pool (or null on failure).
171 deMemPool* pool;
187 /* Alloc pool from initial page. */
189 pool = (deMemPool*)(initialPage + 1);
192 memset(pool, 0, sizeof(deMemPool));
193 pool->currentPage = initialPage;
196 pool->parent = parent;
200 if (parent->firstChild) parent->firstChild->prevPool = pool;
201 pool->nextPool = parent->firstChild;
202 parent->firstChild = pool;
206 pool->util = parent ? parent->util : DE_NULL;
209 pool->allowFailing = parent ? parent->allowFailing : DE_FALSE;
210 deRandom_init(&pool->failRandom, parent ? deRandom_getUint32(&parent->failRandom) : 0x1234abcd);
214 pool->enableDebugAllocs = parent ? parent->enableDebugAllocs : DE_FALSE;
215 pool->debugAllocListHead = DE_NULL;
219 deMemPool* root = pool;
223 if (pool == root)
226 pool->allocIndex = ++root->lastAllocatedIndex;
228 /* \note Put the index of leaking pool here and add a breakpoint to catch leaks easily. */
229 /* if (pool->allocIndex == 51)
234 return pool;
238 * \brief Create a new root memory pool.
239 * \return The created memory pool (or null on failure).
243 deMemPool* pool = createPoolInternal(DE_NULL);
244 if (!pool)
248 pool->allowFailing = DE_TRUE;
253 pool->enableDebugAllocs = DE_TRUE;
254 pool->debugAllocListHead = DE_NULL;
262 deMemPoolUtil* utilCopy = DE_POOL_NEW(pool, deMemPoolUtil);
266 deMemPool_destroy(pool);
271 pool->util = utilCopy;
274 return pool;
278 * \brief Create a sub-pool for an existing memory pool.
279 * \return The created memory pool (or null on failure).
283 deMemPool* pool;
285 pool = createPoolInternal(parent);
286 if (!pool && parent->util)
288 return pool;
292 * \brief Destroy a memory pool.
293 * \param pool Pool to be destroyed.
295 * Frees all the memory allocated from the pool. Also destroyed any child
296 * pools that the pool has (recursively).
298 void deMemPool_destroy (deMemPool* pool)
305 if (pool->parent)
307 deMemPool* root = pool->parent;
316 iter = pool->firstChild;
324 DE_ASSERT(pool->numChildren == 0);
327 if (pool->prevPool) pool->prevPool->nextPool = pool->nextPool;
328 if (pool->nextPool) pool->nextPool->prevPool = pool->prevPool;
330 if (pool->parent)
332 deMemPool* parent = pool->parent;
333 if (parent->firstChild == pool)
334 parent->firstChild = pool->nextPool;
342 if (pool->enableDebugAllocs)
344 DebugAlloc* alloc = pool->debugAllocListHead;
355 pool->debugAllocListHead = DE_NULL;
360 /* \note Pool itself is allocated from first page, so we must not touch the pool after freeing the page! */
362 MemPage* page = pool->currentPage;
375 * \brief Get the number of children for a pool.
376 * \return The number of (immediate) child pools a memory pool has.
378 int deMemPool_getNumChildren (const deMemPool* pool)
380 return pool->numChildren;
384 * \brief Get the number of bytes allocated (by the user) from the pool.
385 * \param pool Pool pointer.
387 * \return The number of bytes allocated by the pool (including child pools
390 int deMemPool_getNumAllocatedBytes (const deMemPool* pool, deBool recurse)
395 for (memPage = pool->currentPage; memPage; memPage = memPage->nextPage)
401 for (child = pool->firstChild; child; child = child->nextPool)
408 int deMemPool_getCapacity (const deMemPool* pool, deBool recurse)
413 for (memPage = pool->currentPage; memPage; memPage = memPage->nextPage)
419 for (child = pool->firstChild; child; child = child->nextPool)
426 DE_INLINE void* deMemPool_allocInternal (deMemPool* pool, size_t numBytes, deUint32 alignBytes)
428 MemPage* curPage = pool->currentPage;
431 if (pool->allowFailing)
433 if ((deRandom_getUint32(&pool->failRandom) & 16383) <= 15)
439 if (pool->enableDebugAllocs)
452 header->next = pool->debugAllocListHead;
453 pool->debugAllocListHead = header;
476 curPage->nextPage = pool->currentPage;
477 pool->currentPage = curPage;
494 * \brief Allocate memory from a pool.
495 * \param pool Memory pool to allocate from.
499 void* deMemPool_alloc (deMemPool* pool, size_t numBytes)
502 DE_ASSERT(pool);
504 ptr = deMemPool_allocInternal(pool, numBytes, DE_POOL_DEFAULT_ALLOC_ALIGNMENT);
505 if (!ptr && pool->util)
506 pool->util->allocFailCallback(pool->util->userPointer);
511 * \brief Allocate aligned memory from a pool.
512 * \param pool Memory pool to allocate from.
517 void* deMemPool_alignedAlloc (deMemPool* pool, size_t numBytes, deUint32 alignBytes)
520 DE_ASSERT(pool);
523 ptr = deMemPool_allocInternal(pool, numBytes, alignBytes);
525 if (!ptr && pool->util)
526 pool->util->allocFailCallback(pool->util->userPointer);
531 * \brief Duplicate a piece of memory into a memory pool.
532 * \param pool Memory pool to allocate from.
536 void* deMemPool_memDup (deMemPool* pool, const void* ptr, size_t numBytes)
538 void* newPtr = deMemPool_alloc(pool, numBytes);
545 * \brief Duplicate a string into a memory pool.
546 * \param pool Memory pool to allocate from.
550 char* deMemPool_strDup (deMemPool* pool, const char* str)
553 char* newStr = (char*)deMemPool_alloc(pool, len+1);
560 * \brief Duplicate a string into a memory pool, with a maximum length.
561 * \param pool Memory pool to allocate from.
566 char* deMemPool_strnDup (deMemPool* pool, const char* str, int maxLength)
569 char* newStr = (char*)deMemPool_alloc(pool, len + 1);
583 int deMemPool_getMaxNumAllocatedBytes (const deMemPool* pool)
585 DE_ASSERT(pool && !pool->parent); /* must be root */
586 return deMax32(pool->maxMemoryAllocated, deMemPool_getNumAllocatedBytes(pool, DE_TRUE));
589 int deMemPool_getMaxCapacity (const deMemPool* pool)
591 DE_ASSERT(pool && !pool->parent); /* must be root */
592 return deMax32(pool->maxMemoryCapacity, deMemPool_getCapacity(pool, DE_TRUE));