home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Programming / tek / mem / createpool.c next >
Encoding:
C/C++ Source or Header  |  2001-05-12  |  1.4 KB  |  73 lines

  1.  
  2. #include "tek/mem.h"
  3. #include "tek/debug.h"
  4.  
  5. /* 
  6. **    TEKlib
  7. **    (C) 2001 TEK neoscientists
  8. **    all rights reserved.
  9. **
  10. **    TAPTR TCreatePool(TAPTR mmu, TUINT chunksize, TUINT thressize, TTAGITEM *tags)
  11. **
  12. **    create pooled allocator
  13. **
  14. **    TODO: implement a tag for pre-allocation size?
  15. **
  16. */
  17.  
  18. static TINT destroypool(TAPTR mp);
  19.  
  20. TAPTR TCreatePool(TAPTR mmu, TUINT chunksize, TUINT thressize, TTAGITEM *tags)
  21. {
  22.     TUINT alignment = TALIGN_DEFAULT;
  23.     TMEMPOOL *pool;
  24.  
  25.     if (chunksize > 0 && thressize <= chunksize)
  26.     {    
  27.         pool = TMMUAllocHandle(mmu, destroypool, sizeof(TMEMPOOL));
  28.         if (pool)
  29.         {
  30.             TInitList(&pool->list);
  31.  
  32.             pool->align = alignment;
  33.             pool->chunksize = (chunksize + alignment) & ~alignment;
  34.             pool->thressize = (thressize + alignment) & ~alignment;
  35.             pool->poolnodesize = (sizeof(TPOOLNODE) + alignment) & ~alignment;
  36.             pool->memnodesize = (sizeof(TMEMNODE) + alignment) & ~alignment;
  37.  
  38.             pool->dyngrow = (TBOOL) TGetTagValue(TMem_DynGrow, (TTAG) TTRUE, tags);
  39.  
  40.             if (pool->dyngrow)
  41.             {
  42.                 pool->dynfactor = (TFLOAT) pool->chunksize / (TFLOAT) pool->thressize;
  43.             }
  44.  
  45.             return pool;
  46.         }
  47.     }
  48.     
  49.     return TNULL;
  50. }
  51.  
  52.  
  53. static TINT destroypool(TAPTR mp)
  54. {
  55.     TMEMPOOL *pool = (TMEMPOOL *) mp;
  56.     TNODE *node;
  57.     TINT numfreed = 0;
  58.  
  59.     while ((node = TRemHead(&pool->list)))
  60.     {
  61.         TMMUFree(pool->handle.mmu, node);
  62.         numfreed++;
  63.     }
  64.  
  65.     if (numfreed)
  66.     {
  67.         tdbprintf1(5, "*** destroypool: %d allocations pending\n", numfreed);
  68.     }
  69.     
  70.     TMMUFreeHandle(pool);
  71.     return numfreed;
  72. }
  73.