[Gc] Weak pointers

S M Ryan wyrmwif at rawbw.com
Thu Mar 1 17:38:33 PST 2007


Does this look like a correct way to do weak pointers?

typedef void *Weak;
static inline Weak weakDef(void *objPtr);
static inline void *weakRef(Weak weakPtr);
GC_PTR weakRefLocked(GC_PTR weakPtr);

static inline Weak weakDef(void *objptr) {
	void **weakobj = GC_MALLOC_ATOMIC(sizeof(void*));
	*weakobj = objptr;
	GC_GENERAL_REGISTER_DISAPPEARING_LINK(weakobj,objptr);
	return weakobj;
}

static inline void *weakRef(Weak weakptr) {
	return GC_call_with_alloc_lock(weakRefLocked,weakptr);
}

GC_PTR weakRefLocked(GC_PTR weakPtr) {
	return *((void**)weakobj);
}


...
Obj *p = GC_MALLOC(sizeof(Obj));
Weak wp = weakDef(p);
...
Obj *q = weakRef(wp);
if (q) {
	access *q with a GC visible pointer
	on the protocol stack
}
...



More information about the Gc mailing list