xref: /minix3/external/public-domain/xz/dist/src/common/tuklib_physmem.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
15a645f22SBen Gras ///////////////////////////////////////////////////////////////////////////////
25a645f22SBen Gras //
35a645f22SBen Gras /// \file       tuklib_physmem.c
45a645f22SBen Gras /// \brief      Get the amount of physical memory
55a645f22SBen Gras //
65a645f22SBen Gras //  Author:     Lasse Collin
75a645f22SBen Gras //
85a645f22SBen Gras //  This file has been put into the public domain.
95a645f22SBen Gras //  You can do whatever you want with this file.
105a645f22SBen Gras //
115a645f22SBen Gras ///////////////////////////////////////////////////////////////////////////////
125a645f22SBen Gras 
135a645f22SBen Gras #include "tuklib_physmem.h"
145a645f22SBen Gras 
155a645f22SBen Gras // We want to use Windows-specific code on Cygwin, which also has memory
165a645f22SBen Gras // information available via sysconf(), but on Cygwin 1.5 and older it
175a645f22SBen Gras // gives wrong results (from our point of view).
185a645f22SBen Gras #if defined(_WIN32) || defined(__CYGWIN__)
195a645f22SBen Gras #	ifndef _WIN32_WINNT
205a645f22SBen Gras #		define _WIN32_WINNT 0x0500
215a645f22SBen Gras #	endif
225a645f22SBen Gras #	include <windows.h>
235a645f22SBen Gras 
245a645f22SBen Gras #elif defined(__OS2__)
255a645f22SBen Gras #	define INCL_DOSMISC
265a645f22SBen Gras #	include <os2.h>
275a645f22SBen Gras 
285a645f22SBen Gras #elif defined(__DJGPP__)
295a645f22SBen Gras #	include <dpmi.h>
305a645f22SBen Gras 
315a645f22SBen Gras #elif defined(__VMS)
325a645f22SBen Gras #	include <lib$routines.h>
335a645f22SBen Gras #	include <syidef.h>
345a645f22SBen Gras #	include <ssdef.h>
355a645f22SBen Gras 
36*0a6a1f1dSLionel Sambuc #elif defined(AMIGA) || defined(__AROS__)
37*0a6a1f1dSLionel Sambuc #	define __USE_INLINE__
38*0a6a1f1dSLionel Sambuc #	include <proto/exec.h>
39*0a6a1f1dSLionel Sambuc 
405a645f22SBen Gras // AIX
415a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_AIX)
425a645f22SBen Gras #	include <sys/systemcfg.h>
435a645f22SBen Gras 
445a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_SYSCONF)
455a645f22SBen Gras #	include <unistd.h>
465a645f22SBen Gras 
475a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_SYSCTL)
485a645f22SBen Gras #	ifdef HAVE_SYS_PARAM_H
495a645f22SBen Gras #		include <sys/param.h>
505a645f22SBen Gras #	endif
515a645f22SBen Gras #	include <sys/sysctl.h>
525a645f22SBen Gras 
535a645f22SBen Gras // Tru64
545a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
555a645f22SBen Gras #	include <sys/sysinfo.h>
565a645f22SBen Gras #	include <machine/hal_sysinfo.h>
575a645f22SBen Gras 
585a645f22SBen Gras // HP-UX
595a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
605a645f22SBen Gras #	include <sys/param.h>
615a645f22SBen Gras #	include <sys/pstat.h>
625a645f22SBen Gras 
635a645f22SBen Gras // IRIX
645a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
655a645f22SBen Gras #	include <invent.h>
665a645f22SBen Gras 
675a645f22SBen Gras // This sysinfo() is Linux-specific.
685a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_SYSINFO)
695a645f22SBen Gras #	include <sys/sysinfo.h>
705a645f22SBen Gras #endif
715a645f22SBen Gras 
725a645f22SBen Gras 
735a645f22SBen Gras extern uint64_t
tuklib_physmem(void)745a645f22SBen Gras tuklib_physmem(void)
755a645f22SBen Gras {
765a645f22SBen Gras 	uint64_t ret = 0;
775a645f22SBen Gras 
785a645f22SBen Gras #if defined(_WIN32) || defined(__CYGWIN__)
795a645f22SBen Gras 	if ((GetVersion() & 0xFF) >= 5) {
805a645f22SBen Gras 		// Windows 2000 and later have GlobalMemoryStatusEx() which
815a645f22SBen Gras 		// supports reporting values greater than 4 GiB. To keep the
825a645f22SBen Gras 		// code working also on older Windows versions, use
835a645f22SBen Gras 		// GlobalMemoryStatusEx() conditionally.
845a645f22SBen Gras 		HMODULE kernel32 = GetModuleHandle("kernel32.dll");
855a645f22SBen Gras 		if (kernel32 != NULL) {
865a645f22SBen Gras 			BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress(
875a645f22SBen Gras 					kernel32, "GlobalMemoryStatusEx");
885a645f22SBen Gras 			if (gmse != NULL) {
895a645f22SBen Gras 				MEMORYSTATUSEX meminfo;
905a645f22SBen Gras 				meminfo.dwLength = sizeof(meminfo);
915a645f22SBen Gras 				if (gmse(&meminfo))
925a645f22SBen Gras 					ret = meminfo.ullTotalPhys;
935a645f22SBen Gras 			}
945a645f22SBen Gras 		}
955a645f22SBen Gras 	}
965a645f22SBen Gras 
975a645f22SBen Gras 	if (ret == 0) {
985a645f22SBen Gras 		// GlobalMemoryStatus() is supported by Windows 95 and later,
995a645f22SBen Gras 		// so it is fine to link against it unconditionally. Note that
1005a645f22SBen Gras 		// GlobalMemoryStatus() has no return value.
1015a645f22SBen Gras 		MEMORYSTATUS meminfo;
1025a645f22SBen Gras 		meminfo.dwLength = sizeof(meminfo);
1035a645f22SBen Gras 		GlobalMemoryStatus(&meminfo);
1045a645f22SBen Gras 		ret = meminfo.dwTotalPhys;
1055a645f22SBen Gras 	}
1065a645f22SBen Gras 
1075a645f22SBen Gras #elif defined(__OS2__)
1085a645f22SBen Gras 	unsigned long mem;
1095a645f22SBen Gras 	if (DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM,
1105a645f22SBen Gras 			&mem, sizeof(mem)) == 0)
1115a645f22SBen Gras 		ret = mem;
1125a645f22SBen Gras 
1135a645f22SBen Gras #elif defined(__DJGPP__)
1145a645f22SBen Gras 	__dpmi_free_mem_info meminfo;
1155a645f22SBen Gras 	if (__dpmi_get_free_memory_information(&meminfo) == 0
1165a645f22SBen Gras 			&& meminfo.total_number_of_physical_pages
1175a645f22SBen Gras 				!= (unsigned long)-1)
1185a645f22SBen Gras 		ret = (uint64_t)meminfo.total_number_of_physical_pages * 4096;
1195a645f22SBen Gras 
1205a645f22SBen Gras #elif defined(__VMS)
1215a645f22SBen Gras 	int vms_mem;
1225a645f22SBen Gras 	int val = SYI$_MEMSIZE;
1235a645f22SBen Gras 	if (LIB$GETSYI(&val, &vms_mem, 0, 0, 0, 0) == SS$_NORMAL)
1245a645f22SBen Gras 		ret = (uint64_t)vms_mem * 8192;
1255a645f22SBen Gras 
126*0a6a1f1dSLionel Sambuc #elif defined(AMIGA) || defined(__AROS__)
127*0a6a1f1dSLionel Sambuc 	ret = AvailMem(MEMF_TOTAL);
128*0a6a1f1dSLionel Sambuc 
1295a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_AIX)
1305a645f22SBen Gras 	ret = _system_configuration.physmem;
1315a645f22SBen Gras 
1325a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_SYSCONF)
1335a645f22SBen Gras 	const long pagesize = sysconf(_SC_PAGESIZE);
1345a645f22SBen Gras 	const long pages = sysconf(_SC_PHYS_PAGES);
1355a645f22SBen Gras 	if (pagesize != -1 && pages != -1)
1365a645f22SBen Gras 		// According to docs, pagesize * pages can overflow.
1375a645f22SBen Gras 		// Simple case is 32-bit box with 4 GiB or more RAM,
1385a645f22SBen Gras 		// which may report exactly 4 GiB of RAM, and "long"
1395a645f22SBen Gras 		// being 32-bit will overflow. Casting to uint64_t
1405a645f22SBen Gras 		// hopefully avoids overflows in the near future.
1415a645f22SBen Gras 		ret = (uint64_t)pagesize * (uint64_t)pages;
1425a645f22SBen Gras 
1435a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_SYSCTL)
1445a645f22SBen Gras 	int name[2] = {
1455a645f22SBen Gras 		CTL_HW,
1465a645f22SBen Gras #ifdef HW_PHYSMEM64
1475a645f22SBen Gras 		HW_PHYSMEM64
1485a645f22SBen Gras #else
1495a645f22SBen Gras 		HW_PHYSMEM
1505a645f22SBen Gras #endif
1515a645f22SBen Gras 	};
1525a645f22SBen Gras 	union {
1535a645f22SBen Gras 		uint32_t u32;
1545a645f22SBen Gras 		uint64_t u64;
1555a645f22SBen Gras 	} mem;
1565a645f22SBen Gras 	size_t mem_ptr_size = sizeof(mem.u64);
1575a645f22SBen Gras 	if (sysctl(name, 2, &mem.u64, &mem_ptr_size, NULL, 0) != -1) {
1585a645f22SBen Gras 		// IIRC, 64-bit "return value" is possible on some 64-bit
1595a645f22SBen Gras 		// BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
1605a645f22SBen Gras 		// so support both.
1615a645f22SBen Gras 		if (mem_ptr_size == sizeof(mem.u64))
1625a645f22SBen Gras 			ret = mem.u64;
1635a645f22SBen Gras 		else if (mem_ptr_size == sizeof(mem.u32))
1645a645f22SBen Gras 			ret = mem.u32;
1655a645f22SBen Gras 	}
1665a645f22SBen Gras 
1675a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
1685a645f22SBen Gras 	// Docs are unclear if "start" is needed, but it doesn't hurt
1695a645f22SBen Gras 	// much to have it.
1705a645f22SBen Gras 	int memkb;
1715a645f22SBen Gras 	int start = 0;
1725a645f22SBen Gras 	if (getsysinfo(GSI_PHYSMEM, (caddr_t)&memkb, sizeof(memkb), &start)
1735a645f22SBen Gras 			!= -1)
1745a645f22SBen Gras 		ret = (uint64_t)memkb * 1024;
1755a645f22SBen Gras 
1765a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
1775a645f22SBen Gras 	struct pst_static pst;
1785a645f22SBen Gras 	if (pstat_getstatic(&pst, sizeof(pst), 1, 0) != -1)
1795a645f22SBen Gras 		ret = (uint64_t)pst.physical_memory * (uint64_t)pst.page_size;
1805a645f22SBen Gras 
1815a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
1825a645f22SBen Gras 	inv_state_t *st = NULL;
1835a645f22SBen Gras 	if (setinvent_r(&st) != -1) {
1845a645f22SBen Gras 		inventory_t *i;
1855a645f22SBen Gras 		while ((i = getinvent_r(st)) != NULL) {
1865a645f22SBen Gras 			if (i->inv_class == INV_MEMORY
1875a645f22SBen Gras 					&& i->inv_type == INV_MAIN_MB) {
1885a645f22SBen Gras 				ret = (uint64_t)i->inv_state << 20;
1895a645f22SBen Gras 				break;
1905a645f22SBen Gras 			}
1915a645f22SBen Gras 		}
1925a645f22SBen Gras 
1935a645f22SBen Gras 		endinvent_r(st);
1945a645f22SBen Gras 	}
1955a645f22SBen Gras 
1965a645f22SBen Gras #elif defined(TUKLIB_PHYSMEM_SYSINFO)
1975a645f22SBen Gras 	struct sysinfo si;
1985a645f22SBen Gras 	if (sysinfo(&si) == 0)
1995a645f22SBen Gras 		ret = (uint64_t)si.totalram * si.mem_unit;
2005a645f22SBen Gras #endif
2015a645f22SBen Gras 
2025a645f22SBen Gras 	return ret;
2035a645f22SBen Gras }
204