Skip navigation.
 
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
FROM : Greg Parker
DATE : Fri Feb 08 05:30:29 2008

Ben Trumbull wrote:
> John Engelhart wrote:

>> - (id *)copyOfObjectArray:(id *)originalObjects length:
>> (NSUInteger)length
>> {
>> id *newObjectArray = NULL;
>> newObjectArray = NSAllocateCollectable(sizeof(id) * length, 
>> NSScannedOption);
>> memcpy(newObjectArray, originalObjects, sizeof(id) * length);
>> return(newObjectArray);
>> }

> This does not work. Pushing GC'd objects through memcpy, a system 
> call that can't know anything about Objective-C Garbage Collection, 
> seems unwise.


Correct. Don't manipulate memory that might have GC pointers in it 
without using a GC-aware function.


> Nonetheless, that also should be better documented, and a bug report 
> for a public GC compatible memory copy API would be good.


The GC-aware memcpy() is in <objc/objc-auto.h>

    void *objc_memmove_collectable(void *dst, const void *src, size_t 
size);

There are also GC-aware versions of OSAtomicCompareAndSwapPtr()

    BOOL objc_atomicCompareAndSwapGlobal(id predicate, id 
replacement, volatile id *objectLocation);
    BOOL objc_atomicCompareAndSwapGlobalBarrier(id predicate, id 
replacement, volatile id *objectLocation);
// atomic update of an instance variable
    BOOL objc_atomicCompareAndSwapInstanceVariable(id predicate, id 
replacement, volatile id *objectLocation);
    BOOL objc_atomicCompareAndSwapInstanceVariableBarrier(id 
predicate, id replacement, volatile id *objectLocation);

These are typedef'd as id, but they work equally well with any pointer.


>> Anyone who's used garbage collection with C is probably familiar 
>> with the Boehm Garbage Collector. [...] It makes no particular 
>> demands of the programmer or compiler, in fact it can be used as a 
>> drop in replacement for malloc() and free(), requiring no changes.


Of course, that only works if malloc() is replaced everywhere in the 
system, which is impractical in a dynamic shared library environment. 
Storing a Boehm-managed pointer in a block allocated from non-Boehm 
malloc() or a non-default malloc zone would cause just as much grief 
as storing a Leopard-GC-managed pointer in a block allocated with 
malloc().

The designer of a GC system always has to draw a line and say "if you 
cross this line, you have to start thinking about memory management 
again".
Java: memory management is easy, until you start working with non-Java 
code via JNI. Benefits: the JVM can use sophisticated GC techniques 
because of its tight control. Drawbacks: working with non-Java code is 
very hard.
Boehm: memory management is easy, until you call mmap() or start 
working with code in shared libraries. Benefits: most ordinary C code 
works. Drawbacks: GC algorithms invented after 1970 or so are 
impractical.
Objective-C: memory management is easy, until you want to use it with 
blocks that aren't Objective-C objects. Benefits: most ordinary 
Objective-C code works. Drawbacks: C code is harder than Boehm; GC 
flexibility is less than Java.


--
Greg Parker    <email_removed>    Runtime Wrangler

Related mailsAuthorDate
mlUse of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 4, 02:57
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Quincey Morris Feb 4, 05:09
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful j o a r Feb 4, 08:18
mlre: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Ben Trumbull Feb 4, 10:19
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Alastair Houghton Feb 4, 14:11
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Greg Titus Feb 4, 16:39
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Sean McBride Feb 4, 22:40
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 5, 01:14
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Chris Hanson Feb 5, 02:21
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Jonathon Mah Feb 5, 08:47
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Alastair Houghton Feb 5, 13:40
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 6, 10:39
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Alastair Houghton Feb 6, 12:15
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 6, 15:59
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Michael Tsai Feb 6, 16:12
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful glenn andreas Feb 6, 16:45
mlBug in CF/NSString's no-copy constructors (was Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful) Alastair Houghton Feb 6, 16:55
mlRe: Bug in CF/NSString's no-copy constructors (was Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful) Michael Tsai Feb 6, 17:46
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Clark Cox Feb 6, 17:47
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Jean-Daniel Dupas Feb 6, 18:05
mlRe: Bug in CF/NSString's no-copy constructors Alastair Houghton Feb 6, 18:46
ml[Moderator] Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Scott Anguish Feb 6, 18:46
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Hamish Allan Feb 6, 18:52
mlRe: Bug in CF/NSString's no-copy constructors Michael Tsai Feb 6, 19:29
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Hamish Allan Feb 6, 19:36
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Alastair Houghton Feb 6, 20:12
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful glenn andreas Feb 6, 21:23
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Sean McBride Feb 6, 21:47
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Michael Ash Feb 6, 22:38
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Hamish Allan Feb 6, 23:49
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 7, 02:06
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Hamish Allan Feb 7, 02:40
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Chris Suter Feb 7, 02:48
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 7, 02:48
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Clark Cox Feb 7, 03:52
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful glenn andreas Feb 7, 04:01
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Timothy Reaves Feb 7, 04:08
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Brady Duga Feb 7, 04:22
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 7, 07:14
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 7, 07:14
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Michael Ash Feb 7, 23:58
mlRe: Bug in CF/NSString's no-copy constructors John Engelhart Feb 8, 03:31
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 8, 05:23
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Greg Parker Feb 8, 05:30
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Michael Tsai Feb 8, 05:56
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Hamish Allan Feb 8, 18:35
ml[Moderator] Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Scott Anguish Feb 8, 22:17