1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: expandtab:ts=8:sw=4:softtabstop=4:
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       physmem.h
6 /// \brief      Get the amount of physical memory
7 //
8 //  Author:     Lasse Collin
9 //
10 //  This file has been put into the public domain.
11 //  You can do whatever you want with this file.
12 //
13 ///////////////////////////////////////////////////////////////////////////////
14 
15 #ifndef PHYSMEM_H
16 #define PHYSMEM_H
17 
18 // Test for Windows first, because we want to use Windows-specific code
19 // on Cygwin, which also has memory information available via sysconf(), but
20 // on Cygwin 1.5 and older it gives wrong results (from our point of view).
21 #if defined(_WIN32) || defined(__CYGWIN__)
22 #	ifndef _WIN32_WINNT
23 #		define _WIN32_WINNT 0x0500
24 #	endif
25 #	include <windows.h>
26 
27 #elif defined(HAVE_PHYSMEM_SYSCONF)
28 #	include <unistd.h>
29 
30 #elif defined(HAVE_PHYSMEM_SYSCTL)
31 #	ifdef HAVE_SYS_PARAM_H
32 #		include <sys/param.h>
33 #	endif
34 #	ifdef HAVE_SYS_SYSCTL_H
35 #		include <sys/sysctl.h>
36 #	endif
37 
38 #elif defined(HAVE_PHYSMEM_SYSINFO)
39 #	include <sys/sysinfo.h>
40 
41 #elif defined(__DJGPP__)
42 #	include <dpmi.h>
43 #endif
44 
45 
46 /// \brief      Get the amount of physical memory in bytes
47 ///
48 /// \return     Amount of physical memory in bytes. On error, zero is
49 ///             returned.
50 static inline uint64_t
physmem(void)51 physmem(void)
52 {
53 	uint64_t ret = 0;
54 
55 #if defined(_WIN32) || defined(__CYGWIN__)
56 	if ((GetVersion() & 0xFF) >= 5) {
57 		// Windows 2000 and later have GlobalMemoryStatusEx() which
58 		// supports reporting values greater than 4 GiB. To keep the
59 		// code working also on older Windows versions, use
60 		// GlobalMemoryStatusEx() conditionally.
61 		HMODULE kernel32 = GetModuleHandle("kernel32.dll");
62 		if (kernel32 != NULL) {
63 			BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress(
64 					kernel32, "GlobalMemoryStatusEx");
65 			if (gmse != NULL) {
66 				MEMORYSTATUSEX meminfo;
67 				meminfo.dwLength = sizeof(meminfo);
68 				if (gmse(&meminfo))
69 					ret = meminfo.ullTotalPhys;
70 			}
71 		}
72 	}
73 
74 	if (ret == 0) {
75 		// GlobalMemoryStatus() is supported by Windows 95 and later,
76 		// so it is fine to link against it unconditionally. Note that
77 		// GlobalMemoryStatus() has no return value.
78 		MEMORYSTATUS meminfo;
79 		meminfo.dwLength = sizeof(meminfo);
80 		GlobalMemoryStatus(&meminfo);
81 		ret = meminfo.dwTotalPhys;
82 	}
83 
84 #elif defined(HAVE_PHYSMEM_SYSCONF)
85 	const long pagesize = sysconf(_SC_PAGESIZE);
86 	const long pages = sysconf(_SC_PHYS_PAGES);
87 	if (pagesize != -1 || pages != -1)
88 		// According to docs, pagesize * pages can overflow.
89 		// Simple case is 32-bit box with 4 GiB or more RAM,
90 		// which may report exactly 4 GiB of RAM, and "long"
91 		// being 32-bit will overflow. Casting to uint64_t
92 		// hopefully avoids overflows in the near future.
93 		ret = (uint64_t)(pagesize) * (uint64_t)(pages);
94 
95 #elif defined(HAVE_PHYSMEM_SYSCTL)
96 	int name[2] = {
97 		CTL_HW,
98 #ifdef HW_PHYSMEM64
99 		HW_PHYSMEM64
100 #else
101 		HW_PHYSMEM
102 #endif
103 	};
104 	union {
105 		uint32_t u32;
106 		uint64_t u64;
107 	} mem;
108 	size_t mem_ptr_size = sizeof(mem.u64);
109 	if (!sysctl(name, 2, &mem.u64, &mem_ptr_size, NULL, NULL)) {
110 		// IIRC, 64-bit "return value" is possible on some 64-bit
111 		// BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
112 		// so support both.
113 		if (mem_ptr_size == sizeof(mem.u64))
114 			ret = mem.u64;
115 		else if (mem_ptr_size == sizeof(mem.u32))
116 			ret = mem.u32;
117 	}
118 
119 #elif defined(HAVE_PHYSMEM_SYSINFO)
120 	struct sysinfo si;
121 	if (sysinfo(&si) == 0)
122 		ret = (uint64_t)(si.totalram) * si.mem_unit;
123 
124 #elif defined(__DJGPP__)
125 	__dpmi_free_mem_info meminfo;
126 	if (__dpmi_get_free_memory_information(&meminfo) == 0
127 			&& meminfo.total_number_of_physical_pages
128 				!= (unsigned long)(-1))
129 		ret = (uint64_t)(meminfo.total_number_of_physical_pages)
130 				* 4096;
131 #endif
132 
133 	return ret;
134 }
135 
136 #endif
137