#include "os.hpp"

#ifdef _WINDOWS
#include <Windows.h>
#include <psapi.h>

os::global os::GetGlobal() {
    global out{};

    MEMORYSTATUSEX memInfo;
    memInfo.dwLength = sizeof(MEMORYSTATUSEX);
    GlobalMemoryStatusEx(&memInfo);
    DWORDLONG totalVirtualMem = memInfo.ullTotalPageFile;

    out.ram.total = memInfo.ullTotalPhys;
    out.ram.free = memInfo.ullAvailPhys
    out.swap.total = memInfo.ullTotalPageFile;
    out.swap.total -= out.ram.total;
    out.swap.free = memInfo.ullAvailPageFile;
    out.swap.free -= out.ram.free;
    return out;
}

os::process os::GetProcess() {
    PROCESS_MEMORY_COUNTERS_EX pmc;
    GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
    process out{};
    out.memused = pmc.PrivateUsage;
    return out;
}

#else
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <sys/resource.h>

os::global os::GetGlobal() {
    struct sysinfo memInfo;
    sysinfo(&memInfo);
    global out{};
    out.ram.total = memInfo.totalram;
    out.ram.total *= memInfo.mem_unit;
    out.ram.free = memInfo.freeram;
    out.ram.free *= memInfo.mem_unit;
    out.swap.total = memInfo.totalswap;
    out.swap.total *= memInfo.mem_unit;
    out.swap.free = memInfo.freeswap;
    out.swap.free *= memInfo.mem_unit;
    return out;
}

os::process os::GetProcess() {
    struct rusage r_usage;
    process out{};
    getrusage(RUSAGE_SELF, &r_usage);
    out.memused = r_usage.ru_maxrss;
    out.memused *= 1000;
    return out;
}
#endif