TweetFollow Us on Twitter

Heap Zones
Volume Number:3
Issue Number:1
Column Tag:ABC's of C

Peeking at Heap Zones

By Bob Gordon, Contributing Editor

Memory is the stuff of which there never is enough. To reduce problems due to lack of memory, most languages provide means to grab some memory for use and later release it. Standard C has a number of functions to manage memory allocation and deallocation. We are not going to discuss these in any detail because the Macintosh has a complete memory management system. We have been using the memory management routines indirectly every time we opened a window, but as we go further into Mac programming we need to manipulate memory directly.

Figure 1: Examining the heap

Macintosh Memory Organization

Just about every book on programming the Macintosh has pictures of the Macintosh memory map. For the 64K ROMS, it looks roughly like:

I left off the physical addresses because they are typically not important, and because many of them can change as a function of memory size. For the 128K ROMS, the arrangement is slightly different. The ROM trap dispatch table has been expanded and is now in two sections, with the second section between the system heap and the second system globals area.

For this discussion, the most important areas are the Application Heap and the Stack. The stack is where all C parameters and local variables reside (actually, this depends on the compiler. Some compilers will pass some parameters in registers, and programmers may specify variables to be register variables, and if a register is available, the variable will be placed in a register). The stack grows and shrinks with the function calls and returns.

The application heap is the area where memory is explictly reserved and released. Unlike the stack, memory reserved in the heap does not disappear when a function returns. This allows the creation of large, complex data structures "on the fly."

C Memory Allocation

Since C is used on systems other than the Mac (really, there are other computers!), you might want to know about the standard C memory functions.

malloc() allocates some memory. It receives one parameter, the number of bytes to allocate, and returns a pointer to the region of memory.

free() deallocates memory previously allocated. It receives one parameter, a pointer previously returned by malloc().

The actual names and implementation will vary somewhat by compiler. Lightspeed C provides eight allocation functions and five deallocation functions in its standard library. Most of these are there to provide compatibility with Unix. The other Macintosh C compilers with which I am familiar also provide the standard C functions.

Unless you are using your Mac to develop software to run on another system, it is not a good idea to use the standard C memory functions. The primary reason for this is that on the Mac there are two kinds of allocated memory, relocatable and non-relocatable. None of the C compilers I have seen make any attempt to relate the standard C memory functions to the Mac memory functions (I assume the standard C functions return pointers to non-relocatable blocks of memory, but this is not made apparent).

Macintosh Memory Allocation

Again, since information about the Mac memory management system is readily available, I am not going to attempt to cover all the details here. There are, however, several key concepts which probably deserve some attention.

As mentioned above, memory comes in two kinds, relocatable and nonrelocatable. A relocatable block may be moved around in the heap by the operating system to make room for additional allocations. Since it may be moved, you do not receive a pointer to the block but a handle that points to a master pointer that points to the block.

Since the master pointer does not move (it is nonrelocatable), the value of the handle remains constant. When you request a nonrelocatable block, you receive a pointer directly to the block since the operating system will not move it. In general it seems that relocatable blocks are preferred since the operating system can move them out of the way when needed. If there are many nonrelocatable blocks, the operating system may not be able to satisfy a memory request because there may be no single space available big enough to meet the need. Without the ability to move blocks in the heap around (compact the heap), you will run out of memory sooner.

One problem with relocatable blocks is that it takes two pointers to access the data. One can always obtain a pointer to the block, but the system may move the block while you are using it. This situation, known as a dangling pointer, can cause quite exciting and hard to diagnose problems. If you need to use a particular block a lot in a function, you may lock the block which makes it temporarily nonrelocatable. Be sure to unlock it when you are done.

Another option for relocatable blocks is to purge them from memory. The operating system will do this if it needs more memory than is available, but only if you have marked a block as purgeable. New relocatable blocks start out as not purgeable. Note that if a block is purged, you will still have the handle, but the pointer it contains will be set to zero. To reuse the block, it is necessary to reallocate the block and rebuild the data. There is a function that reallocates purged blocks; the data is the programmer's responsibility.

Memory management errors are available through the function MemError(). A value of zero (NoErr) means there was no error; negative values represent error codes.

printw

There were a few changes to printw. We have added the ability to print rectangle and point coordinates. This is useful when using graphics. This time I added hex output. It is also now a seperate file. I have found it useful, but we probably won't print the whole thing again after this month, since this is the second time we've seen this routine.

The Program

Our program this month makes a number of trap calls that deal with memory management. From these calls, we can display information about the application heap and see how the creation of handles and pointers affects the available heap space. To keep things simple, we have created a very minimal Mac program that just puts up a blank window. The real heart of the program is the memory functions menu that lets us create handles and pointers and free them so we can see the effect on the heap. Fig. 1 shows how we print the memory information in our debugging print window using the printw() routine we learned about in a previous issue of MacTutor.

