-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This replicates the behaviour of @autoreleasepool blocks in Objective-C and allows higher performance creation of autorelease pools.
- Loading branch information
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::os::raw::c_void; | ||
use runtime::{objc_autoreleasePoolPush, objc_autoreleasePoolPop}; | ||
|
||
// we use a struct to ensure that objc_autoreleasePoolPop during unwinding. | ||
struct AutoReleaseHelper { | ||
context: *mut c_void, | ||
} | ||
|
||
impl AutoReleaseHelper { | ||
unsafe fn new() -> Self { | ||
AutoReleaseHelper { context: objc_autoreleasePoolPush() } | ||
} | ||
} | ||
|
||
impl Drop for AutoReleaseHelper { | ||
fn drop(&mut self) { | ||
unsafe { objc_autoreleasePoolPop(self.context) } | ||
} | ||
} | ||
|
||
/** | ||
Execute `f` in the context of a new autorelease pool. The pool is drained | ||
after the execution of `f` completes. This corresponds to @autoreleasepool blocks | ||
in Objective-c and Swift. | ||
*/ | ||
pub fn autoreleasepool<F: FnOnce()>(f: F) { | ||
let _context = unsafe { AutoReleaseHelper::new() }; | ||
f(); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::autoreleasepool; | ||
|
||
#[test] | ||
fn test_pool() { | ||
autoreleasepool(|| {}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters