Skip to content

Commit 6f07cc9

Browse files
imomalievalexrp
authored andcommitted
add macOS handling for totalSystemMemory (#24903)
* add macos handling for totalSystemMemory * fix return type cast for .freebsd in totalSystemMemory * add handling for the whole Darwin family in totalSystemMemory
1 parent b257832 commit 6f07cc9

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/std/process.zig

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1762,7 +1762,20 @@ pub fn totalSystemMemory() TotalSystemMemoryError!u64 {
17621762
error.NameTooLong, error.UnknownName => unreachable,
17631763
else => return error.UnknownTotalSystemMemory,
17641764
};
1765-
return @as(usize, @intCast(physmem));
1765+
return @as(u64, @intCast(physmem));
1766+
},
1767+
// whole Darwin family
1768+
.driverkit, .ios, .macos, .tvos, .visionos, .watchos => {
1769+
// "hw.memsize" returns uint64_t
1770+
var physmem: u64 = undefined;
1771+
var len: usize = @sizeOf(u64);
1772+
posix.sysctlbynameZ("hw.memsize", &physmem, &len, null, 0) catch |err| switch (err) {
1773+
error.PermissionDenied => unreachable, // only when setting values,
1774+
error.SystemResources => unreachable, // memory already on the stack
1775+
error.UnknownName => unreachable, // constant, known good value
1776+
else => return error.UnknownTotalSystemMemory,
1777+
};
1778+
return physmem;
17661779
},
17671780
.openbsd => {
17681781
const mib: [2]c_int = [_]c_int{

0 commit comments

Comments
 (0)