DoxigAlpha

totalSystemMemory

Returns the total system memory, in bytes as a u64. We return a u64 instead of usize due to PAE on ARM and Linux's /proc/meminfo reporting more memory when using QEMU user mode emulation.

Type definitions in this namespace

Types

#
ArgIteratorWindows
Iterator that implements the Windows command-line parsing algorithm.
ArgIteratorGeneralOptions
Optional parameters for `ArgIteratorGeneral`
ArgIterator
Cross-platform command line argument iterator.

Functions in this namespace

Functions

#
getCwd
The result is a slice of `out_buffer`, from index `0`.
getCwdAlloc
Caller must free the returned memory.
getEnvMap
Returns a snapshot of the environment variables of the current process.
getEnvVarOwned
Caller must free returned memory.
hasEnvVarConstant
On Windows, `key` must be valid WTF-8.
hasNonEmptyEnvVarConstant
On Windows, `key` must be valid WTF-8.
parseEnvVarInt
Parses an environment variable as an integer.
hasEnvVar
On Windows, if `key` is not valid [WTF-8](https://simonsapin.github.io/wtf-8/),
hasNonEmptyEnvVar
On Windows, if `key` is not valid [WTF-8](https://simonsapin.github.io/wtf-8/),
getenvW
Windows-only.
ArgIteratorGeneral
A general Iterator to parse a string into a set of arguments
args
Holds the command-line arguments, with the program name as the first entry.
argsWithAllocator
You must deinitialize iterator's internal buffers by calling `deinit` when done.
argsAlloc
Caller must call argsFree on result.
getUserInfo
POSIX function which gets a uid from username.
posixGetUserInfo
TODO this reads /etc/passwd.
execv
Replaces the current process image with the executed process.
execve
Replaces the current process image with the executed process.
totalSystemMemory
Returns the total system memory, in bytes as a u64.
cleanExit
Indicate that we are now terminating with a successful exit code.
raiseFileDescriptorLimit
Raise the open file descriptor limit.
createEnvironFromMap
Creates a null-delimited environment variable block in the format
createEnvironFromExisting
Creates a null-delimited environment variable block in the format
createWindowsEnvBlock
Caller must free result.
fatal
Logs an error and then terminates the process with exit code 1.

Error sets in this namespace

Error Sets

#

Tells whether calling the `execv` or `execve` functions will be a compile error.

Values

#
can_execv
Tells whether calling the `execv` or `execve` functions will be a compile error.
can_spawn
Tells whether spawning child processes is supported (e.g.

Source

Implementation

#
pub fn totalSystemMemory() TotalSystemMemoryError!u64 {
    switch (native_os) {
        .linux => {
            var info: std.os.linux.Sysinfo = undefined;
            const result: usize = std.os.linux.sysinfo(&info);
            if (std.os.linux.E.init(result) != .SUCCESS) {
                return error.UnknownTotalSystemMemory;
            }
            // Promote to u64 to avoid overflow on systems where info.totalram is a 32-bit usize
            return @as(u64, info.totalram) * info.mem_unit;
        },
        .freebsd => {
            var physmem: c_ulong = undefined;
            var len: usize = @sizeOf(c_ulong);
            posix.sysctlbynameZ("hw.physmem", &physmem, &len, null, 0) catch |err| switch (err) {
                error.NameTooLong, error.UnknownName => unreachable,
                else => return error.UnknownTotalSystemMemory,
            };
            return @as(u64, @intCast(physmem));
        },
        // whole Darwin family
        .driverkit, .ios, .macos, .tvos, .visionos, .watchos => {
            // "hw.memsize" returns uint64_t
            var physmem: u64 = undefined;
            var len: usize = @sizeOf(u64);
            posix.sysctlbynameZ("hw.memsize", &physmem, &len, null, 0) catch |err| switch (err) {
                error.PermissionDenied => unreachable, // only when setting values,
                error.SystemResources => unreachable, // memory already on the stack
                error.UnknownName => unreachable, // constant, known good value
                else => return error.UnknownTotalSystemMemory,
            };
            return physmem;
        },
        .openbsd => {
            const mib: [2]c_int = [_]c_int{
                posix.CTL.HW,
                posix.HW.PHYSMEM64,
            };
            var physmem: i64 = undefined;
            var len: usize = @sizeOf(@TypeOf(physmem));
            posix.sysctl(&mib, &physmem, &len, null, 0) catch |err| switch (err) {
                error.NameTooLong => unreachable, // constant, known good value
                error.PermissionDenied => unreachable, // only when setting values,
                error.SystemResources => unreachable, // memory already on the stack
                error.UnknownName => unreachable, // constant, known good value
                else => return error.UnknownTotalSystemMemory,
            };
            assert(physmem >= 0);
            return @as(u64, @bitCast(physmem));
        },
        .windows => {
            var sbi: windows.SYSTEM_BASIC_INFORMATION = undefined;
            const rc = windows.ntdll.NtQuerySystemInformation(
                .SystemBasicInformation,
                &sbi,
                @sizeOf(windows.SYSTEM_BASIC_INFORMATION),
                null,
            );
            if (rc != .SUCCESS) {
                return error.UnknownTotalSystemMemory;
            }
            return @as(u64, sbi.NumberOfPhysicalPages) * sbi.PageSize;
        },
        else => return error.UnknownTotalSystemMemory,
    }
}