When you run the program, use the Zone Info menu item to see the state of the heap. I left the abc window in (under the File menu). Make a window and then run Zone Info. The Show Zone item traces through the zone and gives a line of information about each block, telling if it is free, relocatable or non-relocatable. By the way, the information needed to trace through the heap is not ordinarily available so there is a structure defined just before main() that defines the information needed to understand the heap. This structure, called memheads, is actually the definition of a block header. The other menu items create handles and pointers using NewHandle and NewPtr, and then if the handle or pointer is valid, another menu item lets us dispose of those handles and pointers, using DisposHandle and DisposPtr. The Zone Info menu item should show the effect of each of these operations on the available space in the heap.

There is another "feature" to this program. Every time you reserve a relocatable block, the size reserved doubles. With zone info, you can watch the memory manager increase the size of the heap and eventually run out of memory.

The key to the program is the GetZone trap call. This returns a pointer to the current heap zone. Once we have this pointer, our ShowZone and CountZone routines will read and print information about the zone for us. See the domem(item) routine in the listing to see where this trail starts with the memory menu item.

The term zone is how the heap is divided. Each zone in the heap is divided into blocks. A block is an even number of bytes, the minimum of which is 12. When your application starts up, call MaxApplZone to expand the application heap zone to its limit. Normally, an application would only be concerned with a single heap zone for itself, but you can create additional zones in the heap. One of our menu items does call MaxApplZone so you can see if it has any effect depending on when you call it. We can find out the free space left in a heap zone by calling FreeMem.

Once we get a pointer to the current heap zone, then we can examine the zone. A heap zone contains a 52 byte zone header, the allocated blocks within the zone, and a final minimum size block at the end of the zone called the zone trailer. The zone header is a pascal record defined below:

Zone = RECORD
 bkLim: Ptr;{zone trailer block}
 purgePtr:Ptr;   {used internally}
 hFstFree:Ptr;   (first free master pointer}
 zcbFree: LONGINT; {number of free bytes}
 gzProc:ProcPtr; {grow zone function}
 moreMast:Integer; {master pointers to allocate}
 flags: Integer; {used internally}
 cntRel:Integer; {not used}
 maxRel:Integer; {not used}
 cntNRel: Integer; {not used}
 maxNRel: Integer; {not used}
 cntEmpty:Integer; {not used}
 cntHandles:Integer; {not used}
 minCBFree: LONGINT; {not used}
 purgeProc: ProcPtr; {purge warning proc}
 sparePtr:Ptr;   {used internally}
 allocPtr:Ptr;   {used internally}
 heapData:Integer{first usable byte in zone}
END;

The zone pointer is defined to be of type THz, which is simply declared to be ^Zone. HeapData is the first two bytes in the block header of the first block in the zone. Hence to find the first usable block, we can say @(myZone^.heapData), which is a pointer to the first block header. Or in c, we would use &zone->heapData. The block header is 8 bytes: the tag byte, a three byte block size, and a four byte identifier that indicates whether the block is relocatable, non-relocatable or free. The four byte identifier is actually a handle, pointer or unused depending on the nature of the block. The tag byte also contains information on the nature of the block in bits 6 and 7. We can examine the nature of every block in the heap zone by adding the block size to the address of the first block and going from block to block checking the two high order bits of the tag byte. All of this is discussed in great detail in the IM chapter on the memory manager. This is implemented in our program in the showzone(zone) routine. Our memhead structure actually combines the tag byte and block size into a single four byte field called physsize. The second four bytes in the block header are stored in our memhead structure in the relhand field. We decode the physsize field to obtain the tag byte, and the block size, then we break down the tag byte to obtain the block status, and the size correction. All of this is done in the showhead(head) routine. Study carefully the three routines showhead, showzone and countzone to see how the block headers are decoded and the block size, status and address are then printed in our print window.

Final Comments

Using the techniques shown in this program, you can construct your own heapshow utility or TMON heap display. A good discussion of how TMON examines and displays the heap, is available in Dan Weston's new book, Macintosh Assembly Language Programming, Vol. II. There are some useful memory management functions not included this month such as MoreMasters() which reserves space for master pointers needed by NewHandle(). These should be easy to include in the program if you need to get an idea of how they work.

Next time we'll return to quickdraw and try to draw polygons, regions, and pictures.

Figure 4: ShowZone gives a heap dump


