win32_glue.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Copyright (c) 2019-2023 Bruce A Henderson
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #define _WIN32_WINNT 0x0601
  19. #include "windows.h"
  20. void bmx_os_getwindowsversion(int * major, int * minor, int * build) {
  21. OSVERSIONINFOEX versionInfo = {0};
  22. versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
  23. HINSTANCE nt = LoadLibrary("ntdll.dll");
  24. if (nt != NULL) {
  25. NTSTATUS (WINAPI *rtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation) = (NTSTATUS (WINAPI *)(PRTL_OSVERSIONINFOW)) GetProcAddress (nt, "RtlGetVersion");
  26. if (rtlGetVersion != NULL) {
  27. rtlGetVersion(&versionInfo);
  28. } else {
  29. GetVersionEx(&versionInfo);
  30. }
  31. int _major = versionInfo.dwMajorVersion;
  32. int _minor = versionInfo.dwMinorVersion;
  33. int _build = versionInfo.dwBuildNumber;
  34. switch (_major) {
  35. case 5:
  36. if (_minor == 1 || _minor == 2) {
  37. *major = _major;
  38. *minor = _minor;
  39. *build = _build;
  40. return;
  41. }
  42. break;
  43. case 6:
  44. switch (_minor) {
  45. case 0:
  46. *major = _major;
  47. *minor = _minor;
  48. *build = 0;
  49. return;
  50. case 1:
  51. *major = 7;
  52. *minor = 0;
  53. *build = 0;
  54. return;
  55. case 2:
  56. *major = 8;
  57. *minor = 0;
  58. *build = 0;
  59. return;
  60. case 3:
  61. *major = 8;
  62. *minor = 1;
  63. *build = 0;
  64. return;
  65. case 4:
  66. *major = 10;
  67. *minor = 0;
  68. *build = 0;
  69. return;
  70. }
  71. break;
  72. case 10:
  73. if (_major == 10 && _minor == 0 && _build == 22000) {
  74. *major = 11;
  75. *minor = 0;
  76. *build = 0;
  77. return;
  78. }
  79. if (_major == 10 && _minor >= 1) {
  80. *major = 12;
  81. *minor = 0;
  82. *build = 0;
  83. return;
  84. }
  85. *major = _major;
  86. *minor = _minor;
  87. *build = _build;
  88. return;
  89. }
  90. }
  91. // don't know what version this is...
  92. *major = 0;
  93. *minor = 0;
  94. *build = 0;
  95. }
  96. typedef DWORD (WINAPI *ActiveProcessorCount)(WORD);
  97. typedef BOOL (WINAPI *LogicalProcessorInformationEx)(LOGICAL_PROCESSOR_RELATIONSHIP, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD);
  98. static int kernelLoaded = 0;
  99. static ActiveProcessorCount gapcFunc = 0;
  100. static LogicalProcessorInformationEx glpieFunc = 0;
  101. int bmx_os_getproccount() {
  102. SYSTEM_INFO info;
  103. GetSystemInfo(&info);
  104. return info.dwNumberOfProcessors;
  105. }
  106. static int bmx_os_calc_processor_count() {
  107. if (glpieFunc) {
  108. DWORD bufferLength = 0;
  109. BOOL result = glpieFunc(RelationAll, NULL, &bufferLength);
  110. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  111. return -1;
  112. }
  113. BYTE buffer[bufferLength];
  114. result = glpieFunc(RelationAll, buffer, &bufferLength);
  115. if (!result) {
  116. return -1;
  117. }
  118. DWORD offset = 0;
  119. int count = 0;
  120. while (offset < bufferLength) {
  121. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)(buffer + offset);
  122. if (info->Relationship == RelationProcessorCore) {
  123. count++;
  124. }
  125. offset += info->Size;
  126. }
  127. return count;
  128. }
  129. return -1;
  130. }
  131. int bmx_os_getphysproccount() {
  132. int count = 0;
  133. if (!kernelLoaded) {
  134. HINSTANCE kernel32 = LoadLibraryA("Kernel32.dll");
  135. if (kernel32) {
  136. glpieFunc = (LogicalProcessorInformationEx)GetProcAddress(kernel32, "GetLogicalProcessorInformationEx");
  137. if (!glpieFunc) {
  138. gapcFunc = (ActiveProcessorCount)GetProcAddress(kernel32, "GetActiveProcessorCount");
  139. }
  140. }
  141. kernelLoaded = 1;
  142. }
  143. count = bmx_os_calc_processor_count();
  144. if (count >= 0) {
  145. return count;
  146. } else if (gapcFunc) { // fallback
  147. count = gapcFunc(ALL_PROCESSOR_GROUPS);
  148. } else { // fallback
  149. count = bmx_os_getproccount();
  150. }
  151. return count;
  152. }