Source code of Windows XP (NT5)
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.7 KiB

  1. /********************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1990-2000 **/
  4. /********************************************************************/
  5. /* :ts=4 */
  6. #ifndef OSCFG_INCLUDED
  7. #define OSCFG_INCLUDED
  8. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  9. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  10. // My binary compatible definition for compiling an Millennium tcpip.sys
  11. #if MILLEN
  12. #include "wdm.h"
  13. #define KdPrintEx(_x_)
  14. #else // MILLEN
  15. #include <ntosp.h>
  16. #include <zwapi.h>
  17. #endif // !MILLEN
  18. #define BEGIN_INIT
  19. #define END_INIT
  20. #if defined (_WIN64)
  21. #define MAX_CACHE_LINE_SIZE 128
  22. #else
  23. #define MAX_CACHE_LINE_SIZE 64
  24. #endif
  25. #define CACHE_ALIGN __declspec(align(MAX_CACHE_LINE_SIZE))
  26. typedef struct CACHE_ALIGN _CACHE_LINE_KSPIN_LOCK {
  27. KSPIN_LOCK Lock;
  28. } CACHE_LINE_KSPIN_LOCK;
  29. C_ASSERT(sizeof(CACHE_LINE_KSPIN_LOCK) % MAX_CACHE_LINE_SIZE == 0);
  30. C_ASSERT(__alignof(CACHE_LINE_KSPIN_LOCK) == MAX_CACHE_LINE_SIZE);
  31. typedef struct CACHE_ALIGN _CACHE_LINE_SLIST_HEADER {
  32. SLIST_HEADER SListHead;
  33. } CACHE_LINE_SLIST_HEADER;
  34. C_ASSERT(sizeof(CACHE_LINE_SLIST_HEADER) % MAX_CACHE_LINE_SIZE == 0);
  35. C_ASSERT(__alignof(CACHE_LINE_SLIST_HEADER) == MAX_CACHE_LINE_SIZE);
  36. typedef struct CACHE_ALIGN _CACHE_LINE_ULONG {
  37. ULONG Value;
  38. } CACHE_LINE_ULONG;
  39. C_ASSERT(sizeof(CACHE_LINE_ULONG) % MAX_CACHE_LINE_SIZE == 0);
  40. C_ASSERT(__alignof(CACHE_LINE_ULONG) == MAX_CACHE_LINE_SIZE);
  41. #if (defined(_M_IX86) && (_MSC_FULL_VER > 13009037)) || ((defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_FULL_VER > 13009175))
  42. #define net_short(_x) _byteswap_ushort((USHORT)(_x))
  43. #define net_long(_x) _byteswap_ulong(_x)
  44. #else
  45. __inline
  46. USHORT
  47. FASTCALL
  48. net_short(
  49. ULONG NaturalData)
  50. {
  51. USHORT ShortData = (USHORT)NaturalData;
  52. return (ShortData << 8) | (ShortData >> 8);
  53. }
  54. // if x is aabbccdd (where aa, bb, cc, dd are hex bytes)
  55. // we want net_long(x) to be ddccbbaa. A small and fast way to do this is
  56. // to first byteswap it to get bbaaddcc and then swap high and low words.
  57. //
  58. __inline
  59. ULONG
  60. FASTCALL
  61. net_long(
  62. ULONG NaturalData)
  63. {
  64. ULONG ByteSwapped;
  65. ByteSwapped = ((NaturalData & 0x00ff00ff) << 8) |
  66. ((NaturalData & 0xff00ff00) >> 8);
  67. return (ByteSwapped << 16) | (ByteSwapped >> 16);
  68. }
  69. #endif
  70. __inline
  71. BOOLEAN
  72. IsPowerOfTwo(
  73. ULONG Value
  74. )
  75. {
  76. return (Value & (Value - 1)) == 0;
  77. }
  78. // Find the highest power of two that is greater
  79. // than or equal to the Value.
  80. //
  81. __inline
  82. ULONG
  83. ComputeLargerOrEqualPowerOfTwo(
  84. ULONG Value
  85. )
  86. {
  87. ULONG Temp;
  88. for (Temp = 1; Temp < Value; Temp <<= 1);
  89. return Temp;
  90. }
  91. // Find the highest power of two, in the form of its shift, that is greater
  92. // than or equal to the Value.
  93. //
  94. __inline
  95. ULONG
  96. ComputeShiftForLargerOrEqualPowerOfTwo(
  97. ULONG Value
  98. )
  99. {
  100. ULONG Shift;
  101. ULONG Temp;
  102. for (Temp = 1, Shift = 0; Temp < Value; Temp <<= 1, Shift++);
  103. return Shift;
  104. }
  105. __inline
  106. VOID
  107. FASTCALL
  108. CTEGetLockAtIrql (
  109. IN PKSPIN_LOCK SpinLock,
  110. IN KIRQL OrigIrql,
  111. OUT PKIRQL OldIrql)
  112. {
  113. #if !MILLEN
  114. if (DISPATCH_LEVEL == OrigIrql) {
  115. ASSERT(DISPATCH_LEVEL == KeGetCurrentIrql());
  116. ExAcquireSpinLockAtDpcLevel(SpinLock);
  117. *OldIrql = DISPATCH_LEVEL;
  118. } else {
  119. ExAcquireSpinLock(SpinLock, OldIrql);
  120. }
  121. #else
  122. *OldIrql = 0;
  123. #endif
  124. }
  125. __inline
  126. VOID
  127. FASTCALL
  128. CTEFreeLockAtIrql (
  129. IN PKSPIN_LOCK SpinLock,
  130. IN KIRQL OrigIrql,
  131. IN KIRQL NewIrql)
  132. {
  133. #if !MILLEN
  134. if (DISPATCH_LEVEL == OrigIrql) {
  135. ASSERT(DISPATCH_LEVEL == NewIrql);
  136. ASSERT(DISPATCH_LEVEL == KeGetCurrentIrql());
  137. ExReleaseSpinLockFromDpcLevel(SpinLock);
  138. } else {
  139. ExReleaseSpinLock(SpinLock, NewIrql);
  140. }
  141. #endif
  142. }
  143. #endif // OSCFG_INCLUDED