(* * * * * * * * * * * * * * * * * * *
 *
 * prog4-5.pas
 *
 *      Demonstrates
 *          - Resize handle
 *
 * * * * * * * * * * * * * * * * * * *)

program prog4_5;

uses    crt,
        _globals,
        _ems;

var
    ems:                EMS_Ems;


procedure displayActiveHandles;

var

    handleInfoArray:   ^EMS_HandleInfoArr;

    numActiveHandles:   Word;
    numActiveHandles2:  Word;

    i:                  Word;

    tmpHandle:          EMS_EmBlk;

    nameBuff:           EMS_HandleName;

begin

    (*
     *  First find out how many active handles there are:
     *)
    if ems.getNumActiveHandles(numActiveHandles) then begin
        ems_demoError('EMS_Ems.getNumActiveHandles');
        end;

    (*
     *  Now allocate a block of handleInfo packets big enough to
     *  hold them.
     *)
    GetMem(handleInfoArray, numActiveHandles * sizeof(EMS_HandleInfo));

    (*
     *  Now get the info.
     *)
    if ems.getPagesAllHandles(handleInfoArray^, numActiveHandles2) then begin
        ems_demoError('EMS_Ems.getNumActiveHandles');
        end;

    (*
     *  The following is a brief sanity clause (Everybody knows
     *  there ain't no sanity clause).
     *)
    if numActiveHandles2 <> numActiveHandles then begin
        writeln('A most unusual situation has occured...');
        Halt;
        end;

    (*
     *  Finally, display it.
     *)
    writeln('                        ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄ´');

    for i:= 0 to numActiveHandles-1 do begin
        tmpHandle.handle:= handleInfoArray^[i].handle;
        if tmpHandle.getHandleName40(nameBuff) then begin
            ems_demoError('EMS_EmBlk.getHandleName40');
            end;

        writeln(
            '                                      ³  ',
            handleInfoArray^[i].handle:3,
            '   ³  ',
            handleInfoArray^[i].numPages:3,
            '  ³ ',
            nameBuff,
            ' ³');

        if i+1 < numActiveHandles then begin
            writeln(
                '                                      ÃÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´');
            end;
        end;

    writeln('ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÍ¾');

    (*
     *  Now free up the arrays.
     *)
    FreeMem(handleInfoArray, numActiveHandles * sizeof(EMS_HandleInfo));

end;

procedure pause;

var
    c:                  Char;

begin

    (*
     *  Display a little message:
     *)
    writeln('Hit <CR> to continue...');

    (*
     *  Wait for a <CR>
     *)
    c:= ReadKey;
    while c <> #13 do begin
        c:= ReadKey;
        end;

end;



var
    totalPages:         Word;
    freePages:          Word;
    handle1:            EMS_EmBlk;

    nameBuff:           EMS_HandleName;

begin

    (*
     *  First check for presence of EMS
     *)
    if ems.init then begin
        writeln('EMS is not present');
        Halt;
        end;

    (*
     *  Now allocate 5 pages.
     *)
    if handle1.allocEM(5) then begin
        ems_demoError('EMS_EmBlk.allocEM');
        end;

    (*
     *  Set the name for the handle
     *)
    nameBuff:= 'OurBlock';
    if handle1.setHandleName40(nameBuff) then begin
        ems_demoError('EMS_EmBlk.setHandleName40');
        end;

    (*
     *  Get the number of free pages
     *)
    if ems.getFreeEM(totalPages, freePages) then begin
        ems_demoError('EMS_Ems.getFreeEM');
        end;

    (*
     *  Print header:
     *)
    writeln('       Operation          Avail Pages         Active Handles');
    writeln('ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¸');
    writeln('After 5 page allocate   ³     ',
                freePages:3,
                '     ³ Handle   Pages     Name   ³');

    displayActiveHandles;
    pause;

    (*
     *  Now resize the block:
     *)
    if handle1.reallocHandPages40(8) then begin
        ems_demoError('EMS_EmBlk.reallocHandPages40');
        end;

    (*
     *  Get the number of free pages
     *)
    if ems.getFreeEM(totalPages, freePages) then begin
        ems_demoError('EMS_Ems.getFreeEM');
        end;

    (*
     *  Print header:
     *)
    writeln('       Operation          Avail Pages         Active Handles');
    writeln('ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¸');
    writeln('After Resize of handle  ³     ',
                freePages,
                '     ³ Handle   Pages     Name   ³');

    displayActiveHandles;

    if handle1.freeEM then begin
        ems_demoError('EMS_EmBlk.freeEM');
        end;
end.
