22//
33// For the full copyright and license information, please view the LICENSE
44// file that was distributed with this source code.
5- // spell-checker:ignore (ToDO) hdsf ghead gtail ACDBK hexdigit
5+ // spell-checker:ignore (ToDO) hdsf ghead gtail ACDBK hexdigit memsize physmem
66
77//! Parser for sizes in SI or IEC units (multiples of 1000 or 1024 bytes).
88
@@ -18,7 +18,7 @@ use procfs::{Current, Meminfo};
1818enum SystemError {
1919 IOError ,
2020 ParseError ,
21- #[ cfg ( not ( target_os = "linux" ) ) ]
21+ #[ allow ( dead_code ) ]
2222 NotFound ,
2323}
2424
@@ -77,10 +77,76 @@ pub fn available_memory_bytes() -> Option<u128> {
7777 None
7878}
7979
80- /// Get the total number of bytes of physical memory.
80+ /// Get the total number of bytes of physical memory on macOS .
8181///
82- /// TODO Implement this for non-Linux systems.
83- #[ cfg( not( target_os = "linux" ) ) ]
82+ /// Uses the `sysctl` command to query `hw.memsize`.
83+ #[ cfg( target_os = "macos" ) ]
84+ fn total_physical_memory ( ) -> Result < u128 , SystemError > {
85+ let output = std:: process:: Command :: new ( "sysctl" )
86+ . arg ( "-n" )
87+ . arg ( "hw.memsize" )
88+ . output ( ) ?;
89+
90+ let mem_str = String :: from_utf8_lossy ( & output. stdout ) ;
91+ let mem_bytes: u128 = mem_str. trim ( ) . parse ( ) ?;
92+ Ok ( mem_bytes)
93+ }
94+
95+ /// Get the total number of bytes of physical memory on FreeBSD.
96+ ///
97+ /// Uses the `sysctl` command to query `hw.physmem`.
98+ #[ cfg( target_os = "freebsd" ) ]
99+ fn total_physical_memory ( ) -> Result < u128 , SystemError > {
100+ let output = std:: process:: Command :: new ( "sysctl" )
101+ . arg ( "-n" )
102+ . arg ( "hw.physmem" )
103+ . output ( ) ?;
104+
105+ let mem_str = String :: from_utf8_lossy ( & output. stdout ) ;
106+ let mem_bytes: u128 = mem_str. trim ( ) . parse ( ) ?;
107+ Ok ( mem_bytes)
108+ }
109+
110+ /// Get the total number of bytes of physical memory on OpenBSD.
111+ ///
112+ /// Uses the `sysctl` command to query `hw.physmem`.
113+ #[ cfg( target_os = "openbsd" ) ]
114+ fn total_physical_memory ( ) -> Result < u128 , SystemError > {
115+ let output = std:: process:: Command :: new ( "sysctl" )
116+ . arg ( "-n" )
117+ . arg ( "hw.physmem" )
118+ . output ( ) ?;
119+
120+ let mem_str = String :: from_utf8_lossy ( & output. stdout ) ;
121+ let mem_bytes: u128 = mem_str. trim ( ) . parse ( ) ?;
122+ Ok ( mem_bytes)
123+ }
124+
125+ /// Get the total number of bytes of physical memory on NetBSD.
126+ ///
127+ /// Uses the `sysctl` command to query `hw.physmem64`.
128+ #[ cfg( target_os = "netbsd" ) ]
129+ fn total_physical_memory ( ) -> Result < u128 , SystemError > {
130+ let output = std:: process:: Command :: new ( "sysctl" )
131+ . arg ( "-n" )
132+ . arg ( "hw.physmem64" )
133+ . output ( ) ?;
134+
135+ let mem_str = String :: from_utf8_lossy ( & output. stdout ) ;
136+ let mem_bytes: u128 = mem_str. trim ( ) . parse ( ) ?;
137+ Ok ( mem_bytes)
138+ }
139+
140+ /// Get the total number of bytes of physical memory (fallback for unsupported platforms).
141+ ///
142+ /// Returns an error for platforms where we haven't implemented memory detection yet.
143+ #[ cfg( not( any(
144+ target_os = "linux" ,
145+ target_os = "macos" ,
146+ target_os = "freebsd" ,
147+ target_os = "openbsd" ,
148+ target_os = "netbsd"
149+ ) ) ) ]
84150fn total_physical_memory ( ) -> Result < u128 , SystemError > {
85151 Err ( SystemError :: NotFound )
86152}
0 commit comments