Blame doc/README.darwin

Packit d28291
Darwin/MacOSX Support - December 16, 2003
Packit d28291
Packit d28291
== Build Notes ==
Packit d28291
Packit d28291
Building can be done with autoconf as normal. If you want to build
Packit d28291
a Universal library using autoconf, you need to disable dependency
Packit d28291
tracking and specify your desired architectures in CFLAGS:
Packit d28291
Packit d28291
CFLAGS="-arch ppc -arch i386 -arch x86_64" ./configure --disable-dependency-tracking
Packit d28291
Packit d28291
Packit d28291
== Important Usage Notes ==
Packit d28291
Packit d28291
GC_init() MUST be called before calling any other GC functions. This
Packit d28291
is necessary to properly register segments in dynamic libraries. This
Packit d28291
call is required even if you code does not use dynamic libraries as the
Packit d28291
dyld code handles registering all data segments.
Packit d28291
Packit d28291
When your use of the garbage collector is confined to dylibs and you
Packit d28291
cannot call GC_init() before your libraries' static initializers have
Packit d28291
run and perhaps called GC_malloc(), create an initialization routine
Packit d28291
for each library to call GC_init():
Packit d28291
Packit d28291
#include "gc.h"
Packit d28291
extern "C" void my_library_init() { GC_init(); }
Packit d28291
Packit d28291
Compile this code into a my_library_init.o, and link it into your
Packit d28291
dylib. When you link the dylib, pass the -init argument with
Packit d28291
_my_library_init (e.g. gcc -dynamiclib -o my_library.dylib a.o b.o c.o
Packit d28291
my_library_init.o -init _my_library_init). This causes
Packit d28291
my_library_init() to be called before any static initializers, and
Packit d28291
will initialize the garbage collector properly.
Packit d28291
Packit d28291
Note: It doesn't hurt to call GC_init() more than once, so it's best,
Packit d28291
if you have an application or set of libraries that all use the
Packit d28291
garbage collector, to create an initialization routine for each of
Packit d28291
them that calls GC_init(). Better safe than sorry.
Packit d28291
Packit d28291
The incremental collector is still a bit flaky on darwin. It seems to
Packit d28291
work reliably with workarounds for a few possible bugs in place however
Packit d28291
these workaround may not work correctly in all cases. There may also
Packit d28291
be additional problems that I have not found.
Packit d28291
Packit d28291
Thread-local GC allocation will not work with threads that are not
Packit d28291
created using the GC-provided override of pthread_create(). Threads
Packit d28291
created without the GC-provided pthread_create() do not have the
Packit d28291
necessary data structures in the GC to store this data.
Packit d28291
Packit d28291
Packit d28291
== Implementation Information ==
Packit d28291
Packit d28291
Darwin/MacOSX support is nearly complete. Thread support is reliable on
Packit d28291
Darwin 6.x (MacOSX 10.2) and there have been reports of success on older
Packit d28291
Darwin versions (MacOSX 10.1). Shared library support had also been
Packit d28291
added and the gc can be run from a shared library.
Packit d28291
Packit d28291
Thread support is implemented in terms of mach thread_suspend and
Packit d28291
thread_resume calls. These provide a very clean interface to thread
Packit d28291
suspension. This implementation doesn't rely on pthread_kill so the
Packit d28291
code works on Darwin < 6.0 (MacOSX 10.1). All the code to stop and
Packit d28291
start the world is located in darwin_stop_world.c.
Packit d28291
Packit d28291
Since not all uses of the GC enable clients to override pthread_create()
Packit d28291
before threads have been created, the code for stopping the world has
Packit d28291
been rewritten to look for threads using Mach kernel calls. Each
Packit d28291
thread identified in this way is suspended and resumed as above. In
Packit d28291
addition, since Mach kernel threads do not contain pointers to their
Packit d28291
stacks, a stack-walking function has been written to find the stack
Packit d28291
limits. Given an initial stack pointer (for the current thread, a
Packit d28291
pointer to a stack-allocated local variable will do; for a non-active
Packit d28291
thread, we grab the value of register 1 (on PowerPC)), it
Packit d28291
will walk the PPC Mach-O-ABI compliant stack chain until it reaches the
Packit d28291
top of the stack. This appears to work correctly for GCC-compiled C,
Packit d28291
C++, Objective-C, and Objective-C++ code, as well as for Java
Packit d28291
programs that use JNI. If you run code that does not follow the stack
Packit d28291
layout or stack pointer conventions laid out in the PPC Mach-O ABI,
Packit d28291
then this will likely crash the garbage collector.
Packit d28291
Packit d28291
The original incremental collector support unfortunately no longer works
Packit d28291
on recent Darwin versions. It also relied on some undocumented kernel
Packit d28291
structures. Mach, however, does have a very clean interface to exception
Packit d28291
handing. The current implementation uses Mach's exception handling.
Packit d28291
Packit d28291
Much thanks goes to Andrew Stone, Dietmar Planitzer, Andrew Begel,
Packit d28291
Jeff Sturm, and Jesse Rosenstock for all their work on the
Packit d28291
Darwin/OS X port.
Packit d28291
Packit d28291
-Brian Alliet
Packit d28291
Packit d28291
== gc_cpp.h usage ==
Packit d28291
Packit d28291
Replacement of operator new and delete is apparently not supported with
Packit d28291
dynamic libraries.  This means that applications using gc_cpp.h
Packit d28291
(including the built-in test) will probably not work correctly with
Packit d28291
the collector in a dynamic library, unless special care is taken.
Packit d28291
Packit d28291
See
Packit d28291
http://article.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/1421
Packit d28291
for some details.
Packit d28291
Packit d28291
- Hans Boehm (based on information from Andrew Begel)
Packit d28291
Packit d28291
Packit d28291
== Older Information (Most of this no longer applies to the current code) ==
Packit d28291
Packit d28291
While the GC should work on MacOS X Server, MacOS X and Darwin, I only tested
Packit d28291
it on MacOS X Server.
Packit d28291
I've added a PPC assembly version of GC_push_regs(), thus the setjmp() hack is
Packit d28291
no longer necessary. Incremental collection is supported via mprotect/signal.
Packit d28291
The current solution isn't really optimal because the signal handler must decode
Packit d28291
the faulting PPC machine instruction in order to find the correct heap address.
Packit d28291
Further, it must poke around in the register state which the kernel saved away
Packit d28291
in some obscure register state structure before it calls the signal handler -
Packit d28291
needless to say the layout of this structure is no where documented.
Packit d28291
Threads and dynamic libraries are not yet supported (adding dynamic library
Packit d28291
support via the low-level dyld API shouldn't be that hard).
Packit d28291
Packit d28291
The original MacOS X port was brought to you by Andrew Stone.
Packit d28291
Packit d28291
Packit d28291
June, 1 2000
Packit d28291
Packit d28291
Dietmar Planitzer
Packit d28291
Packit d28291
Note from Andrew Begel:
Packit d28291
Packit d28291
One more fix to enable gc.a to link successfully into a shared library for
Packit d28291
MacOS X. You have to add -fno-common to the CFLAGS in the Makefile. MacOSX
Packit d28291
disallows common symbols in anything that eventually finds its way into a
Packit d28291
shared library. (I don't completely understand why, but -fno-common seems to
Packit d28291
work and doesn't mess up the garbage collector's functionality).
Packit d28291
Packit d28291
Feb 26, 2003
Packit d28291
Packit d28291
Jeff Sturm and Jesse Rosenstock provided a patch that adds thread support.
Packit d28291
GC_MACOSX_THREADS should be defined in the build and in clients.  Real
Packit d28291
dynamic library support is still missing, i.e. dynamic library data segments
Packit d28291
are still not scanned.  Code that stores pointers to the garbage collected
Packit d28291
heap in statically allocated variables should not reside in a dynamic
Packit d28291
library.  This still doesn't appear to be 100% reliable.
Packit d28291
Packit d28291
Mar 10, 2003
Packit d28291
Brian Alliet contributed dynamic library support for MacOSX.  It could also
Packit d28291
use more testing.