Skip to content

Commit

Permalink
Wrap Apple disk refreshing in autorelease pool to avoid system caching
Browse files Browse the repository at this point in the history
  • Loading branch information
complexspaces committed Aug 28, 2024
1 parent d0122ae commit 811701b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/unix/apple/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,23 @@ impl crate::DisksInner {
}

pub(crate) fn refresh_list(&mut self) {
struct AutoreleasePool {
ctx: *mut c_void,
}

impl Drop for AutoreleasePool {
fn drop(&mut self) {
// SAFETY: We have not manipulated `pool_ctx` since it was received from a corresponding
// pool push call.
unsafe { ffi::objc_autoreleasePoolPop(self.ctx) }
}
}

unsafe {
// SAFETY: Creating a new pool is safe in any context. They can be arbitrarily nested
// as long as pool objects are not used in deeper layers, but we only have one and don't
// allow it to leave this scope.
let _refresh_pool = AutoreleasePool { ctx: ffi::objc_autoreleasePoolPush() };
get_list(&mut self.disks);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/unix/apple/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cfg_if! {
array::CFArrayRef, dictionary::CFDictionaryRef, error::CFErrorRef, string::CFStringRef,
url::CFURLRef,
};
use std::ffi::c_void;

#[link(name = "CoreFoundation", kind = "framework")]
extern "C" {
Expand All @@ -32,6 +33,12 @@ cfg_if! {
pub static kCFURLVolumeIsInternalKey: CFStringRef;
pub static kCFURLVolumeIsBrowsableKey: CFStringRef;
}

#[link(name = "objc", kind = "dylib")]
extern "C" {
pub fn objc_autoreleasePoolPop(pool: *mut c_void);
pub fn objc_autoreleasePoolPush() -> *mut c_void;
}
}
}

Expand Down

0 comments on commit 811701b

Please sign in to comment.