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.

157 lines
3.9 KiB

  1. #ifndef __DUALMODE_H__
  2. #define __DUALMODE_H__
  3. // @doc
  4. /**********************************************************************
  5. *
  6. * @module DualMode.h |
  7. *
  8. * Contains functions/variables etc. for writing components that can
  9. * be run in user or kernel mode.
  10. *
  11. *
  12. * History
  13. * ----------------------------------------------------------
  14. * Mitchell S. Dernis Original
  15. *
  16. * (c) 1986-1998 Microsoft Corporation. All right reserved.
  17. *
  18. * @topic DualMode |
  19. * Provides special services for drivers that differ from
  20. * those of Ring3. Particularly memory allocation.
  21. * This file should be included for any component library that
  22. * must be source compatible for WDM kernel, VxD ring 0, or
  23. * Win32 User mode. Two compile time constants control the
  24. * compilation.<nl>
  25. * COMPILE_FOR_WDM_KERNEL_MODE is defined if the WDM version is needed.
  26. * COMPILE_FOR_VXD_RING0_MODE is defined if the VxD version is needed.<nl>
  27. * NOTIMPL<nl>
  28. * NOTIMPL - VxD version will only be done as a contigency<nl>
  29. * NOTIMPL<nl>
  30. * Neither is defined for user mode.<nl>
  31. **********************************************************************/
  32. #ifdef COMPILE_FOR_WDM_KERNEL_MODE
  33. //
  34. // @topic Overriding global new and delete |
  35. // The global new and delete are overriden to require
  36. // a placement argument specify the pool memory comes from.
  37. // The POOL_TYPE structure is defined in the NTDDK specifying
  38. // the page.<nl>
  39. // The user mode version ignores the POOL_TYPE (but must typedef it)
  40. // and uses the global new and delete.
  41. //
  42. #if (DBG==1)
  43. extern void * __cdecl operator new(unsigned int uSize, POOL_TYPE poolType, LPSTR lpszFile, unsigned int uLine);
  44. #define EXTRANEWPARAMS ,__FILE__,__LINE__
  45. #else
  46. extern void * __cdecl operator new(unsigned int uSize, POOL_TYPE poolType);
  47. #define EXTRANEWPARAMS
  48. #endif
  49. extern void __cdecl operator delete (void * pvRawMemory);
  50. namespace DualMode
  51. {
  52. template<class Type>
  53. Type *Allocate(POOL_TYPE poolType)
  54. {
  55. return new (poolType EXTRANEWPARAMS) Type;
  56. }
  57. template<class Type>
  58. void Deallocate(Type *pMemory)
  59. {
  60. delete pMemory;
  61. }
  62. template<class Type>
  63. Type *AllocateArray(POOL_TYPE poolType, ULONG ulLength)
  64. {
  65. return new (poolType EXTRANEWPARAMS) Type[ulLength];
  66. }
  67. template<class Type>
  68. void DeallocateArray(Type *pMemory)
  69. {
  70. delete [] pMemory;
  71. }
  72. inline void BufferCopy(PVOID pvDest, PVOID pvSrc, ULONG ulByteCount)
  73. {
  74. RtlCopyMemory(pvDest, pvSrc, ulByteCount);
  75. }
  76. };
  77. #if (DBG==1)
  78. #define WDM_NON_PAGED_POOL (NonPagedPool, __FILE__, __LINE__)
  79. #define WDM_PAGED_POOL (PagedPool, __FILE__, __LINE__)
  80. #else
  81. #define WDM_NON_PAGED_POOL (NonPagedPool)
  82. #define WDM_PAGED_POOL (PagedPool)
  83. #endif
  84. #else //END WDM KERNEL MODE SECTION
  85. typedef int POOL_TYPE;
  86. #define NonPagedPool 0
  87. #define NonPagedPoolMustSucceed 0
  88. #define NonPagedPoolCacheAligned 0
  89. #define NonPagedPoolCacheAlignedMustS 0
  90. #define PagedPool 0
  91. #define PagedPoolCacheAligned 0
  92. namespace DualMode
  93. {
  94. template<class Type>
  95. Type *Allocate(POOL_TYPE)
  96. {
  97. return new Type;
  98. }
  99. template<class Type>
  100. void Deallocate(Type *pMemory)
  101. {
  102. delete pMemory;
  103. }
  104. template<class Type>
  105. Type *AllocateArray(POOL_TYPE, ULONG ulLength)
  106. {
  107. return new Type[ulLength];
  108. }
  109. template<class Type>
  110. void DeallocateArray(Type *pMemory)
  111. {
  112. delete [] pMemory;
  113. }
  114. inline void BufferCopy(PVOID pvDest, PVOID pvSrc, ULONG ulByteCount)
  115. {
  116. memcpy(pvDest, pvSrc, ulByteCount);
  117. }
  118. };
  119. //
  120. // Macros for interpreting NTSTATUS codes
  121. //
  122. #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
  123. #define NT_INFORMATION(Status) ((ULONG)(Status) >> 30 == 1)
  124. #define NT_WARNING(Status) ((ULONG)(Status) >> 30 == 2)
  125. #define NT_ERROR(Status) ((ULONG)(Status) >> 30 == 3)
  126. #define WDM_NON_PAGED_POOL
  127. //
  128. // Debug macro definitions
  129. //
  130. #ifndef ASSERT
  131. #define ASSERT _ASSERTE
  132. #endif
  133. #endif //END USER MODE SECTION
  134. #endif //__DUALMODE_H__