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

  1. /*
  2.     (C) 1995 AROS - The Amiga Replacement OS
  3.     $Id: availmem.c 1.1 1995/11/14 22:31:07 digulla Exp digulla $
  4.     $Log: availmem.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 <exec/alerts.h>
  13. #include <exec/execbase.h>
  14.  
  15. /*****************************************************************************
  16.  
  17.     NAME */
  18.     #include <exec/memory.h>
  19.     #include <clib/exec_protos.h>
  20.  
  21.     __AROS_LH1(ULONG, AvailMem,
  22.  
  23. /*  SYNOPSIS */
  24.     __AROS_LA(unsigned long, requirements, D1),
  25.  
  26. /*  LOCATION */
  27.     struct ExecBase *, SysBase, 36, Exec)
  28.  
  29. /*  FUNCTION
  30.     Return either the total available memory or the largest available
  31.     chunk of a given type of memory.
  32.  
  33.     INPUTS
  34.     requirements - The same attributes you would give to AllocMem().
  35.  
  36.     RESULT
  37.     Either the total of the available memory or the largest chunk if
  38.     MEMF_LARGEST ist set in the attributes.
  39.  
  40.     NOTES
  41.     Due to the nature of multitasking the returned value may already
  42.     be obsolete if this function returns.
  43.  
  44.     EXAMPLE
  45.     Print the total available memory.
  46.  
  47.     printf("Free memory: %lu bytes\n",AvailMem(0));
  48.  
  49.     Print the size of the largest chunk of chip memory.
  50.  
  51.     printf("Largest chipmem chunk: %lu bytes\n",
  52.            AvailMem(MEMF_CHIP|MEMF_LARGEST));
  53.  
  54.     BUGS
  55.  
  56.     SEE ALSO
  57.  
  58.     INTERNALS
  59.  
  60.     HISTORY
  61.     15-10-95    created by m. fleischer
  62.     26-10-95    digulla adjusted to new calling scheme
  63.  
  64. ******************************************************************************/
  65. {
  66.     __AROS_FUNC_INIT
  67.     ULONG ret=0;
  68.     struct MemHeader *mh;
  69.  
  70.     /* Nobody else should access the memory lists now. */
  71.     Forbid();
  72.  
  73.     /* Get pointer to first memory header... */
  74.     mh=(struct MemHeader *)&SysBase->MemList.lh_Head;
  75.     /* And follow the list. */
  76.     while(mh->mh_Node.ln_Succ!=NULL)
  77.     {
  78.         /*
  79.         The current memheader is OK if there's no bit in the
  80.         'requirements' that isn't set in the 'mh->mh_Attributes'.
  81.         MEMF_CLEAR, MEMF_REVERSE, MEMF_NO_EXPUNGE and
  82.         MEMF_LARGEST are treated as if they were always set in
  83.         the memheader.
  84.         */
  85.         if(!(requirements&~(MEMF_CLEAR|MEMF_REVERSE|MEMF_NO_EXPUNGE
  86.                   |MEMF_LARGEST|mh->mh_Attributes)))
  87.         {
  88.         /* Find largest chunk? */
  89.         if(requirements&MEMF_LARGEST)
  90.         {
  91.             /*
  92.             Yes. Follow the list of MemChunks and set 'ret' to
  93.             each value that is bigger than all previous ones.
  94.             */
  95.             struct MemChunk *mc=mh->mh_First;
  96.             while(mc!=NULL)
  97.             {
  98. #if !defined(NO_CONSISTENCY_CHECKS)
  99.             /*
  100.                 Do some constistency checks:
  101.                 1. All MemChunks must be aligned to
  102.                    sizeof(struct MemChunk).
  103.                 2. The end (+1) of the current MemChunk
  104.                    must be lower than the start of the next one.
  105.             */
  106.             if(  ((ULONG)mc|mc->mc_Bytes)&(sizeof(struct MemChunk)-1)
  107.                ||(  (UBYTE *)mc+mc->mc_Bytes>=(UBYTE *)mc->mc_Next
  108.                   &&mc->mc_Next!=NULL))
  109.                 Alert(AT_DeadEnd|AN_MemoryInsane);
  110. #endif
  111.             if(mc->mc_Bytes>ret)
  112.                 ret=mc->mc_Bytes;
  113.             mc=mc->mc_Next;
  114.             }
  115.         }
  116.         else
  117.             /* No. Just sum up. */
  118.             ret+=mh->mh_Free;
  119.         }
  120.         mh=(struct MemHeader *)mh->mh_Node.ln_Succ;
  121.     }
  122.     /* All done. Permit dispatches and return. */
  123.     Permit();
  124.     return ret;
  125.     __AROS_FUNC_EXIT
  126. } /* AvailMem */
  127.  
  128.