Added an API to get the amount of system RAM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
diff --git a/include/SDL_cpuinfo.h b/include/SDL_cpuinfo.h
index dde3074..6d72bbb 100644
--- a/include/SDL_cpuinfo.h
+++ b/include/SDL_cpuinfo.h
@@ -134,6 +134,11 @@ extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE41(void);
*/
extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE42(void);
+/**
+ * This function returns the amount of RAM configured in the system, in MB.
+ */
+extern DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
+
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
diff --git a/src/cpuinfo/SDL_cpuinfo.c b/src/cpuinfo/SDL_cpuinfo.c
index 3054985..f6f4542 100644
--- a/src/cpuinfo/SDL_cpuinfo.c
+++ b/src/cpuinfo/SDL_cpuinfo.c
@@ -607,6 +607,60 @@ SDL_HasSSE42(void)
return SDL_FALSE;
}
+static int SDL_SystemRAM = 0;
+
+int
+SDL_GetSystemRAM(void)
+{
+ if (!SDL_SystemRAM) {
+#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
+ if (SDL_SystemRAM <= 0) {
+ SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
+ }
+#endif
+#ifdef HAVE_SYSCTLBYNAME
+ if (SDL_SystemRAM <= 0) {
+ int mib[2] = {CTL_HW, HW_MEMSIZE};
+ uint64 memsize = 0;
+ size_t len = sizeof(memsize);
+
+ if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
+ SDL_SystemRAM = (int)(memsize / (1024*1024));
+ }
+ }
+#endif
+#ifdef __WIN32__
+ if (SDL_SystemRAM <= 0) {
+ MEMORYSTATUSEX stat;
+ if (GlobalMemoryStatusEx(&stat)) {
+ SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
+ }
+ }
+#endif
+#if 0 //def __LINUX__
+ FILE *fpMemInfo = fopen("/proc/meminfo", "r");
+ if (fpMemInfo) {
+ char line[1024];
+ const char *search = "MemTotal:";
+ const size_t searchlen = SDL_strlen(search);
+ while (fgets(line, sizeof(line), fpMemInfo)) {
+ if (SDL_strncasecmp(search, line, searchlen) == 0) {
+ char *val = line+searchlen;
+ while (SDL_isspace(*val)) {
+ ++val;
+ }
+ SDL_SystemRAM = SDL_atoi(val) / 1024; /* convert from kB to MB */
+ break;
+ }
+ }
+ fclose(fpMemInfo);
+ }
+#endif
+ }
+ return SDL_SystemRAM;
+}
+
+
#ifdef TEST_MAIN
#include <stdio.h>
@@ -627,6 +681,7 @@ main()
printf("SSE3: %d\n", SDL_HasSSE3());
printf("SSE4.1: %d\n", SDL_HasSSE41());
printf("SSE4.2: %d\n", SDL_HasSSE42());
+ printf("RAM: %d MB\n", SDL_GetSystemRAM());
return 0;
}