| ... | @@ -1522,8 +1522,11 @@ pub const TotalSystemMemoryError = error{ | ... | @@ -1522,8 +1522,11 @@ pub const TotalSystemMemoryError = error{ |
| 1522 | UnknownTotalSystemMemory, | 1522 | UnknownTotalSystemMemory, |
| 1523 | }; | 1523 | }; |
| 1524 | | 1524 | |
| 1525 | /// Returns the total system memory, in bytes. | 1525 | /// Returns the total system memory, in bytes as a u64. |
| 1526 | pub fn totalSystemMemory() TotalSystemMemoryError!usize { | 1526 | /// We return a u64 instead of usize due to PAE on ARM |
| | 1527 | /// and Linux's /proc/meminfo reporting more memory when |
| | 1528 | /// using QEMU user mode emulation. |
| | 1529 | pub fn totalSystemMemory() TotalSystemMemoryError!u64 { |
| 1527 | switch (builtin.os.tag) { | 1530 | switch (builtin.os.tag) { |
| 1528 | .linux => { | 1531 | .linux => { |
| 1529 | return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory; | 1532 | return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory; |
| ... | @@ -1552,7 +1555,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize { | ... | @@ -1552,7 +1555,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize { |
| 1552 | else => return error.UnknownTotalSystemMemory, | 1555 | else => return error.UnknownTotalSystemMemory, |
| 1553 | }; | 1556 | }; |
| 1554 | assert(physmem >= 0); | 1557 | assert(physmem >= 0); |
| 1555 | return @as(usize, @bitCast(physmem)); | 1558 | return @as(u64, @bitCast(physmem)); |
| 1556 | }, | 1559 | }, |
| 1557 | .windows => { | 1560 | .windows => { |
| 1558 | var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined; | 1561 | var sbi: std.os.windows.SYSTEM_BASIC_INFORMATION = undefined; |
| ... | @@ -1565,13 +1568,13 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize { | ... | @@ -1565,13 +1568,13 @@ pub fn totalSystemMemory() TotalSystemMemoryError!usize { |
| 1565 | if (rc != .SUCCESS) { | 1568 | if (rc != .SUCCESS) { |
| 1566 | return error.UnknownTotalSystemMemory; | 1569 | return error.UnknownTotalSystemMemory; |
| 1567 | } | 1570 | } |
| 1568 | return @as(usize, sbi.NumberOfPhysicalPages) * sbi.PageSize; | 1571 | return @as(u64, sbi.NumberOfPhysicalPages) * sbi.PageSize; |
| 1569 | }, | 1572 | }, |
| 1570 | else => return error.UnknownTotalSystemMemory, | 1573 | else => return error.UnknownTotalSystemMemory, |
| 1571 | } | 1574 | } |
| 1572 | } | 1575 | } |
| 1573 | | 1576 | |
| 1574 | fn totalSystemMemoryLinux() !usize { | 1577 | fn totalSystemMemoryLinux() !u64 { |
| 1575 | var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{}); | 1578 | var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{}); |
| 1576 | defer file.close(); | 1579 | defer file.close(); |
| 1577 | var buf: [50]u8 = undefined; | 1580 | var buf: [50]u8 = undefined; |
| ... | @@ -1583,7 +1586,7 @@ fn totalSystemMemoryLinux() !usize { | ... | @@ -1583,7 +1586,7 @@ fn totalSystemMemoryLinux() !usize { |
| 1583 | const int_text = it.next() orelse return error.Unexpected; | 1586 | const int_text = it.next() orelse return error.Unexpected; |
| 1584 | const units = it.next() orelse return error.Unexpected; | 1587 | const units = it.next() orelse return error.Unexpected; |
| 1585 | if (!std.mem.eql(u8, units, "kB")) return error.Unexpected; | 1588 | if (!std.mem.eql(u8, units, "kB")) return error.Unexpected; |
| 1586 | const kilobytes = try std.fmt.parseInt(usize, int_text, 10); | 1589 | const kilobytes = try std.fmt.parseInt(u64, int_text, 10); |
| 1587 | return kilobytes * 1024; | 1590 | return kilobytes * 1024; |
| 1588 | } | 1591 | } |
| 1589 | | 1592 | |