Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Mac OS Runtime Architectures /
Chapter 10 - Classic 68K Runtime Architecture


Program Segmentation

The classic 68K runtime architecture reflects the need for maximum memory efficiency in the original Macintosh computer which had 128 KB of RAM and an MC68000 CPU. To run large applications in this limited memory environment, Macintosh applications were broken up into segments ('CODE' resources) that could be loaded into the application heap as necessary.

When you compile and link a program, the linker places your program's routines into code segments and constructs 'CODE' resources for each program segment. The Process Manager loads some code segments into memory when you launch an application. Later, if the code calls a routine stored in an unloaded segment, the Segment Manager loads that new segment into memory. These operations occur automatically by using information stored in the application's jump table and in the individual code segments themselves.

Note that although the Segment Manager loads segments automatically, it does not unload segments. The Segment Manager locks the segment when it is first loaded into memory and any time thereafter when routines in that segment are executing. This locking prevents the segment from being moved during heap compaction and from being purged during heap purging.

Your development environment lets you specify compiler directives to indicate which routines should be grouped together in the same segment. For example, if you have code that is not executed very often (for example, code for printing a document), you can store that in a separate segment, so it does not occupy memory when it is not needed. Here are some general guidelines for grouping routines into segments:

A typical strategy is to unload all segments except segment 1 (the main segment) and any other essential code segments each time through your application's main loop.

To unload a segment you must call the UnloadSeg routine from your application. The UnloadSeg routine does not actually remove a segment from memory, but merely unlocks it, indicating to the Segment Manager that it may be relocated or purged if necessary. To unload a particular segment, you pass the address of any externally referenced routine contained in that segment. For example, if you wanted to unload the segment that contains the routine happyMoo, you can execute the following:

UnloadSeg(&happyMoo);

WARNING
Before you unload a segment, make sure that your application no longer needs it. Never unload a segment that contains a completion routine or other interrupt task (such as a Time Manager or VBL task) that might be executed after the segment is unloaded. Also, you must never unload a segment that contains routines in the current call chain.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
11 MARCH 1997