Alan Quatermain

The Tumblog of one Jim Dovey, iOS Software Chief Architect at Kobo in Toronto, Ontario.
He Twitters, he has an , and can occasionally be found on LinkedIn or Facebook.
If you have a query, you can ask it here.

This blog contains personal opinions, and is not endorsed by any company.

321743291

CoreFoundation dual-mode code macros

Since I’m doing some work on AquaticPrime using SecIdentityRefs, SecKeyRefs, etc, and since I want it to be compatible with either Garbage Collection or manual memory management, I need to handle CoreFoundation objects properly in both cases. It’s not difficult to do, but it lends itself to some nice syntactic sugar, which I’ve chosen to implement as the following macros. Hopefully these will prove useful to others as well.

// Use this when storing a passed-in object (i.e. one you haven't retained/created)
#define CFKeep(cf) CFMakeCollectable( CFRetain(cf) )

// Use this when you'll return something you've allocated/copied just to return
#define CFMakeReturnable(cf) (void) [NSMakeCollectable(cf) autorelease]

You’ll use the two items like this:

- (id) initWithIdentity: (SecIdentityRef) identity
{
    self = [super init];
    if ( self == nil )
        return ( nil );

    signingIdentity = CFKeep(identity);

    return ( self );
}

- (SecKeyRef) privateKey
{
    SecKeyRef privKey = NULL;
    OSStatus err = SecIdentityCopyPrivateKey( signingIdentity, &privKey );
    if ( err != errAuthorizationSuccess )
        return ( NULL );

    CFMakeReturnable(privKey);
    return ( privKey );
}

2 notes

  1. quatermain posted this