WindowsDelayLoadedSymbols.cpp 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2013-2015 Josh Blum
  2. // SPDX-License-Identifier: BSL-1.0
  3. #include <Pothos/Config.hpp>
  4. #include <windows.h>
  5. #include <iostream>
  6. #include <cassert>
  7. /***********************************************************************
  8. * delay load the kernel32 library
  9. **********************************************************************/
  10. static FARPROC GetKernelProcAddress(LPCSTR lpProcName)
  11. {
  12. static HMODULE hKernel = LoadLibrary("kernel32.dll");
  13. assert(hKernel);
  14. FARPROC r = GetProcAddress(hKernel, lpProcName);
  15. if (not r) std::cerr << "This kernel32 does not support " << lpProcName << std::endl;
  16. return r;
  17. }
  18. /***********************************************************************
  19. * set error mode with non-thread safe backup
  20. **********************************************************************/
  21. BOOL DL_SetThreadErrorMode(DWORD dwNewMode, LPDWORD lpOldMode)
  22. {
  23. typedef BOOL (WINAPI * SetThreadErrorMode_t)(DWORD, LPDWORD);
  24. static auto fcn = (SetThreadErrorMode_t)GetKernelProcAddress("SetThreadErrorMode");
  25. if (not fcn)
  26. {
  27. *lpOldMode = SetErrorMode(dwNewMode);
  28. return true;
  29. }
  30. return fcn(dwNewMode, lpOldMode);
  31. }
  32. /***********************************************************************
  33. * numa information
  34. **********************************************************************/
  35. BOOL DL_GetNumaAvailableMemoryNodeEx(USHORT Node, PULONGLONG AvailableBytes)
  36. {
  37. typedef BOOL (WINAPI * GetNumaAvailableMemoryNodeEx_t)(USHORT, PULONGLONG);
  38. static auto fcn = (GetNumaAvailableMemoryNodeEx_t)GetKernelProcAddress("GetNumaAvailableMemoryNodeEx");
  39. if (not fcn) return false;
  40. return fcn(Node, AvailableBytes);
  41. }
  42. BOOL DL_GetNumaNodeProcessorMaskEx(USHORT Node, PGROUP_AFFINITY ProcessorMask)
  43. {
  44. typedef BOOL (WINAPI * GetNumaNodeProcessorMaskEx_t)(USHORT, PGROUP_AFFINITY);
  45. static auto fcn = (GetNumaNodeProcessorMaskEx_t)GetKernelProcAddress("GetNumaNodeProcessorMaskEx");
  46. if (not fcn) return false;
  47. return fcn(Node, ProcessorMask);
  48. }
  49. /***********************************************************************
  50. * numa allocation -- with non-numa backup support
  51. **********************************************************************/
  52. LPVOID DL_VirtualAllocExNuma(HANDLE hProcess, LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect, DWORD nndPreferred)
  53. {
  54. typedef LPVOID (WINAPI * VirtualAllocExNuma_t)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD, DWORD);
  55. static auto fcn = (VirtualAllocExNuma_t)GetKernelProcAddress("VirtualAllocExNuma");
  56. if (not fcn) return VirtualAllocEx(hProcess, lpAddress, dwSize, flAllocationType, flProtect);
  57. return fcn(hProcess, lpAddress, dwSize, flAllocationType, flProtect, nndPreferred);
  58. }
  59. HANDLE DL_CreateFileMappingNuma(HANDLE hFile, LPSECURITY_ATTRIBUTES lpFileMappingAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCTSTR lpName, DWORD nndPreferred)
  60. {
  61. typedef HANDLE (WINAPI * CreateFileMappingNuma_t)(HANDLE, LPSECURITY_ATTRIBUTES, DWORD, DWORD, DWORD, LPCTSTR, DWORD);
  62. static auto fcn = (CreateFileMappingNuma_t)GetKernelProcAddress("CreateFileMappingNumaA");
  63. if (not fcn) return CreateFileMapping(hFile, lpFileMappingAttributes, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
  64. return fcn(hFile, lpFileMappingAttributes, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName, nndPreferred);
  65. }
  66. LPVOID DL_MapViewOfFileExNuma(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap, LPVOID lpBaseAddress, DWORD nndPreferred)
  67. {
  68. typedef LPVOID (WINAPI * MapViewOfFileExNuma_t)(HANDLE, DWORD, DWORD, DWORD, SIZE_T, LPVOID, DWORD);
  69. static auto fcn = (MapViewOfFileExNuma_t)GetKernelProcAddress("MapViewOfFileExNuma");
  70. if (not fcn) return MapViewOfFileEx(hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap, lpBaseAddress);
  71. return fcn(hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap, lpBaseAddress, nndPreferred);
  72. }
  73. /***********************************************************************
  74. * get processor information
  75. **********************************************************************/
  76. BOOL DL_GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP RelationshipType, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX Buffer, PDWORD ReturnedLength)
  77. {
  78. typedef BOOL (WINAPI * GetLogicalProcessorInformationEx_t)(LOGICAL_PROCESSOR_RELATIONSHIP, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD);
  79. static auto fcn = (GetLogicalProcessorInformationEx_t)GetKernelProcAddress("GetLogicalProcessorInformationEx");
  80. if (not fcn) return false;
  81. return fcn(RelationshipType, Buffer, ReturnedLength);
  82. }