Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.8 KiB

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright 1998-2003 Microsoft Corporation. All Rights Reserved.
  7. //
  8. // FILE: KMode.h
  9. //
  10. //
  11. // PURPOSE: Definitions and routines for compiling kernel mode instead of user mode.
  12. //
  13. // PLATFORMS:
  14. // Windows 2000, Windows XP, Windows Server 2003
  15. //
  16. //
  17. #ifndef _KMODE_H
  18. #define _KMODE_H
  19. // Define from ntdef.h in Win2K SDK.
  20. // NT 4 may not have this defined
  21. // in the public headers.
  22. #ifndef NOP_FUNCTION
  23. #if (_MSC_VER >= 1210)
  24. #define NOP_FUNCTION __noop
  25. #else
  26. #define NOP_FUNCTION (void)0
  27. #endif
  28. #endif
  29. #ifdef USERMODE_DRIVER
  30. //
  31. // User mode difinitions to get rid of defines for kernel mode.
  32. //
  33. // Don't need critical section in user mode.
  34. #define DECLARE_CRITICAL_SECTION ;
  35. #define INIT_CRITICAL_SECTION() NOP_FUNCTION
  36. #define DELETE_CRITICAL_SECTION() NOP_FUNCTION
  37. #define IS_VALID_CRITICAL_SECTION() (TRUE)
  38. #else // !USERMODE_DRIVER
  39. ////////////////////////////////////////////////////////
  40. // Kernel Mode Defines
  41. ////////////////////////////////////////////////////////
  42. extern HSEMAPHORE ghOEMSemaphore;
  43. #define DECLARE_CRITICAL_SECTION HSEMAPHORE ghOEMSemaphore = NULL;
  44. #define INIT_CRITICAL_SECTION() ghOEMSemaphore = EngCreateSemaphore()
  45. #define ENTER_CRITICAL_SECTION() EngAcquireSemaphore(ghOEMSemaphore)
  46. #define LEAVE_CRITICAL_SECTION() EngReleaseSemaphore(ghOEMSemaphore)
  47. #define DELETE_CRITICAL_SECTION() EngDeleteSemaphore(ghOEMSemaphore)
  48. #define IS_VALID_CRITICAL_SECTION() (NULL != ghOEMSemaphore)
  49. #define DebugBreak EngDebugBreak
  50. // Pool tag marker for memory marking memory allocations.
  51. #define DRV_MEM_POOL_TAG 'meoD'
  52. // Debug prefix that is outputted in the debug messages.
  53. #define DEBUG_PREFIX "OEMDLL: "
  54. // Remap user mode functions that don't have kernel mode
  55. // equivalents to functions that we implement ourselves.
  56. #define OutputDebugStringA(pszMsg) (MyDebugPrint(DEBUG_PREFIX, "%hs", pszMsg))
  57. #define OutputDebugStringW(pszMsg) (MyDebugPrint(DEBUG_PREFIX, "%ls", pszMsg))
  58. #if !defined(_M_ALPHA) && !defined(_M_IA64)
  59. #define InterlockedIncrement DrvInterlockedIncrement
  60. #define InterlockedDecrement DrvInterlockedDecrement
  61. #endif
  62. #define SetLastError NOP_FUNCTION
  63. #define GetLastError NOP_FUNCTION
  64. ////////////////////////////////////////////////////////
  65. // Kernel Mode Functions
  66. ////////////////////////////////////////////////////////
  67. //
  68. // Implement inline functions to replace user mode
  69. // functions that don't have kernel mode equivalents.
  70. //
  71. inline int __cdecl _purecall (void)
  72. {
  73. #ifdef DEBUG
  74. EngDebugBreak();
  75. #endif
  76. return E_FAIL;
  77. }
  78. inline LONG DrvInterlockedIncrement(PLONG pRef)
  79. {
  80. ENTER_CRITICAL_SECTION();
  81. ++(*pRef);
  82. LEAVE_CRITICAL_SECTION();
  83. return (*pRef);
  84. }
  85. inline LONG DrvInterlockedDecrement(PLONG pRef)
  86. {
  87. ENTER_CRITICAL_SECTION();
  88. --(*pRef);
  89. LEAVE_CRITICAL_SECTION();
  90. return (*pRef);
  91. }
  92. inline void* __cdecl operator new(size_t nSize)
  93. {
  94. // return pointer to allocated memory
  95. return EngAllocMem(0, nSize, DRV_MEM_POOL_TAG);
  96. }
  97. inline void __cdecl operator delete(void *pMem)
  98. {
  99. if(pMem)
  100. EngFreeMem(pMem);
  101. }
  102. inline VOID MyDebugPrint(PCHAR pszPrefix, PCHAR pszFormat, ...)
  103. {
  104. va_list VAList;
  105. va_start(VAList, pszFormat);
  106. EngDebugPrint(DEBUG_PREFIX, pszFormat, VAList);
  107. va_end(VAList);
  108. return;
  109. }
  110. #endif // USERMODE_DRIVER
  111. #endif // _KMODE_H