1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file tuklib_physmem.c
4 /// \brief Get the amount of physical memory
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "tuklib_physmem.h"
15 // We want to use Windows-specific code on Cygwin, which also has memory
16 // information available via sysconf(), but on Cygwin 1.5 and older it
17 // gives wrong results (from our point of view).
18 #if defined(_WIN32) || defined(__CYGWIN__)
20 # define _WIN32_WINNT 0x0500
24 #elif defined(__OS2__)
28 #elif defined(__DJGPP__)
32 # include <lib$routines.h>
36 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
39 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
40 # ifdef HAVE_SYS_PARAM_H
41 # include <sys/param.h>
43 # include <sys/sysctl.h>
45 // This sysinfo() is Linux-specific.
46 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
47 # include <sys/sysinfo.h>
56 #if defined(_WIN32) || defined(__CYGWIN__)
57 if ((GetVersion() & 0xFF) >= 5) {
58 // Windows 2000 and later have GlobalMemoryStatusEx() which
59 // supports reporting values greater than 4 GiB. To keep the
60 // code working also on older Windows versions, use
61 // GlobalMemoryStatusEx() conditionally.
62 HMODULE kernel32 = GetModuleHandle("kernel32.dll");
63 if (kernel32 != NULL) {
64 BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress(
65 kernel32, "GlobalMemoryStatusEx");
67 MEMORYSTATUSEX meminfo;
68 meminfo.dwLength = sizeof(meminfo);
70 ret = meminfo.ullTotalPhys;
76 // GlobalMemoryStatus() is supported by Windows 95 and later,
77 // so it is fine to link against it unconditionally. Note that
78 // GlobalMemoryStatus() has no return value.
80 meminfo.dwLength = sizeof(meminfo);
81 GlobalMemoryStatus(&meminfo);
82 ret = meminfo.dwTotalPhys;
85 #elif defined(__OS2__)
87 if (DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM,
88 &mem, sizeof(mem)) == 0)
91 #elif defined(__DJGPP__)
92 __dpmi_free_mem_info meminfo;
93 if (__dpmi_get_free_memory_information(&meminfo) == 0
94 && meminfo.total_number_of_physical_pages
96 ret = (uint64_t)meminfo.total_number_of_physical_pages * 4096;
100 int val = SYI$_MEMSIZE;
101 if (LIB$GETSYI(&val, &vms_mem, 0, 0, 0, 0) == SS$_NORMAL)
102 ret = (uint64_t)vms_mem * 8192;
104 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
105 const long pagesize = sysconf(_SC_PAGESIZE);
106 const long pages = sysconf(_SC_PHYS_PAGES);
107 if (pagesize != -1 || pages != -1)
108 // According to docs, pagesize * pages can overflow.
109 // Simple case is 32-bit box with 4 GiB or more RAM,
110 // which may report exactly 4 GiB of RAM, and "long"
111 // being 32-bit will overflow. Casting to uint64_t
112 // hopefully avoids overflows in the near future.
113 ret = (uint64_t)pagesize * (uint64_t)pages;
115 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
128 size_t mem_ptr_size = sizeof(mem.u64);
129 if (sysctl(name, 2, &mem.u64, &mem_ptr_size, NULL, 0) != -1) {
130 // IIRC, 64-bit "return value" is possible on some 64-bit
131 // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
133 if (mem_ptr_size == sizeof(mem.u64))
135 else if (mem_ptr_size == sizeof(mem.u32))
139 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
141 if (sysinfo(&si) == 0)
142 ret = (uint64_t)si.totalram * si.mem_unit;