home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 137.lha / MergeMem.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  4KB  |  138 lines

  1. /* MergeMem.c v2 - by  Carolyn Scheppner  CBM  02/87
  2.  *    modified:  07/87 to move printf's outside of Forbid
  3.  *               (printf eventually Waits which breaks a Forbid)
  4.  *               Now responsive from WB, linkage with TWstartup.obj
  5.  * 
  6.  *  Attempts to merge the memlists of sequentially configged ram boards
  7.  *  which have the same Attributes (for contiguous expansion ram)
  8.  *
  9.  *  Note:  This program has been tested with an Alegra plugged into
  10.  *         a Microbotics Starboard' pass-thru, and with an A2000
  11.  *         with multiple 2-meg ram boards.  This program makes
  12.  *         the assumption that sequentially configged ram boards
  13.  *         have sequential entries in the MemHeader list.
  14.  *         If ram boards are not installed largest to smallest,
  15.  *         this may not be true and this program will not be able
  16.  *         to merge them.
  17.  *
  18.  *  Alink with TWStartup.obj ... Amiga.lib, LC.lib
  19.  *
  20.  */
  21.  
  22. #include "exec/types.h"
  23. #include "exec/exec.h" 
  24. #include "exec/execbase.h" 
  25.  
  26. extern struct ExecBase *SysBase;
  27. extern LONG   stdin;
  28.  
  29. char TWspec[] = {"CON:100/50/440/100/ MergeMem v2 "};
  30. char auth[]   = {"cas/cbm"};
  31.  
  32. main(argc,argv)
  33. int argc;
  34. char **argv;
  35.    {
  36.    struct MemChunk *chunk;
  37.    struct MemHeader *mem, *firstmem, *prevmem = 0;
  38.    struct ExecBase *eb = SysBase;
  39.    ULONG  memsize;
  40.  
  41.    ULONG  mems[32], ends[32], atts[32], prev[32], mrgs[32];
  42.    ULONG  k, memCnt = 0, mrgCnt = 0;
  43.  
  44.    BOOL  FromWb;
  45.  
  46.    /* Temps */
  47.    struct MemChunk *oldFirst;
  48.    APTR   oldLower, oldUpper;
  49.    ULONG  oldFree;
  50.  
  51.    FromWb = (argc==0) ? TRUE : FALSE;
  52.    if((argc==2)&&(argv[1][0]=='?'))
  53.       {
  54.       printf("MergeMem v2  ---  Carolyn Scheppner   CBM   07/87\n");
  55.       printf("Usage: MergeMem\n");
  56.       printf("Attempts to merge sequentially config'd ram boards\n");
  57.       exit(0);
  58.       }
  59.  
  60.    Forbid();
  61.    firstmem = (struct MemHeader *)eb->MemList.lh_Head;
  62.  
  63.    /* Go to end of MemHeader list */
  64.    for (mem = firstmem;
  65.            mem->mh_Node.ln_Succ;
  66.               mem = (struct MemHeader *)mem->mh_Node.ln_Succ)
  67.       {
  68.       mems[memCnt] = (ULONG)mem;
  69.       ends[memCnt] = (ULONG)mem->mh_Upper;
  70.       atts[memCnt] = (ULONG)mem->mh_Attributes;
  71.       memCnt++;
  72.       }
  73.  
  74.    /* Back up from terminal node to point at last MemHeader */
  75.    mem = (struct MemHeader *)mem->mh_Node.ln_Pred;
  76.  
  77.    /* Backwards, for each except first */
  78.    for ( ; (ULONG)mem != (ULONG)firstmem; mem = prevmem)
  79.       {
  80.       prevmem = (struct MemHeader *)mem->mh_Node.ln_Pred;
  81.  
  82.       /* If prev MemHeader describes neighboring ram of same Attributes */
  83.       if(((ULONG)prevmem->mh_Upper == (ULONG)mem->mh_Lower - 32)&&
  84.            (prevmem->mh_Attributes == mem->mh_Attributes))
  85.          {
  86.          prev[mrgCnt] = (ULONG)prevmem;
  87.          mrgs[mrgCnt] = (ULONG)mem;
  88.          mrgCnt++;
  89.  
  90.          /* Save needed stuff from MemHeader before Remove()ing it */
  91.          oldFirst = mem->mh_First;
  92.          oldLower = mem->mh_Lower;
  93.          oldUpper = mem->mh_Upper;
  94.          oldFree  = mem->mh_Free;
  95.          Remove(mem);
  96.  
  97.          /* Adjust Upper and Free in prev MemHeader to include this mem */
  98.          memsize = (ULONG)oldUpper - (ULONG)oldLower +32L;
  99.          prevmem->mh_Upper = (APTR)((ULONG)prevmem->mh_Upper + memsize);
  100.          prevmem->mh_Free += oldFree;
  101.  
  102.          /* Link last free chunk of prevmem to first free of mem */
  103.          for (chunk = prevmem->mh_First;
  104.                   chunk->mc_Next; chunk = chunk->mc_Next);
  105.          chunk->mc_Next = oldFirst;
  106.  
  107.          /* Now FreeMem() the old MemHeader as a 32 byte chunk */
  108.          FreeMem(mem,32);
  109.          }
  110.       }
  111.    Permit();
  112.  
  113.    if(stdin > 0)
  114.       {
  115.       printf("RAM configuration:\n");
  116.       for(k=0; k<memCnt; k++)
  117.          {
  118.          printf("   Memory type $%lx from $%lx to $%lx\n",
  119.             atts[k], mems[k], (ends[k]) - 1);
  120.          }
  121.      
  122.       if(!mrgCnt)  printf("No merging possible.\n");
  123.       else
  124.          {
  125.          printf("Merged:\n");
  126.          for(k=0; k<mrgCnt; k++)
  127.             {
  128.             printf("   $%lx with $%lx\n",mrgs[k], prev[k]);
  129.            }
  130.          }
  131.       }
  132.    if(FromWb)
  133.       {
  134.       printf("\nPress RETURN to exit: ");
  135.       while(getchar() != '\n');
  136.       }
  137.    }
  138.