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: static CFSTR in C



On 8/27/03 2:12 PM, Marc Poirier didst favor us with:

> On Wed, 27 Aug 2003, Laurence Harris wrote:
>
>> A while back, Steve Zellers posted the following:
>>
>>> CFSTR, when compiled on 10.2 or later with GCC 3.x with
>>> -fconstant-cfstrings will lay out a memory object with your CFString in
>>> your TEXT section; it'll be as efficient as possible.
>>
>> So in this case, using a macro as Jim suggested will do essentially what you
>> want.
>>
>> Otherwise, CFSTR is actually a function which takes a key and returns a
>> CFStringRef, and you can't do that with a global (outside a function)
>> variable. Or at least I've never figured out a way to do it.
>
> You can with C++.

Well, I don't know how do it with CFSTR(), and I'd like to for improved
efficiency (see below).

> But I guess, as you and others have said, just using
> #define is probably best and simplest. I didn't know about what you said
> above about CFSTR in 10.2 with gcc 3 (thank you for informing me), so that
> settles my qualms about using a macro.
>
>> Out of curiosity, why do you want to do this?
>
> Just the same reason as defining any constant, I just have a particular
> name that I will use more than once.

Common practice is to use a macro, but with the exception of the above,
people should understand that CFSTR() really is a function. Basically what's
going on here is that the system maintains a dictionary of CFStringRefs for
you. The first time you call CFSTR with a particular key, the system does a
lookup in that dictionary for an entry with that key, and not finding one,
it creates a CFStringRef containing the text in the key, adds the
CFStringRef to the dictionary, and then returns the CFStringRef. Future
calls to CFSTR() result in the system searching the dictionary and returning
the CFStringRef corresponding to the key. So there is a performance hit
here, especially if you're using a CFSTR() call in a loop, for example. I
don't honestly know how efficient those lookups are, but given all the
strings you could be defining with CFSTR(), the dictionary could have
hundreds of entries.

It also explains why you should never release the results of a call to
CFSTR().

Larry
_______________________________________________
carbon-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/carbon-development
Do not post admin requests to the list. They will be ignored.



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.