src/cpuinfo/SDL_cpuinfo.c
changeset 7821 c47ae79f0de8
parent 6885 700f1b25f77f
child 7822 6bd98963606b
equal deleted inserted replaced
7820:5ce21e1ca0c3 7821:c47ae79f0de8
   605         return SDL_TRUE;
   605         return SDL_TRUE;
   606     }
   606     }
   607     return SDL_FALSE;
   607     return SDL_FALSE;
   608 }
   608 }
   609 
   609 
       
   610 static int SDL_SystemRAM = 0;
       
   611 
       
   612 int
       
   613 SDL_GetSystemRAM(void)
       
   614 {
       
   615     if (!SDL_SystemRAM) {
       
   616 #if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
       
   617         if (SDL_SystemRAM <= 0) {
       
   618             SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
       
   619         }
       
   620 #endif
       
   621 #ifdef HAVE_SYSCTLBYNAME
       
   622         if (SDL_SystemRAM <= 0) {
       
   623             int mib[2] = {CTL_HW, HW_MEMSIZE};
       
   624             uint64 memsize = 0;
       
   625             size_t len = sizeof(memsize);
       
   626             
       
   627             if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
       
   628                 SDL_SystemRAM = (int)(memsize / (1024*1024));
       
   629             }
       
   630         }
       
   631 #endif
       
   632 #ifdef __WIN32__
       
   633         if (SDL_SystemRAM <= 0) {
       
   634             MEMORYSTATUSEX stat;
       
   635             if (GlobalMemoryStatusEx(&stat)) {
       
   636                 SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
       
   637             }
       
   638         }
       
   639 #endif
       
   640 #if 0 //def __LINUX__
       
   641         FILE *fpMemInfo = fopen("/proc/meminfo", "r");
       
   642         if (fpMemInfo) {
       
   643             char line[1024];
       
   644             const char *search = "MemTotal:";
       
   645             const size_t searchlen = SDL_strlen(search);
       
   646             while (fgets(line, sizeof(line), fpMemInfo)) {
       
   647                 if (SDL_strncasecmp(search, line, searchlen) == 0) {
       
   648                     char *val = line+searchlen;
       
   649                     while (SDL_isspace(*val)) {
       
   650                         ++val;
       
   651                     }
       
   652                     SDL_SystemRAM = SDL_atoi(val) / 1024; /* convert from kB to MB */
       
   653                     break;
       
   654                 }
       
   655             }
       
   656             fclose(fpMemInfo);
       
   657         }
       
   658 #endif
       
   659     }
       
   660     return SDL_SystemRAM;
       
   661 }
       
   662 
       
   663 
   610 #ifdef TEST_MAIN
   664 #ifdef TEST_MAIN
   611 
   665 
   612 #include <stdio.h>
   666 #include <stdio.h>
   613 
   667 
   614 int
   668 int
   625     printf("SSE: %d\n", SDL_HasSSE());
   679     printf("SSE: %d\n", SDL_HasSSE());
   626     printf("SSE2: %d\n", SDL_HasSSE2());
   680     printf("SSE2: %d\n", SDL_HasSSE2());
   627     printf("SSE3: %d\n", SDL_HasSSE3());
   681     printf("SSE3: %d\n", SDL_HasSSE3());
   628     printf("SSE4.1: %d\n", SDL_HasSSE41());
   682     printf("SSE4.1: %d\n", SDL_HasSSE41());
   629     printf("SSE4.2: %d\n", SDL_HasSSE42());
   683     printf("SSE4.2: %d\n", SDL_HasSSE42());
       
   684     printf("RAM: %d MB\n", SDL_GetSystemRAM());
   630     return 0;
   685     return 0;
   631 }
   686 }
   632 
   687 
   633 #endif /* TEST_MAIN */
   688 #endif /* TEST_MAIN */
   634 
   689