Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Handles and Pointers [was: performance issues]




On Mar 30, 2005, at 10:37 AM, Chris Espinosa wrote:

On Mar 30, 2005, at 10:24 AM, Mike Lazear wrote:
P.S. By the way, handles are completely unnecessary in Mac OS X, except for backwards-compatibility with Carbon functions that take them.  With Mac OS X's VM system, you can resize a pointer just as easily as you could resize a handle in Mac OS 9.


"Mac OS X implements a highly-tuned, threadsafe allocation library, providing standard implementations of the malloc, calloc, realloc, and free routines, among others. If you are allocating memory using older routines such as NewPtr or NewHandle, you should change your code to use malloc instead. The end result is the same since most legacy routines are now wrappers for malloc anyway."


I thought I had read on this list in the past where someone said handles were no longer necessary but I've never taken them out of my program.  Before I go thru and rip out hundreds of calls involving handles I want to make sure that I'm clear (though it sounds pretty darn clear from what Chris already said).   Per what Chris says this means there is no reason to ever call HLock or HUnlock, is that correct?  Nor do we need to call NewPtr, NewHandle, DisposePtr, DisposeHandle?

Yes and no.  HLock and HUnlock are essentially noops (they do set and change the locked bits, but they have no other effect, as the master pointer values never change anyway).  There are still plenty of Carbon routines that expect or return handles, and you pretty much have to use valid handles for them, though over time more and more of these are becoming opaque objects (such as the DataHandle in an AEDesc, which stopped being a handle in Panther).  

If you're still using handles for your own values that you never pass to Carbon system functions, you can stop now.  They don't buy you anything and they cost you extra instructions to dereference.

As for NewPtr and DisposePtr, you should use malloc() and free() instead, and in most cases you can use a properly-cast return value from malloc() anywhere you'd use a Ptr from NewPtr.

Okay, If HLock and HUnlock are just no-ops I'm just going to remove them. I have a wrapper around NewPtr and DisposePtr so that should be easy to change to malloc() and free(). I could easily remove my wrapper for NewPtr and just call malloc throughout my code but free is another story. My DisposePtr wrapper always makes sure that the pointer just freed is set to NULL.

As for the NewHandle, DisposeHandle I'm calling a number of API Carbon routines that still use handles:
GetResource, Get1Resource, AddResource, WriteResource, ChangedResource, LoadResource, ReleaseResoure, DrawPicture, KillPicture, GetHandleSize

I'm also using handles (dereferenced) with functions I'm not sure require them: (though most calls are probably due to list above that does need handles)
GetScrapFlavorData, CFDataCreate, PutScrapFlavor, FSRead, FSWrite.

It appears that the handles that I use in the program are because I haven't fully moved to Nibs and haven't switched to Pasteboard.

I also ran across HNoPurge and HPurge. Are those also no longer necessary?

Mike
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden

References: 
 >performance issues (From: "Edwards, Waverly" <email@hidden>)
 >Re: performance issues (From: Chris Espinosa <email@hidden>)
 >Re: performance issues (From: Mike Lazear <email@hidden>)
 >Handles and Pointers [was: performance issues] (From: Chris Espinosa <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.