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

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: availmem.c,v 1.8 1997/01/01 03:46:06 ldp Exp $
  4.     $Log: availmem.c,v $
  5.     Revision 1.8  1997/01/01 03:46:06  ldp
  6.     Committed Amiga native (support) code
  7.  
  8.     Changed clib to proto
  9.  
  10.     Revision 1.7  1996/12/10 13:51:39  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:45  aros
  14.     Use the official AROS macros over the __AROS versions.
  15.  
  16.     Revision 1.5  1996/09/13 17:51:22  digulla
  17.     Use IPTR
  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/alerts.h>
  31. #include <exec/execbase.h>
  32. #include <aros/libcall.h>
  33. #include <exec/memory.h>
  34. #include <proto/exec.h>
  35.  
  36. /*****************************************************************************
  37.  
  38.     NAME */
  39.  
  40.     AROS_LH1(ULONG, AvailMem,
  41.  
  42. /*  SYNOPSIS */
  43.     AROS_LHA(ULONG, attributes, D1),
  44.  
  45. /*  LOCATION */
  46.     struct ExecBase *, SysBase, 36, Exec)
  47.  
  48. /*  FUNCTION
  49.     Return either the total available memory or the largest available
  50.     chunk of a given type of memory.
  51.  
  52.     INPUTS
  53.     attributes - The same attributes you would give to AllocMem().
  54.  
  55.     RESULT
  56.     Either the total of the available memory or the largest chunk if
  57.     MEMF_LARGEST ist set in the attributes.
  58.  
  59.     NOTES
  60.     Due to the nature of multitasking the returned value may already
  61.     be obsolete if this function returns.
  62.  
  63.     EXAMPLE
  64.     Print the total available memory.
  65.  
  66.     printf("Free memory: %lu bytes\n",AvailMem(0));
  67.  
  68.     Print the size of the largest chunk of chip memory.
  69.  
  70.     printf("Largest chipmem chunk: %lu bytes\n",
  71.            AvailMem(MEMF_CHIP|MEMF_LARGEST));
  72.  
  73.     BUGS
  74.  
  75.     SEE ALSO
  76.  
  77.     INTERNALS
  78.  
  79.     HISTORY
  80.     15-10-95    created by m. fleischer
  81.  
  82. ******************************************************************************/
  83. {
  84.     AROS_LIBFUNC_INIT
  85.  
  86.     ULONG ret=0;
  87.     struct MemHeader *mh;
  88.  
  89.     /* Nobody else should access the memory lists now. */
  90.     Forbid();
  91.  
  92.     /* Get pointer to first memory header... */
  93.     mh=(struct MemHeader *)SysBase->MemList.lh_Head;
  94.     /* And follow the list. */
  95.     while(mh->mh_Node.ln_Succ!=NULL)
  96.     {
  97.         /*
  98.         The current memheader is OK if there's no bit in the
  99.         'attributes' that isn't set in the 'mh->mh_Attributes'.
  100.         MEMF_CLEAR, MEMF_REVERSE, MEMF_NO_EXPUNGE, MEMF_TOTAL and
  101.         MEMF_LARGEST are treated as if they were always set in
  102.         the memheader.
  103.         */
  104.         if(!(attributes&~(MEMF_CLEAR|MEMF_REVERSE|MEMF_NO_EXPUNGE
  105.             |MEMF_TOTAL|MEMF_LARGEST|mh->mh_Attributes)))
  106.         {
  107.         /* Find largest chunk? */
  108.         if(attributes&MEMF_LARGEST)
  109.         {
  110.             /*
  111.             Yes. Follow the list of MemChunks and set 'ret' to
  112.             each value that is bigger than all previous ones.
  113.             */
  114.             struct MemChunk *mc=mh->mh_First;
  115.             while(mc!=NULL)
  116.             {
  117. #if !defined(NO_CONSISTENCY_CHECKS)
  118.             /*
  119.                 Do some constistency checks:
  120.                 1. All MemChunks must be aligned to
  121.                    sizeof(struct MemChunk).
  122.                 2. The end (+1) of the current MemChunk
  123.                    must be lower than the start of the next one.
  124.             */
  125.             if(  ((IPTR)mc|mc->mc_Bytes)&(sizeof(struct MemChunk)-1)
  126.                ||(  (UBYTE *)mc+mc->mc_Bytes>=(UBYTE *)mc->mc_Next
  127.                   &&mc->mc_Next!=NULL))
  128.                 Alert(AT_DeadEnd|AN_MemoryInsane);
  129. #endif
  130.             if(mc->mc_Bytes>ret)
  131.                 ret=mc->mc_Bytes;
  132.             mc=mc->mc_Next;
  133.             }
  134.         }
  135.         else if(attributes&MEMF_TOTAL)
  136.             /* Determine total size. */
  137.             ret+=(STRPTR)mh->mh_Upper-(STRPTR)mh->mh_Lower;
  138.         else
  139.             /* Sum up free memory. */
  140.             ret+=mh->mh_Free;
  141.         }
  142.         mh=(struct MemHeader *)mh->mh_Node.ln_Succ;
  143.     }
  144.     /* All done. Permit dispatches and return. */
  145.     Permit();
  146.     return ret;
  147.     AROS_LIBFUNC_EXIT
  148. } /* AvailMem */
  149.  
  150.