TweetFollow Us on Twitter

January 93 - OOPC Memory Management

OOPC Memory Management

Gary Odom

The November 1992 issue of Frameworks had an article by Jeff Alger about memory management in C++ with MacApp. Presented as a contrast to that article, this article describes memory management with OOPC.

Programming with objects involves allocating thousands of blocks. By the time the base classes in OOPC have been initialized, 7000 blocks have been allocated. Within a few minutes of working with a document, 20,000 blocks or more have been allocated. This is typical of object-oriented applications.

Macintosh Memory Management

The top-priority design goal for Macintosh memory management was to fully utilize available memory (space efficiency), even at the expense of other considerations, such as speed efficiency. This was sensible for the original Macintosh, a machine limited to 128 KB, but with relatively large memory demands. The chosen solution was to have relocatable blocks of memory, accessed using pointers to pointers, called handles.

The danger with handles is using them. To access a block, the handle must be dereferenced to its pointer. If the data is accessed a lot in a function, it is more efficient to dereference a handle to its pointer, and repeatedly use the pointer. The pointer is valid as long as the Memory Manager doesn't have a chance to move memory. But if you call any function, you have to know whether that function can move memory. If the function can move memory, you need to dereference the handle to get the pointer again. If memory is moved, the master pointer may change, making the pointer you were using invalid. The problem pointer is called a 'dangling pointer'.

Dangling pointers can be very hard to find, because memory may not move consistently (it depends on how tight memory is at the time). You could be writing into an invalid memory location and not discover a problem until much later. Dangling pointers are a debugging nightmare.

The Macintosh Memory Manager keeps track of every allocated block. This means memory management is optimized for a small number of blocks. With the relocatable block memory scheme, keeping track of a large number of moving blocks imposes significant overhead. This penalty is paid most in trying to allocate non-relocatable (pointer) blocks. To maintain efficient memory use, the Memory Manager always tries to allocate a non-relocatable block toward the bottom or top of the heap, so it won't get in the way during a memory shuffle. Finding space at the edge of the heap while a program is running typically involves a major memory move.

The result is that Memory Manager allocation time per block is distinctly non-linear. While handle allocation drags its tail, the time needed to allocate non-relocatable blocks is atrocious. Chart 1 below illustrates block allocation times in ticks (a tick is 1/60th of a second).

So, the Macintosh Memory Manager optimizes memory usage, but at a significant cost in speed, and with danger lurking in the form of dangling pointers.

OOPC Low-Level Memory Management

OOPC has its own memory manager, using pointers. This avoids the dangling pointer problem caused by using handles. The OOPC memory manager grabs a large chunk of memory from the heap, then slices it up for use. The first chunk is 128 KB. Other chunks are as big as needed, but at least 32 KB. (Chunk sizes are adjustable by an OOPC programmer). Chunks are locked at the top of the heap so they don't get in the way of operating system memory management. Chunks are allocated and freed as needed.

The result is pointer block allocation that does not fragment the heap. Because empty space is managed, not allocated blocks, allocation time is linear. Chart 2 below compares OOPC memory management times to Macintosh Memory Manager times. The times shown are for allocation and deallocation.At 20,000 blocks, OOPC is 20 times faster than Mac OS handles, and 100 times faster than Mac OS pointers.

Low-level memory management is implemented in OOPC using platform-independent functions, such as get_block and free_block. Handle functions (such as get_handle and free_handle) are also supported (using the Mac OS Memory Manager for the Mac version of OOPC). The OS requires handles for resources and some other tasks (such as color pixel maps).

Low-level memory management is only occasionally called directly (by an application programmer). More often, objects are created and released. (Of course, internal methods rely upon low-level memory management.) Let's look at how objects are allocated and deallocated in OOPC.

OOPC Object Memory Management

OOPC object memory management is quite simple. There are six functions/methods involved: new_object (a function), and new, bind, trash, release and empty (all methods).

The new_object function call creates an object, allocating a small amount memory required for all objects (36 bytes), plus any application data the object keeps. new_object calls the new function, which dispatches to an initial data assignment method (the method depends on the class of the object being created). After the new method finishes, an object is fully allocated and initialized.

Some objects need to keep track of other objects. A document object, for example, owns the window object that displays the document. A document object also keeps track of the pages in the document. These links between objects must be maintained.

OOPC's object system has built-in support for maintaining object links. Links between objects are established by calling bind(thisobject, thatobject). By this call, thatobject has created a link (or bond) to thisobject. thisobject cannot be released until thatobject trashes the bond between the objects by calling trash(thisobject, thatobject). Once all links to an object are trashed, the object is released by a trash (or release) call. Using bind and trash functions provides simple garbage collection, preventing objects from being released prematurely (and risking access to 'dead' objects).

release releases an object, but only if there are no links to other objects. When an object is released, empty is called (by release) to deallocate any data allocated in the new method. The release method disposes of the object itself.

