MenuMem is the name that I have given to a desk accessory that I created one weekend back in 87. Its function is to display the amount of free memory (in the application heap) in the menu bar (in K). Sort of like the way those clock INITs keep the time in the menu bar.

A Brief History

Actually, when I first came up with the idea, I wanted to make an INIT, but the only way that I could think of to update the menu was to install a VBL task. Since the Memory Manger needs to be called, a VBL task is out of the question. A desk accessory seemed like a nice compromise since it would enable the addition of menu items.

This worked fine. As long as you didnt mind choosing the desk accessory every time you opened a new application. After a few seconds I realized that if a trap was patched when the DA received a goodbye kiss, the DA could be opened back up again when a new application is started. Which one to patch? SystemTask seemed a logical choice. This way, the DA would never open up in an application that didnt support DAs.

The code went something like this:

case goodBye:
.
.
.
INSTALLPATCH()
break;

where ThePatch() and INSTALLPATCH() are two assembly language routines, the code for which is given below (in MPW Asm).

This worked exactly as desired until MultiFinder appeared. When opened under MultiFinder, MenuMem acted like every other DA. That is, it would open up within the DA handler (unless the option key was held down when it was opened. In that case, like all other DAs, it would open up within the application running in the foreground). But when I switched to another application, no menu. Even worse, if I tried to open MenuMem again from within another application, MultiFinder would automatically switch me to the layer it was currently opened in. Furthermore, since MultiFinder keeps track of any patches that are installed by the application, and removes them when you switch to another one, my patch would never be called, and a small block of memory in the System Heap would be lost. This was totally unacceptable.

The Solution

The answer lies in opening the DA up before MultiFinder is ever run, and repeatedly making control calls to allow it to update its menu. If opened at startup time, there is an added advantage: No more trips to the Apple Menu. This approach is also perhaps somewhat simpler as there are no traps to patch, and no Assembly is required. Youll still need two AA batteries though (Couldnt resist). Of course, the internals of the DA have changed somewhat. It is now written in THINKC (The previous version was written in MPW C), and uses only 2 global variables.

Before I discuss the structure of the latest (and hopefully last) version of MenuMem I would like to mention a few words about THINKC. My advice for the few people out there who dont have THINKC? GET IT. It is an absolutely wonderful development environment. Especially if you are into creating Desk Accessories or code resources (FKEYs, INITs, XCMDs, XFCNs etc.).

Since the beginning of time (January 1, 1904 to be exact) the creation of code resources in HLLs such as FORTRAN or Pascal has been hindered by the fact that no global variables were allowed (Most Mac compilers generate code that accesses global variables indirectly using register A5. Read the Memory and Segment Loader chapters of Inside Macintosh for more details on this). The more creative compiler writers have developed various implementations to allow the declaration of global variables in code resources, including appending globals to the end of the code. THINKC accesses global variables in code resources indirectly from register A4. In addition, if you are creating a device DRiVeR, THINKC will dynamically allocate a handle for your globals and store it in the dCtlStorage field of the DRiVeRs device control entry, lock it, and dereference it into register A4. Though it is not stated in the manual, it is important to note that THINKC allocates this memory by calling GetResource(DATA, drvrID), where drvrID is the owned resource ID of the DRiVeR, and then passing the returned handle to DetachResource() to remove it from the resource map. So if you need your globals to stick around between applications, you can use ResEdit to set the System Heap attribute.

Below is a description of the program and all of the routines, followed by the source code. This code is written under the assumption that the INIT, DRVR and DATA resources will be in the resource fork of the same file.

MenuMem: The INIT

pascal void main()

The INIT portion of MenuMem needs to load in and open the DRiVeR portion. However, if there is a resource of type DRVR with the same ID as ours in the System file, or a DRiVeR with the same ID as ours is already installed in the Unit Table (but possibly not in the System file, just like MenuMem), then both our DRVR and DATA resources need to be renumbered. If the MenuMem DRiVeR cant be loaded, then a beep will be heard during startup.

LoadDRVR(theName, theID)

