[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Implementing CFSTR() in corebase
From: |
David Chisnall |
Subject: |
Re: Implementing CFSTR() in corebase |
Date: |
Fri, 22 Jan 2010 16:53:34 +0000 |
Hi Stef,
I had this discussion with Eric yesterday, so good timing...
On OS X, CSTR() calls one of two functions.
One is a builtin that tells the compiler to emit a CFString. This is
implemented in Clang, but currently emits a CFString on all platforms. It
would be a relatively minor change to get it to call the ObjC runtime functions
for emitting constant strings, however. Of course, that doesn't help us with
GCC support...
The other function is Apple's fallback for other compilers, and is horrible.
It basically looks in a map to see if a CF string has already been allocated,
returns it if it has, and allocates it if it hasn't.
As to how constant strings are implemented in the runtime, it's a bit more
tricky. Every constant string object has three fields:
Class isa;
char *c_string;
unsigned int len;
The isa pointer is set, by the compiler, to the name of the class. When the
compile unit is loaded, the runtime looks up the class and sets the isa pointer
to the correct location. This is quite a general mechanism, and in fact allows
arbitrary other objects to be created, which is quite nice but is not actually
used. With Apple's implementation, the isa pointer is set to a class
statically allocated in CoreFoundation. I'd rather we didn't copy that...
Now, you could possibly define the macro like this:
#define CFSTR(x) ({\
static struct { id isa, char *c_string, unsigned int len } _str = { 0,
x, 0};\
if (_str.isa == 0)\
{\
pthread_mutex_lock(&CFStringInitLock);\
if (_str.isa == 0)\
{\
_str.isa = objc_getClass("NSConstantString");\
_str.len = strlen(_str.c_string);
}\
pthread_mutex_unlock(&CFStringInitLock);\
}\
(CFStringRef)_str;\
})
That's pretty horrible though. It's probably better to implement Apple's slow
solution for now, and I'll add support for the fast one to clang.
David
On 22 Jan 2010, at 16:33, Stef Bidi wrote:
> Fred and I have been discussing how to implement this function/macro for use
> with corebase. If you haven't noticed yet CFSTR() is not currently
> implemented in corebase. The problem we've been running into is how is
> constant strings implemented in ObjC. Using a test and the GCC manual we
> were able to get an idea of what the constant string structure looks like at
> compile time, but still have no idea how it's implemented after that (GCC
> manual says the runtime sets the Class * to the constant string class at
> runtime).
>
> I was hoping someone with more experience of how the objc runtimes and
> compilers work to help solve this dilema.
>
> Stefan
> _______________________________________________
> Gnustep-dev mailing list
> address@hidden
> http://lists.gnu.org/mailman/listinfo/gnustep-dev
-- Sent from my brain
- Implementing CFSTR() in corebase, Stef Bidi, 2010/01/22
- Re: Implementing CFSTR() in corebase,
David Chisnall <=
- Re: Implementing CFSTR() in corebase, David Chisnall, 2010/01/22
- Re: Implementing CFSTR() in corebase, Eric Wasylishen, 2010/01/22
- Re: Implementing CFSTR() in corebase, Vincent R., 2010/01/22
- Re: Implementing CFSTR() in corebase, Eric Wasylishen, 2010/01/22
- Re: Implementing CFSTR() in corebase, Fred Kiefer, 2010/01/23
- Re: Implementing CFSTR() in corebase, Vincent Richomme, 2010/01/23
- Re: Implementing CFSTR() in corebase, David Chisnall, 2010/01/23
- Re: Implementing CFSTR() in corebase, Stef Bidi, 2010/01/22
- Re: Implementing CFSTR() in corebase, David Chisnall, 2010/01/22
- Re: Implementing CFSTR() in corebase, David Chisnall, 2010/01/23