Added an API to get the amount of system RAM
authorSam Lantinga <slouken@libsdl.org>
Thu, 17 Oct 2013 11:32:56 -0700
changeset 7821c47ae79f0de8
parent 7820 5ce21e1ca0c3
child 7822 6bd98963606b
Added an API to get the amount of system RAM
include/SDL_cpuinfo.h
src/cpuinfo/SDL_cpuinfo.c
     1.1 --- a/include/SDL_cpuinfo.h	Mon Oct 14 09:15:41 2013 -0700
     1.2 +++ b/include/SDL_cpuinfo.h	Thu Oct 17 11:32:56 2013 -0700
     1.3 @@ -134,6 +134,11 @@
     1.4   */
     1.5  extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE42(void);
     1.6  
     1.7 +/**
     1.8 + *  This function returns the amount of RAM configured in the system, in MB.
     1.9 + */
    1.10 +extern DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
    1.11 +
    1.12  
    1.13  /* Ends C function definitions when using C++ */
    1.14  #ifdef __cplusplus
     2.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Mon Oct 14 09:15:41 2013 -0700
     2.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Thu Oct 17 11:32:56 2013 -0700
     2.3 @@ -607,6 +607,60 @@
     2.4      return SDL_FALSE;
     2.5  }
     2.6  
     2.7 +static int SDL_SystemRAM = 0;
     2.8 +
     2.9 +int
    2.10 +SDL_GetSystemRAM(void)
    2.11 +{
    2.12 +    if (!SDL_SystemRAM) {
    2.13 +#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
    2.14 +        if (SDL_SystemRAM <= 0) {
    2.15 +            SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
    2.16 +        }
    2.17 +#endif
    2.18 +#ifdef HAVE_SYSCTLBYNAME
    2.19 +        if (SDL_SystemRAM <= 0) {
    2.20 +            int mib[2] = {CTL_HW, HW_MEMSIZE};
    2.21 +            uint64 memsize = 0;
    2.22 +            size_t len = sizeof(memsize);
    2.23 +            
    2.24 +            if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
    2.25 +                SDL_SystemRAM = (int)(memsize / (1024*1024));
    2.26 +            }
    2.27 +        }
    2.28 +#endif
    2.29 +#ifdef __WIN32__
    2.30 +        if (SDL_SystemRAM <= 0) {
    2.31 +            MEMORYSTATUSEX stat;
    2.32 +            if (GlobalMemoryStatusEx(&stat)) {
    2.33 +                SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
    2.34 +            }
    2.35 +        }
    2.36 +#endif
    2.37 +#if 0 //def __LINUX__
    2.38 +        FILE *fpMemInfo = fopen("/proc/meminfo", "r");
    2.39 +        if (fpMemInfo) {
    2.40 +            char line[1024];
    2.41 +            const char *search = "MemTotal:";
    2.42 +            const size_t searchlen = SDL_strlen(search);
    2.43 +            while (fgets(line, sizeof(line), fpMemInfo)) {
    2.44 +                if (SDL_strncasecmp(search, line, searchlen) == 0) {
    2.45 +                    char *val = line+searchlen;
    2.46 +                    while (SDL_isspace(*val)) {
    2.47 +                        ++val;
    2.48 +                    }
    2.49 +                    SDL_SystemRAM = SDL_atoi(val) / 1024; /* convert from kB to MB */
    2.50 +                    break;
    2.51 +                }
    2.52 +            }
    2.53 +            fclose(fpMemInfo);
    2.54 +        }
    2.55 +#endif
    2.56 +    }
    2.57 +    return SDL_SystemRAM;
    2.58 +}
    2.59 +
    2.60 +
    2.61  #ifdef TEST_MAIN
    2.62  
    2.63  #include <stdio.h>
    2.64 @@ -627,6 +681,7 @@
    2.65      printf("SSE3: %d\n", SDL_HasSSE3());
    2.66      printf("SSE4.1: %d\n", SDL_HasSSE41());
    2.67      printf("SSE4.2: %d\n", SDL_HasSSE42());
    2.68 +    printf("RAM: %d MB\n", SDL_GetSystemRAM());
    2.69      return 0;
    2.70  }
    2.71