> 1) NO STATIC VARIABLES! EVER! > 2) Don't hold on to pointers to memory across calls to routines that > might call the GC. > 3) Don't hold on to pointers to allocated PMCs that aren't accessible > from the root set
I don't think the rule #2 and #3 can be achieved without systematic effort. In practice, GC can happen at any time. When I worked on JVM, we used something call references, which is pretty much Object**. The object pointer is almost always put on a per-thread object pointer stack. The C code always refer to the stack slot. The GC will scan the entire object pointer stack, which is considered as part of root set. Couple of macros will be very helpful. #define ENTER \ void* local_frame_start = current_thread->oop_stack #define LEAVE \ current_thread->oop_stack = local_frame_start #define DEREF(ref) \ (*ref) #define REF(o) \ (*current_thread->oop_stack++ = o, current_thread_oop_stack) For each object pointer type, there is a reference type. struct Object; typedef Object* ObjectPtr; typedef ObjectPtr* ObjectRef; Only references should be used for function calls. Pointers should be only used without function body, and should not be used cross function calls. This way, we don't have to worry about which functions may cause GC. Hong