From 35a2cf26a7615a12bf439c1ed875258bcb86a9a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=BE=BE?= Date: Fri, 22 Sep 2023 16:35:10 +0800 Subject: [PATCH 1/7] add basic os&cpu information to C interface and example --- examples/simple.c | 12 +++ src/c_interface.rs | 177 +++++++++++++++++++++++++++++++++++++++++++-- src/sysinfo.h | 31 ++++++-- 3 files changed, 208 insertions(+), 12 deletions(-) diff --git a/examples/simple.c b/examples/simple.c index 0dc994869..f97ba720f 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -63,6 +63,18 @@ int main() { sysinfo_refresh_all(system); sysinfo_networks_refresh_list(networks); + printf("os name: %s\n", sysinfo_system_name(system)); + printf("os version: %s\n", sysinfo_system_version(system)); + printf("kernel version: %s\n", sysinfo_system_kernel_version(system)); + printf("long os version: %s\n", sysinfo_system_long_version(system)); + printf("host name: %s\n", sysinfo_system_host_name(system)); + + printf("cpu vendor id: %s\n", sysinfo_cpu_vendor_id(system)); + printf("cpu brand: %s\n", sysinfo_cpu_brand(system)); + printf("cpu frequency: %ld\n", sysinfo_cpu_frequency(system)); + printf("cpu cores: %d\n", sysinfo_cpu_physical_cores(system)); + + printf("total memory: %ld\n", sysinfo_total_memory(system)); printf("free memory: %ld\n", sysinfo_free_memory(system)); printf("used memory: %ld\n", sysinfo_used_memory(system)); diff --git a/src/c_interface.rs b/src/c_interface.rs index 2461bcfb4..c1aea52f5 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -4,10 +4,18 @@ use crate::{ CpuExt, Disks, DisksExt, NetworkExt, Networks, NetworksExt, Pid, Process, ProcessExt, System, SystemExt, }; -use libc::{self, c_char, c_float, c_uint, c_void, pid_t, size_t}; +use libc::{self, c_char, c_float, c_uint, c_void, size_t}; use std::borrow::BorrowMut; use std::ffi::CString; +/// on windows, libc has not include pid_t. +#[cfg(target_os = "windows")] +pub type PID = usize; + +/// other platforms, use libc::pid_t +#[cfg(not(target_os = "windows"))] +pub type PID = libc::pid_t; + /// Equivalent of [`System`][crate::System] struct. pub type CSystem = *mut c_void; /// Equivalent of [`Process`][crate::Process] struct. @@ -15,7 +23,7 @@ pub type CProcess = *const c_void; /// C string returned from `CString::into_raw`. pub type RString = *const c_char; /// Callback used by [`processes`][crate::System#method.processes]. -pub type ProcessLoop = extern "C" fn(pid: pid_t, process: CProcess, data: *mut c_void) -> bool; +pub type ProcessLoop = extern "C" fn(pid: PID, process: CProcess, data: *mut c_void) -> bool; /// Equivalent of [`Networks`][crate::Networks] struct. pub type CNetworks = *mut c_void; /// Equivalent of [`Disks`][crate::Disks] struct. @@ -110,7 +118,7 @@ pub extern "C" fn sysinfo_refresh_processes(system: CSystem) { /// Equivalent of [`System::refresh_process()`][crate::System#method.refresh_process]. #[cfg(target_os = "linux")] #[no_mangle] -pub extern "C" fn sysinfo_refresh_process(system: CSystem, pid: pid_t) { +pub extern "C" fn sysinfo_refresh_process(system: CSystem, pid: PID) { assert!(!system.is_null()); unsafe { let mut system: Box = Box::from_raw(system as *mut System); @@ -381,7 +389,7 @@ pub extern "C" fn sysinfo_processes( /// While having this method returned process, you should *never* call any /// refresh method! #[no_mangle] -pub extern "C" fn sysinfo_process_by_pid(system: CSystem, pid: pid_t) -> CProcess { +pub extern "C" fn sysinfo_process_by_pid(system: CSystem, pid: PID) -> CProcess { assert!(!system.is_null()); unsafe { let system: Box = Box::from_raw(system as *mut System); @@ -425,7 +433,7 @@ pub extern "C" fn sysinfo_process_tasks( /// Equivalent of [`Process::pid()`][crate::Process#method.pid]. #[no_mangle] -pub extern "C" fn sysinfo_process_pid(process: CProcess) -> pid_t { +pub extern "C" fn sysinfo_process_pid(process: CProcess) -> PID { assert!(!process.is_null()); let process = process as *const Process; unsafe { (*process).pid().0 } @@ -435,7 +443,7 @@ pub extern "C" fn sysinfo_process_pid(process: CProcess) -> pid_t { /// /// In case there is no known parent, it returns `0`. #[no_mangle] -pub extern "C" fn sysinfo_process_parent_pid(process: CProcess) -> pid_t { +pub extern "C" fn sysinfo_process_parent_pid(process: CProcess) -> PID { assert!(!process.is_null()); let process = process as *const Process; unsafe { (*process).parent().unwrap_or(Pid(0)).0 } @@ -519,3 +527,160 @@ pub extern "C" fn sysinfo_rstring_free(s: RString) { } } } + +/// Equivalent of [`cpu::vendor_id()`]. +#[no_mangle] +pub extern "C" fn sysinfo_cpu_vendor_id(system: CSystem) -> RString { + assert!(!system.is_null()); + unsafe { + let mut system: Box = Box::from_raw(system as *mut System); + system.refresh_cpu(); + + let cpu = system.cpus().first().unwrap(); + if let Ok(c) = CString::new(cpu.vendor_id()) { + Box::into_raw(system); + return c.into_raw() as _; + } + Box::into_raw(system); + std::ptr::null() + } +} + +/// Equivalent of [`cpu::brand()`]. +#[no_mangle] +pub extern "C" fn sysinfo_cpu_brand(system: CSystem) -> RString { + assert!(!system.is_null()); + unsafe { + let mut system: Box = Box::from_raw(system as *mut System); + system.refresh_cpu(); + + let cpu = system.cpus().first().unwrap(); + if let Ok(c) = CString::new(cpu.brand()) { + Box::into_raw(system); + return c.into_raw() as _; + } + + Box::into_raw(system); + std::ptr::null() + } +} + +/// Equivalent of [`system::physical_core_count()`]. +#[no_mangle] +pub extern "C" fn sysinfo_cpu_physical_cores(system: CSystem) -> u32 { + assert!(!system.is_null()); + unsafe { + let system: Box = Box::from_raw(system as *mut System); + if let Some(c) = system.physical_core_count() { + Box::into_raw(system); + return c as u32; + } + Box::into_raw(system); + 0 + } +} + +/// Equivalent of [`cpu::frequency()`]. +#[no_mangle] +pub extern "C" fn sysinfo_cpu_frequency(system: CSystem) -> u64 { + assert!(!system.is_null()); + unsafe { + let mut system: Box = Box::from_raw(system as *mut System); + system.refresh_cpu(); + let cpu = system.cpus().first().unwrap(); + let ret = cpu.frequency() as u64; + Box::into_raw(system); + + ret + } +} + +/// Equivalent of [`System::name()`][crate::System#method.name]. +#[no_mangle] +pub extern "C" fn sysinfo_system_name(system: CSystem) -> RString { + assert!(!system.is_null()); + unsafe { + let system: Box = Box::from_raw(system as *mut System); + if let Some(p) = system.name() { + if let Ok(c) = CString::new(p) { + Box::into_raw(system); + return c.into_raw() as _; + } + } + + Box::into_raw(system); + std::ptr::null() + } +} + +/// Equivalent of [`System::version()`][crate::System#method.version]. +#[no_mangle] +pub extern "C" fn sysinfo_system_version(system: CSystem) -> RString { + assert!(!system.is_null()); + unsafe { + let system: Box = Box::from_raw(system as *mut System); + if let Some(p) = system.os_version() { + if let Ok(c) = CString::new(p) { + Box::into_raw(system); + return c.into_raw() as _; + } + } + + Box::into_raw(system); + std::ptr::null() + } +} + +/// Equivalent of [`System::kernel_version()`][crate::System#method.kernel_version]. +#[no_mangle] +pub extern "C" fn sysinfo_system_kernel_version(system: CSystem) -> RString { + assert!(!system.is_null()); + unsafe { + let system: Box = Box::from_raw(system as *mut System); + + if let Some(p) = system.kernel_version() { + if let Ok(c) = CString::new(p) { + Box::into_raw(system); + return c.into_raw() as _; + } + } + + Box::into_raw(system); + std::ptr::null() + } +} + +/// Equivalent of [`System::host_name()`][crate::System#method.host_name]. +#[no_mangle] +pub extern "C" fn sysinfo_system_host_name(system: CSystem) -> RString { + assert!(!system.is_null()); + unsafe { + let system: Box = Box::from_raw(system as *mut System); + if let Some(p) = system.host_name() { + if let Ok(c) = CString::new(p) { + Box::into_raw(system); + return c.into_raw() as _; + } + } + Box::into_raw(system); + std::ptr::null() + } +} + +/// Equivalent of [`System::long_os_version()`][crate::System#method.long_os_version]. +#[no_mangle] +pub extern "C" fn sysinfo_system_long_version(system: CSystem) -> RString { + assert!(!system.is_null()); + unsafe { + let system: Box = Box::from_raw(system as *mut System); + if let Some(p) = system.long_os_version() { + if let Ok(c) = CString::new(p) { + Box::into_raw(system); + return c.into_raw() as _; + } + } + + Box::into_raw(system); + std::ptr::null() + } +} diff --git a/src/sysinfo.h b/src/sysinfo.h index b20d4b4d2..75f5f4c0a 100644 --- a/src/sysinfo.h +++ b/src/sysinfo.h @@ -4,6 +4,7 @@ #include #include +#include typedef void* CSystem; typedef const void* CProcess; @@ -11,6 +12,13 @@ typedef const char* RString; typedef void* CNetworks; typedef void* CDisks; +#ifdef WIN32 +typedef uint32_t PID; +#else +typedef pid_t PID; +#endif + + CSystem sysinfo_init(void); void sysinfo_destroy(CSystem system); CNetworks sysinfo_networks_init(void); @@ -22,7 +30,7 @@ void sysinfo_refresh_cpu(CSystem system); void sysinfo_refresh_all(CSystem system); void sysinfo_refresh_processes(CSystem system); #ifdef __linux__ -void sysinfo_refresh_process(CSystem system, pid_t pid); +void sysinfo_refresh_process(CSystem system, PID pid); #endif @@ -40,15 +48,15 @@ size_t sysinfo_used_swap(CSystem system); void sysinfo_cpus_usage(CSystem system, unsigned int *length, float **cpus); -size_t sysinfo_processes(CSystem system, bool (*fn_pointer)(pid_t, CProcess, void*), +size_t sysinfo_processes(CSystem system, bool (*fn_pointer)(PID, CProcess, void*), void *data); #ifdef __linux__ -size_t sysinfo_process_tasks(CProcess process, bool (*fn_pointer)(pid_t, CProcess, void*), +size_t sysinfo_process_tasks(CProcess process, bool (*fn_pointer)(PID, CProcess, void*), void *data); #endif -CProcess sysinfo_process_by_pid(CSystem system, pid_t pid); -pid_t sysinfo_process_pid(CProcess process); -pid_t sysinfo_process_parent_pid(CProcess process); +CProcess sysinfo_process_by_pid(CSystem system, PID pid); +PID sysinfo_process_pid(CProcess process); +PID sysinfo_process_parent_pid(CProcess process); float sysinfo_process_cpu_usage(CProcess process); size_t sysinfo_process_memory(CProcess process); size_t sysinfo_process_virtual_memory(CProcess process); @@ -60,4 +68,15 @@ void sysinfo_networks_refresh(CNetworks networks); size_t sysinfo_networks_received(CNetworks networks); size_t sysinfo_networks_transmitted(CNetworks networks); +RString sysinfo_cpu_vendor_id(CSystem system); +RString sysinfo_cpu_brand(CSystem system); +uint64_t sysinfo_cpu_frequency(CSystem system); +uint32_t sysinfo_cpu_physical_cores(CSystem system); +RString sysinfo_system_name(CSystem system); +RString sysinfo_system_kernel_version(CSystem system); +RString sysinfo_system_version(CSystem system); +RString sysinfo_system_host_name(CSystem system); +RString sysinfo_system_long_version(CSystem system); + void sysinfo_rstring_free(RString str); + From 5ac7b6ac3abca267e6c89c3de8a614d47bd171a4 Mon Sep 17 00:00:00 2001 From: TigerLau1985 Date: Fri, 22 Sep 2023 17:28:12 +0800 Subject: [PATCH 2/7] Use `and_then` to make more elegant Co-authored-by: Guillaume Gomez --- src/c_interface.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/c_interface.rs b/src/c_interface.rs index c1aea52f5..c2cc2958c 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -536,13 +536,13 @@ pub extern "C" fn sysinfo_cpu_vendor_id(system: CSystem) -> RString { let mut system: Box = Box::from_raw(system as *mut System); system.refresh_cpu(); - let cpu = system.cpus().first().unwrap(); - if let Ok(c) = CString::new(cpu.vendor_id()) { - Box::into_raw(system); - return c.into_raw() as _; - } + let c_string = if let Some(c) = system.cpus().first().and_then(|cpu| CString::new(cpu.vendor_id()).ok()) { + c.into_raw() as RString + } else { + std::ptr::null() + }; Box::into_raw(system); - std::ptr::null() + c_string } } From fd3b569d32725772978817fe702180a2a02ed1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=BE=BE?= Date: Fri, 22 Sep 2023 21:02:38 +0800 Subject: [PATCH 3/7] use `and_then`&`map` handle option item. --- src/c_interface.rs | 118 ++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/src/c_interface.rs b/src/c_interface.rs index c2cc2958c..7ffed6f04 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -14,7 +14,7 @@ pub type PID = usize; /// other platforms, use libc::pid_t #[cfg(not(target_os = "windows"))] -pub type PID = libc::pid_t; +pub type PID = libc::pid_t; /// Equivalent of [`System`][crate::System] struct. pub type CSystem = *mut c_void; @@ -535,8 +535,11 @@ pub extern "C" fn sysinfo_cpu_vendor_id(system: CSystem) -> RString { unsafe { let mut system: Box = Box::from_raw(system as *mut System); system.refresh_cpu(); - - let c_string = if let Some(c) = system.cpus().first().and_then(|cpu| CString::new(cpu.vendor_id()).ok()) { + let c_string = if let Some(c) = system + .cpus() + .first() + .and_then(|cpu| CString::new(cpu.vendor_id()).ok()) + { c.into_raw() as RString } else { std::ptr::null() @@ -553,15 +556,17 @@ pub extern "C" fn sysinfo_cpu_brand(system: CSystem) -> RString { unsafe { let mut system: Box = Box::from_raw(system as *mut System); system.refresh_cpu(); - - let cpu = system.cpus().first().unwrap(); - if let Ok(c) = CString::new(cpu.brand()) { - Box::into_raw(system); - return c.into_raw() as _; - } - + let c_string = if let Some(c) = system + .cpus() + .first() + .and_then(|cpu| CString::new(cpu.brand()).ok()) + { + c.into_raw() as RString + } else { + std::ptr::null() + }; Box::into_raw(system); - std::ptr::null() + c_string } } @@ -571,12 +576,13 @@ pub extern "C" fn sysinfo_cpu_physical_cores(system: CSystem) -> u32 { assert!(!system.is_null()); unsafe { let system: Box = Box::from_raw(system as *mut System); - if let Some(c) = system.physical_core_count() { - Box::into_raw(system); - return c as u32; - } + let count = if let Some(c) = system.physical_core_count() { + c + } else { + 0 + }; Box::into_raw(system); - 0 + count as u32 } } @@ -587,11 +593,13 @@ pub extern "C" fn sysinfo_cpu_frequency(system: CSystem) -> u64 { unsafe { let mut system: Box = Box::from_raw(system as *mut System); system.refresh_cpu(); - let cpu = system.cpus().first().unwrap(); - let ret = cpu.frequency() as u64; + let freq = if let Some(f) = system.cpus().first().map(|cpu| cpu.frequency()) { + f + } else { + 0 + }; Box::into_raw(system); - - ret + freq } } @@ -601,15 +609,13 @@ pub extern "C" fn sysinfo_system_name(system: CSystem) -> RString { assert!(!system.is_null()); unsafe { let system: Box = Box::from_raw(system as *mut System); - if let Some(p) = system.name() { - if let Ok(c) = CString::new(p) { - Box::into_raw(system); - return c.into_raw() as _; - } - } - + let c_string = if let Some(c) = system.name().and_then(|p| CString::new(p).ok()) { + c.into_raw() as _ + } else { + std::ptr::null() + }; Box::into_raw(system); - std::ptr::null() + c_string } } @@ -619,15 +625,13 @@ pub extern "C" fn sysinfo_system_version(system: CSystem) -> RString { assert!(!system.is_null()); unsafe { let system: Box = Box::from_raw(system as *mut System); - if let Some(p) = system.os_version() { - if let Ok(c) = CString::new(p) { - Box::into_raw(system); - return c.into_raw() as _; - } - } - + let c_string = if let Some(c) = system.os_version().and_then(|c| CString::new(c).ok()) { + c.into_raw() as _ + } else { + std::ptr::null() + }; Box::into_raw(system); - std::ptr::null() + c_string } } @@ -637,16 +641,14 @@ pub extern "C" fn sysinfo_system_kernel_version(system: CSystem) -> RString { assert!(!system.is_null()); unsafe { let system: Box = Box::from_raw(system as *mut System); - - if let Some(p) = system.kernel_version() { - if let Ok(c) = CString::new(p) { - Box::into_raw(system); - return c.into_raw() as _; - } - } + let c_string = if let Some(c) = system.kernel_version().and_then(|c| CString::new(c).ok()) { + c.into_raw() as _ + } else { + std::ptr::null() + }; Box::into_raw(system); - std::ptr::null() + c_string } } @@ -656,14 +658,13 @@ pub extern "C" fn sysinfo_system_host_name(system: CSystem) -> RString { assert!(!system.is_null()); unsafe { let system: Box = Box::from_raw(system as *mut System); - if let Some(p) = system.host_name() { - if let Ok(c) = CString::new(p) { - Box::into_raw(system); - return c.into_raw() as _; - } - } + let c_string = if let Some(c) = system.host_name().and_then(|c| CString::new(c).ok()) { + c.into_raw() as _ + } else { + std::ptr::null() + }; Box::into_raw(system); - std::ptr::null() + c_string } } @@ -673,14 +674,13 @@ pub extern "C" fn sysinfo_system_long_version(system: CSystem) -> RString { assert!(!system.is_null()); unsafe { let system: Box = Box::from_raw(system as *mut System); - if let Some(p) = system.long_os_version() { - if let Ok(c) = CString::new(p) { - Box::into_raw(system); - return c.into_raw() as _; - } - } - + let c_string = if let Some(c) = system.long_os_version().and_then(|c| CString::new(c).ok()) + { + return c.into_raw() as _; + } else { + std::ptr::null() + }; Box::into_raw(system); - std::ptr::null() + c_string } } From f9af18c937ab48fae8a5d6277cf2fc7a489169af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=BE=BE?= Date: Fri, 22 Sep 2023 21:09:51 +0800 Subject: [PATCH 4/7] use size_t but not uint32_t. --- src/sysinfo.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sysinfo.h b/src/sysinfo.h index 75f5f4c0a..627e24585 100644 --- a/src/sysinfo.h +++ b/src/sysinfo.h @@ -13,7 +13,7 @@ typedef void* CNetworks; typedef void* CDisks; #ifdef WIN32 -typedef uint32_t PID; +typedef size_t PID; #else typedef pid_t PID; #endif From 831bec20e655aa649192af586cc2222331a9bb71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=BE=BE?= Date: Mon, 25 Sep 2023 10:29:25 +0800 Subject: [PATCH 5/7] fixed: code style. --- examples/simple.c | 3 --- src/c_interface.rs | 15 +++------------ src/sysinfo.h | 9 ++++----- 3 files changed, 7 insertions(+), 20 deletions(-) diff --git a/examples/simple.c b/examples/simple.c index f97ba720f..6f4777c0a 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -68,13 +68,10 @@ int main() { printf("kernel version: %s\n", sysinfo_system_kernel_version(system)); printf("long os version: %s\n", sysinfo_system_long_version(system)); printf("host name: %s\n", sysinfo_system_host_name(system)); - printf("cpu vendor id: %s\n", sysinfo_cpu_vendor_id(system)); printf("cpu brand: %s\n", sysinfo_cpu_brand(system)); printf("cpu frequency: %ld\n", sysinfo_cpu_frequency(system)); printf("cpu cores: %d\n", sysinfo_cpu_physical_cores(system)); - - printf("total memory: %ld\n", sysinfo_total_memory(system)); printf("free memory: %ld\n", sysinfo_free_memory(system)); printf("used memory: %ld\n", sysinfo_used_memory(system)); diff --git a/src/c_interface.rs b/src/c_interface.rs index 7ffed6f04..028e6c827 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -576,11 +576,7 @@ pub extern "C" fn sysinfo_cpu_physical_cores(system: CSystem) -> u32 { assert!(!system.is_null()); unsafe { let system: Box = Box::from_raw(system as *mut System); - let count = if let Some(c) = system.physical_core_count() { - c - } else { - 0 - }; + let count = system.physical_core_count().unwrap_or(0); Box::into_raw(system); count as u32 } @@ -591,13 +587,8 @@ pub extern "C" fn sysinfo_cpu_physical_cores(system: CSystem) -> u32 { pub extern "C" fn sysinfo_cpu_frequency(system: CSystem) -> u64 { assert!(!system.is_null()); unsafe { - let mut system: Box = Box::from_raw(system as *mut System); - system.refresh_cpu(); - let freq = if let Some(f) = system.cpus().first().map(|cpu| cpu.frequency()) { - f - } else { - 0 - }; + let system: Box = Box::from_raw(system as *mut System); + let freq = system.cpus().first().map(|cpu| cpu.frequency()).unwrap_or(0); Box::into_raw(system); freq } diff --git a/src/sysinfo.h b/src/sysinfo.h index 627e24585..8f4b9b8cd 100644 --- a/src/sysinfo.h +++ b/src/sysinfo.h @@ -33,7 +33,6 @@ void sysinfo_refresh_processes(CSystem system); void sysinfo_refresh_process(CSystem system, PID pid); #endif - CDisks sysinfo_disks_init(void); void sysinfo_disks_destroy(CDisks disks); void sysinfo_disks_refresh(CDisks disks); @@ -55,8 +54,8 @@ size_t sysinfo_process_tasks(CProcess process, bool (*fn_pointer)(PID, CPro void *data); #endif CProcess sysinfo_process_by_pid(CSystem system, PID pid); -PID sysinfo_process_pid(CProcess process); -PID sysinfo_process_parent_pid(CProcess process); +PID sysinfo_process_pid(CProcess process); +PID sysinfo_process_parent_pid(CProcess process); float sysinfo_process_cpu_usage(CProcess process); size_t sysinfo_process_memory(CProcess process); size_t sysinfo_process_virtual_memory(CProcess process); @@ -72,11 +71,11 @@ RString sysinfo_cpu_vendor_id(CSystem system); RString sysinfo_cpu_brand(CSystem system); uint64_t sysinfo_cpu_frequency(CSystem system); uint32_t sysinfo_cpu_physical_cores(CSystem system); + RString sysinfo_system_name(CSystem system); RString sysinfo_system_kernel_version(CSystem system); RString sysinfo_system_version(CSystem system); RString sysinfo_system_host_name(CSystem system); RString sysinfo_system_long_version(CSystem system); -void sysinfo_rstring_free(RString str); - +void sysinfo_rstring_free(RString str); \ No newline at end of file From 75b98a762de8a360bfc3e1d0dd5f6b4c5e6cddfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=BE=BE?= Date: Mon, 25 Sep 2023 10:38:32 +0800 Subject: [PATCH 6/7] remove redundant CPU refresh. --- src/c_interface.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/c_interface.rs b/src/c_interface.rs index 028e6c827..33c91b4c4 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -533,8 +533,7 @@ pub extern "C" fn sysinfo_rstring_free(s: RString) { pub extern "C" fn sysinfo_cpu_vendor_id(system: CSystem) -> RString { assert!(!system.is_null()); unsafe { - let mut system: Box = Box::from_raw(system as *mut System); - system.refresh_cpu(); + let system: Box = Box::from_raw(system as *mut System); let c_string = if let Some(c) = system .cpus() .first() @@ -554,8 +553,7 @@ pub extern "C" fn sysinfo_cpu_vendor_id(system: CSystem) -> RString { pub extern "C" fn sysinfo_cpu_brand(system: CSystem) -> RString { assert!(!system.is_null()); unsafe { - let mut system: Box = Box::from_raw(system as *mut System); - system.refresh_cpu(); + let system: Box = Box::from_raw(system as *mut System); let c_string = if let Some(c) = system .cpus() .first() From f35bba3be7b70db171d6534ed17242a3abdfce8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=BE=BE?= Date: Mon, 25 Sep 2023 10:42:25 +0800 Subject: [PATCH 7/7] format with rustfmt --- src/c_interface.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/c_interface.rs b/src/c_interface.rs index 33c91b4c4..5cab028fc 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -586,7 +586,11 @@ pub extern "C" fn sysinfo_cpu_frequency(system: CSystem) -> u64 { assert!(!system.is_null()); unsafe { let system: Box = Box::from_raw(system as *mut System); - let freq = system.cpus().first().map(|cpu| cpu.frequency()).unwrap_or(0); + let freq = system + .cpus() + .first() + .map(|cpu| cpu.frequency()) + .unwrap_or(0); Box::into_raw(system); freq } @@ -665,7 +669,7 @@ pub extern "C" fn sysinfo_system_long_version(system: CSystem) -> RString { let system: Box = Box::from_raw(system as *mut System); let c_string = if let Some(c) = system.long_os_version().and_then(|c| CString::new(c).ok()) { - return c.into_raw() as _; + c.into_raw() as _ } else { std::ptr::null() };