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.

116 lines
4.6 KiB

  1. #ifndef _ALIGN_HPP_
  2. #define _ALIGN_HPP_
  3. // Ruler
  4. // 1 2 3 4 5 6 7 8
  5. //345678901234567890123456789012345678901234567890123456789012345678901234567890
  6. /********************************************************************/
  7. /* */
  8. /* The standard layout. */
  9. /* */
  10. /* The standard layout for 'hpp' files for this code is as */
  11. /* follows: */
  12. /* */
  13. /* 1. Include files. */
  14. /* 2. Constants exported from the class. */
  15. /* 3. Data structures exported from the class. */
  16. /* 4. Forward references to other data structures. */
  17. /* 5. Class specifications (including inline functions). */
  18. /* 6. Additional large inline functions. */
  19. /* */
  20. /* Any portion that is not required is simply omitted. */
  21. /* */
  22. /********************************************************************/
  23. #include "Global.hpp"
  24. #include "New.hpp"
  25. /********************************************************************/
  26. /* */
  27. /* Alignment of structures to cache line boundaries. */
  28. /* */
  29. /* This class aligns data structures to cache line boundaries. */
  30. /* */
  31. /********************************************************************/
  32. template <class TYPE> class ALIGN
  33. {
  34. //
  35. // Private data.
  36. //
  37. TYPE *Aligned;
  38. CHAR *Allocated;
  39. public:
  40. //
  41. // Public functions.
  42. //
  43. ALIGN( VOID );
  44. ~ALIGN( VOID );
  45. //
  46. // Public inline functions.
  47. //
  48. INLINE TYPE *operator&( VOID )
  49. { return Aligned; }
  50. private:
  51. //
  52. // Disabled operations.
  53. //
  54. ALIGN( CONST ALIGN & Copy );
  55. VOID operator=( CONST ALIGN & Copy );
  56. };
  57. /********************************************************************/
  58. /* */
  59. /* Class constructor. */
  60. /* */
  61. /* Ceate a memory allocation and initialize it. This call is */
  62. /* not thread safe and should only be made in a single thread */
  63. /* environment. */
  64. /* */
  65. /********************************************************************/
  66. template <class TYPE> ALIGN<TYPE>::ALIGN( VOID )
  67. {
  68. REGISTER CHAR *Address;
  69. //
  70. // Allocate space for the data structure.
  71. //
  72. Allocated = new CHAR[ (CacheLineSize + sizeof(TYPE)) ];
  73. //
  74. // Call the constructor to initialize the structure.
  75. //
  76. Address = ((CHAR*) ((((LONG) Allocated) + CacheLineMask) & ~CacheLineMask));
  77. Aligned = PLACEMENT_NEW( Address,TYPE );
  78. }
  79. /********************************************************************/
  80. /* */
  81. /* Class destructor. */
  82. /* */
  83. /* Destory the memory allocation. This call is not thread safe */
  84. /* and should only be made in a single thread environment. */
  85. /* */
  86. /********************************************************************/
  87. template <class TYPE> ALIGN<TYPE>::~ALIGN( VOID )
  88. {
  89. //
  90. // Call the destructor for the allocated type.
  91. //
  92. PLACEMENT_DELETE( Aligned,TYPE );
  93. //
  94. // Delete the data structure.
  95. //
  96. delete [] Allocated;
  97. }
  98. #endif