Windows NT 4.0 source code leak
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.

144 lines
3.9 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  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. #define DECL_ALL_ALIGN(type) \
  47. DECL_ALIGN_N(type) \
  48. DECL_ALIGN(2, type) \
  49. DECL_ALIGN(4, type) \
  50. DECL_ALIGN(8, type) \
  51. DECL_ALIGN(16, type) \
  52. DECL_ALIGN(32, type)
  53. #define DECL_ALL_PAD(type) \
  54. DECL_PAD_N(type) \
  55. DECL_PAD(2, type) \
  56. DECL_PAD(4, type) \
  57. DECL_PAD(8, type) \
  58. DECL_PAD(16, type) \
  59. DECL_PAD(32, type)
  60. #define DECL_ALL_ALIGN_AND_PAD(type) \
  61. DECL_ALL_PAD(type) \
  62. DECL_ALL_ALIGN(type)
  63. DECL_ALL_ALIGN_AND_PAD(short)
  64. DECL_ALL_ALIGN_AND_PAD(unsigned short)
  65. DECL_ALL_ALIGN_AND_PAD(long)
  66. DECL_ALL_ALIGN_AND_PAD(unsigned long)
  67. DECL_ALL_ALIGN_AND_PAD(int)
  68. DECL_ALL_ALIGN_AND_PAD(unsigned int)
  69. DECL_ALL_ALIGN_AND_PAD(void __RPC_FAR *)
  70. #ifdef DOS
  71. DECL_ALL_ALIGN_AND_PAD(void __near *)
  72. #endif
  73. #else
  74. // C interface.
  75. #define AlignN(p, poft) ( ((unsigned long)(p) + ((poft)-1)) & ~(poft - 1) )
  76. #define PadN(p, poft) ( (-(long)p) & (poft - 1) )
  77. #ifdef DOS
  78. #define AlignPtrN(value, poft) (void __far *)AlignN(value, poft)
  79. #define AlignNearPtrN(value, poft) (void __near *)AlignN(value, poft)
  80. #else
  81. #define AlignPtrN(value, poft) (void *)AlignN(value, poft)
  82. #define AlignNearPtrN(value, poft) (void *)AlignN(value, poft)
  83. #endif
  84. // For aligning integer values
  85. #define Align2(p) AlignN((p), 2)
  86. #define Align4(p) AlignN((p), 4)
  87. #define Align8(p) AlignN((p), 8)
  88. #define Align16(p) AlignN((p), 16)
  89. #define Align32(p) AlignN((p), 32)
  90. // For aligning pointers
  91. #define AlignPtr2(p) AlignPtrN((p), 2)
  92. #define AlignPtr4(p) AlignPtrN((p), 4)
  93. #define AlignPtr8(p) AlignPtrN((p), 8)
  94. #define AlignPtr16(p) AlignPtrN((p), 16)
  95. #define AlignPtr32(p) AlignPtrN((p), 32)
  96. // For near pointers
  97. #define AlignNearPtr2(p) AlignNearPtrN((p), 2)
  98. #define AlignNearPtr4(p) AlignNearPtrN((p), 4)
  99. #define AlignNearPtr8(p) AlignNearPtrN((p), 8)
  100. #define AlignNearPtr16(p) AlignNearPtrN((p), 16)
  101. #define AlignNearPtr32(p) AlignNearPtrN((p), 32)
  102. // For everything
  103. #define Pad2(p) PadN((p), 2)
  104. #define Pad4(p) PadN((p), 4)
  105. #define Pad8(p) PadN((p), 8)
  106. #define Pad16(p) PadN((p), 16)
  107. #define Pad32(p) PadN((p), 32)
  108. #endif // __cplusplus
  109. #endif // _ALIGN_H