Stack Usage

Stack space is at a premium on the Apple IIgs. Process stacks can only be located in Bank 0 — a total of 64k. This theoretical limit doesn't apply, however, as GS/OS and other bits of system software reserve a large chunk of this without any way to reclaim it. There is approximately 48K of usable stack space. This space also has to be shared with direct page space for Tools and certain types of device drivers, however. For a program to be GNO compliant, stack usage analysis must be done and acted upon. Use of the stack should be minimized so that many processes can coexist peacefully. From experience we've found that 1K usually suffices for well-written C applications, and at a maximum 4K can be allocated.

Assembly language programs tend to be very efficient when it comes to use of the stack. The 4K provided by default to applications is usually more than enough for assembly language programs. C programs can use up tremendous amounts of stack space, especially if recursion is employed or string manipulation is done without concern for stack usage; however, even assembly programs can be written poorly and use a lot of stack space. Below are some hints to keep stack usage at a minimum.

  1. Avoid use of large local arrays and character strings. Instead, dynamically allocate large structures such as GS/OS strings with malloc(3) or the Memory Manager. Alternatively, you can designate such items as ”static”, which causes the C compiler to allocate the space for the variable from main memory.

  2. Try not to use recursion unless absolutely necessary. All recursive functions can be rewritten using standard loops and creative programming. This is a good general programming rule because your program will run faster because setting up stack frames is expensive in terms of time and memory.

  3. ORCA/C 1.3 (and older) generates 8K of stack by default, in case the desktop is started up. Since GNO/ME compliant programs generally will not be desktop-based, make sure you judge how much stack your program will require and use the #pragma stacksize directive (or the occ(1) -S flag) to limit how much stack space ORCA/C tries to allocate for your program. Also, since ORCA/C 1.3 programs don't use the stack given them by GNO/ME and GS/OS, when you link your program include a small (256 bytes) stack segment. See the utilities sources for examples of this. ORCA/C 2.0.x (and later) allocates stack via the GS/OS supported method, so ORCA/C 2.0 programs use exactly the amount of stack specified by #pragma stacksize.

Feedback