#include <stdio.h>
#include <i86.h>

// DOS ALLOCTATIONS - Mr.BAGGiNS / EXTRAVAGANZA in 1995

void __far *DOS_malloc( size_t size )
{
  union REGS r;

  if ( size > 0xA0000 ) return( NULL );

  r.w.ax = 0x100;
  r.w.bx = size >> 4;
  if ( (size & 15) != 0 ) r.w.bx++;
  int386(0x31, &r, &r);

  if (r.x.cflag != 0) return( NULL );
    else return( MK_FP(r.w.dx, 0) );
}

int DOS_free( void __far *__ptr )
{
  union REGS r;

  if ( __ptr == NULL ) return ( -2 );

  r.x.eax = 0x101;
  r.x.edx = FP_SEG(__ptr);
  int386(0x31, &r, &r);

  if (r.x.cflag != 0) return( -1 );
    else return( 0 );
}

int DOS_maxavail(void)
{
  union REGS r;

  r.x.eax = 0x100;
  r.x.ebx = 0xFFFF;
  int386(0x31, &r, &r);

  if (r.x.cflag != 0) return( r.x.ebx << 4 );
    else return( 0 );
}
