tuklib_physmem.c 6.2 KB

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