(* * * * * * * * * * * * * * * * * * *
 *
 * prog5-8.pas
 *
 *  Demonstrates resizing blocks and getting block info
 *
 * * * * * * * * * * * * * * * * * * *)


program prog5_8;


uses
    _Globals,
    _Xms;

var
    xms:            XMS_Xms;


    lockCount:      Word;
    numFreeHandles: Word;
    blockSize:      Word;
    
    handle1:        XMS_XmBlk;
    handle2:        XMS_XmBlk;
    handle3:        XMS_XmBlk;

    physAddr1:      DWord;
    physAddr2:      DWord;
    physAddr3:      DWord;


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(
'                               Address      Size (bytes)  Locks');
    writeln(
'ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍ¸');

    (*
     *  Allocate three blocks:
     *)
    if handle1.allocXM(16) then begin
        xms_demoError('XMS_XmBlk.allocXM');
        end;
    if handle2.allocXM(16) then begin
        xms_demoError('XMS_XmBlk.allocXM');
        end;
    if handle3.allocXM(16) then begin
        xms_demoError('XMS_XmBlk.allocXM');
        end;

    (*
     *  Now lock 'em and get their physical addresses.
     *)
    if handle1.lockXM(physAddr1) then begin
        xms_demoError('xms_lockXM');
        end;
    if handle2.lockXM(physAddr2) then begin
        xms_demoError('xms_lockXM');
        end;
    if handle3.lockXM(physAddr3) then begin
        xms_demoError('xms_lockXM');
        end;
    
    (*
     *  Now one by one get info on them:
     *)
    if handle1.getHandInfo(blockSize, numFreeHandles, lockCount) then begin
        xms_demoError('xms_getHandInfo');
        end;
    writeln('After allocation:  block 1 ³   0x',
            hex(physAddr1, 6),
            '   ³   0x',
            hex(blockSize*1024, 6),
            '   ³  ',
            lockCount:3,
            ' ³');

    writeln(
'                   ÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ´');

    if handle2.getHandInfo(blockSize, numFreeHandles, lockCount) then begin
        xms_demoError('xms_getHandInfo');
        end;
    writeln('                   block 2 ³   0x',
            hex(physAddr2, 6),
            '   ³   0x',
            hex(blockSize*1024, 6),
            '   ³  ',
            lockCount:3,
            ' ³');

    writeln(
'                   ÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ´');

    if handle3.getHandInfo(blockSize, numFreeHandles, lockCount) then begin
        xms_demoError('xms_getHandInfo');
        end;
    writeln('                   block 3 ³   0x',
            hex(physAddr3, 6),
            '   ³   0x',
            hex(blockSize*1024, 6),
            '   ³  ',
            lockCount:3,
            ' ³');

    writeln(
'ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍØÍÍÍÍÍÍÍÍÍÍÍÍÍÍØÍÍÍÍÍÍÍÍÍÍÍÍÍÍØÍÍÍÍÍÍµ');


    (*
     *  Now let's resize the middle block and see what happens.
     *  First, however, we want to unlock all of the blocks so
     *  that they can move as necessary.
     *)
    if handle1.unlockXM then begin
        xms_demoError('xms_unlockXM');
        end;
    if handle2.unlockXM then begin
        xms_demoError('xms_unlockXM');
        end;
    if handle3.unlockXM then begin
        xms_demoError('xms_unlockXM');
        end;

    (*
     *  Do the resize:
     *)
    if handle2.resizeXM(32) then begin
        xms_demoError('xms_resizeXM');
        end;

    (*
     *  Lock them all again.
     *)
    if handle1.lockXM(physAddr1) then begin
        xms_demoError('xms_lockXM');
        end;
    if handle2.lockXM(physAddr2) then begin
        xms_demoError('xms_lockXM');
        end;
    if handle3.lockXM(physAddr3) then begin
        xms_demoError('xms_lockXM');
        end;

    (*
     *  Now report on the new situation.
     *)
    if handle1.getHandInfo(blockSize, numFreeHandles, lockCount) then begin
        xms_demoError('xms_getHandInfo');
        end;
    writeln('After allocation:  block 1 ³   0x',
            hex(physAddr1, 6),
            '   ³   0x',
            hex(blockSize*1024, 6),
            '   ³  ',
            lockCount:3,
            ' ³');

    writeln(
'                   ÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ´');

    if handle2.getHandInfo(blockSize, numFreeHandles, lockCount) then begin
        xms_demoError('xms_getHandInfo');
        end;
    writeln('                   block 2 ³   0x',
            hex(physAddr2, 6),
            '   ³   0x',
            hex(blockSize*1024, 6),
            '   ³  ',
            lockCount:3,
            ' ³');

    writeln(
'                   ÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ´');

    if handle3.getHandInfo(blockSize, numFreeHandles, lockCount) then begin
        xms_demoError('xms_getHandInfo');
        end;
    writeln('                   block 3 ³   0x',
            hex(physAddr3, 6),
            '   ³   0x',
            hex(blockSize*1024, 6),
            '   ³  ',
            lockCount:3,
            ' ³');

    writeln(
'ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍ¾');

    (*
     *  Unlock the blocks:
     *)
    if handle1.unlockXM then begin
        xms_demoError('xms_unlockXM');
        end;
    if handle2.unlockXM then begin
        xms_demoError('xms_unlockXM');
        end;
    if handle3.unlockXM then begin
        xms_demoError('xms_unlockXM');
        end;

    (*
     *  And free them
     *)
    if handle1.freeXM then begin
        xms_demoError('XMS_XmsBlk.freeXM');
        end;
    if handle2.freeXM then begin
        xms_demoError('XMS_XmsBlk.freeXM');
        end;
    if handle3.freeXM then begin
        xms_demoError('XMS_XmsBlk.freeXM');
        end;

end.

