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.

236 lines
7.0 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1995 - 1999
  3. Module Name:
  4. Align.h
  5. Abstract:
  6. Defines a macro for aligning an integer value or pointer
  7. to 0 mod 2^n for any n.
  8. Author:
  9. Mario Goertzel [MarioGo]
  10. Revision History:
  11. MarioGo 12-22-95 Bits 'n pieces
  12. MarioGo 02-19-96 Made type safe for C++.
  13. --*/
  14. #ifndef _ALIGN_H
  15. #define _ALIGN_H
  16. #ifdef __cplusplus
  17. //
  18. // The C++ interface looks like
  19. //
  20. // val = Align(val, 8) // returns val aligned to 0 mod 8
  21. // val = Align16(val); // returns val aligned to 0 mod 16
  22. //
  23. // Boths forms on the interface are equally efficient.
  24. //
  25. // Returns the argument aligned up to the nearest "0 mod factor" boundary. Has
  26. // no affect on values which are already aligned to 0 mod factor. The argument
  27. // maybe any integer or void pointer type.
  28. //
  29. //
  30. #define DECL_ALIGN_N(type) inline type Align( type value, int poft) \
  31. { \
  32. return (type)( ((unsigned long)(value) + ((poft)-1)) & ~(poft - 1) ); \
  33. }
  34. #define DECL_ALIGN(poft, type) inline type Align##poft ( type value ) \
  35. { \
  36. return Align(value, poft); \
  37. }
  38. #define DECL_PAD_N(type) inline unsigned int Pad( type value, int poft ) \
  39. { \
  40. return (-(long)value) & (poft - 1); \
  41. }
  42. #define DECL_PAD(poft, type) inline unsigned int Pad##poft (type value) \
  43. { \
  44. return Pad(value, poft); \
  45. }
  46. // same padding, but on pointer type size of argument
  47. #define DECL_ALIGN_PTR_N(type) inline type AlignPtr( type value, int poft) \
  48. { \
  49. return (type)( ((ULONG_PTR)(value) + ((poft)-1)) & ~(poft - 1) ); \
  50. }
  51. #define DECL_ALIGN_PTR(poft, type) inline type AlignPtr##poft ( type value ) \
  52. { \
  53. return AlignPtr(value, poft); \
  54. }
  55. #define DECL_PAD_PTR_N(type) inline unsigned int PadPtr( type value, int poft ) \
  56. { \
  57. return (unsigned int)((-(LONG_PTR)value) & (poft - 1)); \
  58. }
  59. #define DECL_PAD_PTR(poft, type) inline unsigned int PadPtr##poft (type value) \
  60. { \
  61. return PadPtr(value, poft); \
  62. }
  63. #define DECL_ALL_ALIGN(type) \
  64. DECL_ALIGN_N(type) \
  65. DECL_ALIGN(2, type) \
  66. DECL_ALIGN(4, type) \
  67. DECL_ALIGN(8, type) \
  68. DECL_ALIGN(16, type) \
  69. DECL_ALIGN(32, type)
  70. #define DECL_ALL_PAD(type) \
  71. DECL_PAD_N(type) \
  72. DECL_PAD(2, type) \
  73. DECL_PAD(4, type) \
  74. DECL_PAD(8, type) \
  75. DECL_PAD(16, type) \
  76. DECL_PAD(32, type)
  77. #define DECL_ALL_ALIGN_PTR(type) \
  78. DECL_ALIGN_PTR_N(type) \
  79. DECL_ALIGN_PTR(2, type) \
  80. DECL_ALIGN_PTR(4, type) \
  81. DECL_ALIGN_PTR(8, type) \
  82. DECL_ALIGN_PTR(16, type) \
  83. DECL_ALIGN_PTR(32, type)
  84. #define DECL_ALL_PAD_PTR(type) \
  85. DECL_PAD_PTR_N(type) \
  86. DECL_PAD_PTR(2, type) \
  87. DECL_PAD_PTR(4, type) \
  88. DECL_PAD_PTR(8, type) \
  89. DECL_PAD_PTR(16, type) \
  90. DECL_PAD_PTR(32, type)
  91. #define DECL_ALL_ALIGN_AND_PAD(type) \
  92. DECL_ALL_PAD(type) \
  93. DECL_ALL_ALIGN(type)
  94. #define DECL_ALL_ALIGN_AND_PAD_PTR(type) \
  95. DECL_ALL_PAD_PTR(type) \
  96. DECL_ALL_ALIGN_PTR(type)
  97. DECL_ALL_ALIGN_AND_PAD(short)
  98. DECL_ALL_ALIGN_AND_PAD(unsigned short)
  99. DECL_ALL_ALIGN_AND_PAD(long)
  100. DECL_ALL_ALIGN_AND_PAD(unsigned long)
  101. DECL_ALL_ALIGN_AND_PAD(int)
  102. DECL_ALL_ALIGN_AND_PAD(unsigned int)
  103. DECL_ALL_ALIGN_AND_PAD_PTR(void __RPC_FAR *)
  104. #ifdef _WIN64
  105. DECL_ALL_ALIGN_AND_PAD(unsigned __int64)
  106. #endif
  107. #if defined(_WIN64)
  108. #define RPCRT_DEFAULT_STRUCT_ALIGNMENT 8
  109. #define RPCRT_NATURAL_BOUNDARY 16
  110. #else
  111. #define RPCRT_DEFAULT_STRUCT_ALIGNMENT 4
  112. #define RPCRT_NATURAL_BOUNDARY 8
  113. #endif
  114. inline BOOL IsBufferAligned(PVOID p)
  115. {
  116. return (((ULONG_PTR)p % RPCRT_NATURAL_BOUNDARY) == 0);
  117. }
  118. inline BOOL IsBufferAlignedOnStructBoundary(PVOID p)
  119. {
  120. return (((ULONG_PTR)p % RPCRT_DEFAULT_STRUCT_ALIGNMENT) == 0);
  121. }
  122. inline BOOL IsBufferSizeAligned(size_t s)
  123. {
  124. return ((s % RPCRT_NATURAL_BOUNDARY) == 0);
  125. }
  126. inline unsigned int PadToNaturalBoundary (unsigned int Value)
  127. {
  128. #if defined(_WIN64)
  129. return Pad16(Value);
  130. #else
  131. return Pad8(Value);
  132. #endif
  133. }
  134. inline PVOID AlignOnNaturalBoundary (PVOID Value)
  135. {
  136. #if defined(_WIN64)
  137. return AlignPtr16(Value);
  138. #else
  139. return AlignPtr8(Value);
  140. #endif
  141. }
  142. // required for global constant expressions
  143. #define ConstPadN(p, poft) ( (-(long)p) & (poft - 1) )
  144. // The maximum size of the padding when aligning to a natural boundary.
  145. // It is at most (natural boundary - 1) (or max(x mod natural boundary) for all x).
  146. #define RPCRT_NATURAL_BOUNDARY_ALIGNMENT_MAX_SHIFT (RPCRT_NATURAL_BOUNDARY-1)
  147. #define SIZE_OF_OBJECT_AND_PADDING(ObjectType) \
  148. (sizeof(ObjectType) + ConstPadN(sizeof(ObjectType), RPCRT_DEFAULT_STRUCT_ALIGNMENT))
  149. #else
  150. // C interface.
  151. #define AlignN(p, poft) ( ((unsigned long)(p) + ((poft)-1)) & ~(poft - 1) )
  152. #define PadN(p, poft) ( (-(long)p) & (poft - 1) )
  153. #ifdef DOS
  154. #define AlignPtrN(value, poft) (void __far *)AlignN(value, poft)
  155. #define AlignNearPtrN(value, poft) (void __near *)AlignN(value, poft)
  156. #else
  157. #define AlignPtrN(value, poft) (void *)AlignN(value, poft)
  158. #define AlignNearPtrN(value, poft) (void *)AlignN(value, poft)
  159. #endif
  160. // For aligning integer values
  161. #define Align2(p) AlignN((p), 2)
  162. #define Align4(p) AlignN((p), 4)
  163. #define Align8(p) AlignN((p), 8)
  164. #define Align16(p) AlignN((p), 16)
  165. #define Align32(p) AlignN((p), 32)
  166. // For aligning pointers
  167. #define AlignPtr2(p) AlignPtrN((p), 2)
  168. #define AlignPtr4(p) AlignPtrN((p), 4)
  169. #define AlignPtr8(p) AlignPtrN((p), 8)
  170. #define AlignPtr16(p) AlignPtrN((p), 16)
  171. #define AlignPtr32(p) AlignPtrN((p), 32)
  172. // For near pointers
  173. #define AlignNearPtr2(p) AlignNearPtrN((p), 2)
  174. #define AlignNearPtr4(p) AlignNearPtrN((p), 4)
  175. #define AlignNearPtr8(p) AlignNearPtrN((p), 8)
  176. #define AlignNearPtr16(p) AlignNearPtrN((p), 16)
  177. #define AlignNearPtr32(p) AlignNearPtrN((p), 32)
  178. // For everything
  179. #define Pad2(p) PadN((p), 2)
  180. #define Pad4(p) PadN((p), 4)
  181. #define Pad8(p) PadN((p), 8)
  182. #define Pad16(p) PadN((p), 16)
  183. #define Pad32(p) PadN((p), 32)
  184. #endif // __cplusplus
  185. #endif // _ALIGN_H