Important: The information in this document is obsolete and should not be used for new development.
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.
- Group related routines in the same segment.
- Put your main event loop into the main segment (that is, the segment that contains the main entry point).
- Put any routines that handle low-memory conditions into a locked segment (usually the main segment). For example, if your application provides a grow-zone function, you should put that function in a locked segment.
- Put any routines that execute at interrupt time, including VBL tasks and Time Manager tasks, into a locked segment.
- Any initialization routines that are executed only once at application startup time should be put in a separate segment. This grouping allows you to unload the segment after executing the routines. However, routines that allocate non relocatable objects (for example,
MoreMasters
orInitWindows
) in your application heap should be called in the main segment, before loading any code segments that will later be unloaded. If you put such allocation routines in a segment that is later unloaded and purged, you increase heap fragmentation.
To unload a segment you must call the
UnloadSeg
routine from your application. TheUnloadSeg
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 routinehappyMoo
, 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.
© Apple Computer, Inc.
11 MARCH 1997