/*
STRATST.C - Jim Kyle - June 5, 1990
demonstrates RAM-management strategy function
*/

#include <dos.h>
#include <stdio.h>

union REGS reg;
struct SREGS seg;
unsigned bigblok, tinyblok;
char *codes[] = { "First-fit", " Best-fit", " Last-fit" };

unsigned int GetRam ( unsigned para )   /* paragraphs!  */
{ reg.x.bx = para;
  reg.x.ax = 0x4800;            /* get RAM from DOS     */
  intdos( &reg, &reg );
  printf("Got blk at %04X, size=%u\n", reg.x.ax, reg.x.bx );
  return reg.x.ax;
}

void RelRam ( unsigned segmt )  /* release RAM to DOS   */
{ segread( &seg );
  seg.es   = segmt;
  reg.x.ax = 0x4900;
  printf("Released block at %04X\n", seg.es );
  intdosx( &reg, &reg, &seg );
}

void SetStrat ( char strat )
{ reg.x.ax = 0x5801;            /* set strategy code    */
  reg.h.bl = strat;
  intdos( &reg, &reg );
}

void GetStrat ( void )
{ reg.x.ax = 0x5800;            /* read strategy code   */
  intdos( &reg, &reg );
  printf("Strategy code: %u (%s): ",
         reg.x.ax, codes[reg.x.ax] );
}

void main ( void )
{ bigblok = GetRam ( 0x100 );   /* allocate big block   */
  GetRam ( 0x080 );             /* allocate a fence     */
  tinyblok = GetRam ( 0x080 );  /* allocate tiny block  */
  GetRam ( 0x080 );             /* allocate a fence     */

  RelRam ( bigblok );           /* now free two blocks  */
  RelRam ( tinyblok );          /* but leave fences     */

  SetStrat ( 0 );               /* set first-fit        */
  GetStrat ();
  RelRam ( GetRam ( 0x80 ));    /* get, then release    */

  SetStrat ( 1 );               /* set best-fit         */
  GetStrat ();
  RelRam ( GetRam ( 0x80 ));    /* get, then release    */

  SetStrat ( 2 );               /* set last-fit         */
  GetStrat ();
  RelRam ( GetRam ( 0x80 ));    /* get, then release    */

  SetStrat ( 0 );               /* set first-fit at end */
}
