Apple II
Technical Notes
_____________________________________________________________________________
                                                  Developer Technical Support


Apple IIGS
#56:    Managing Dynamic Segments

Written by:    Eric Soldan                                          July 1989

This Technical Note discusses application difficulties when transferring 
control to dynamic segments during low-memory conditions.
_____________________________________________________________________________

Dynamic segments have a drawback in low-memory situations.  If the Loader 
cannot load the dynamic segment because there is not enough memory, it cannot 
just return to the application with an out-of-memory error since your program 
may have pushed parameters onto the stack which the dynamic segment would have 
removed.  If this is the case, the Loader does not have a valid return 
address, so it cannot return to the application; therefore, it gives a fatal 
error and dies.  Because of this problem, an application must make sure that 
there is sufficient memory available before calling the dynamic segment.

Just checking the amount of free memory does not guarantee that the dynamic 
segment will load.  If memory is fragmented, the Loader may not be able to 
allocate a block large enough to load the segment, even if the total amount of 
free memory is greater than the size of the segment.  Just checking MaxBlock 
is not a good solution either, since it can indicate that there is not enough 
free memory to load the segment when it is actually available.  However, 
calling MaxBlock is preferable to just checking the amount of free memory, 
since not attempting to load the dynamic segment will not cause a fatal error.

Using the method of checking for out-of-memory conditions outlined in Apple 
IIGS Technical Note #51, Reserving Memory for the Toolbox, guarantees that 
there is sufficient space for the dynamic segment.  This method involves 
maintaining a purgeable handle to a segment of memory at least as large as the 
dynamic segment and relocation dictionary.  Before loading the dynamic 
segment, check to see if the handle has been purged.  If it has not been 
purged, then you can load the dynamic segment without worrying about a fatal 
error from the Loader due to an out-of-memory condition.  If it has been 
purged and you cannot reallocate it, then you know that there is not enough 
free memory available to load the dynamic segment.


Further Reference
_____________________________________________________________________________
    o    GS/OS Reference, Volume 2
    o    Apple IIGS Technical Note #51, Reserving Memory for the Toolbox