tuklib_physmem.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file tuklib_physmem.c
  5. /// \brief Get the amount of physical memory
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "tuklib_physmem.h"
  11. // We want to use Windows-specific code on Cygwin, which also has memory
  12. // information available via sysconf(), but on Cygwin 1.5 and older it
  13. // gives wrong results (from our point of view).
  14. #if defined(_WIN32) || defined(__CYGWIN__)
  15. # ifndef _WIN32_WINNT
  16. # define _WIN32_WINNT 0x0500
  17. # endif
  18. # include <windows.h>
  19. #elif defined(__OS2__)
  20. # define INCL_DOSMISC
  21. # error #include <os2.h>
  22. #elif defined(__DJGPP__)
  23. # error #include <dpmi.h>
  24. #elif defined(__VMS)
  25. # error #include <lib$routines.h>
  26. # error #include <syidef.h>
  27. # error #include <ssdef.h>
  28. #elif defined(AMIGA) || defined(__AROS__)
  29. # define __USE_INLINE__
  30. # error #include <proto/exec.h>
  31. #elif defined(__QNX__)
  32. # error #include <sys/syspage.h>
  33. # include <string.h>
  34. #elif defined(TUKLIB_PHYSMEM_AIX)
  35. # include <sys/systemcfg.h>
  36. #elif defined(TUKLIB_PHYSMEM_SYSCONF)
  37. # include <unistd.h>
  38. #elif defined(TUKLIB_PHYSMEM_SYSCTL)
  39. # ifdef HAVE_SYS_PARAM_H
  40. # include <sys/param.h>
  41. # endif
  42. # include <sys/sysctl.h>
  43. // Tru64
  44. #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
  45. # include <sys/sysinfo.h>
  46. # error #include <machine/hal_sysinfo.h>
  47. // HP-UX
  48. #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
  49. # include <sys/param.h>
  50. # include <sys/pstat.h>
  51. // IRIX
  52. #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
  53. # error #include <invent.h>
  54. // This sysinfo() is Linux-specific.
  55. #elif defined(TUKLIB_PHYSMEM_SYSINFO)
  56. # include <sys/sysinfo.h>
  57. #endif
  58. extern uint64_t
  59. tuklib_physmem(void)
  60. {
  61. uint64_t ret = 0;
  62. #if defined(_WIN32) || defined(__CYGWIN__)
  63. // This requires Windows 2000 or later.
  64. MEMORYSTATUSEX meminfo;
  65. meminfo.dwLength = sizeof(meminfo);
  66. if (GlobalMemoryStatusEx(&meminfo))
  67. ret = meminfo.ullTotalPhys;
  68. /*
  69. // Old version that is compatible with even Win95:
  70. if ((GetVersion() & 0xFF) >= 5) {
  71. // Windows 2000 and later have GlobalMemoryStatusEx() which
  72. // supports reporting values greater than 4 GiB. To keep the
  73. // code working also on older Windows versions, use
  74. // GlobalMemoryStatusEx() conditionally.
  75. HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  76. if (kernel32 != NULL) {
  77. typedef BOOL (WINAPI *gmse_type)(LPMEMORYSTATUSEX);
  78. #ifdef CAN_DISABLE_WCAST_FUNCTION_TYPE
  79. # pragma GCC diagnostic push
  80. # pragma GCC diagnostic ignored "-Wcast-function-type"
  81. #endif
  82. gmse_type gmse = (gmse_type)GetProcAddress(
  83. kernel32, "GlobalMemoryStatusEx");
  84. #ifdef CAN_DISABLE_WCAST_FUNCTION_TYPE
  85. # pragma GCC diagnostic pop
  86. #endif
  87. if (gmse != NULL) {
  88. MEMORYSTATUSEX meminfo;
  89. meminfo.dwLength = sizeof(meminfo);
  90. if (gmse(&meminfo))
  91. ret = meminfo.ullTotalPhys;
  92. }
  93. }
  94. }
  95. if (ret == 0) {
  96. // GlobalMemoryStatus() is supported by Windows 95 and later,
  97. // so it is fine to link against it unconditionally. Note that
  98. // GlobalMemoryStatus() has no return value.
  99. MEMORYSTATUS meminfo;
  100. meminfo.dwLength = sizeof(meminfo);
  101. GlobalMemoryStatus(&meminfo);
  102. ret = meminfo.dwTotalPhys;
  103. }
  104. */
  105. #elif defined(__OS2__)
  106. unsigned long mem;
  107. if (DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM,
  108. &mem, sizeof(mem)) == 0)
  109. ret = mem;
  110. #elif defined(__DJGPP__)
  111. __dpmi_free_mem_info meminfo;
  112. if (__dpmi_get_free_memory_information(&meminfo) == 0
  113. && meminfo.total_number_of_physical_pages
  114. != (unsigned long)-1)
  115. ret = (uint64_t)meminfo.total_number_of_physical_pages * 4096;
  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. #elif defined(AMIGA) || defined(__AROS__)
  122. ret = AvailMem(MEMF_TOTAL);
  123. #elif defined(__QNX__)
  124. const struct asinfo_entry *entries = SYSPAGE_ENTRY(asinfo);
  125. size_t count = SYSPAGE_ENTRY_SIZE(asinfo) / sizeof(struct asinfo_entry);
  126. const char *strings = SYSPAGE_ENTRY(strings)->data;
  127. for (size_t i = 0; i < count; ++i)
  128. if (strcmp(strings + entries[i].name, "ram") == 0)
  129. ret += entries[i].end - entries[i].start + 1;
  130. #elif defined(TUKLIB_PHYSMEM_AIX)
  131. ret = _system_configuration.physmem;
  132. #elif defined(TUKLIB_PHYSMEM_SYSCONF)
  133. const long pagesize = sysconf(_SC_PAGESIZE);
  134. const long pages = sysconf(_SC_PHYS_PAGES);
  135. if (pagesize != -1 && pages != -1)
  136. // According to docs, pagesize * pages can overflow.
  137. // Simple case is 32-bit box with 4 GiB or more RAM,
  138. // which may report exactly 4 GiB of RAM, and "long"
  139. // being 32-bit will overflow. Casting to uint64_t
  140. // hopefully avoids overflows in the near future.
  141. ret = (uint64_t)pagesize * (uint64_t)pages;
  142. #elif defined(TUKLIB_PHYSMEM_SYSCTL)
  143. int name[2] = {
  144. CTL_HW,
  145. #ifdef HW_PHYSMEM64
  146. HW_PHYSMEM64
  147. #else
  148. HW_PHYSMEM
  149. #endif
  150. };
  151. union {
  152. uint32_t u32;
  153. uint64_t u64;
  154. } mem;
  155. size_t mem_ptr_size = sizeof(mem.u64);
  156. if (sysctl(name, 2, &mem.u64, &mem_ptr_size, NULL, 0) != -1) {
  157. // IIRC, 64-bit "return value" is possible on some 64-bit
  158. // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
  159. // so support both.
  160. if (mem_ptr_size == sizeof(mem.u64))
  161. ret = mem.u64;
  162. else if (mem_ptr_size == sizeof(mem.u32))
  163. ret = mem.u32;
  164. }
  165. #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
  166. // Docs are unclear if "start" is needed, but it doesn't hurt
  167. // much to have it.
  168. int memkb;
  169. int start = 0;
  170. if (getsysinfo(GSI_PHYSMEM, (caddr_t)&memkb, sizeof(memkb), &start)
  171. != -1)
  172. ret = (uint64_t)memkb * 1024;
  173. #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
  174. struct pst_static pst;
  175. if (pstat_getstatic(&pst, sizeof(pst), 1, 0) != -1)
  176. ret = (uint64_t)pst.physical_memory * (uint64_t)pst.page_size;
  177. #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
  178. inv_state_t *st = NULL;
  179. if (setinvent_r(&st) != -1) {
  180. inventory_t *i;
  181. while ((i = getinvent_r(st)) != NULL) {
  182. if (i->inv_class == INV_MEMORY
  183. && i->inv_type == INV_MAIN_MB) {
  184. ret = (uint64_t)i->inv_state << 20;
  185. break;
  186. }
  187. }
  188. endinvent_r(st);
  189. }
  190. #elif defined(TUKLIB_PHYSMEM_SYSINFO)
  191. struct sysinfo si;
  192. if (sysinfo(&si) == 0)
  193. ret = (uint64_t)si.totalram * si.mem_unit;
  194. #endif
  195. return ret;
  196. }