TweetFollow Us on Twitter

June 91 - Children of the DogCow - Segments from Outer Space

Children of the DogCow - Segments from Outer Space

Kent Sandvik

Greetings! This is Worldwide Developer Conference week, and DTS is busy working at the debugging lab. Thanks to a great manager, I am able to stay home half time, check out my new kid, and at the same time write sample code and another article for FrameWorks.

In this column, I'm taking a closer look at segmentation: MacApp segmentation strategies, virtual memory possibilities, and other issues related to segmentation. Some parts will be elementary for Veterans of the MacApp Psychic Wars, but I hope the science fiction references in some of the headings will keep such readers awake. Onward!

THE BASICS OF SEGMENTATION

First, you need to specify the segment where each method (or member function, in C++) should reside. This is done using the notation {$S SegmentName} in Object Pascal, and #pragma segment SegmentName in C++. If you forget to place this segment compiler directive in your methods, it will inherit the earlier directive (in C++ as well as Object Pascal) all the way to the end of the file, so suddenly you'll find a lot of methods inside one of your segments. Methods without any defined segment go into the Main segment, which could get really crowded after a while. So, check your segmentation directive for each method. MacBrowse has a neat function for doing this that shows what each segment contains.

The Segmentation Makers sidebar provides guidelines on organizing your methods into segments.

THE TRUE NATURE OF SEGMENTS

