Re: Garbage Collection

From:

Thomas Davie

Subject:

Re: Garbage Collection

Date:

Wed, 25 May 2011 15:42:29 +0100

On 25 May 2011, at 13:04, David Chisnall wrote:
> On 25 May 2011, at 12:47, David Chisnall wrote:
>
>> {lots of stuff, and then forgot to say half of what he actually meant say}
>
> Apple's GC APIs are designed to be trivial to use, there are just a few
> things you need to do:
>
> If you are allocating memory that contains pointers, use
> NSAllocateCollectable(size, NSScannedOption), not malloc().
>
> All memory allocated by NSAllocateCollectable() will be GC'd automatically,
> so it does not require an explicit call to any function like free(). If you
> add NSCollectorDisabledOption, then the GC will be temporarily disabled for
> that allocation. You can reenable it by calling NSMakeCollectable() on the
> pointer. This is only intended for interoperability with CoreFoundation, so
> generally GNUstep code can ignore this.
>
> Implement -finalize to do cleanup, not -dealloc (well, you can implement
> both, but -dealloc won't be called in non-GC mode). This should free memory
> allocated with malloc() and close file descriptors, but little else. For
> example, you don't need to unregister with notification centres here - they
> use __weak pointers.
>
> Use __strong {type}* on pointers that will store GC'd references. This is
> not actually required currently, but it may be in the future (it is on OS X,
> because they are a bit more aggressive about reducing the number of paths
> that the GC has to follow than I am).
>
> Use __weak id for zeroing weak references. These will not prevent the
> referenced object from being finalized, and will become 0 when the object is
> finalized.
>
> You can skip -retain/-release/-autorelease messages, but the compile will
> ignore them if you compile in -fobjc-gc-only mode, so you can leave them in
> if you want to write code that works with GC or non-GC mode.
>
> If you want to pass an object to some C code that is not GC-aware, you can
> use CFRetain() and CFRelease(). These increment and decrement a reference
> count for the object, so must be paired. When an object's reference count is
> nonzero, it will not be eligible for collection. Objects do not have a
> reference count associated with them by default.
>
> If you write code that only works in GC mode, then add this to prevent people
> compiling it in non-GC mode and wondering why it's broken:
>
> #ifndef __OBJC_GC__
> #error This code requires garbage collection
> #endif
I take it that along with compiling your runtime with gc on, we need a brand
new svn head of clang?
Bob