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 1 - CFM-Based Runtime Architecture / The Structure of Fragments


The Code Fragment Resource

If the Code Fragment Manager is to search for a fragment by name, the file containing the fragment must contain a code fragment resource in the resource fork. A code fragment resource is a resource of type 'cfrg' with ID 0 ('cfrg'0 resource).

The code fragment resource has the form shown in Listing 1-2.

Listing 1-2 The code fragment resource

   struct CFragResource {
      UInt32            reservedA;/* must be zero! */
      UInt32            reservedB;/* must be zero! */
      UInt16            reservedC;/* must be zero! */
      UInt16            version;
      UInt32            reservedD;/* must be zero! */
      UInt32            reservedE;/* must be zero! */
      UInt32            reservedF;/* must be zero! */
      UInt32            reservedG;/* must be zero! */
      UInt16            reservedH;/* must be zero! */
      UInt16            memberCount;
      CFragResourceMemberfirstMember;
   };
Since the 'cfrg'0 resource is an array, it is possible to store information for several fragments in one file. The fragments remain separate and the Code Fragment Manager can prepare them independently, but they can be shipped and marketed as a single file. In addition, the code fragment resource can point to fragments of multiple architectures, allowing you to create fat applications and shared libraries that can execute on multiple platforms. See Chapter 7, "Fat Binary Programs," for more information.

Note
Typically you can use a development tool (such as MergeFragment in MPW) to place multiple fragments in a file.
The structure of the code fragment resource is identical for all fragment types, although some of the field values may differ. Field values in the code fragment resource are determined and set at link time, but some may be changed later using a resource editor (such as ResEdit). Field values are defined in CodeFragments.h.

A code fragment resource entry has the form shown in Listing 1-3.

Listing 1-3 A code fragment resource entry

   struct CFragResourceMember {
      CFragArchitecturearchitecture;
      UInt16         reservedA;  /* zero */
      UInt8          reservedB;  /* zero */
      UInt8          updateLevel;
      CFragVersionNumbercurrentVersion;
      CFragVersionNumberoldDefVersion;
      CFragUsage1UnionuUsage1;
      CFragUsage2UnionuUsage2;
      CFragUsage     usage;
      CFragLocatorKindwhere;
      UInt32         offset;
      UInt32         length;
      UInt32         reservedC;  /* zero */
      UInt32         reservedD;  /* zero */
      UInt16         extensionCount;/* number of extensions */
      UInt16         memberSize; /* total size in bytes */
      unsigned char  name [kDefaultCFragNameLen];
   };

Extensions to Code Fragment Resource Entries

The basic code fragment resource entry structure shown in Listing 1-3 is used for most applications and shared libraries. However, a code fragment resource entry can also contain one or more extensions, which appear directly after the fragment name. Such an extended code fragment resource entry stores additional information about the fragment that may be used by third-party software. For example, while the regular entry might simply indicate that a fragment mooLib is an import library, an extension could also indicate that it is a SOM class library that inherits from the class cow.

Note
A code fragment resource can contain any combination of extended and regular entries.
Padding is added after the name field to begin the extensions on a 4-byte boundary (the length byte of the name string does not include this padding). All extensions must be aligned on 4-byte boundaries, with padding added after each if necessary. The memberSize field includes any padding added after the last extension.

An extension to the code fragment resource has the form shown in Listing 1-4.

Listing 1-4 Structure of a sample code fragment resource extension

   struct CFragResourceSearchExtension {
      CFragResourceExtensionHeaderextensionHeader;
      ExtensionData           data [1];
   };
The extensionHeader field contains a data structure defined as shown in Listing 1-5.

Listing 1-5 The code fragment resource extension header

   struct CFragResourceExtensionHeader {
      UInt16extensionKind;
      UInt16extensionSize;
   };
The information that follows the extensionHeader field depends on the value of extensionKind. As an example, Listing 1-6 shows the format of the code fragment resource extension of type 30EE.

Listing 1-6 A code fragment resource extension of type 30EE

   struct CFragResourceSearchExtension {
      CFragResourceExtensionHeaderextensionHeader;
      OSType                  libKind;
      unsigned char           qualifiers [1];
   };

Sample Code Fragment Resource Entry Definitions

This section contains examples of the most common types of code fragment resource entries.

A PowerPC Application 'cfrg' 0 Resource Definition

Listing 1-7 shows an example of a 'cfrg'0 resource definition for a PowerPC application.

Listing 1-7 A sample 'cfrg'0 resource for a PowerPC runtime application

#include "CodeFragmentTypes.r"
resource 'cfrg' (0) {
   {
      kPowerPCCFragArch,/* runtime environment */
      kIsCompleteCFrag, /* base-level library */
      kNoVersionNum,    /* current version number*/
      kNoVersionNum,    /* oldest definition version number */
      kDefaultStackSize,/* use default stack size */
      kNoAppSubFolder,  /* no library directory */
      kApplicationCFrag,/* fragment is an application */
      kDataForkCFragLocator,/* fragment is in the data fork */
      kZeroOffset,      /* beginning offset of fragment */
      kCFragGoesToEOF,  /* ending offset of fragment */
      "mooApp"          /* name of the fragment*/
   }

A CFM-68K Application 'cfrg' 0 Resource Definition

Listing 1-8 shows a sample 'cfrg'0 resource definition for a CFM-68K runtime application. The fields that have values different from those in a PowerPC application 'cfrg'0 resource entry are underlined.

Listing 1-8 A sample 'cfrg'0 resource for a CFM-68K runtime application

#include "CodeFragmentTypes.r"
resource 'cfrg' (0) {
   {
      kMotorola68KCFragArch,   /* runtime environment */
      kIsCompleteCFrag,        /* base-level library */
      kNoVersionNum,           /* no current version number*/
      kNoVersionNum,           /* no oldest definition version number */
      kDefaultStackSize,       /* use default stack size */
      kNoAppSubFolder,         /* no library directory */
      kApplicationCFrag,       /* fragment is an application */
      kResourceCFragLocator,   /* fragment is in a resource */
      kRSEG,                   /* resource type = 'rseg' */
      kSegIDZero,              /* resource ID = 0 */
      "mooApp"       /* name of the application fragment*/
   }
For more information about the structure of CFM-68K applications, see "CFM-68K Application Structure," beginning on page 9-3.

A Shared Library 'cfrg' 0 Resource Definition

Shared libraries have essentially the same 'cfrg'0 resource entry for both PowerPC and CFM-68K implementations (only the field indicating the runtime environment differs).

Listing 1-9 shows the 'cfrg'0 resource for an import library (plug-ins are identical except the fragment type is set to kDropInAdditionCFrag). Values that differ from an application's 'cfrg'0 resource are underlined.

Listing 1-9 A sample 'cfrg'0 resource for an import library

#include "CodeFragmentTypes.r"
resource 'cfrg' (0) {
   {
      kPowerPCCFragArch,          /* runtime environment */
      kIsCompleteCFrag,           /* base-level library */
      6,                          /* current version number*/
      4,                          /* oldest definition version number */
      kDefaultStackSize,          /* use default stack size */
      kNoAppSubFolder,            /* no library directory */
      kImportLibraryCFrag,        /* fragment is a library */
      kDataForkCFragLocator,      /* fragment is in the data fork */
      kZeroOffset,                /* fragment starts at offset 0 */
      kCFragGoesToEOF,            /* fragment occupies entire fork */
      "mooLib"                    /* name of the library fragment */
   }

Previous Book Contents Book Index Next

© Apple Computer, Inc.
11 MARCH 1997