(* * * * * * * * * * * * * * * * * * *
 *
 * prog5-3.pas
 *
 * Get amount of free XMS
 *
 * * * * * * * * * * * * * * * * * * *)


program prog5_3;


uses
    _Globals,
    _Xms;

var
    xms:            XMS_Xms;
    totalFree:      Word;
    largestFree:    Word;
    handle1:        XMS_XmBlk;
    handle2:        XMS_XmBlk;


begin

    (*
     *  Attempt to initialize XMS. If there is an error, report it.
     *)
    if xms.init then begin
        writeln('XMS is not present');
        end;

    (*
     *  Print header:
     *)
    writeln('                                  Total       Largest Block');
    writeln('════════════════════════════╤═══════════════╤═══════════════╕');

    (*
     *  Report the free memory available:
     *)
    if xms.getFreeXM(totalFree, largestFree) then begin
        xms_demoError('XMS_Xms.getFreeXM');
        end;

    writeln('After initialization        │    ',
                totalFree:4,
                ' KB    │    ',
                largestFree:4,
                ' KB    │');
    writeln('────────────────────────────┼───────────────┼───────────────┤');

    (*
     *  Now allocate 16KB and note the change:
     *)
    if handle1.allocXM(16) then begin
        xms_demoError('XMS_XmsBlk.allocXM');
        end;

    if xms.getFreeXM(totalFree, largestFree) then begin
        xms_demoError('XMS_Xms.getFreeXM');
        end;

    writeln('After 16 KB allocate        │    ',
                totalFree:4,
                ' KB    │    ',
                largestFree:4,
                ' KB    │');
    writeln('────────────────────────────┼───────────────┼───────────────┤');

    (*
     *  Now allocate another 32KB and note the change:
     *)
    if handle2.allocXM(32) then begin
        xms_demoError('XMS_XmsBlk.allocXM');
        end;

    if xms.getFreeXM(totalFree, largestFree) then begin
        xms_demoError('XMS_Xms.getFreeXM');
        end;

    writeln('After 32 KB allocate        │    ',
                totalFree:4,
                ' KB    │    ',
                largestFree:4,
                ' KB    │');
    writeln('────────────────────────────┼───────────────┼───────────────┤');

    (*
     *  Now free the FIRST block and see what happens:
     *)
    if handle1.freeXM then begin
        xms_demoError('XMS_XmsBlk.freeXM');
        end;

    if xms.getFreeXM(totalFree, largestFree) then begin
        xms_demoError('XMS_Xms.getFreeXM');
        end;

    writeln('After free of 16 KB block   │    ',
                totalFree:4,
                ' KB    │    ',
                largestFree:4,
                ' KB    │');
    writeln('────────────────────────────┼───────────────┼───────────────┤');

    (*
     *  Now free second block of 32KB
     *)
    if handle2.freeXM then begin
        xms_demoError('XMS_XmsBlk.freeXM');
        end;
    if xms.getFreeXM(totalFree, largestFree) then begin
        xms_demoError('XMS_Xms.getFreeXM');
        end;
    writeln('After free of 32 KB block   │    ',
                totalFree:4,
                ' KB    │    ',
                largestFree:4,
                ' KB    │');
    writeln('════════════════════════════╧═══════════════╧═══════════════╛');

    (*
     *  Now attempt to free the handles again. We expect errors.
     *)
    if handle1.freeXM then begin
        writeln('XMS_XmsBlk.freeXM() of already freed handle gives "',
                    xms.errorText(errno), '"');
        end;
    if handle2.freeXM then begin
        writeln('XMS_XmsBlk.freeXM() of already freed handle gives "',
                    xms.errorText(errno), '"');
        end;


end.


