Skip to content

Instantly share code, notes, and snippets.

@JensAyton
Last active September 28, 2015 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JensAyton/1385556 to your computer and use it in GitHub Desktop.
Save JensAyton/1385556 to your computer and use it in GitHub Desktop.
A trivial implementation of Objective-C ARC runtime support for the Cocotron, excluding weak references.
/* Copyright (c) 2011 Jens Ayton
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*
This is a minimal implementation of Objective-C automatic reference counting
runtime support. It's implemented in terms of Foundation facilities, which
is a layering violation, but is the simplest way to bootstrap ARC
functionality. Moving forward, responsibility for reference counting
semantics should be moved to the runtime, as in Mac OS X 10.7.
__weak pointer support is not implemented.
ARC runtime function semantics are documented at:
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime
*/
#import <Foundation/Foundation.h>
#import <Foundation/NSAutoreleasePool-private.h>
id objc_autorelease(id value) {
if (value != nil) NSAutorelease(value);
return value;
}
void objc_autoreleasePoolPop(void *pool) {
[(NSAutoreleasePool *)pool release];
}
void *objc_autoreleasePoolPush(void) {
return [[NSAutoreleasePool alloc] init];
}
id objc_autoreleaseReturnValue(id value) {
return objc_autorelease(value);
}
void objc_release(id value) {
[value release];
}
id objc_retain(id value) {
return [value retain];
}
id objc_retainAutorelease(id value) {
return objc_autorelease(objc_retain(value));
}
id objc_retainAutoreleaseReturnValue(id value) {
return objc_autoreleaseReturnValue(objc_retain(value));
}
id objc_retainBlock(id value) {
/*
Blocks aren't supported at the time of writing. This will do the right
thing when they are, but calling _Block_copy() or using attribute alias
would be more efficient.
*/
return [value copy];
}
/*
Note: at the time of writing, objc_storeStrong() is incorrectly documented
as returning value. The compiler never uses the return value, and it
returns void under Mac OS X.
*/
void objc_storeStrong(id *object, id value) __attribute__((nonnull (1)));
void objc_storeStrong(id *object, id value) {
id oldValue = *object;
if (oldValue == value) return;
/*
Atomic update: it is imperative that *object never holds a non-owning
reference. Ensuring that we have owning references to both the new and
the old value when the change is made ensures this on strongly-ordered
architextures like x86.
FIXME: do we need barriers for, say, PPC?
*/
value = objc_retain(value);
*object = value;
objc_release(oldValue);
}
// MARK: Weak reference support (unimplemented)
// void objc_copyWeak(id *dest, id *src)
// void objc_destroyWeak(id *object)
// id objc_initWeak(id *object, id value)
// id objc_loadWeak(id *object)
// id objc_loadWeakRetained(id *object)
// void objc_moveWeak(id *dest, id *src)
// id objc_storeWeak(id *object, id value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment