[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Garbage Collection
From: |
Thomas Davie |
Subject: |
Re: Garbage Collection |
Date: |
Wed, 25 May 2011 16:31:49 +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
Are there any more dependancies for this than boehm-gc and the trunks of the
relevant packages? Using trunk clang, trunk libobjc2 and a fresh install of
ubuntu's libgc-dev I'm getting this:
tatd2@GLaDOS:~/libobjc2$ make boehm_gc=yes
This is gnustep-make 2.6.0. Type 'make print-gnustep-make-help' for help.
Making all for library libobjc...
Compiling file gc_boehm.c ...
gc_boehm.c:133:15: warning: incompatible pointer types passing 'id *' (aka
'struct objc_object **') to parameter
of type 'char *' [-Wincompatible-pointer-types]
GC_add_roots(ptr, ptr+1);
^~~
/usr/include/gc/gc.h:353:26: note: passing argument to parameter 'low_address'
here
GC_API void GC_add_roots GC_PROTO((char * low_address,
^
/usr/include/gc/gc.h:353:43: note: instantiated from:
GC_API void GC_add_roots GC_PROTO((char * low_address,
^
gc_boehm.c:133:20: warning: incompatible pointer types passing 'id *' (aka
'struct objc_object **') to parameter
of type 'char *' [-Wincompatible-pointer-types]
GC_add_roots(ptr, ptr+1);
^~~~~
/usr/include/gc/gc.h:353:26: note: passing argument to parameter
'high_address_plus_1' here
GC_API void GC_add_roots GC_PROTO((char * low_address,
^
/usr/include/gc/gc.h:354:15: note: instantiated from:
char * high_address_plus_1));
^
gc_boehm.c:285:23: error: variable has incomplete type 'struct GC_stack_base'
struct GC_stack_base base;
^
gc_boehm.c:285:9: note: forward declaration of 'struct GC_stack_base'
struct GC_stack_base base;
^
gc_boehm.c:286:6: warning: implicit declaration of function 'GC_get_stack_base'
is invalid in C99
[-Wimplicit-function-declaration]
if (GC_get_stack_base(&base) != GC_SUCCESS)
^
gc_boehm.c:313:2: warning: implicit declaration of function
'GC_unregister_my_thread' is invalid in C99
[-Wimplicit-function-declaration]
GC_unregister_my_thread();
^
4 warnings and 1 error generated.
make[3]: *** [obj/libobjc.obj/gc_boehm.c.o] Error 1
make[2]: *** [internal-library-all_] Error 2
make[1]: *** [libobjc.all.library.variables] Error 2
make: *** [internal-all] Error 2