/* mem.c
 * explore memory allocation
 * LS C by Bob Gordon
 */

 #include "abc.h"
 #include "MemoryMgr.h"
 #include "Quickdraw.h"
 #include "EventMgr.h"
 #include "WindowMgr.h"
 #include "MenuMgr.h"
 #include "FontMgr.h"
 
 /* defines for menu ID's */
 
 #defineMdesk    100
 #defineMfile    101
 #defineMedit    102
 #defineMmem103
 
 /* File */
 #defineiNew1
 #defineiClose   2
 #defineiQuit    3
 
 /* Edit */
 #defineiUndo    1
 #defineiCut3
 #defineiCopy    4
 #defineiPaste   5
 
 /* Memory */
 #defineiZone    1
 #defineiMzone   2
 #defineiInfo    3
 #defineiHand    4
 #defineiPtr5
 #defineiFreeH   6
 #defineiFreeP   7
 
 /* Global variables */
 
 MenuHandle menuDesk;/* menu handles */
 MenuHandle menuFile;
 MenuHandle menuEdit;
 MenuHandle menuMem;
 
 WindowPtrtheWindow;
 WindowRecord  windowRec;
 Rect   dragbound;
 Rect   limitRect;
 
/*
 * structure needed to examine heap not typically
 * supplied by Mac development systems because normal
 * applications do not need to examine the heap. 
 */
 struct memheads
 {
 long   physsize;
 long   relhand;
 };
  
main()
{
 initsys(); /* sys init */
 initapp(); /* appl init */
 eventloop();
}

/* system initialization  */
initsys() 
{
 InitGraf(&thePort); 
 InitFonts();    
 InitWindows();
 InitCursor();
 InitMenus();
 theWindow = Nil;/* no window */
 SetRect(&dragbound,0,0,512,250);
 SetRect(&limitRect,60,40,508,244);
}

/*
 * application initialization
 * Sets up menus.*/
initapp()
{
 setupmenu();
}

/*
 * set up application's menus
 * Each menu is a separate group
 * of lines.  
 */
setupmenu()
{
 menuDesk = NewMenu(Mdesk,CtoPstr("\24"));
 AddResMenu (menuDesk, 'DRVR');
 InsertMenu (menuDesk, 0);
 
 menuFile = NewMenu(Mfile, CtoPstr("File"));
 AppendMenu (menuFile, 
 CtoPstr("New/N;Close;Quit/Q"));
 InsertMenu (menuFile, 0);
 
 menuEdit = NewMenu(Medit, CtoPstr("Edit"));
 AppendMenu (menuEdit, 
 CtoPstr("(Undo/Z;(-;(Cut/X;(Copy/C;(Paste/V;(Clear"));
 InsertMenu (menuEdit, 0);
 
 menuMem = NewMenu(Mmem, CtoPstr("Memory"));
 AppendMenu (menuMem,
 CtoPstr("Show Zone;Max Zone;Zone Info;New Handle;New Pointer"));
 AppendMenu (menuMem,CtoPstr("Free Handle;Free Pointer"));
 InsertMenu (menuMem, 0);
 
 DrawMenuBar();
}
 
/* Event Loop 
 * Loop forever until Quit
 */
eventloop()
{
 EventRecordtheEvent;
 char   c;
 short  windowcode;
 WindowPtrww;
 
 while(True)
 {
 if (theWindow)      /* this code is here to */
 { /* prevent closing an */
 EnableItem(menuFile,2);  /* already closed window */
 DisableItem(menuFile,1);
 }
 else   
 { 
 EnableItem(menuFile,1);
 DisableItem(menuFile,2);
 }
 if (GetNextEvent(everyEvent,&theEvent))
   
 switch(theEvent.what)    
 { /* only check mouse */
 case mouseDown:
 domousedown(&theEvent);
 break;
 default:
 break;
 }
 }
}

/* domousedown
 * handle mouse down events
 */
domousedown(er)
 EventRecord*er;
{
 short  windowcode;
 WindowPtrwhichWindow;
 short  ingo;
 long   size;
 long   newsize;
 RgnPtr rp;
 Rect   box;
 Rect   *boxp;
 
 windowcode = FindWindow(er->where, &whichWindow);
 switch (windowcode)
 {
 case inDesk:
 if (theWindow notequal 0)
 {
 HiliteWindow(theWindow, False);
 DrawGrowIcon(theWindow);
 }
 break;
 case inMenuBar:
 domenu(MenuSelect(er->where));
 break;
 }
}
 
/* domenu
 * handles menu activity
 * simply a dispatcher for each
 * menu.
 */
domenu(mc)
 long   mc; /* menu result */
{
 short  menuId;
 short  menuitem;
 
 menuId = HiWord(mc);
 menuitem = LoWord(mc);
 
 switch (menuId)
 {
 case Mdesk : break; /* not handling DA's */
 case Mfile : dofile(menuitem);
  break;
 case Mmem  : domem(menuitem);
     break;
 }
 HiliteMenu(0);
}