This function calls GetResource(DRVR, theID) to load MenuMem DRVR into memory. The System Heap attribute of both this and the associated DATA resource should be set to insure that both will hang around when the application heap is re-initialized. If the DRiVeR is loaded OK then DetachResource() is called to insure that the code will hang around when the resource fork of this startup document is closed. NOTE: THINKC detaches the associated DATA from the resource fork automatically, so the global variables that are needed will be fine as long as the System Heap attribute is set. If the DRiVeR is opened successfully, the function returns noErr as its result.

Boolean DRVRXistsP(theID)

This function will return TRUE if a DRiVeR with a resource ID = theID exists in the resource fork of the System file or if one with the same ID has already been installed in the Unit Table (sort of like a librarian for DRiVeRs. This table is used by the system to keep track of data for opened ones) and FALSE otherwise. NOTE: It is important to check both the System file and the Unit Table since:

1) A DRiVeR installed in the System will not have an entry in the Unit Table at startup time, but WILL be entered when it is opened for the first time, and

2) A DRiVeR can be installed in the Unit Table and NOT in the System file (as is the case here).

If we dont check the Unit Table, we could install ourselves over an installed DRiVeR that was meant to do some REAL work.

Boolean vacant_slotP(SUN, theSlot)

This function repeatedly searches the System file and checks the unit table. If/When it finds an ID for which no DRiVeR has been installed, it sets theSlot to the ID and returns TRUE. Otherwise it returns FALSE and the value of theSlot is undefined.

Boolean slot_takenP(unit_number)

This function checks to see if a DRiVeR with a reference number corresponding to unit_number has been installed in the Unit Table. It does so by calling GetDCtlEntry(). If the call returns a device control entry, then a DRiVeR has already been opened (and thus installed), in which case the function returns TRUE. Otherwise it returns FALSE.

MenuMem: The DRiVeR

main(p,d,i)

This function gets called by THINKC when we receive Open, Close and Control calls. We just need to check the selector (i) to determine what the request is and call the appropriate function.

OpenDRVR(d,p)

This function simply resets the Flags, Delay, and Menu fields of the device control pointer since the device manager will set these fields from the header information.

setup_theMenu(d)

This is the routine that sets up the menu for the DRiVeR. It does not check to see if the menu already exists. Pascal style string constants must be declared as such since the CtoPstr() routine transforms the string in place.

ControlDRVR(d,p)

This routine is called periodically (every 5 seconds) to update the menu and to handle the case when our lone menu item Compact Memory is chosen.

update_theMenu(d)

This routine will create and install a menu for the DRiVeR, if the current amount of free memory the previous amount of free memory, or there is no menu with an ID = the owned resource ID of this DRiVeR. The latter will occur when opening a new application (whether running under MultiFinder or not). Any existing menu is deleted and disposed of before the new one is created.

CloseDRVR(d,p,x)

Since the dNeedGoodbye bit of the Flags field is set, the DRiVeR will get a goodbye kiss every time the application heap is re-initialized. This opportunity is taken to delete and dispose of the menu except for the first time (immediately before the Finder is run). In this case, not only has the Menu Manager not been initialized, but we dont have a menu anyway. This routine will always return closeErr to insure that the DRiVeR is never closed.

Software Updates via MacUpdate

SpamSieve 2.9.27 - Robust spam filter fo...

SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering.
SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more

VueScan 9.5.62 - Scanner software with a...

VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more

Fantastical 2.3.2 - Create calendar even...

Fantastical 2 is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun:
Open Fantastical with a single click or keystroke
Type in your event... Read more

PCalc 4.4.4 - Full-featured scientific c...

PCalc is a full-featured, scriptable scientific calculator with support for hexadecimal, octal, and binary calculations, as well as an RPN mode, programmable functions, and an extensive set of unit... Read more

Alfred 3.2.1 - Quick launcher for apps a...

Alfred is an award-winning productivity application for OS X. Alfred saves you time when you search for files online or on your Mac. Be more productive with hotkeys, keywords, and file actions at... Read more

OmniPlan 3.6 - Robust project management...

With OmniPlan, you can create logical, manageable project plans with Gantt charts, schedules, summaries, milestones, and critical paths. Break down the tasks needed to make your project a success,... Read more

Backblaze 4.2.0.990 - Online backup serv...

Backblaze is an online backup service designed from the ground-up for the Mac. With unlimited storage available for $5 per month, as well as a free 15-day trial, peace of mind is within reach with... Read more

