home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / aros / source / exec / memory / src / allocpooled.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-16  |  3.2 KB  |  142 lines

  1. /*
  2.     (C) 1995 AROS - The Amiga Replacement OS
  3.     $Id: allocpooled.c 1.1 1995/11/14 22:31:07 digulla Exp digulla $
  4.     $Log: allocpooled.c $
  5.  * Revision 1.1  1995/11/14  22:31:07  digulla
  6.  * Initial revision
  7.  *
  8.     Desc:
  9.     Lang: english
  10. */
  11. #include "exec_intern.h"
  12. #include "memory.h"
  13.  
  14. /*****************************************************************************
  15.  
  16.     NAME */
  17.     #include <exec/memory.h>
  18.     #include <clib/exec_protos.h>
  19.  
  20.     __AROS_LH2(APTR, AllocPooled,
  21.  
  22. /*  SYNOPSIS */
  23.     __AROS_LA(APTR         , poolHeader, A0),
  24.     __AROS_LA(unsigned long, memSize, D0),
  25.  
  26. /*  LOCATION */
  27.     struct ExecBase *, SysBase, 118, Exec)
  28.  
  29. /*  FUNCTION
  30.     Allocate memory out of a private memory pool.
  31.  
  32.     INPUTS
  33.     poolHeader - Handle of the memory pool
  34.     memSize    - Number of bytes you want to get
  35.  
  36.     RESULT
  37.     A pointer to the number of bytes you wanted or NULL if the memory
  38.     couldn't be allocated
  39.  
  40.     NOTES
  41.  
  42.     EXAMPLE
  43.  
  44.     BUGS
  45.  
  46.     SEE ALSO
  47.     CreatePool(), DeletePool(), FreePooled()
  48.  
  49.     INTERNALS
  50.  
  51.     HISTORY
  52.     16-10-95    created by M. Fleischer
  53.     26-10-95    digulla adjusted to new calling scheme
  54.  
  55. ******************************************************************************/
  56. {
  57.     __AROS_FUNC_INIT
  58.     APTR ret;
  59.     struct Pool *pool=(struct Pool *)poolHeader;
  60.  
  61.     /* If the memSize is bigger than the ThreshSize allocate seperately. */
  62.     if(memSize>pool->ThreshSize)
  63.     {
  64.     ULONG size;
  65.     struct Block *bl;
  66.  
  67.     /* Get enough memory for the memory block including the header. */
  68.     size=memSize+BLOCK_TOTAL;
  69.     bl=(struct Block *)AllocMem(size,pool->Requirements);
  70.  
  71.     /* No memory left */
  72.     if(bl==NULL)
  73.         return NULL;
  74.  
  75.     /* Initialize the header */
  76.     bl->Size=size;
  77.  
  78.     /* Add the block to the BlockList */
  79.     AddHead((struct List *)&pool->BlockList,(struct Node *)&bl->Node);
  80.  
  81.     /* Set pointer to allocated memory */
  82.     ret=(UBYTE *)bl+BLOCK_TOTAL;
  83.     }else
  84.     {
  85.     struct MemHeader *mh;
  86.  
  87.     /* Follow the list of MemHeaders */
  88.     mh=(struct MemHeader *)pool->PuddleList.mlh_Head;
  89.     for(;;)
  90.     {
  91.         /* Are there no more MemHeaders? */
  92.         if(mh->mh_Node.ln_Succ==NULL)
  93.         {
  94.         /* Get a new one */
  95.         mh=(struct MemHeader *)
  96.            AllocMem(pool->PuddleSize+MEMHEADER_TOTAL,pool->Requirements);
  97.  
  98.         /* No memory left? */
  99.         if(mh==NULL)
  100.             return NULL;
  101.  
  102.         /* Initialize new MemHeader */
  103.         mh->mh_First=(struct MemChunk *)((UBYTE *)mh+MEMHEADER_TOTAL);
  104.         mh->mh_First->mc_Next=NULL;
  105.         mh->mh_First->mc_Bytes=pool->PuddleSize;
  106.         mh->mh_Lower=mh->mh_First;
  107.         mh->mh_Upper=(UBYTE *)mh->mh_First+pool->PuddleSize;
  108.         mh->mh_Free=pool->PuddleSize;
  109.  
  110.         /* And add it to the list */
  111.         AddHead((struct List *)&pool->PuddleList,(struct Node *)&mh->mh_Node);
  112.         /* Fall through to get the memory */
  113.         }
  114.         /* Try to get the memory */
  115.         ret=Allocate(mh,memSize);
  116.  
  117.         /* Got it? */
  118.         if(ret!=NULL)
  119.         break;
  120.  
  121.         /* No. Try next MemHeader */
  122.         mh=(struct MemHeader *)mh->mh_Node.ln_Succ;
  123.     }
  124.     /* Allocate does not clear the memory! */
  125.     if(pool->Requirements&MEMF_CLEAR)
  126.     {
  127.         ULONG *p=(ULONG *)ret;
  128.  
  129.         /* Round up (clearing longs is faster than just bytes) */
  130.         memSize=(memSize+sizeof(ULONG)-1)/sizeof(ULONG);
  131.  
  132.         /* NUL the memory out */
  133.         while(memSize--)
  134.         *p++=0;
  135.     }
  136.     }
  137.     /* Everything fine */
  138.     return ret;
  139.     __AROS_FUNC_EXIT
  140. } /* AllocPooled */
  141.  
  142.