[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CoreFoundation

From: David Chisnall
Subject: Re: CoreFoundation
Date: Fri, 23 Oct 2009 22:34:19 +0100

Hi Stef,

Having an implementation of Core Foundation would be nice, but I believe your approach is wrong.

First, please do not use the info field in the class structure for anything. That field is for the runtime to use. The GCC runtime uses several of them, the GNUstep runtime uses a few more. It should not be used; there is no guarantee that future runtime versions won't use them all.

With regard to the CFTypeID, this should be the address of the class structure. For those unfamiliar with how CoreFoundation works:

Each of the CF classes has an isa pointer, just like an Objective-C object. Unlike Objective-C objects, this is set to a value below 64KB, which is guaranteed not to be the address of a real class. This number is the CFTypeID, which is a pseudo-class.

The reason that is that Apple's CoreFoundation does not depend on libobjc (although it does link to it). If you use CF without libobjc, then you use static dispatch for all of the CF calls. When you call CFSomething() the function checks that the object's isa pointer is set to the magic constant that it expects and if not then it calls another function (via some ugly macros). This is done so CF objects can be used without relying on libobjc having finished loading classes.

If you use Objective-C then you get the toll-free bridging. If you send a message to an object with an isa pointer that is less than 2^16, then the runtime uses a special lookup mechanism. If you call a CF function with an object with an isa pointer above 2^16, it bounces it to the Objective-C runtime for looking up the method (this is how, for example, you can use CFString functions on your own NSString subclass).

Unless you want to use CF for some very low-level stuff, then there is really no point in copying this. You can just:

1) Define Objective-C classes that implement the CF types (many of these already exist in GNUstep).
2) Write wrapper functions that call the ObjC methods.
3) Return the address of the class as the isa pointer.

This will then work with all code that uses CF types unless they either rely on the TypeID < 2^16 thing or uses CF functions in +load or __attribute__((constructor)) functions.

Most of the CF equivalents of class methods take a CFAllocator. You can declare this as a typedef from NSZone; they are functionally equivalent. For a first pass, you can just implement most of the CF functions as trivial Objective-C functions, for example:

typedef NSArray *CFArrayRef;
typedef NSZone *CFAllocatorRef;
CFArrayRef CFArrayCreateCopy(CFAllocatorRef allocator, CFArrayRef theArray)
        [(NSArray*)theArray copyWithZone: allocator];

Once this is working, then you can start worrying about optimising it. You might also consider making these in static inline functions in the header wrapped in #ifdef __OBJC__ so that people using CF functions in ObjC call the real methods.

I wrote some macros for declaring / defining these CF functions a while back, which I can send you if you're interested, and I'm happy to review any code you're working on.


On 23 Oct 2009, at 21:23, Stef Bidi wrote:

Just testing the water... I've been a little bored and started looking at implementing a portion of CoreFoundation on top of libobjc and GNUstep-base, I'm tentatively calling it CoreBase.

At this point I've spent most of my time on the runtime (CFRuntime) and CFBase. I'm not sure if it works, because in CF the runtime requires CFString and CFDictionary and I'll have to at least do some work there before even trying to compile. Because I don't want to have to rewrite/copy parts of what's already in GNUstep-base (like reference counting) I've decided to depend on it. Libobjc is also required because I'm using that as a gateway to -base, and using some of it's features (like the class number stored in class->info to coinside with the CFTypeID).

Before continuing with it (because, of course, time is precious) I want to make sure there's at least some interest. I'm also going to need a fairly high degree of help on this project because certain parts are out of my league (I still think it's an interesting learning experience).

Anyway, just let me know if it's a project I should keep moving forward.

Gnustep-dev mailing list

-- Send from my Jacquard Loom

reply via email to

[Prev in Thread] Current Thread [Next in Thread]