On Monday, 21 April 2014 at 23:02:54 UTC, Walter Bright wrote:
There is JUST NO WAY that:
struct RefCount {
T* data;
int* count;
}
This is actually quite efficient compared to the standard
NSObject which uses a hashtable for refcounting:
http://www.opensource.apple.com/source/objc4/objc4-551.1/runtime/NSObject.mm
http://www.opensource.apple.com/source/objc4/objc4-551.1/runtime/llvm-DenseMap.h
This is how Core Foundation does it:
http://www.opensource.apple.com/source/CF/CF-855.11/CFRuntime.c
Pretty longwinded:
CFTypeRef CFRetain(CFTypeRef cf) {
if (NULL == cf) { CRSetCrashLogMessage("*** CFRetain() called
with NULL ***"); HALT; }
if (cf) __CFGenericAssertIsCF(cf);
return _CFRetain(cf, false);
}
static CFTypeRef _CFRetain(CFTypeRef cf, Boolean tryR) {
uint32_t cfinfo = *(uint32_t *)&(((CFRuntimeBase
*)cf)->_cfinfo);
if (cfinfo & 0x800000) { // custom ref counting for object
...stuff deleted…
refcount(+1, cf);
return cf;
}
…lots of stuff deleted…
return cf;
}