tuklib_physmem.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. # include <os2.h>
  24. #elif defined(__DJGPP__)
  25. # error #include <dpmi.h>
  26. #elif defined(__VMS)
  27. # include <lib$routines.h>
  28. # include <syidef.h>
  29. # include <ssdef.h>
  30. #elif defined(AMIGA) || defined(__AROS__)
  31. # define __USE_INLINE__
  32. # include <proto/exec.h>
  33. #elif defined(__QNX__)
  34. # 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. extern uint64_t
  61. tuklib_physmem(void)
  62. {
  63. uint64_t ret = 0;
  64. #if defined(_WIN32) || defined(__CYGWIN__)
  65. if ((GetVersion() & 0xFF) >= 5) {
  66. // Windows 2000 and later have GlobalMemoryStatusEx() which
  67. // supports reporting values greater than 4 GiB. To keep the
  68. // code working also on older Windows versions, use
  69. // GlobalMemoryStatusEx() conditionally.
  70. HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  71. if (kernel32 != NULL) {
  72. typedef BOOL (WINAPI *gmse_type)(LPMEMORYSTATUSEX);
  73. gmse_type gmse = (gmse_type)GetProcAddress(
  74. kernel32, "GlobalMemoryStatusEx");
  75. if (gmse != NULL) {
  76. MEMORYSTATUSEX meminfo;
  77. meminfo.dwLength = sizeof(meminfo);
  78. if (gmse(&meminfo))
  79. ret = meminfo.ullTotalPhys;
  80. }
  81. }
  82. }
  83. if (ret == 0) {
  84. // GlobalMemoryStatus() is supported by Windows 95 and later,
  85. // so it is fine to link against it unconditionally. Note that
  86. // GlobalMemoryStatus() has no return value.
  87. MEMORYSTATUS meminfo;
  88. meminfo.dwLength = sizeof(meminfo);
  89. GlobalMemoryStatus(&meminfo);
  90. ret = meminfo.dwTotalPhys;
  91. }
  92. #elif defined(__OS2__)
  93. unsigned long mem;
  94. if (DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM,
  95. &mem, sizeof(mem)) == 0)
  96. ret = mem;
  97. #elif defined(__DJGPP__)
  98. __dpmi_free_mem_info meminfo;
  99. if (__dpmi_get_free_memory_information(&meminfo) == 0
  100. && meminfo.total_number_of_physical_pages
  101. != (unsigned long)-1)
  102. ret = (uint64_t)meminfo.total_number_of_physical_pages * 4096;
  103. #elif defined(__VMS)
  104. int vms_mem;
  105. int val = SYI$_MEMSIZE;
  106. if (LIB$GETSYI(&val, &vms_mem, 0, 0, 0, 0) == SS$_NORMAL)
  107. ret = (uint64_t)vms_mem * 8192;
  108. #elif defined(AMIGA) || defined(__AROS__)
  109. ret = AvailMem(MEMF_TOTAL);
  110. #elif defined(__QNX__)
  111. const struct asinfo_entry *entries = SYSPAGE_ENTRY(asinfo);
  112. size_t count = SYSPAGE_ENTRY_SIZE(asinfo) / sizeof(struct asinfo_entry);
  113. const char *strings = SYSPAGE_ENTRY(strings)->data;
  114. for (size_t i = 0; i < count; ++i)
  115. if (strcmp(strings + entries[i].name, "ram") == 0)
  116. ret += entries[i].end - entries[i].start + 1;
  117. #elif defined(TUKLIB_PHYSMEM_AIX)
  118. ret = _system_configuration.physmem;
  119. #elif defined(TUKLIB_PHYSMEM_SYSCONF)
  120. const long pagesize = sysconf(_SC_PAGESIZE);
  121. const long pages = sysconf(_SC_PHYS_PAGES);
  122. if (pagesize != -1 && pages != -1)
  123. // According to docs, pagesize * pages can overflow.
  124. // Simple case is 32-bit box with 4 GiB or more RAM,
  125. // which may report exactly 4 GiB of RAM, and "long"
  126. // being 32-bit will overflow. Casting to uint64_t
  127. // hopefully avoids overflows in the near future.
  128. ret = (uint64_t)pagesize * (uint64_t)pages;
  129. #elif defined(TUKLIB_PHYSMEM_SYSCTL)
  130. int name[2] = {
  131. CTL_HW,
  132. #ifdef HW_PHYSMEM64
  133. HW_PHYSMEM64
  134. #else
  135. HW_PHYSMEM
  136. #endif
  137. };
  138. union {
  139. uint32_t u32;
  140. uint64_t u64;
  141. } mem;
  142. size_t mem_ptr_size = sizeof(mem.u64);
  143. if (sysctl(name, 2, &mem.u64, &mem_ptr_size, NULL, 0) != -1) {
  144. // IIRC, 64-bit "return value" is possible on some 64-bit
  145. // BSD systems even with HW_PHYSMEM (instead of HW_PHYSMEM64),
  146. // so support both.
  147. if (mem_ptr_size == sizeof(mem.u64))
  148. ret = mem.u64;
  149. else if (mem_ptr_size == sizeof(mem.u32))
  150. ret = mem.u32;
  151. }
  152. #elif defined(TUKLIB_PHYSMEM_GETSYSINFO)
  153. // Docs are unclear if "start" is needed, but it doesn't hurt
  154. // much to have it.
  155. int memkb;
  156. int start = 0;
  157. if (getsysinfo(GSI_PHYSMEM, (caddr_t)&memkb, sizeof(memkb), &start)
  158. != -1)
  159. ret = (uint64_t)memkb * 1024;
  160. #elif defined(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
  161. struct pst_static pst;
  162. if (pstat_getstatic(&pst, sizeof(pst), 1, 0) != -1)
  163. ret = (uint64_t)pst.physical_memory * (uint64_t)pst.page_size;
  164. #elif defined(TUKLIB_PHYSMEM_GETINVENT_R)
  165. inv_state_t *st = NULL;
  166. if (setinvent_r(&st) != -1) {
  167. inventory_t *i;
  168. while ((i = getinvent_r(st)) != NULL) {
  169. if (i->inv_class == INV_MEMORY
  170. && i->inv_type == INV_MAIN_MB) {
  171. ret = (uint64_t)i->inv_state << 20;
  172. break;
  173. }
  174. }
  175. endinvent_r(st);
  176. }
  177. #elif defined(TUKLIB_PHYSMEM_SYSINFO)
  178. struct sysinfo si;
  179. if (sysinfo(&si) == 0)
  180. ret = (uint64_t)si.totalram * si.mem_unit;
  181. #endif
  182. return ret;
  183. }