]> icculus.org git repositories - icculus/xz.git/blob - src/common/tuklib_physmem.c
Fix incorrect use of "restrict".
[icculus/xz.git] / src / common / tuklib_physmem.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       tuklib_physmem.c
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 #include "tuklib_physmem.h"
14
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__)
19 #       ifndef _WIN32_WINNT
20 #               define _WIN32_WINNT 0x0500
21 #       endif
22 #       include <windows.h>
23
24 #elif defined(__OS2__)
25 #       define INCL_DOSMISC
26 #       include <os2.h>
27
28 #elif defined(__DJGPP__)
29 #       include <dpmi.h>
30
31 #elif defined(__VMS)
32 #       include <lib$routines.h>
33 #       include <syidef.h>
34 #       include <ssdef.h>
35
36 #elif defined(TUKLIB_PHYSMEM_SYSCONF)
37 #       include <unistd.h>
38
39 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
40 #       ifdef HAVE_SYS_PARAM_H
41 #               include <sys/param.h>
42 #       endif
43 #       include <sys/sysctl.h>
44
45 // This sysinfo() is Linux-specific.
46 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
47 #       include <sys/sysinfo.h>
48 #endif
49
50
51 extern uint64_t
52 tuklib_physmem(void)
53 {
54         uint64_t ret = 0;
55
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");
66                         if (gmse != NULL) {
67                                 MEMORYSTATUSEX meminfo;
68                                 meminfo.dwLength = sizeof(meminfo);
69                                 if (gmse(&meminfo))
70                                         ret = meminfo.ullTotalPhys;
71                         }
72                 }
73         }
74
75         if (ret == 0) {
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.
79                 MEMORYSTATUS meminfo;
80                 meminfo.dwLength = sizeof(meminfo);
81                 GlobalMemoryStatus(&meminfo);
82                 ret = meminfo.dwTotalPhys;
83         }
84
85 #elif defined(__OS2__)
86         unsigned long mem;
87         if (DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM,
88                         &mem, sizeof(mem)) == 0)
89                 ret = mem;
90
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
95                                 != (unsigned long)-1)
96                 ret = (uint64_t)meminfo.total_number_of_physical_pages * 4096;
97
98 #elif defined(__VMS)
99         int vms_mem;
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;
103
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;
114
115 #elif defined(TUKLIB_PHYSMEM_SYSCTL)
116         int name[2] = {
117                 CTL_HW,
118 #ifdef HW_PHYSMEM64
119                 HW_PHYSMEM64
120 #else
121                 HW_PHYSMEM
122 #endif
123         };
124         union {
125                 uint32_t u32;
126                 uint64_t u64;
127         } mem;
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),
132                 // so support both.
133                 if (mem_ptr_size == sizeof(mem.u64))
134                         ret = mem.u64;
135                 else if (mem_ptr_size == sizeof(mem.u32))
136                         ret = mem.u32;
137         }
138
139 #elif defined(TUKLIB_PHYSMEM_SYSINFO)
140         struct sysinfo si;
141         if (sysinfo(&si) == 0)
142                 ret = (uint64_t)si.totalram * si.mem_unit;
143 #endif
144
145         return ret;
146 }