Deleted Added
full compact
tuklib_physmem.c (207842) tuklib_physmem.c (213700)
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.

--- 19 unchanged lines hidden (view full) ---

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
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.

--- 19 unchanged lines hidden (view full) ---

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// AIX
37#elif defined(TUKLIB_PHYSMEM_AIX)
38# include <sys/systemcfg.h>
39
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
40#elif defined(TUKLIB_PHYSMEM_SYSCONF)
41# include <unistd.h>
42
43#elif defined(TUKLIB_PHYSMEM_SYSCTL)
44# ifdef HAVE_SYS_PARAM_H
45# include <sys/param.h>
46# endif
47# include <sys/sysctl.h>
48
49// Tru64
50#elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
51# include <sys/sysinfo.h>
52# include <machine/hal_sysinfo.h>
53
54// HP-UX
55#elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
56# include <sys/param.h>
57# include <sys/pstat.h>
58
45// IRIX
46#elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
47# include <invent.h>
48
49// This sysinfo() is Linux-specific.
50#elif defined(TUKLIB_PHYSMEM_SYSINFO)
51# include <sys/sysinfo.h>
52#endif

--- 47 unchanged lines hidden (view full) ---

100 ret = (uint64_t)meminfo.total_number_of_physical_pages * 4096;
101
102#elif defined(__VMS)
103 int vms_mem;
104 int val = SYI$_MEMSIZE;
105 if (LIB$GETSYI(&val, &vms_mem, 0, 0, 0, 0) == SS$_NORMAL)
106 ret = (uint64_t)vms_mem * 8192;
107
59// IRIX
60#elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
61# include <invent.h>
62
63// This sysinfo() is Linux-specific.
64#elif defined(TUKLIB_PHYSMEM_SYSINFO)
65# include <sys/sysinfo.h>
66#endif

--- 47 unchanged lines hidden (view full) ---

114 ret = (uint64_t)meminfo.total_number_of_physical_pages * 4096;
115
116#elif defined(__VMS)
117 int vms_mem;
118 int val = SYI$_MEMSIZE;
119 if (LIB$GETSYI(&val, &vms_mem, 0, 0, 0, 0) == SS$_NORMAL)
120 ret = (uint64_t)vms_mem * 8192;
121
122#elif defined(TUKLIB_PHYSMEM_AIX)
123 ret = _system_configuration.physmem;
124
108#elif defined(TUKLIB_PHYSMEM_SYSCONF)
109 const long pagesize = sysconf(_SC_PAGESIZE);
110 const long pages = sysconf(_SC_PHYS_PAGES);
125#elif defined(TUKLIB_PHYSMEM_SYSCONF)
126 const long pagesize = sysconf(_SC_PAGESIZE);
127 const long pages = sysconf(_SC_PHYS_PAGES);
111 if (pagesize != -1 || pages != -1)
128 if (pagesize != -1 && pages != -1)
112 // According to docs, pagesize * pages can overflow.
113 // Simple case is 32-bit box with 4 GiB or more RAM,
114 // which may report exactly 4 GiB of RAM, and "long"
115 // being 32-bit will overflow. Casting to uint64_t
116 // hopefully avoids overflows in the near future.
117 ret = (uint64_t)pagesize * (uint64_t)pages;
118
119#elif defined(TUKLIB_PHYSMEM_SYSCTL)

--- 15 unchanged lines hidden (view full) ---

135 // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
136 // so support both.
137 if (mem_ptr_size == sizeof(mem.u64))
138 ret = mem.u64;
139 else if (mem_ptr_size == sizeof(mem.u32))
140 ret = mem.u32;
141 }
142
129 // According to docs, pagesize * pages can overflow.
130 // Simple case is 32-bit box with 4 GiB or more RAM,
131 // which may report exactly 4 GiB of RAM, and "long"
132 // being 32-bit will overflow. Casting to uint64_t
133 // hopefully avoids overflows in the near future.
134 ret = (uint64_t)pagesize * (uint64_t)pages;
135
136#elif defined(TUKLIB_PHYSMEM_SYSCTL)

--- 15 unchanged lines hidden (view full) ---

152 // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
153 // so support both.
154 if (mem_ptr_size == sizeof(mem.u64))
155 ret = mem.u64;
156 else if (mem_ptr_size == sizeof(mem.u32))
157 ret = mem.u32;
158 }
159
160#elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
161 // Docs are unclear if "start" is needed, but it doesn't hurt
162 // much to have it.
163 int memkb;
164 int start = 0;
165 if (getsysinfo(GSI_PHYSMEM, (caddr_t)&memkb, sizeof(memkb), &start)
166 != -1)
167 ret = (uint64_t)memkb * 1024;
168
169#elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
170 struct pst_static pst;
171 if (pstat_getstatic(&pst, sizeof(pst), 1, 0) != -1)
172 ret = (uint64_t)pst.physical_memory * (uint64_t)pst.page_size;
173
143#elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
144 inv_state_t *st = NULL;
145 if (setinvent_r(&st) != -1) {
146 inventory_t *i;
147 while ((i = getinvent_r(st)) != NULL) {
148 if (i->inv_class == INV_MEMORY
149 && i->inv_type == INV_MAIN_MB) {
150 ret = (uint64_t)i->inv_state << 20;

--- 15 unchanged lines hidden ---
174#elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
175 inv_state_t *st = NULL;
176 if (setinvent_r(&st) != -1) {
177 inventory_t *i;
178 while ((i = getinvent_r(st)) != NULL) {
179 if (i->inv_class == INV_MEMORY
180 && i->inv_type == INV_MAIN_MB) {
181 ret = (uint64_t)i->inv_state << 20;

--- 15 unchanged lines hidden ---