Pascal II: Intrinsic Units compiler options RESIDENT & NOLOAD

You can get Apple Pascal version 1.1 to treat intrinsic units as segments (overlays) by using the noload (*$N+*) and the resident (*$R unit name*) compiler options. Units specified by these options are read in from the disk only when a reference is made to code contained within the unit. When the calling procedure is exited and all active references to the unit have been resolved, the unit is "swapped out" and the memory range can be used for other code segments. For more details on the use of these options, refer to pages 66-67 of the Pascal Language Reference Manual, and the Language Manual addendum.
The unit code with the noload option will remain in memory until the calling procedure is exited. This may result in a stack overflow (out of memory) if control never exits from such a unit after it is called repeatedly from inside the same procedure. This is a known problem, and can be avoided by specifying the unit as "Resident" within that procedure or segment. Calling the unit from within a different procedure will also remedy this. The following examples illustrate this situation:

EXAMPLEA below will generate an out of memory STACK OVERFLOW error by repeatedly loading APPLESTUFF.

PROGRAM EXAMPLEA;
USES APPLESTUFF;

VAR I: INTEGER;

BEGIN (* MAIN PROGRAM *)
(*$N+*)
WRITE ('NUMBER OF WORDS AVAILABLE AT START OF PROGRAM : ');
WRITELN (MEMAVAIL);
FOR I:=1 TO 100 DO BEGIN
WRITELN (I,' ',MEMAVAIL,' WORDS');
NOTE (25,1) (* APPLESTUFF MUSIC ROUTINE *)
END;
WRITELN ('PROGRAM COMPLETED SUCCESSFULLY')
END. (* MAIN PROGRAM *)

EXAMPLEB uses the RESIDENT OPTION to prevent APPLESTUFF from being reloaded.

PROGRAM EXAMPLEB;
USES APPLESTUFF;

VAR I: INTEGER;

BEGIN (* MAIN PROGRAM *)
(*$N+*)
(*$R APPLESTUFF *)
WRITE ('NUMBER OF WORDS AVAILABLE AT START OF PROGRAM : ');
WRITELN (MEMAVAIL);
FOR I:=1 TO 100 DO BEGIN
WRITELN (I,' ',MEMAVAIL,' WORDS');
NOTE (25,1) (* APPLESTUFF MUSIC ROUTINE *)
END;
WRITELN ('PROGRAM COMPLETED SUCCESSFULLY')
END. (* MAIN PROGRAM *)

EXAMPLEC uses individual procedure to call APPLESTUFF routine, which allows unit to be released and then reloaded.

PROGRAM EXAMPLEC;
USES APPLESTUFF;

VAR I: INTEGER;

PROCEDURE PLAY;
BEGIN WRITELN (I,' ',MEMAVAIL,' WORDS');
NOTE (25,1) (* APPLESTUFF MUSIC ROUTINE *)
END;

BEGIN (* MAIN PROGRAM *)
(*$N+*)
WRITE ('NUMBER OF WORDS AVAILABLE AT START OF PROGRAM : ');
WRITELN (MEMAVAIL);
FOR I:=1 TO 100 DO PLAY;
WRITELN ('PROGRAM COMPLETED SUCCESSFULLY')
END. (* MAIN PROGRAM *)
Published Date: Feb 18, 2012