From 052bcd697a08286a8f416d78c5a38d19765ce952 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sat, 4 Sep 2021 03:41:57 +0200 Subject: [PATCH] sys: Move ARC functions to their own file, and add missing declarations --- objc_sys/src/bindings.rs | 6 ----- objc_sys/src/lib.rs | 20 +++------------- objc_sys/src/rc.rs | 51 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 objc_sys/src/rc.rs diff --git a/objc_sys/src/bindings.rs b/objc_sys/src/bindings.rs index 4332f39c3..d173182c2 100644 --- a/objc_sys/src/bindings.rs +++ b/objc_sys/src/bindings.rs @@ -376,12 +376,6 @@ extern "C" { extern "C" { pub fn imp_removeBlock(anImp: IMP) -> BOOL; } -extern "C" { - pub fn objc_loadWeak(location: *mut id) -> id; -} -extern "C" { - pub fn objc_storeWeak(location: *mut id, obj: id) -> id; -} extern "C" { pub fn objc_setAssociatedObject( object: id, diff --git a/objc_sys/src/lib.rs b/objc_sys/src/lib.rs index 547247fe0..f37d08885 100644 --- a/objc_sys/src/lib.rs +++ b/objc_sys/src/lib.rs @@ -16,16 +16,17 @@ extern crate std; -use core::ffi::c_void; use std::os::raw::{c_char, c_int, c_uint}; +mod bindings; mod constants; mod message; +mod rc; mod types; -mod bindings; pub use constants::*; pub use message::*; +pub use rc::*; pub use types::*; #[link(name = "objc", kind = "dylib")] @@ -92,9 +93,6 @@ extern "C" { pub fn objc_allocateProtocol(name: *const c_char) -> *mut objc_protocol; pub fn objc_registerProtocol(proto: *mut objc_protocol); - pub fn objc_autoreleasePoolPush() -> *mut c_void; - pub fn objc_autoreleasePoolPop(context: *mut c_void); - pub fn protocol_addMethodDescription( proto: *mut objc_protocol, name: *const objc_selector, @@ -125,16 +123,4 @@ extern "C" { pub fn method_getNumberOfArguments(method: *const objc_method) -> c_uint; pub fn method_setImplementation(method: *mut objc_method, imp: IMP) -> IMP; pub fn method_exchangeImplementations(m1: *mut objc_method, m2: *mut objc_method); - - pub fn objc_retain(obj: *mut objc_object) -> *mut objc_object; - pub fn objc_release(obj: *mut objc_object); - pub fn objc_autorelease(obj: *mut objc_object); - - pub fn objc_loadWeakRetained(location: *mut *mut objc_object) -> *mut objc_object; - pub fn objc_initWeak( - location: *mut *mut objc_object, - obj: *mut objc_object, - ) -> *mut objc_object; - pub fn objc_destroyWeak(location: *mut *mut objc_object); - pub fn objc_copyWeak(to: *mut *mut objc_object, from: *mut *mut objc_object); } diff --git a/objc_sys/src/rc.rs b/objc_sys/src/rc.rs new file mode 100644 index 000000000..3b9d60838 --- /dev/null +++ b/objc_sys/src/rc.rs @@ -0,0 +1,51 @@ +//! ARC functions. +//! +//! All available since macOS `10.7`. +//! +//! Defined in `objc-internal.h`, but is available in Clang's +//! [documentation][ARC] so these are safe to rely on. +//! +//! [ARC]: https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support> +use core::ffi::c_void; + +use crate::objc_object; + +extern "C" { + // Autorelease + + pub fn objc_autorelease(value: *mut objc_object) -> *mut objc_object; + pub fn objc_autoreleasePoolPop(pool: *mut c_void); + pub fn objc_autoreleasePoolPush() -> *mut c_void; + pub fn objc_autoreleaseReturnValue(value: *mut objc_object) -> *mut objc_object; + + // Weak pointers + + pub fn objc_copyWeak(to: *mut *mut objc_object, from: *mut *mut objc_object); + pub fn objc_destroyWeak(location: *mut *mut objc_object); + pub fn objc_initWeak( + location: *mut *mut objc_object, + value: *mut objc_object, + ) -> *mut objc_object; + // Defined in runtime.h + pub fn objc_loadWeak(location: *mut *mut objc_object) -> *mut objc_object; + pub fn objc_loadWeakRetained(location: *mut *mut objc_object) -> *mut objc_object; + pub fn objc_moveWeak(to: *mut *mut objc_object, from: *mut *mut objc_object); + + // Retain / release + + pub fn objc_release(value: *mut objc_object); + pub fn objc_retain(value: *mut objc_object) -> *mut objc_object; + pub fn objc_retainAutorelease(value: *mut objc_object) -> *mut objc_object; + pub fn objc_retainAutoreleasedReturnValue(value: *mut objc_object) -> *mut objc_object; + // Defined in objc-abi.h + pub fn objc_retainBlock(value: *mut objc_object) -> *mut objc_object; + + // Storing values + + pub fn objc_storeStrong(location: *mut *mut objc_object, value: *mut objc_object); + // Defined in runtime.h + pub fn objc_storeWeak( + location: *mut *mut objc_object, + value: *mut objc_object, + ) -> *mut objc_object; +}