domem(item)
 short  item;
{
 THz    zone;
 static longsize = 1024;
 static Handle   hand = 0;
 static Ptr ptr = 0;
 struct memheads *memhead;
 
 switch (item)
 {
 case iZone :
 zone = GetZone();
 printw("\nzone %ld ",zone);
 showzone(zone);
 break;
 case iMzone :
 MaxApplZone();
 break;
 case iInfo :
 zone = GetZone();
 countzone(zone);
 break;
 case iHand :
 hand = NewHandle(size);
 printw("\nhandle %ld pointer %lx error %d ",hand,*hand,MemError());
 printw(" free mem %ld ",FreeMem());
 size = size * 2;
 break;
 case iPtr :
 ptr = NewPtr(size);
 printw("\npointer %ld error %d free mem %ld",ptr,MemError(),FreeMem());
 showhead(ptr-8);
 break;
 case iFreeH :
 if (hand equals 0)
 printw("\nNo current handle to free");
 else
 {
 DisposHandle(hand);
 hand = 0;
 }
 break;
 case iFreeP :
 if (ptr equals 0)
 printw("\nNo current pointer to free");
 else
 {
 DisposPtr(ptr);
 ptr = 0;
 }
 }
}

showhead(head)
 struct memheads *head;
{
 uchar  tagbyte;
 
 printw ("\naddress %ld ",(char*)head + 8);
 tagbyte = head->physsize >> 24;
 printw ("size correction %d ",tagbyte & 0xF);
 tagbyte >>= 6;
 switch (tagbyte)
 {
 case 0:
 printw(" free block ");
 break;
 case 1:
 printw(" non rel    ");
 break;
 case 2:
 printw(" rel        ");
 break;
 }
 printw(" physical size %ld ",head->physsize & 0x00FFFFFF);
 return (tagbyte);
}

showzone(zone)
 THz    zone;
{
 struct memheads *memhead;
 short  tblocks = 0;
 short  tfree = 0;
 short  trl = 0;
 short  tnonrel = 0;
 
 memhead = (struct memheads*)&zone->heapData;
 while (memhead < (struct memheads*)zone->bkLim)
 {
 switch (showhead(memhead))
 {
 case 0 : tfree++; break;
 case 1 : tnonrel++; break;
 case 2 : trel++; break;
 }
 tblocks++;
 memhead = (struct memheads*)((char*)memhead + (memhead->physsize & 0x00FFFFFF));
 }
 printw("\n blocks %d free %d relocatable %d non-relocatable %d ",
   tblocks, tfree, trel,tnonrel);
}
 
countzone(zone)
 THz    zone;
{
 struct memheads *memhead;
 short  tblocks = 0;
 short  tfree = 0;
 short  trel = 0;
 short  tnonrel = 0;
 uchar  tagbyte;
 
 memhead = (struct memheads*)&zone->heapData;
 while (memhead < (struct memheads*)zone->bkLim)
 {
 tagbyte = memhead->physsize >> 24;
 tagbyte >>= 6;
 switch (tagbyte)
 {
 case 0 : tfree++; break;
 case 1 : tnonrel++; break;
 case 2 : trel++; break;
 }
 tblocks++;
 memhead = (struct memheads*)((char*)memhead + (memhead->physsize & 0x00FFFFFF));
 }
 printw("\n blocks %d free %d relocatable %d non-relocatable %d ",
   tblocks, tfree, trel, tnonrel);
}
 
/* dofile
 * handles file menu
 */
dofile(item)
 short  item;
{
 char   *title1; /* first title for window */
 Rect   boundsRect;
 
 switch (item)
 {
 case iNew :/* open the window */
 title1 = "ABC Window";
 SetRect(&boundsRect,50,50,400,200);
 theWindow = NewWindow(&windowRec, &boundsRect,
 CtoPstr(title1),True,documentProc,
 (WindowPtr) -1, True, 0);
 DrawGrowIcon(theWindow);
 PtoCstr(title1);
 DisableItem(menuFile,1);
 EnableItem(menuFile,2);
 break;
 
 case iClose :   /* close the window */
 CloseWindow(theWindow);
 theWindow = Nil;
 DisableItem(menuFile,2);
 EnableItem(menuFile,1);
 break;
 
 case iQuit :    /* Quit */
 ExitToShell();
 break; 
 }
}
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »

Price Scanner via MacPrices.net

Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more

Jobs Board

*Apple* Systems Administrator - JAMF - Syste...
Title: Apple Systems Administrator - JAMF ALTA is supporting a direct hire opportunity. This position is 100% Onsite for initial 3-6 months and then remote 1-2 Read more
Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.