Diet For Fats
Volume Number: | | 11
|
Issue Number: | | 10
|
Column Tag: | | Powering Up
|
A Diet For Your Fat Applications
How to create fat applications that can strip their own unneeded code.
By Blake Ward, Ph.D., Idaho Falls, Idaho
Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.
With well over a million Power Macintoshes sold, and no sign of slowing sales, it has become critical to provide a PowerPC-native version of every application you write. However, with a huge installed base of 680x0-based Macintoshes, youll also need to ship a 680x0-native version of your application for the foreseeable future. This presents a problem for the developer - how do you give customers a convenient choice between the two versions without forcing them to live with the disk space overhead of both.
Large complex commercial applications that require multiple floppies and an installer can include separate 680x0 and PowerPC-native versions on the floppies and automatically install the proper version. This is a relatively clean solution that avoids the disk space overhead of providing both versions, but it still has its drawbacks. If they are installing your application on an external hard disk, this approach can lead to confusion and frustration if they move that hard disk to another Macintosh and discover that your application either runs very slowly, or worse yet will not run at all. The installer-based solution is also not always ideal for shareware or freeware applications. Since these applications are normally sold for little or nothing and are distributed over the Internet, the added complexity, overhead and cost of an installer may not be an option.
One simple solution to the problem of mismatched machines and programs is the fat application. A fat application is a single application file that includes both 680x0 and PowerPC-native versions of the application. When you launch a fat application, the system figures out which version of the application to execute. Fat applications are convenient for the end user since they dont have to understand or worry about the type of Macintosh they have - the application will do the right thing. Fat applications do have one serious drawback though. Users with only one type of Macintosh and no plans to buy the other pay a permanent disk space penalty for the much larger file that essentially contains two copies of the application. There are simple utilities that will strip the unneeded code from an application, but most users arent likely to have them, and you certainly dont want to ship another utility with your product. The ideal solution would really be a fat application that knows how to strip out its own unneeded code...
This article describes a simple technique for creating self-stripping fat applications. Using the supplied source code (SlimApp), in a few hours youll be able to modify your fat application so users may click a button and strip off the code unneeded for the Macintosh theyre running on. The solution described here works equally well whether youre writing in C or C++ and whether youre using no framework or a framework like MacApp, PowerPlant or Sprocket. Ive even included a sample fat application and project files for CodeWarrior to show you how its done and suggest a friendly user interface for this new feature. It should also work with little or no change in other development environments.
Fat Binaries
Before jumping into an explanation of how well strip a fat application, Ill begin with a begin with a brief description of what a fat application looks like on the inside. If youre an old pro with fat applications, just skip ahead to the next section.
Figure 1 shows the organization of a traditional Macintosh application. The application file has two or more CODE resources containing the 680x0 instructions for the application, an assortment of DLOG, MENU, etc. resources, a SIZE resource and no data fork. Depending on the development environment that created the application, it might also contain a DATA resource (that holds initial values for the applications globals). Even if your application contains only one segment, there will still be two CODE resources since the first one (ID = 0) is actually the jump table for the application.
Figure 1. A Typical 680x0 Application
A typical PowerPC-native application has the organization shown in Figure 2. The same DLOG, MENU, etc. resources are present, but there is also a cfrg resource and there are no CODE resources. The actual PowerPC instructions are stored in the data fork of the file. The important thing to notice is that the executable code in each version of the application is stored in a different location, but non-code resources are identical in both PowerPC and 680x0 versions. So we can create a fat application by essentially just merging the 680x0 and PowerPC versions of the application.
Figure 2. A Typical PowerPC-Native Application
When your application launches, the System can take advantage of the fact that the two code types are stored in separate locations. If your application is launched on a 680x0-based Macintosh that knows nothing at all about fat applications, it works the same as it always did - the data fork and extra resources are simply ignored. When your application is launched on a Power Macintosh, the new Process Manager on these systems first looks for a cfrg resource. If one is present, it is used to find and load the PowerPC instructions from the data fork, and the old CODE resources are simply ignored. If no cfrg resource is present, then the Process Manager just falls back on the old way of doing things, looks for the necessary CODE resources and runs the 680x0 code in them using the Power Macintoshs built-in 68LC040 emulator. The DATA resource (if present) is only used by the 680x0 version of your application, the PowerPC-native version uses the Code Fragment Manager which stores each code fragments globals within the fragment.
Stripping Unneeded Code
Given the fat application organization just described, the process of stripping away unnecessary code to reduce an applications file size is fairly obvious:
If the application will be used on a 680x0-based Macintosh, we can safely eliminate the data fork of the application file since the old Process Manager doesnt even expect it to be there. The cfrg resource is also no longer needed. In fact, since the stripped application could be run on a Power Macintosh some time in the future, we have to get rid of the cfrg resource or the new Process Manager will see it and assume that theres some PowerPC code in the empty data fork.
If the application will be used on a Power Macintosh, the CODE resources and the DATA resource are going to be ignored, so we can safely eliminate them. Unfortunately, after removing the CODE resources, we end up with an application that will only run on a Power Macintosh. If it is ever moved to 680x0-base Macintosh and launched, the Finder will report a resource not found error! Since this isnt very user friendly, we will replace the applications CODE resources with a tiny stub application that will warn the user that they have the wrong version and quit gracefully.
From this description, its obvious how a utility to strip unnecessary code from an application would be written. Its a little less obvious how we write an application that can strip out its own unneeded code. However, with one simple trick, conditional compilation, we can actually implement internal code stripping without worrying about yanking running code out from under ourselves, and even without having to explicitly figure out which processor were running on:
// Note that for the sake of brevity all of the error checking and some of the
// setup code and comments have been removed from the listings in this
// article. See the file SlimApp.c for all the details...
OSErr StripFatApplication(void)
{
OSErr err;
short int currResFork, applicationResourceFork;
// Save away the current resource fork, make the applications
// resource fork current
currResFork = CurResFile();
applicationResourceFork = GetApplicationResourceFork();
UseResFile(applicationResourceFork);
// Get the applications file name
// Removed for brevity...
// Strip away the unneeded code
err = StripUnneededCode(applicationResourceFork,
appFileVRefNum, appFileDirID, appFileName);
// If we successfully stripped the unneeded code, we also want to try to
// change the applications name and its long version string so that
// the user can tell months from now which version he/she has.
if (err == noErr)
RenameSlimApplication(appFileVRefNum, appFileDirID,
appFileName);
UseResFile(currResFork);
return err;
}
#ifdef powerc
// This version of the function will only be compiled into the PowerPC version
// of the application. Therefore if this PowerPC code is running we can safely
// remove the 680x0 code since it cant possibly be in use.
OSErr StripUnneededCode(short int appResFork,
short int /*appVRefNum*/, short int /*appDirID*/,
StringPtr /*appFileName*/)
{
OSErr err;
short int n;
Handle resourceHandle;
// Remove all of the CODE resources from the application
n = Count1Resources('CODE');
SetResLoad(false);
for (; n > 0; n--) {
resourceHandle = Get1IndResource('CODE', 1);
// Code resources start out protected, so we have to clear the
// protected flag before they can be removed
SetResAttrs(resourceHandle,
GetResAttrs(resourceHandle) & ~resProtected);
RemoveResource(resourceHandle);
DisposeHandle(resourceHandle);
}
// Do the same for the DATA resource if it exists
resourceHandle = Get1Resource('DATA', 0);
if ((err = ResError()) == noErr && resourceHandle) {
SetResAttrs(resourceHandle,
GetResAttrs(resourceHandle) & ~resProtected);
RemoveResource(resourceHandle);
DisposeHandle(resourceHandle);
}
SetResLoad(true);
// OK, now we want to move our tiny 68K stub into place so that this
// application will still run long enough to warn the user if ever moved to
// a 68K machine.
// It consists of two code resources and a new DATA resource that we stored
// using different resource types in the SlimApp.rsrc file.
resourceHandle = Get1Resource(kStubCODEType, kStubCodeID);
SetResAttrs(resourceHandle,
GetResAttrs(resourceHandle) & ~resProtected);
RemoveResource(resourceHandle);
AddResource(resourceHandle,'CODE',0,"\p");
WriteResource(resourceHandle);
ReleaseResource(resourceHandle);
resourceHandle = Get1Resource(kStubCODEType, kStubCodeID + 1);
SetResAttrs(resourceHandle,
GetResAttrs(resourceHandle) & ~resProtected);
RemoveResource(resourceHandle);
AddResource(resourceHandle,'CODE',1,"\p");
WriteResource(resourceHandle);
ReleaseResource(resourceHandle);
// Move our DATA resource that goes with the code resources we just moved
resourceHandle = Get1Resource(kStubDATAType, kStubDataID);
SetResAttrs(resourceHandle,
GetResAttrs(resourceHandle) & ~resProtected);
RemoveResource(resourceHandle);
AddResource(resourceHandle,'DATA',0,"\p");
WriteResource(resourceHandle);
ReleaseResource(resourceHandle);
// Write all of the changes
UpdateResFile(appResFork);
return noErr;
}
#else
// This version of the function will only be compiled into the 680x0 version
// of the application. Therefore if this 680x0 code is running we can safely
// remove the data fork and cfrg resources since they cant possibly be in use.
OSErr StripUnneededCode(short int appResFork,
short int appVRefNum, short int appDirID,
StringPtr appFileName)
{
OSErr err;
short int n, refNum;
Handle resourceHandle;
// First, remove any cfrg resources in the application resource fork
// If we dont get rid of these and someone runs the application on a
// PowerPC, the finder will think theres native PowerPC code available
// and wont emulate the 68K version. There should be only one, but
// lets be general.
n = Count1Resources('cfrg');
SetResLoad(false);
for (; n > 0; n--) {
resourceHandle = Get1IndResource('cfrg', n);
RemoveResource(resourceHandle);
DisposeHandle(resourceHandle);
}
// Since weve just stripped the PowerPC version of the application, we know
// that theyll never be able to strip the 68K version, so theres no need to keep
// around the stub code. Therefore, well make the app a little smaller by
// removing it too.
resourceHandle = Get1Resource(kStubCODEType, kStubCodeID);
SetResAttrs(resourceHandle,
GetResAttrs(resourceHandle) & ~resProtected);
RemoveResource(resourceHandle);
DisposeHandle(resourceHandle);
resourceHandle = Get1Resource(kStubCODEType, kStubCodeID + 1);
SetResAttrs(resourceHandle,
GetResAttrs(resourceHandle) & ~resProtected);
RemoveResource(resourceHandle);
DisposeHandle(resourceHandle);
resourceHandle = Get1Resource(kStubDATAType, kStubDataID);
SetResAttrs(resourceHandle,
GetResAttrs(resourceHandle) & ~resProtected);
RemoveResource(resourceHandle);
DisposeHandle(resourceHandle);
SetResLoad(true);
// Write the changes
UpdateResFile(appResFork);
// Now we have to remove the actual PowerPC code.
// Open the data fork (which contains all of the PPC code)
err = HOpen(appVRefNum, appDirID, appFileName,
fsRdWrPerm, &refNum);
// And eliminate the whole data fork
err = SetEOF(refNum, 0);
err = FSClose(refNum);
return noErr;
}
#endif
When you want to strip unneeded code from your application, just call StripFatApplication(). It gets references to the applications resource fork and the application file and then calls StripUnneededCode() with those values. The source code actually contains two versions of StripUnneededCode(), one is conditionally compiled into the 680x0 version of your application, the other is conditionally compiled into the PowerPC version. By using conditional compilation to select what we strip from the application, we dont have to try to figure out which processor were running on. Each version of StripUnneededCode() just uses standard Resource Manager calls to remove the code that would be used by the other version of the application.
In addition to removing all of the CODE and DATA resources from the application, the PowerPC version of StripUnneededCode() also moves three small resources to take their place. These resources are provided with the source for this article, but you can also create them yourself. You can create resources for a startup stub by simply building a separate minimal application that does nothing but initialize the Toolbox, put up a warning alert and then quit:
void main(void)
{
// Initialize Toolbox Managers so we can get the alert up
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(nil);
InitCursor();
// Warn the user that this version of the application only runs on a
// Power Macintosh. You can customize this alert to list a phone number
// for your company so that the user can inquire about getting a replacement
// unstripped copy of the application.
StopAlert(kNo68KCodeErrorDialog, 0L);
}
When the StartupStub project is built, its application file will contain two CODE resources (the jump table and the main segment) and one DATA resource that total just over 1K. These resources have already been moved into SlimApp.rsrc (which you need to include in your application project). Their types and IDs were changed (to CoDe and DaTa) so that they wouldnt conflict with the real resources of your application but would be available when the real 680x0 version of your application is stripped away.
The stripping process described above should be general enough to work with just about any application you might have. However, it doesnt deal with stripping fat resources (for instance fat versions of custom WDEFs, etc.) since theyre probably small enough that stripping wont be worth the effort. If your application uses fat resources and you also want to strip them, youll have to add the appropriate functionality to StripUnneededCode(). The routine listed above also assumes that all code fragments are PowerPC code and it simply eliminates all cfrg resources and the entire data fork. If you have an unusual application that uses the Code Fragment Manager for other types of code, youll have to make StripUnneededCode() a little more selective about what it deletes.
The User Interface
Since the concept of a fat application is a programmer notion that the average user will neither care about nor understand, its especially important that we put a friendly, easy-to-use interface.
The first step is to decide how to give the user access to this functionality. The sample application provided with the source code uses a button in its About Box. This seems more appropriate than a menu command or a preferences dialog since stripping away unneeded code is an unusual, one time action that cant be undone. If we dont have a fat version of the application, the button can be hidden. If visible, we can make it totally clear what the button will do, by setting its name depending on the machine were currently running on:
Figure 3. Sample User Interface
To pick a label for the button, just use same conditional compilation trick we used above to figure out which version to strip. You may also want to display a short message indicating which version of the application is running. The message will make everything clearer to your users and make it possible for your customer support people to ask users which version theyre running. The function Has68KPowerPCCode() checks to see if there are any cfrg resources (in which case the application has PowerPC code) and if the StartupStub resources havent been moved (in which case it must still include a 68K version):
OSErr Has68KPowerPCCode(void)
{
OSErr err;
short int currResFork, applicationResourceFork;
Boolean is68KApp, isPowerPCApp;
// Keep track of the current resource fork so that we can restore everything
// to its previous state when were done
currResFork = CurResFile();
applicationResourceFork = GetApplicationResourceFork();
UseResFile(applicationResourceFork);
// First, see if our replacement 68K stub code resources are still stored
// under a different resource type. .
if (Count1Resources(kStubCODEType) == 2)
is68KApp = true;
else is68KApp = false;
// Also see if there are any cfrg resources in the application
if (Count1Resources('cfrg') > 0)
isPowerPCApp = true;
else isPowerPCApp = false;
UseResFile(currResFork);
if (is68KApp && isPowerPCApp)
return kFatBinaryApplication;
else if (isPowerPCApp)
return kPowerPCApplication;
else return k68KApplication;
}
There are also situations in which we either wont be able to strip out the unneeded code, or it wouldnt be a such a good idea to strip out the code. For instance, if the application is currently on a locked volume, we wont be successful. If the application is being run from a server, we might be able to change it depending on the users access permission, but we probably dont want to since other users with different machine types might be planning to use the same copy of the application. You can call the function SafeToStrip() and only enable the button if it returns true:
Boolean SafeToStrip(void)
{
OSErr err;
FCBPBRec fcbParams;
Str63 appFileName;
HParamBlockRec params;
CInfoPBRec pb;
GetVolParmsInfoBuffer volParms;
// Build a parameter block for an FCB info request.
fcbParams.ioCompletion = nil;
fcbParams.ioNamePtr = appFileName;
fcbParams.ioFCBIndx = 0;
fcbParams.ioRefNum = GetApplicationResourceFork();
// First, check to see if the volume that contains the application is
// currently locked. If so, we wont be able to change the application.
// We get the volumes vRefNum from the values returned by the FCB call.
params.volumeParam.ioCompletion = nil;
params.volumeParam.ioVRefNum = fcbParams.ioFCBVRefNum;
params.volumeParam.ioVolIndex = 0;
params.volumeParam.ioNamePtr = nil;
err = PBHGetVInfo(¶ms, false);
// Check the volume locked bits
if (err != noErr || (params.volumeParam.ioVAtrb & 0x0080) != 0)
return false; // volume locked by hardware
else if ((params.volumeParam.ioVAtrb & 0x8000) != 0)
return false; // volume locked by software
// Is the file itself locked?
pb.hFileInfo.ioNamePtr = appFileName;
pb.hFileInfo.ioVRefNum = fcbParams.ioFCBVRefNum;
pb.hFileInfo.ioDirID = fcbParams.ioFCBParID;
pb.hFileInfo.ioFDirIndex = 0;
err = PBGetCatInfoSync(&pb);
if (err != noErr || (pb.hFileInfo.ioFlAttrib & 0x01) != 0)
return false;
// Get some general volume information to help us figure out whether were
// running from a local volume or from a server.
params.ioParam.ioCompletion = nil;
params.ioParam.ioVRefNum = fcbParams.ioFCBVRefNum;
params.ioParam.ioNamePtr = nil;
params.ioParam.ioBuffer = (Ptr)&volParms;
params.ioParam.ioReqCount = sizeof(GetVolParmsInfoBuffer);
err = PBHGetVolParms(¶ms, false);
if (err != noErr)
return false;
// If its a local volume, then there wont be any server address
if (volParms.vMServerAdr == 0)
return true;
return false;
}
The final step in providing a clear user interface is leaving behind some indication that the application has been stripped. Its easy to install an application when you have only a 680x0 Macintosh, strip away the PowerPC version and then months later after buying a new Power Macintosh start to wonder whether the application was stripped or not. The SampleApp About Box helps by including a brief message indicating which version you have, but this requires that the user actually launch your application to find out. The StripFatApplication() function also calls RenameSlimApplication() to also provide feedback in two other optional ways. If the name of your shipped application file ends in (Fat), it will remove the this suffix and if the long version string for your application (the one shown by the Finders Get Info command) contains the string Fat Application, it will be replaced by Power Mac ONLY or 680x0 Application as appropriate. Of course, all of these strings (along with every other string used by SlimApp) are defined in resources in SlimApp.rsrc to make localization and customization easy. In particular, several of these strings contain the placeholder <the application> that youll want to replace with the name of your application.
Putting Everything Together
Now that youve seen how the actual stripping takes place, all that remains is the easiest part - actually incorporating it into your fat application. To help illustrate this process, Ive included a complete running sample fat application along with project files for CodeWarrior. If youre building your fat application with CodeWarrior, youll likely have two projects. The first builds a 68K version of your application and probably looks something like the following:
Figure 4. Project Window for the 680x0 Version of SampleApp
To add self-stripping, weve added two files to the basic application: SlimApp.c and SlimApp.rsrc. After adding these files to your project, the only other thing required is to provide a user interface for the stripping feature. Just include SlimApp.h and call one or more of the following SlimApp functions from that code:
Has68KPowerPCCode()
Returns kFatBinaryApplication, kPowerPCApplication or k68KApplication.
SafeToStrip()
Returns true if the application file isnt locked, or on a locked volume or server.
StripFatApplication()
Returns noErr if it was successful in stripping the unneeded code from the application.
The function DoAboutSampleApp() in the sample application illustrates the use of these functions and the issues mentioned in the User Interface section. Feel free to use any or all of it in your application.
When youre building a fat application, be careful to make sure that none of your segments are preloaded. If they are, your 680x0 code will load at launch time and occupy valuable memory even when youre running native on a Power Macintosh. (The stripping code also assumes that none of the resources are already in memory when it removes them.) If youre using CodeWarrior, just double-click the segment name to bring up a dialog box and make sure that Preloaded isnt set:
Figure 5. Setting the Segment Attributes
Once youve modified and built the 68K version of your fat application, you can move on to the fat version of your application. If youre using CodeWarrior, youll have a PPC project that includes all of the source files for your application. However, instead of including your applications resource files, it just includes the whole finished 680x0 version of the application:
Figure 6. Project Window for the Fat Version of SampleApp
This time youll only need to include the SlimApp source file - the SlimApp resources are already in 680x0 application file that you included. Build the PPC project and youre finished. Youve got a fat application that can strip its own unneeded code to become a slim application.
Conclusion
SlimApp was used in ToDo List (a slick to do list manager available in the archives on the Internet) where it received nothing but praise from users. New users who had one type of Macintosh, and worried about disk space, had no problem using it to reduce the size of their copy of the application. Perhaps more importantly, users who didnt know or care about fat applications were free to ignore the whole issue - their copy of the application works fine on any Macintosh. Take a few hours and put your fat applications on a diet, your users will thank you.