AppDelete 4.3.1 - $7.99

AppDelete is an uninstaller that will remove not only applications but also widgets, preference panes, plugins, and screensavers along with their associated files. Without AppDelete these associated... Read more

Apple GarageBand 10.1.4 - Complete recor...

The new GarageBand is a whole music creation studio right inside your Mac -- complete with keyboard, synths, orchestral and percussion instruments, presets for guitar and voice, an entirely... Read more

Adobe Lightroom 6.8 - Import, develop, a...

Adobe Lightroom is available as part of Adobe Creative Cloud for as little as $9.99/month bundled with Photoshop CC as part of the photography package. Lightroom 6 is also available for purchase as a... Read more

Latest Forum Discussions

Galaxy on Fire 3 - Manticore brings the series back for another round of daring space battles. It's familiar territory for folks who are familiar with the franchise. If you've beaten the game and are looking to broaden your horizons, might we... | Read more »

The best apps for your holiday gift exch...

What's that, you say? You still haven't started your holiday shopping? Don't beat yourself up over it -- a lot of people have been putting it off, too. It's become easier and easier to procrastinate gift shopping thanks to a number of apps that... | Read more »

MyTona, based in the chilly Siberian city of Yakutsk, has brought a little festive fun to its hidden object game Seekers Notes: Hidden Mystery. The Christmas update introduces some new inhabitants to players, and with them a chance to win plenty of... | Read more »

PINE GROVE 1.0
Device: iOS Universal
Category: Games
Price: $1.99, Version: 1.0 (iTunes)
Description:
A pine grove where there are no footsteps of people due to continuous missing cases. The case is still unsolved and nothing has... | Read more »

Niantic teases new Pokémon announcement...

After rumors started swirling yesterday, it turns out there is an official Pokémon GO update on its way. We’ll find out what’s in store for us and our growing Pokémon collections tomorrow during the Starbucks event, but Niantic will be revealing... | Read more »

3 reasons why Nicki Minaj: The Empire is...

Nicki Minaj is as business-savvy as she is musically talented and she’s proved that by launching her own game. Designed by Glu, purveyors of other fine celebrity games like cult favorite Kim Kardashian: Hollywood, Nicki Minaj: The Empire launched... | Read more »

Clash of Clans is getting its own animat...

Riding on its unending wave of fame and success, Clash of Clans is getting an animated web series based on its Clash-A-Rama animated shorts.As opposed to the current shorts' 60 second run time, the new and improved Clash-A-Rama will be comprised of... | Read more »

Leaks hint at Pokémon GO and Starbucks C...

Leaked images from a hub for Starbucks employees suggests that a big Pokémon GO event with the coffee giant could begin this very week. The images appeared on Reddit and hint at some exciting new things to come for Niantic's smash hit game.
| Read more »

DietSensor, Inc., a developer of smart food and nutrition applications designed to fight diabetes and obesity and help improve overall fitness, has announced the launch of its DietSensor app for... Read more

Best Buy has dropped their price on the 64GB Apple TV to $159.99 including free shipping. That’s $40 off MSRP.
32GB Apple TVs are on sale right now for $98 on Sams Club’s online store. That’s $51 off... Read more

12-inch Retina MacBooks, Apple refurbished, n...

Apple has restocked a full line of Certified Refurbished 2016 12″ Retina MacBooks, now available for $200-$260 off MSRP. Refurbished 2015 models are available starting at $929. Apple will include a... Read more

Apple has Certified Refurbished 13″ MacBook Airs available starting at $849. An Apple one-year warranty is included with each MacBook, and shipping is free:
- 13″ 1.6GHz/8GB/128GB MacBook Air: $849 $... Read more

Apple refurbished iMacs available for up to $...

Apple has Certified Refurbished 2015 21″ & 27″ iMacs available for up to $350 off MSRP. Apple’s one-year warranty is standard, and shipping is free. The following models are available:
- 21″ 3.... Read more

Apple’s Education discount saves up to $300 o...

Purchase a new Mac or iPad using Apple’s Education Store and take up to $300 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount. Shipping is free:
-... Read more

MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders. Not responsible for typographical errors.

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