Segments are really CODE resources in disguise, so MacApp is able to control the purge and lock bits on segments just as it does on handles or objects (as in TObject's Lock and UnLock methods).

A locked handle is also unpurgeable, so you don't need to worry about purging once you have locked the object in memory. MacApp's global function DBUnloadSeg makes handles, or CODE segments, unlocked-which makes the resource available for purging as well.

Methods are the actual routines that are stored in the CODE resources; data is stored either on the stack, in the heap or in the A5-world, depending. In many cases, calling a method whose segment is not currently stored in memory causes a segment load to occur that might have to move heap blocks in order to locate a place to put the new segment. This is one reason why calling a new method can suddenly make dereferencing bugs pop up.

THE FALL OF UNLOADALLSEGMENTS

One key to the mysteries of MacApp segment loading and unloading is the global function UnloadAllSegments. UnloadAllSegments is called when the application starts, and also when the application has too little memory to do its duties. UnloadAllSegments purges all code segments from memory except resident segments.

It's UnloadAllSegments that displays "I really don't think that you want to unload a segment into which you are going to return!" in the debug window. This happens when MacApp has determined that you are about to unload a segment containing a method that you will need to return to later. (In other words, your stack currently references a method contained in the segment that is about to be unloaded.)

If you do a "find references to UnloadAllSegments" in MacBrowse, you'll find that this function is called from many places; from the initialization phase of the application, and here and there from within various loops.

GOOD NEWS FROM DEBUGGER SPACE

The MacApp debugger can list all the segments that are currently loaded in application heap memory. When you drop down to the debugger level, type 'H' for the segment and heap subsection. Inside this level, to show the segments, type either 'S' (segments currently in memory) or 'ß' (all segments, whether or not they are currently in memory). This shows the segment names, sizes, states (loaded, resident), and so on.

You can do additional resource manipulation from the debugger with the Toggle, or 'X' flag. Inside this level, type 'S' so that each time a segment load occurs, MacApp will break into the debugger and print the routine name that triggered the segment load.

The 'U' flag turns off the automatic segment unloading done by the UnloadAllSegments routine. This is handy for finding out if your program's crashes have been due to mysterious jumps into unexpected routines: if a pointer to another method was suddenly being made invalid as that method's segment was unloaded, the stage is set for a healthy crash. Your code may stop crashing when you use the 'U' flag to turn automatic segment unloading off; if so, that's a good hint to look for problems of this kind.

The 'R' flag checks to see if the total size of the currently loaded resource exceeds the maximum. You can also set the maximum to a new value.

SERVANTS OF THE LIB

Sometimes we want to move a separate segment back into the Main segment, in order to avoid too many segments-a condition that can lead to heap fragmentation. Link can remap the segmentation with routines to other segment names. The syntax looks like this:
Link [options...] objectfile… ≥ progress.file
-sn=oldSegName1=newSegName1
-sn=oldSegName2=newSegName2

This is useful when using MPW 3.2 with MacApp 2.0 or 2.0.1 to remap segments back to the Main segment. The story is, some of the standard functions in libraries in MPW 3.2 have been split from the Main segment. This causes serious heap fragmentation in your MacApp application-for example, when you try to call SetHandleSize(). To avoid this, make the following modifications in the Basic Definitions file:

SegmentMappings = ð
SegmentMappings = ð
#-- insert here
-sn PASLIB=Main ð
-sn STDCLIB=Main ð
#-- end of insertion

This causes the errant routines in the Pascal and Standard C libraries to be remapped back into the Main segment. Also, change the lines in the MacApp.r file as shown in the MacApp.r changes sidebar.

Another solution is to use the linker to mark code resources from the libraries that were once in main as locked. These segments will then be loaded into memory and placed with the main segment, avoiding fragmentation problems. To do this, modify the user variable OtherLinkOptions in the Basic Definitions file:

OtherLinkOptions = ð
-ra PASLIB=resLocked ð
-ra STDCLIB=resLocked

You can also use this technique of locking code resources into memory in your MAMake files (OtherLinkOptions=)-but be careful with these experiments. Finally, you can use the linker to merge old segments into new segments with the -sg option:

-sg newSeg[=old[,old]…] # merge old segments into newSeg

The MPW Lib tool also contains options for changing segment names and merging segments into a segment, which is useful for cases where you only have access to the object code library.

THE RES! AND SEG! OF ETERNITY

Sometimes there is confusion about res! and seg! resources. The seg! resource defines those segments that are loaded to memory when the program is making maximum use of memory. MacApp uses this information when keeping track of the code reserve in order to ensure there is room for the seg! code segments at the maximum point of memory use.

The res! resource defines those segments that are always resident in the heap (segments are made permanently resident via a global function called SetResidentSegment). Note that even if you define a segment in the res! resource, because it's a handle it will still float around in memory.

One use for making segments permanently resident is for time-critical functions that are grouped together in a special segment; thus, loading the segment doesn't require overhead if the method is suddenly needed. For example, this could be used to reduce overhead for time-critical communication methods. Here's an example of a res! resource defined in the resource file:

resource 'res!' (kMyMacApp,    purgeable) {
{   "AWriteConn";
    "AReadConn";
    "APoll";
#if qInspector && !qDebug
    "GDebugConn";
#endif
#if qPerform
    "GPerformanceComms";
#endif
};
};

VIRTUAL MEMORY ARRIVES AT EASTERWINE

One reason other operating systems don't require programmers to specify segmentation is the underlying mechanism of virtual memory and page segmentation, which makes the issue of loading code on demand an automatic process.

With System 7, the Macintosh operating system now has virtual memory. However, there is still need for the programmer to specify segments in the code.

The basics of virtual memory

In a virtual memory system the whole memory is divided into pages. These pages can be loaded into memory or reside on the hard disk at any time. When the program refers to a page that isn't in physical memory, the system generates a page fault, and the memory management unit (MMU) loads the page from the backing store on the hard disk.

When the page fault occurs, the memory manager (with the help of the MMU) first frees up physical memory so it can load the needed page by selecting unused page frames and writing them to the backing store. Then it reads the page data for the needed frame. Thus, pages that aren't needed are usually residing on the hard disk. This event, usually called page-fault handling, requires special hardware in most cases.

Apple's VM scheme for the Mac

Apple's virtual memory only runs on Macintosh computers with a MMU unit. It requires the virtual memory mode to be on (it is not always on by default). The Macintosh implementation of virtual memory is not based on the so-called copy-on-demand, where page after page is loaded in per demand. In the Mac's VM scheme, the Segment Loader has to load in the whole segment. And the Mac's virtual memory model is single-virtual (not multiple-virtual), which means the applications share the address space with other applications and the system.

Global-replacement algorithm

Apple's implementation is a global-replacement algorithm. It keeps a queue of physical page frames in memory, and the frames in this queue are accessed sequentially by page- frame numbers. Each page frame in memory is automatically marked by the MMU according to whether the frame has been recently accessed by the CPU. Mac VM keeps a pointer to the last page frame that has been replaced. When a page-fault occurs, this pointer looks in the queue for a page frame that hasn't been modified since it was read from the disk, and that also has not been accessed recently (an "old" frame). When it find such a beast, it returns information about it to the memory manager. This page can now be "stolen" to use for other purposes.

If the VM does not find such a frame, it looks for a page that has been modified but not replaced recently. If this doesn't work, the VM tries to find the first frame that doesn't contain a page held in physical memory.

This algorithm is simple and fast. It doesn't need to know about application states, and it's space efficient. For more on this algorithm, read the article in the November 1989 issue of Byte, "Mac VM Revealed" by Phil Goldman.

Backing store management

In the Mac's VM implementation, the pages are locked to 4096 byte sizes (this is also the minimal optimal transfer size for the SCSI Manager). The complete virtual memory image is kept on the backing store, instead of pages. This means the end user uses more disk space, but that the page swapping speed is probably improved. If the whole virtual image is not on the disk, then every time a page is replaced, it must be written to the backing store, even if it has not been modified. The biggest burden with virtual memory is the transfer time to and from the hard disk; with the whole virtual disk image on the backing store, the system does not need to write pages that are not modified back to the disk.

VM & segmentation

Automatic segmentation handling is impractical for VM's current implementation. Note that even UNIX™ and other modern (UNIX modern? well…) operating systems still have problems with code that generates a lot of page faults (by loading in a lot of page frames for each function), which leads to unnecessary paging, which leads to swapping (where whole processes are swapped out of physical memory), which leads to thrashing (when all the operating system does is swap processes in and out, 24 hours a day). So, some of these systems have automatic tools that analyze the segmentation strategies and, if possible, move code segments around to avoid paging (which is evil after all).

The best solution is a combination of both virtual memory and segmentation. VM allows the user to run larger programs than would otherwise be possible, and if the developer organizes segments intelligently, excess paging is avoided.

There is still a need for some smart segmentation analysis tool which could produce segmentation directives by analyzing each function in order to figure out how to produce a segment organization such that methods are grouped together for maximum efficiency. VACUUM JUMP TABLES There is a known relation between jump table sizes and segmentation. For normal procedures and functions, a jump table entry is not needed if all calls to the routine are from the same segment. But if there are calls to other segments from the routine, jump table entries are needed. Examine the segmentation of your code; you might find places where a change in segmentation would eliminate jump table entries. The linkmap output (using the MABuild -LinkMap option) shows what each segment contains. With some effort you may shrink big jump tables and improve the performance of your whole application.

Some people worry that many Get and Set methods will increase the jump table entries considerably, but you can avoid this by using clever segmentation strategies or by using C++'s inline functions. Anyway, if your classes are infested with millions of Get and Set methods, perhaps it is time to examine the object. Is it really a structure in disguise?

Caching of results inside the class decreases the need for Get and Set calls. Plus, the major parts of an object can be placed inside one single segment for another performance improvement.You can use dumpobj to dump the object file and find information about each segment.

The Segment Loader has to fill the jump table with the right addresses when the segments are loaded in. When the segment is unloaded, the jump table has to be reset with information about the missing segment. MacApp has to make sure that memory is always available for data and unloaded segments. All this takes time, so clever segmentation does improve performance. For example, if important functions are in the same segment, you eliminate other segment loading events, and when MacApp calls UnloadAllSegments, a place is created for the next suite of segments needed.

STRANGE MPW 3.2 TOYS

For a long time segments were restricted to 32k sizes due to the A5-relative data referencing with 16 bit offsets, but MPW 3.2 eliminates this 32k limit on segment size via new switches to the compilers and the linker.

The 68020 introduced 32-bit PC-relative branching (BSR.L statements), but that didn't help the Classic and other 68000-based Macintosh computers. Instead, MPW 3.2 makes use of branch islands. This simple, elegant concept is based on the implementation of PC-relative code-to-code references. The linker splits a large code segment up into smaller 32k areas by inserting branch islands. These branch islands serve as intermediate points that are within range of PC-relative jumps, thus making it possible to make a call across a segment that would otherwise result in a larger-than-32k jump.

Another new feature is "32-bit everything," which transparently removes the infamous limitations on code segment sizes, jump table sizes and the size of the global data areas. The drawback is a larger code size footprint and some slowdown due to increased load time for the larger code segments. But hey, look what you get!

32-bit everything is activated by using -model far options while compiling and linking. The Release Notes for MPW 3.2 will explain the implementation completely; basically, the trick is that the compilers generate instructions with 32-bit addresses (instead of the normal 16-bit offsets), and that these 32-bit addresses are relocated at load time by the segment load address or by the contents of A5, as appropriate.

Finally, one can generate larger than 32K jump tables using the -wrap option. This uses unused space in the global data area for additional jump table entries when it starts to get crowded inside the 32K segment. Programmers doing large MacApp programs will love this! However, at best this utility doubles the jump table size, and if your global data area is already filled with data, you're out of luck.

If you want to use these new 32-bit everything features from MPW 3.2 with MacApp, you'll need a couple of new MacApp library files. These are available on ETO #3, as well as most of the 32-bit everything support. ETO#4 will contain the final MPW 3.2 with tools and libraries to support these new features.

THE END OF THIS ARTICLE

I hope I have helped to explain the basic issues concerning segmentation and MacApp. The brave MacApp explorer could investigate further to learn about the global functions that manipulate segments-such as GetSegFromPC, GetSegNumber, and GetResLoad. Perhaps this will be the topic of a future column.

REFERENCES

  • Mac VM Revealed, Byte November 1989
  • Inside Macintosh, II, Chapter 2 (Segment Loader)
  • MPW 3.2 Release Information (available real soon now)
  • MacApp & Object Oriented Programming (using C++) handouts, Dave Wilson
  • Many various MacApp.Tech$ links
 
AAPL
$97.67
Apple Inc.
+0.64
MSFT
$44.50
Microsoft Corpora
+0.10
GOOG
$589.02
Google Inc.
-4.33

MacTech Search:
Community Search:

Software Updates via MacUpdate

TotalFinder 1.6.2 - Adds tabs, hotkeys,...
TotalFinder is a universally acclaimed navigational companion for your Mac. Enhance your Mac's Finder with features so smart and convenient, you won't believe you ever lived without them. Tab-based... Read more
Vienna 3.0.0 RC 2 :be5265e: - RSS and At...
Vienna is a freeware and Open-Source RSS/Atom newsreader with article storage and management via a SQLite database, written in Objective-C and Cocoa, for the OS X operating system. It provides... Read more
VLC Media Player 2.1.5 - Popular multime...
VLC Media Player is a highly portable multimedia player for various audio and video formats (MPEG-1, MPEG-2, MPEG-4, DivX, MP3, OGG, ...) as well as DVDs, VCDs, and various streaming protocols. It... Read more
Default Folder X 4.6.7 - Enhances Open a...
Default Folder X attaches a toolbar to the right side of the Open and Save dialogs in any OS X-native application. The toolbar gives you fast access to various folders and commands. You just click... Read more
TinkerTool 5.3 - Expanded preference set...
TinkerTool is an application that gives you access to additional preference settings Apple has built into Mac OS X. This allows to activate hidden features in the operating system and in some of the... Read more
Audio Hijack Pro 2.11.0 - Record and enh...
Audio Hijack Pro drastically changes the way you use audio on your computer, giving you the freedom to listen to audio when you want and how you want. Record and enhance any audio with Audio Hijack... Read more
Intermission 1.1.1 - Pause and rewind li...
Intermission allows you to pause and rewind live audio from any application on your Mac. Intermission will buffer up to 3 hours of audio, allowing users to skip through any assortment of audio... Read more
Autopano Giga 3.6 - Stitch multiple imag...
Autopano Giga allows you to stitch 2, 20, or 2,000 images. Version 3.0 integrates impressive new features that will definitely make you adopt Autopano Pro or Autopano Giga: Choose between 9... Read more
Airfoil 4.8.7 - Send audio from any app...
Airfoil allows you to send any audio to AirPort Express units, Apple TVs, and even other Macs and PCs, all in sync! It's your audio - everywhere. With Airfoil you can take audio from any... Read more
Microsoft Remote Desktop 8.0.8 - Connect...
With Microsoft Remote Desktop, you can connect to a remote PC and your work resources from almost anywhere. Experience the power of Windows with RemoteFX in a Remote Desktop client designed to help... Read more

Latest Forum Discussions

See All

This Week at 148Apps: July 21-25, 2014
Another Week of Expert App Reviews   At 148Apps, we help you sort through the great ocean of apps to find the ones we think you’ll like and the ones you’ll need. Our top picks become Editor’s Choice, our stamp of approval for apps with that little... | Read more »
Reddme for iPhone - The Reddit Client (...
Reddme for iPhone - The Reddit Client 1.0 Device: iOS iPhone Category: News Price: $.99, Version: 1.0 (iTunes) Description: Reddme for iPhone is an iOS 7-optimized Reddit client that offers a refreshing new way to experience Reddit... | Read more »
Jacob Jones and the Bigfoot Mystery : Ep...
Jacob Jones and the Bigfoot Mystery : Episode 2 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Jacob Jones is back in Episode 2 of one of Apples 'Best of 2013' games and an App Store... | Read more »
New Trailer For Outcast Odyssey, A New K...
New Trailer For Outcast Odyssey, A New Kind of Card Battler Posted by Jennifer Allen on July 25th, 2014 [ permalink ] Out this Fall is a new kind of card battle game: Outcast Odyssey. | Read more »
Hay Day – Tip, Tricks, Strategies, and C...
Recently got into Supercell’s other huge hit, Hay Day and could do with some advice on what to do? We’ve got you covered with some helpful trips and tricks to bear in mind! Ticking Along One of the key things to keep in mind while building up that... | Read more »
Monster Head Review
Monster Head Review By Nadia Oxford on July 25th, 2014 Our Rating: :: FEEDING TIMEUniversal App - Designed for iPhone and iPad Racking up a high score with Monster Head is trickier than it first appears. The appeal wears out fairly... | Read more »
Garfield: Survival of the Fattest Coming...
Garfield: Survival of the Fattest Coming to iOS this Fall Posted by Jennifer Allen on July 25th, 2014 [ permalink ] Who loves lasagna? Me. Also everyone’s favorite grumpy fat cat, Garfield. | Read more »
Happy Flock Review
Happy Flock Review By Andrew Fisher on July 25th, 2014 Our Rating: :: HERD IT ALL BEFOREUniversal App - Designed for iPhone and iPad Underneath the gloss of Happy Flock’s visuals is a game of very little substance. It’s cute, but... | Read more »
Square Register Updates Adds Offline Pay...
Square Register Updates Adds Offline Payments Posted by Ellis Spice on July 25th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Looking For Group – Hearthstone’s Curse...
For the first time since its release (which has thankfully been a much shorter window for iPad players than their PC counterparts), Blizzard’s wildly successful Hearthstone: Heroes of Warcraft CCG is sporting some brand new content: the single... | Read more »

Price Scanner via MacPrices.net

13-inch 2.5GHz MacBook Pro on sale for $1099,...
Best Buy has the 13″ 2.5GHz MacBook Pro available for $1099.99 on their online store. Choose free shipping or free instant local store pickup (if available). Their price is $100 off MSRP. Price is... Read more
Roundup of Apple refurbished MacBook Pros, th...
The Apple Store has Apple Certified Refurbished 13″ and 15″ MacBook Pros available for up to $400 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free. Their prices... Read more
Record Mac Shipments In Q2/14 Confound Analys...
A Seeking Alpha Trefis commentary notes that Apple’s fiscal Q3 2014 results released July 22, beat market predictions on earnings, although revenues were slightly lower than anticipated. Apple’s Mac’... Read more
Intel To Launch Core M Silicon For Use In Not...
Digitimes’ Monica Chen and Joseph Tsai, report that Intel will launch 14nm-based Core M series processors specifically for use in fanless notebook/tablet 2-in-1 models in Q4 2014, with many models to... Read more
Apple’s 2014 Back to School promotion: $100 g...
 Apple’s 2014 Back to School promotion includes a free $100 App Store Gift Card with the purchase of any new Mac (Mac mini excluded), or a $50 Gift Card with the purchase of an iPad or iPhone,... Read more
iMacs on sale for $150 off MSRP, $250 off for...
Best Buy has iMacs on sale for up to $160 off MSRP for a limited time. Choose free home shipping or free instant local store pickup (if available). Prices are valid for online orders only, in-store... Read more
Mac minis on sale for $100 off MSRP, starting...
Best Buy has Mac minis on sale for $100 off MSRP. Choose free shipping or free instant local store pickup. Prices are for online orders only, in-store prices may vary: 2.5GHz Mac mini: $499.99 2.3GHz... Read more
Global Tablet Market Grows 11% in Q2/14 Notwi...
Worldwide tablet sales grew 11.0 percent year over year in the second quarter of 2014, with shipments reaching 49.3 million units according to preliminary data from the International Data Corporation... Read more
New iPhone 6 Models to Have Staggered Release...
Digitimes’ Cage Chao and Steve Shen report that according to unnamed sources in Apple’s upstream iPhone supply chain, the new 5.5-inch iPhone will be released several months later than the new 4.7-... Read more
New iOS App Helps People Feel Good About thei...
Mobile shoppers looking for big savings at their favorite stores can turn to the Goodshop app, a new iOS app with the latest coupons and deals at more than 5,000 online stores. In addition to being a... Read more

Jobs Board

*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
WW Sales Program Manager, *Apple* Online St...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
Lead Software Engineer, *Apple* Online Stor...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
Manager, *Apple* Fullfillment Operation (AF...
…cross-functional teams to drive the highest level of program management quality for Apple . You will help plan launch strategy and demand generation programs with Read more
Project Manager / Business Analyst, WW *Appl...
…a senior project manager / business analyst to work within our Worldwide Apple Fulfillment Operations and the Business Process Re-engineering team. This role will work Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.