Previous Book Contents Book Index Next

Inside Macintosh: Programmer's Guide to MacApp / Part 1 - MacApp Theory and Architecture
Chapter 3 - Core Technologies

Trap Patching

MacApp supplies a general-purpose mechanism for patching Toolbox traps, using universal procedure pointers, that works for either 68K-based or Power Macintosh machines. MacApp uses the patching mechanism itself to patch a number of Toolbox routines. For example, MacApp patches the Toolbox routine ExitToShell to call ExitToShellCleanupMacApp, which in turn calls MacApp's cleanup routine, CleanupMacApp.

Patching Traps in Your Application

You patch a trap by defining a subclass, TYourTrapPatch, of the TrapPatch struct and overriding the CallInherited and Install methods. You also supply a compiler definition that generates the appropriate style of procedure pointer to handle patches for either 68K-based or Power Macintosh machines, depending on the type of application built.

You define a static variable of type TYourTrapPatch, and call its PatchTrap method to do the work. The PatchTrap method saves the old trap address, sets the trap to the new address, and inserts the TrapPatch record into a linked list. This linked list is used later when the CleanupMacApp routine calls UnpatchAll to set all patched traps back to their original addresses.

When the application makes a call to a patched trap, the trap jumps to the address you have supplied instead of the address it previously jumped to. The patch is set to the address of a universal procedure pointer that points to your routine. Your code has access to the old trap address through the TrapPatch method GetOldTrapAddr, so your patch can do its own special task and then call the previous trap.

MacApp's Patching for Segmentation

MacApp applications that run on 68K-based machines store their code in segments. Code segments are loaded and unloaded as needed while the application is running (page 75). MacApp manages memory so that there will always be room to load a required segment. However, it must be informed when a segment is about to be loaded so it can free memory if necessary to make room for the segment. The next sections describe the patches MacApp makes so that it will be notified before and after a segment is loaded.

Patching for Nonstandard Builds

If you build your application with the -ModelFar or -CFM68K option, MacApp causes the runtime library (RTLib) to be linked with your application. The RTLib provides a convenient mechanism to specify routines that will be called automatically before and after a segment is loaded. When the RTLib is available, MacApp uses it to install segment load callback routines. MacApp takes care of this automatically--your application doesn't have to do anything special.

Patching for a Standard Build

For a standard build (if you do not use either the -ModelFar option or the -CFM68K option), MacApp does not link the RTLib with your application. When the RTLib is not available, MacApp patches the segment loader directly to notify MacApp before a segment is loaded. Again, this takes place automatically and requires no special effort by your application.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
25 JUL 1996