src/cpuinfo/SDL_cpuinfo.c
changeset 7821 c47ae79f0de8
parent 6885 700f1b25f77f
child 7822 6bd98963606b
     1.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Mon Oct 14 09:15:41 2013 -0700
     1.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Thu Oct 17 11:32:56 2013 -0700
     1.3 @@ -607,6 +607,60 @@
     1.4      return SDL_FALSE;
     1.5  }
     1.6  
     1.7 +static int SDL_SystemRAM = 0;
     1.8 +
     1.9 +int
    1.10 +SDL_GetSystemRAM(void)
    1.11 +{
    1.12 +    if (!SDL_SystemRAM) {
    1.13 +#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
    1.14 +        if (SDL_SystemRAM <= 0) {
    1.15 +            SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
    1.16 +        }
    1.17 +#endif
    1.18 +#ifdef HAVE_SYSCTLBYNAME
    1.19 +        if (SDL_SystemRAM <= 0) {
    1.20 +            int mib[2] = {CTL_HW, HW_MEMSIZE};
    1.21 +            uint64 memsize = 0;
    1.22 +            size_t len = sizeof(memsize);
    1.23 +            
    1.24 +            if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
    1.25 +                SDL_SystemRAM = (int)(memsize / (1024*1024));
    1.26 +            }
    1.27 +        }
    1.28 +#endif
    1.29 +#ifdef __WIN32__
    1.30 +        if (SDL_SystemRAM <= 0) {
    1.31 +            MEMORYSTATUSEX stat;
    1.32 +            if (GlobalMemoryStatusEx(&stat)) {
    1.33 +                SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
    1.34 +            }
    1.35 +        }
    1.36 +#endif
    1.37 +#if 0 //def __LINUX__
    1.38 +        FILE *fpMemInfo = fopen("/proc/meminfo", "r");
    1.39 +        if (fpMemInfo) {
    1.40 +            char line[1024];
    1.41 +            const char *search = "MemTotal:";
    1.42 +            const size_t searchlen = SDL_strlen(search);
    1.43 +            while (fgets(line, sizeof(line), fpMemInfo)) {
    1.44 +                if (SDL_strncasecmp(search, line, searchlen) == 0) {
    1.45 +                    char *val = line+searchlen;
    1.46 +                    while (SDL_isspace(*val)) {
    1.47 +                        ++val;
    1.48 +                    }
    1.49 +                    SDL_SystemRAM = SDL_atoi(val) / 1024; /* convert from kB to MB */
    1.50 +                    break;
    1.51 +                }
    1.52 +            }
    1.53 +            fclose(fpMemInfo);
    1.54 +        }
    1.55 +#endif
    1.56 +    }
    1.57 +    return SDL_SystemRAM;
    1.58 +}
    1.59 +
    1.60 +
    1.61  #ifdef TEST_MAIN
    1.62  
    1.63  #include <stdio.h>
    1.64 @@ -627,6 +681,7 @@
    1.65      printf("SSE3: %d\n", SDL_HasSSE3());
    1.66      printf("SSE4.1: %d\n", SDL_HasSSE41());
    1.67      printf("SSE4.2: %d\n", SDL_HasSSE42());
    1.68 +    printf("RAM: %d MB\n", SDL_GetSystemRAM());
    1.69      return 0;
    1.70  }
    1.71