| author | |
| committer | |
| log | 3b29d00c9826d8053b63fe2bcd86c6d1517fcc51 |
| tree | 391525ad33c6f60d9205535549fe4641fd333f62 |
| parent | 6377aad23a51ba6d69a01fafa60001e2238cde0f |
2 files changed, 38 insertions(+), 0 deletions(-)
lib/std/os/windows/kernel32.zig+3| ... | @@ -67,6 +67,7 @@ const RUNTIME_FUNCTION = windows.RUNTIME_FUNCTION; | ... | @@ -67,6 +67,7 @@ const RUNTIME_FUNCTION = windows.RUNTIME_FUNCTION; |
| 67 | const KNONVOLATILE_CONTEXT_POINTERS = windows.KNONVOLATILE_CONTEXT_POINTERS; | 67 | const KNONVOLATILE_CONTEXT_POINTERS = windows.KNONVOLATILE_CONTEXT_POINTERS; |
| 68 | const EXCEPTION_ROUTINE = windows.EXCEPTION_ROUTINE; | 68 | const EXCEPTION_ROUTINE = windows.EXCEPTION_ROUTINE; |
| 69 | const MODULEENTRY32 = windows.MODULEENTRY32; | 69 | const MODULEENTRY32 = windows.MODULEENTRY32; |
| 70 | const ULONGLONG = windows.ULONGLONG; | ||
| 70 | 71 | ||
| 71 | pub extern "kernel32" fn AddVectoredExceptionHandler(First: c_ulong, Handler: ?VECTORED_EXCEPTION_HANDLER) callconv(WINAPI) ?*anyopaque; | 72 | pub extern "kernel32" fn AddVectoredExceptionHandler(First: c_ulong, Handler: ?VECTORED_EXCEPTION_HANDLER) callconv(WINAPI) ?*anyopaque; |
| 72 | pub extern "kernel32" fn RemoveVectoredExceptionHandler(Handle: HANDLE) callconv(WINAPI) c_ulong; | 73 | pub extern "kernel32" fn RemoveVectoredExceptionHandler(Handle: HANDLE) callconv(WINAPI) c_ulong; |
| ... | @@ -457,3 +458,5 @@ pub extern "kernel32" fn RegOpenKeyExW( | ... | @@ -457,3 +458,5 @@ pub extern "kernel32" fn RegOpenKeyExW( |
| 457 | samDesired: REGSAM, | 458 | samDesired: REGSAM, |
| 458 | phkResult: *HKEY, | 459 | phkResult: *HKEY, |
| 459 | ) callconv(WINAPI) LSTATUS; | 460 | ) callconv(WINAPI) LSTATUS; |
| 461 | |||
| 462 | pub extern "kernel32" fn GetPhysicallyInstalledSystemMemory(TotalMemoryInKilobytes: *ULONGLONG) BOOL; |
lib/std/process.zig+35| ... | @@ -1169,3 +1169,38 @@ pub fn execve( | ... | @@ -1169,3 +1169,38 @@ pub fn execve( |
| 1169 | 1169 | ||
| 1170 | return os.execvpeZ_expandArg0(.no_expand, argv_buf.ptr[0].?, argv_buf.ptr, envp); | 1170 | return os.execvpeZ_expandArg0(.no_expand, argv_buf.ptr[0].?, argv_buf.ptr, envp); |
| 1171 | } | 1171 | } |
| 1172 | |||
| 1173 | pub const TotalSystemMemoryError = error{ | ||
| 1174 | UnknownTotalSystemMemory, | ||
| 1175 | }; | ||
| 1176 | |||
| 1177 | /// Returns the total system memory, in bytes. | ||
| 1178 | pub fn totalSystemMemory() TotalSystemMemoryError!usize { | ||
| 1179 | switch (builtin.os.tag) { | ||
| 1180 | .linux => { | ||
| 1181 | return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory; | ||
| 1182 | }, | ||
| 1183 | .windows => { | ||
| 1184 | var kilobytes: std.os.windows.ULONGLONG = undefined; | ||
| 1185 | assert(std.os.windows.kernel32.GetPhysicallyInstalledSystemMemory(&kilobytes) == std.os.windows.TRUE); | ||
| 1186 | return kilobytes * 1024; | ||
| 1187 | }, | ||
| 1188 | else => return error.UnknownTotalSystemMemory, | ||
| 1189 | } | ||
| 1190 | } | ||
| 1191 | |||
| 1192 | fn totalSystemMemoryLinux() !usize { | ||
| 1193 | var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{}); | ||
| 1194 | defer file.close(); | ||
| 1195 | var buf: [50]u8 = undefined; | ||
| 1196 | const amt = try file.read(&buf); | ||
| 1197 | if (amt != 50) return error.Unexpected; | ||
| 1198 | var it = std.mem.tokenize(u8, buf[0..amt], " \n"); | ||
| 1199 | const label = it.next().?; | ||
| 1200 | if (!std.mem.eql(u8, label, "MemTotal:")) return error.Unexpected; | ||
| 1201 | const int_text = it.next() orelse return error.Unexpected; | ||
| 1202 | const units = it.next() orelse return error.Unexpected; | ||
| 1203 | if (!std.mem.eql(u8, units, "kB")) return error.Unexpected; | ||
| 1204 | const kilobytes = try std.fmt.parseInt(usize, int_text, 10); | ||
| 1205 | return kilobytes * 1024; | ||
| 1206 | } |