home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / rom / exec / allocpooled.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-09  |  3.7 KB  |  163 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: allocpooled.c,v 1.8 1997/01/01 03:46:05 ldp Exp $
  4.     $Log: allocpooled.c,v $
  5.     Revision 1.8  1997/01/01 03:46:05  ldp
  6.     Committed Amiga native (support) code
  7.  
  8.     Changed clib to proto
  9.  
  10.     Revision 1.7  1996/12/10 13:51:37  aros
  11.     Moved all #include's in the first column so makedepend can see it.
  12.  
  13.     Revision 1.6  1996/10/24 15:50:44  aros
  14.     Use the official AROS macros over the __AROS versions.
  15.  
  16.     Revision 1.5  1996/10/19 17:07:24  aros
  17.     Include <aros/machine.h> instead of machine.h
  18.  
  19.     Revision 1.4  1996/08/13 13:55:58  digulla
  20.     Replaced AROS_LA by AROS_LHA
  21.     Replaced some AROS_LH*I by AROS_LH*
  22.     Sorted and added includes
  23.  
  24.     Revision 1.3  1996/08/01 17:41:05  digulla
  25.     Added standard header for all files
  26.  
  27.     Desc:
  28.     Lang:
  29. */
  30. #include "exec_intern.h"
  31. #include <aros/libcall.h>
  32. #include <aros/machine.h>
  33. #include "memory.h"
  34. #include <exec/memory.h>
  35. #include <proto/exec.h>
  36.  
  37. /*****************************************************************************
  38.  
  39.     NAME */
  40.  
  41.     AROS_LH2(APTR, AllocPooled,
  42.  
  43. /*  SYNOPSIS */
  44.     AROS_LHA(APTR,  poolHeader, A0),
  45.     AROS_LHA(ULONG, memSize,    D0),
  46.  
  47. /*  LOCATION */
  48.     struct ExecBase *, SysBase, 118, Exec)
  49.  
  50. /*  FUNCTION
  51.     Allocate memory out of a private memory pool.
  52.  
  53.     INPUTS
  54.     poolHeader - Handle of the memory pool
  55.     memSize    - Number of bytes you want to get
  56.  
  57.     RESULT
  58.     A pointer to the number of bytes you wanted or NULL if the memory
  59.     couldn't be allocated
  60.  
  61.     NOTES
  62.  
  63.     EXAMPLE
  64.  
  65.     BUGS
  66.  
  67.     SEE ALSO
  68.     CreatePool(), DeletePool(), FreePooled()
  69.  
  70.     INTERNALS
  71.  
  72.     HISTORY
  73.     16-10-95    created by M. Fleischer
  74.  
  75. ******************************************************************************/
  76. {
  77.     AROS_LIBFUNC_INIT
  78.  
  79.     APTR ret;
  80.     struct Pool *pool=(struct Pool *)poolHeader;
  81.  
  82.     /* If the memSize is bigger than the ThreshSize allocate seperately. */
  83.     if(memSize>pool->ThreshSize)
  84.     {
  85.     ULONG size;
  86.     struct Block *bl;
  87.  
  88.     /* Get enough memory for the memory block including the header. */
  89.     size=memSize+BLOCK_TOTAL;
  90.     bl=(struct Block *)AllocMem(size,pool->Requirements);
  91.  
  92.     /* No memory left */
  93.     if(bl==NULL)
  94.         return NULL;
  95.  
  96.     /* Initialize the header */
  97.     bl->Size=size;
  98.  
  99.     /* Add the block to the BlockList */
  100.     AddHead((struct List *)&pool->BlockList,(struct Node *)&bl->Node);
  101.  
  102.     /* Set pointer to allocated memory */
  103.     ret=(UBYTE *)bl+BLOCK_TOTAL;
  104.     }else
  105.     {
  106.     struct MemHeader *mh;
  107.  
  108.     /* Follow the list of MemHeaders */
  109.     mh=(struct MemHeader *)pool->PuddleList.mlh_Head;
  110.     for(;;)
  111.     {
  112.         /* Are there no more MemHeaders? */
  113.         if(mh->mh_Node.ln_Succ==NULL)
  114.         {
  115.         /* Get a new one */
  116.         mh=(struct MemHeader *)
  117.            AllocMem(pool->PuddleSize+MEMHEADER_TOTAL,pool->Requirements);
  118.  
  119.         /* No memory left? */
  120.         if(mh==NULL)
  121.             return NULL;
  122.  
  123.         /* Initialize new MemHeader */
  124.         mh->mh_First=(struct MemChunk *)((UBYTE *)mh+MEMHEADER_TOTAL);
  125.         mh->mh_First->mc_Next=NULL;
  126.         mh->mh_First->mc_Bytes=pool->PuddleSize;
  127.         mh->mh_Lower=mh->mh_First;
  128.         mh->mh_Upper=(UBYTE *)mh->mh_First+pool->PuddleSize;
  129.         mh->mh_Free=pool->PuddleSize;
  130.  
  131.         /* And add it to the list */
  132.         AddHead((struct List *)&pool->PuddleList,(struct Node *)&mh->mh_Node);
  133.         /* Fall through to get the memory */
  134.         }
  135.         /* Try to get the memory */
  136.         ret=Allocate(mh,memSize);
  137.  
  138.         /* Got it? */
  139.         if(ret!=NULL)
  140.         break;
  141.  
  142.         /* No. Try next MemHeader */
  143.         mh=(struct MemHeader *)mh->mh_Node.ln_Succ;
  144.     }
  145.     /* Allocate does not clear the memory! */
  146.     if(pool->Requirements&MEMF_CLEAR)
  147.     {
  148.         ULONG *p=(ULONG *)ret;
  149.  
  150.         /* Round up (clearing longs is faster than just bytes) */
  151.         memSize=(memSize+sizeof(ULONG)-1)/sizeof(ULONG);
  152.  
  153.         /* NUL the memory out */
  154.         while(memSize--)
  155.         *p++=0;
  156.     }
  157.     }
  158.     /* Everything fine */
  159.     return ret;
  160.     AROS_LIBFUNC_EXIT
  161. } /* AllocPooled */
  162.  
  163.