Skip to content

Commit

Permalink
Added Helpers::GetTotalSystemMemory to get the total system memory in…
Browse files Browse the repository at this point in the history
… bytes.
  • Loading branch information
bXi committed Jun 19, 2024
1 parent 74e85a8 commit 8edaedc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
60 changes: 60 additions & 0 deletions utils/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@

#include "SDL3/SDL.h"

#include <iostream>

#if defined(_WIN32) || defined(_WIN64)

#include <windows.h>

#elif defined(__linux__) && !defined(__ANDROID__)
#include <sys/sysinfo.h>
#elif defined(__APPLE__)
#include <sys/types.h>
#include <sys/sysctl.h>
#elif defined(__ANDROID__)
#include <sys/sysinfo.h>
#include <jni.h>
#include <android/log.h>
#elif defined(__EMSCRIPTEN__)
#include <emscripten.h>
#endif

bool Helpers::imguiTexturesVisible = false;
bool Helpers::imguiAudioVisible = false;
bool Helpers::imguiInputVisible = false;
Expand Down Expand Up @@ -525,4 +544,45 @@ const char *Helpers::TextFormat(const char *text, ...) {
return currentBuffer;
}

uint64_t Helpers::GetTotalSystemMemory() {
#if defined(_WIN32) || defined(_WIN64)
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
if (GlobalMemoryStatusEx(&statex)) {
return statex.ullTotalPhys;
} else {
return 0; // Failed to get memory status
}
#elif defined(__linux__) && !defined(__ANDROID__)
struct sysinfo info;
if (sysinfo(&info) == 0) {
return info.totalram * info.mem_unit;
} else {
return 0; // Failed to get memory status
}
#elif defined(__APPLE__)
int mib[2] = {CTL_HW, HW_MEMSIZE};
uint64_t totalMemory = 0;
size_t length = sizeof(totalMemory);
if (sysctl(mib, 2, &totalMemory, &length, NULL, 0) == 0) {
return totalMemory;
} else {
return 0; // Failed to get memory status
}
#elif defined(__ANDROID__)
struct sysinfo info;
if (sysinfo(&info) == 0) {
return info.totalram * info.mem_unit;
} else {
return 0; // Failed to get memory status
}
#elif defined(__EMSCRIPTEN__)
return EM_ASM_INT({
return HEAP8.length;
});
#else
return 0; // Unsupported platform
#endif
}


1 change: 1 addition & 0 deletions utils/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Helpers {

static int GetRandomValue(int min, int max);

static uint64_t GetTotalSystemMemory();

static bool imguiTexturesVisible;
static bool imguiAudioVisible;
Expand Down

0 comments on commit 8edaedc

Please sign in to comment.