Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ objc2-system-configuration = { version = "0.3", features = ["SCNetworkConfigurat
plist = "1.8"

[target.'cfg(target_os = "macos")'.dependencies]
objc2 = { version = "0.6" }
objc2-core-wlan = { version = "0.3" }
objc2-foundation = { version = "0.3" }

Expand Down
24 changes: 18 additions & 6 deletions src/os/macos/wifi.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
use objc2::rc::autoreleasepool;
use objc2_core_wlan::CWWiFiClient;
use objc2_foundation::NSString;

/// Returns the macOS Wi-Fi transmit rate in bps for the given interface name.
///
/// The CoreWLAN calls below produce autoreleased Obj-C/XPC objects
/// (`CWFRequestParameters`, `CWFInterface`, the `CWFXPCRequestProtocolCoreWLAN`
/// proxy). This is called from long-lived threads with no draining autorelease
/// pool (e.g. `netwatch`'s interface monitor re-enumerates on every network
/// event), so without an explicit pool those objects are added to a pool that
/// never drains and accumulate without bound — a steady multi-MB/hour macOS
/// leak. A scoped pool per call frees them immediately.
pub(crate) fn get_wifi_transmit_rate(iface_name: &str) -> Option<u64> {
let client = unsafe { CWWiFiClient::sharedWiFiClient() };
let name = NSString::from_str(iface_name);
autoreleasepool(|_pool| {
let client = unsafe { CWWiFiClient::sharedWiFiClient() };
let name = NSString::from_str(iface_name);

let wifi_iface = unsafe { client.interfaceWithName(Some(&name)) };
wifi_iface.map(|i| {
let transmit_rate = unsafe { i.transmitRate() };
return (transmit_rate * 1e6) as u64;
let wifi_iface = unsafe { client.interfaceWithName(Some(&name)) };
wifi_iface.map(|i| {
let transmit_rate = unsafe { i.transmitRate() };
(transmit_rate * 1e6) as u64
})
})
}

Loading