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.

137 lines
3.4 KiB

  1. /*++
  2. Copyright (c) 1988-1992 Microsoft Corporation
  3. Module Name:
  4. Align.h
  5. Author:
  6. John Rogers (JohnRo) 15-May-1991
  7. Environment:
  8. This code assumes that sizeof(DWORD_PTR) >= sizeof(LPVOID).
  9. Revision History:
  10. 15-May-1991 JohnRo
  11. Created align.h for NT/LAN from OS/2 1.2 HPFS pbmacros.h.
  12. 19-Jun-1991 JohnRo
  13. Make sure pointer-to-wider-then-byte doesn't get messed up.
  14. 10-Jul-1991 JohnRo
  15. Added ALIGN_BYTE and ALIGN_CHAR for completeness.
  16. 21-Aug-1991 CliffV
  17. Fix ROUND_DOWN_* to include ~
  18. 03-Dec-1991 JohnRo
  19. Worst-case on MIPS is 8-byte alignment.
  20. Added COUNT_IS_ALIGNED() and POINTER_IS_ALIGNED() macros.
  21. 26-Jun-1992 JohnRo
  22. RAID 9933: ALIGN_WORST should be 8 for x86 builds.
  23. --*/
  24. #ifndef _ALIGN_
  25. #define _ALIGN_
  26. // BOOL
  27. // COUNT_IS_ALIGNED(
  28. // IN DWORD Count,
  29. // IN DWORD Pow2 // undefined if this isn't a power of 2.
  30. // );
  31. //
  32. #define COUNT_IS_ALIGNED(Count,Pow2) \
  33. ( ( ( (Count) & (((Pow2)-1)) ) == 0) ? TRUE : FALSE )
  34. // BOOL
  35. // POINTER_IS_ALIGNED(
  36. // IN LPVOID Ptr,
  37. // IN DWORD Pow2 // undefined if this isn't a power of 2.
  38. // );
  39. //
  40. #define POINTER_IS_ALIGNED(Ptr,Pow2) \
  41. ( ( ( ((ULONG_PTR)(Ptr)) & (((Pow2)-1)) ) == 0) ? TRUE : FALSE )
  42. #define ROUND_DOWN_COUNT(Count,Pow2) \
  43. ( (Count) & (~((Pow2)-1)) )
  44. #define ROUND_DOWN_POINTER(Ptr,Pow2) \
  45. ( (LPVOID) ROUND_DOWN_COUNT( ((ULONG_PTR)(Ptr)), (Pow2) ) )
  46. // If Count is not already aligned, then
  47. // round Count up to an even multiple of "Pow2". "Pow2" must be a power of 2.
  48. //
  49. // DWORD
  50. // ROUND_UP_COUNT(
  51. // IN DWORD Count,
  52. // IN DWORD Pow2
  53. // );
  54. #define ROUND_UP_COUNT(Count,Pow2) \
  55. ( ((Count)+(Pow2)-1) & (~((Pow2)-1)) )
  56. // LPVOID
  57. // ROUND_UP_POINTER(
  58. // IN LPVOID Ptr,
  59. // IN DWORD Pow2
  60. // );
  61. // If Ptr is not already aligned, then round it up until it is.
  62. #define ROUND_UP_POINTER(Ptr,Pow2) \
  63. ( (LPVOID) ( (((ULONG_PTR)(Ptr))+(Pow2)-1) & (~((Pow2)-1)) ) )
  64. // Usage: myPtr = ROUND_UP_POINTER(unalignedPtr, ALIGN_DWORD);
  65. #if defined(_X86_)
  66. #define ALIGN_BYTE 1
  67. #define ALIGN_CHAR 1
  68. #define ALIGN_DESC_CHAR sizeof(DESC_CHAR)
  69. #define ALIGN_DWORD 4
  70. #define ALIGN_LONG 4
  71. #define ALIGN_LPBYTE 4
  72. #define ALIGN_LPDWORD 4
  73. #define ALIGN_LPSTR 4
  74. #define ALIGN_LPTSTR 4
  75. #define ALIGN_LPVOID 4
  76. #define ALIGN_LPWORD 4
  77. #define ALIGN_TCHAR sizeof(TCHAR)
  78. #define ALIGN_WCHAR sizeof(WCHAR)
  79. #define ALIGN_WORD 2
  80. #define ALIGN_QUAD 8
  81. #define ALIGN_WORST 8
  82. #elif defined(_AMD64_) || defined(_IA64_)
  83. #define ALIGN_BYTE 1
  84. #define ALIGN_CHAR 1
  85. #define ALIGN_DESC_CHAR sizeof(DESC_CHAR)
  86. #define ALIGN_DWORD 4
  87. #define ALIGN_LONG 4
  88. #define ALIGN_LPBYTE 8
  89. #define ALIGN_LPDWORD 8
  90. #define ALIGN_LPSTR 8
  91. #define ALIGN_LPTSTR 8
  92. #define ALIGN_LPVOID 8
  93. #define ALIGN_LPWORD 8
  94. #define ALIGN_TCHAR sizeof(TCHAR)
  95. #define ALIGN_WCHAR sizeof(WCHAR)
  96. #define ALIGN_WORD 2
  97. #define ALIGN_QUAD 8
  98. #define ALIGN_WORST 8
  99. #else // none of the above
  100. #error "Unknown alignment requirements for align.h"
  101. #endif // none of the above
  102. #endif // _ALIGN_