home *** CD-ROM | disk | FTP | other *** search
- {$R-,S-} {turn off range & stack checks }
-
- Program STI_Demo; {demonstration program for STI }
-
- Uses Crt,STIAlloc; {large allocatio unit }
-
- Type
- DataPtr = ^DataRec; {pointer to the test data type }
- DataRec = record {the actual test data type }
- Name : string[80];
- Age : longint;
- end;
-
- var
- Loop, {general loop variable }
- ArraySize, {size of the array }
- Errors : LongInt; {number of errors }
- DataArray : DataPtr; {the array }
- DataAddr : LongInt; {the address (linear) of the arr}
- Dummy : DataRec; {dummy data variable }
-
- {---------------------------------------------------------------------------}
-
- function GetElementPtr(ElementNo : LongInt) : DataPtr;
-
- begin
- GetElementPtr := {return a pointer to the data }
- STI_Linear2Ptr(DataAddr+(ElementNo*SizeOf(DataRec)));
- end;
-
- {---------------------------------------------------------------------------}
-
- procedure GetElement(ElementNo : LongInt; Var GotData : DataRec);
-
- begin
- GotData := GetElementPtr(ElementNo)^; {return the actual data }
- end;
-
- {---------------------------------------------------------------------------}
-
- procedure PutElement(ElementNo : Longint; NewData : DataRec);
-
- begin
- GetElementPtr(ElementNo)^ := NewData; {store the data }
- end;
-
- {---------------------------------------------------------------------------}
-
- procedure Explain;
-
- Var
- Dummy : char;
-
- begin
- writeln(' STI_DEMO ');
- writeln(' ');
- writeln(' A demonstration program for STIALLOC.PAS, a unit ');
- writeln(' that supplies the ability to allocate very large ');
- writeln(' amounts of memory. ');
- writeln(' ');
- writeln(' Press any key to continue.... ');
- repeat until keypressed;
- Dummy := ReadKey;
- ClrScr;
- end;
-
- {---------------------------------------------------------------------------}
-
- begin
- ClrScr; {clear the screen }
- Explain;
- WriteLn('Maximum Available Memory = ',MaxAvail); {write the available size}
- ArraySize := (MaxAvail div SizeOf(DataRec)); {get the array size }
- STI_GetMem(DataArray,ArraySize * SizeOf(DataRec)); {get the memory }
- if DataArray = nil then begin {make sure we did get it }
- WriteLn('Unable to allocate array of ',ArraySize ,' elements');
- Halt(1);
- end;
-
- {this saves us from recomputing Linear(LongArray) repeatedly}
-
- DataAddr := STI_Ptr2Linear(DataArray);
-
- {show memory status}
-
- WriteLn('Allocated ',ArraySize * SizeOf(DataRec), ' bytes');
- WriteLn('Maximum Available Memory = ',MaxAvail);
- WriteLn('Successfully allocated array of ',ArraySize ,' elements');
-
- {initialize the array}
-
- writeln;
- WriteLn('Initializing array...');
- for Loop := 0 to ArraySize -1 do
- begin
- Dummy.Age := Loop;
- Dummy.Name := 'Software Technology International';
- PutElement(Loop, Dummy);
- GotoXY(1,7); writeln(Dummy.Name);
- GotoXY(1,8); writeln(Dummy.Age);
- end;
-
- {validate the array contents}
-
- writeln;
- WriteLn('Validating array contents...');
- Errors := 0;
- for Loop := 0 to ArraySize -1 do
- begin
- GetElement(Loop,Dummy);
- GotoXY(1,11); writeln(Dummy.Name);
- GotoXY(1,12); writeln(Dummy.Age);
- if (Dummy.Age <> Loop) then
- begin
- WriteLn('Error at element ',Loop,': should be ',Loop,', is ',Dummy.Age);
- Inc(Errors);
- end;
- end;
- writeln;
-
- {show status}
-
- if Errors = 0 then
- WriteLn('No errors found')
- else
- WriteLn(Errors, ' errors found');
-
- {release the memory}
-
- WriteLn('Releasing memory...');
- STI_FreeMem(DataArray, ArraySize * SizeOf(DataRec));
- WriteLn('MaxAvail = ', MaxAvail);
- end.