Conclusion

Having its own low-level memory management gives a significant speed boost to OOPC-built applications and keeps programming chores simple. At the high level, built-in support for object linking provides garbage collection and safety with object access.
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Combo Quest (Games)
Combo Quest 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Combo Quest is an epic, time tap role-playing adventure. In this unique masterpiece, you are a knight on a heroic quest to retrieve... | Read more »
Hero Emblems (Games)
Hero Emblems 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: ** 25% OFF for a limited time to celebrate the release ** ** Note for iPhone 6 user: If it doesn't run fullscreen on your device... | Read more »
Puzzle Blitz (Games)
Puzzle Blitz 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Puzzle Blitz is a frantic puzzle solving race against the clock! Solve as many puzzles as you can, before time runs out! You have... | Read more »
Sky Patrol (Games)
Sky Patrol 1.0.1 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0.1 (iTunes) Description: 'Strategic Twist On The Classic Shooter Genre' - Indie Game Mag... | Read more »
The Princess Bride - The Official Game...
The Princess Bride - The Official Game 1.1 Device: iOS Universal Category: Games Price: $3.99, Version: 1.1 (iTunes) Description: An epic game based on the beloved classic movie? Inconceivable! Play the world of The Princess Bride... | Read more »
Frozen Synapse (Games)
Frozen Synapse 1.0 Device: iOS iPhone Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Frozen Synapse is a multi-award-winning tactical game. (Full cross-play with desktop and tablet versions) 9/10 Edge 9/10 Eurogamer... | Read more »
Space Marshals (Games)
Space Marshals 1.0.1 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.1 (iTunes) Description: ### IMPORTANT ### Please note that iPhone 4 is not supported. Space Marshals is a Sci-fi Wild West adventure taking place... | Read more »
Battle Slimes (Games)
Battle Slimes 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: BATTLE SLIMES is a fun local multiplayer game. Control speedy & bouncy slime blobs as you compete with friends and family.... | Read more »
Spectrum - 3D Avenue (Games)
Spectrum - 3D Avenue 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: "Spectrum is a pretty cool take on twitchy/reaction-based gameplay with enough complexity and style to stand out from the... | Read more »
Drop Wizard (Games)
Drop Wizard 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Bring back the joy of arcade games! Drop Wizard is an action arcade game where you play as Teo, a wizard on a quest to save his... | Read more »

Price Scanner via MacPrices.net

13-inch M4 MacBook Airs on sale for $150 off...
Amazon has new 13″ M4 MacBook Airs on sale for $150 off MSRP right now, starting at $849. Sale prices apply to most colors and configurations. Be sure to select Amazon as the seller, rather than a... Read more
15-inch M4 MacBook Airs on sale for $150 off...
Amazon has new 15″ M4 MacBook Airs on sale for $150 off Apple’s MSRP, starting at $1049. Be sure to select Amazon as the seller, rather than a third-party: – 15″ M4 MacBook Air (16GB/256GB): $1049, $... Read more
Amazon is offering a $50 discount on Apple’s...
Amazon has Apple’s 11th-generation A16 iPads in stock on sale for $50 (or a little more) off MSRP this week. Shipping is free: – 11″ 11th-generation 128GB WiFi iPads: $299 $50 off MSRP – 11″ 11th-... Read more
Clearance 13-inch M1 MacBook Airs available f...
Walmart has clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) available online for $649, $360 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks for... Read more
iPad minis on sale for $100 off Apple’s MSRP...
Amazon is offering $100 discounts (up to 20% off) on Apple’s newest 2024 WiFi iPad minis, each with free shipping. These are the lowest prices available for new minis among the Apple retailers we... Read more
AirPods Max headphones on sale for $479, $70...
Amazon has AirPods Max with USB-C on sale for $479.99 in all colors. Shipping is free. Their price is $70 off Apple’s MSRP, and it’s the lowest price available today for AirPods Max. Keep an eye on... Read more
14-inch M4 Pro/M4 Max MacBook Pros on sale th...
Don’t pay full price! Get a new 14″ MacBook Pro with an M4 Pro or M4 Max CPU for up to $320 off Apple’s MSRP this weekend at these retailers…they are the lowest prices available for these MacBook... Read more
Get a 15-inch M4 MacBook Air for $150 off App...
A couple of Apple retailers are offering $150 discounts on new 15″ M4 MacBook Airs this weekend. Prices at these retailers start at $1049: (1): Amazon has new 15″ M4 MacBook Airs on sale for $150 off... Read more
Unreal Mobile is offering a $100 discount on...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 13, and SE phones... Read more
16-inch M4 Pro MacBook Pros on sale for $250-...
Don’t pay full price! Amazon has 16-inch M4 Pro MacBook Pros (Silver or Black colors) on sale right now for up to $300 off Apple’s MSRP. Shipping is free. These are the lowest prices currently... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.