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