Weak refs were being left as dangling pointers after being deleted. The
load that caused the deallocation would return nil, but then the next
one would dereference a dangling pointer.
Fixes a bug introduced (replacing a similar bug) in the last rewrite of
objc_moveWeak. This version should now be correct.
objc_copyWeak was implemented in a naive way, which had a lot more
overhead than required.
Rather than tracking all of the locations of weak pointers, keep a weak
refcount. This should also make it easier to add finer-grained locking to weak
references.
We were not correctly checking that the object being assigned had a
reference count in the object header. We're also now avoiding assigning
small objects to the weak reference table, which is quite important as
they have no destructor.
Fixes#12
the table incorrectly, causing objects inserted at offsets due to hash
collisions to fail to be moved up and then fail to be found later.
Test by Eric Wasylishen!
- Make objc_retain() check for stack blocks and implicitly copy them. This fixes the case where a block pointer is stored in an object-typed variable and is assigned.
- Tweak objc_retainAutoreleaseReturnValue() so that the ARC optimiser doesn't detect that the body looks like a call to objc_retainAutoreleaseReturnValue() and replace it with an infinitely recursive call.
The second fix now means that arc.m will compile with clang without producing a function that calls itself.
In a simple example:
- foo { return self; }
void someFunction(void)
{
id a = foo;
...
}
In ARC mode, this expands to:
- foo { return objc_retainAutoreleaseReturnValue(self); }
void someFunction(void)
{
id a = objc_retainAutoreleasedReturnValue(foo);
...
}
In the slow path, this is equivalent to:
- foo { return [[self retain] autorelease]; }
void someFunction(void)
{
id a = [foo retain];
...
[a release];
}
The fast path skips the autorelease / retain pair. The return value is stored
in thread-local storage temporarily and then retrieved, the retain balancing
out the autorelease.
This gives a 50% speedup on a single thread. It also avoids some atomic
operations.
Weak references are still not supported, but code that doesn't use them (i.e. any code that wants to be compatible with OS X 10.6) will work fine.
The current implementation is VERY inefficient and has a large number of missed optimisation opportunities: this is the 'make it right' phase, and should be almost equivalent to explicit retain / release code.