File tree 1 file changed +6
-13
lines changed
1 file changed +6
-13
lines changed Original file line number Diff line number Diff line change @@ -6,28 +6,21 @@ use std::num::NonZeroUsize;
6
6
const PROC_TASKINFO_SIZE : usize = mem:: size_of :: < libc:: proc_taskinfo > ( ) ;
7
7
8
8
pub ( crate ) fn num_threads ( ) -> Option < NonZeroUsize > {
9
- let buffer = unsafe { libc:: malloc ( PROC_TASKINFO_SIZE ) } ;
10
- if buffer. is_null ( ) {
11
- return None ;
12
- }
9
+ let mut pti: libc:: proc_taskinfo = unsafe { mem:: zeroed ( ) } ;
13
10
14
11
let result = unsafe {
15
12
libc:: proc_pidinfo (
16
13
libc:: getpid ( ) ,
17
14
libc:: PROC_PIDTASKINFO ,
18
15
0 ,
19
- buffer ,
16
+ & mut pti as * mut libc :: proc_taskinfo as * mut libc :: c_void ,
20
17
PROC_TASKINFO_SIZE as libc:: c_int ,
21
18
)
22
19
} ;
23
- if result != PROC_TASKINFO_SIZE as libc:: c_int {
24
- return None ;
25
- }
26
20
27
- let pti = buffer as * mut libc:: proc_taskinfo ;
28
- // Safety: `malloc`ed memory is aligned for repr(C) structs, so dereference is safe.
29
- let num_threads = NonZeroUsize :: new ( unsafe { pti . as_ref ( ) } ? . pti_threadnum as usize ) ;
21
+ if result == PROC_TASKINFO_SIZE as libc:: c_int {
22
+ return NonZeroUsize :: new ( pti . pti_threadnum as usize ) ;
23
+ }
30
24
31
- unsafe { libc:: free ( pti as * mut libc:: c_void ) } ;
32
- num_threads
25
+ None
33
26
}
You can’t perform that action at this time.
0 commit comments