EAAlignment.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>
  2. <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  3. <Title>EAAlignment</title>
  4. <link type="text/css" rel="stylesheet" href="UTFDoc.css">
  5. <meta name="author" content="Paul Pedriana">
  6. </head>
  7. <body bgcolor="#FFFFFF">
  8. <h1>EAAlignment</h1>
  9. <h2>Introduction</h2>
  10. <p>EAAlignment provides a number of utilities for working with variable alignment. These include:</p>
  11. <blockquote>
  12. <table border="1">
  13. <tr>
  14. <td><strong>Entity</strong></td>
  15. <td><strong>Description</strong></td>
  16. </tr>
  17. <tr>
  18. <td>EAAlignOf(type)</td>
  19. <td valign="top">Macro which returns the alignment of the given type as a constant expression. </td>
  20. </tr>
  21. <tr>
  22. <td>AlignOf&lt;T&gt;</td>
  23. <td valign="top">Template which returns the alignment of the given type. </td>
  24. </tr>
  25. <tr>
  26. <td>AlignAddressUp<br>
  27. AlignAddressDown</td>
  28. <td valign="top">Function which aligns an arbitrary address up or down to the next user-supplied power of two. </td>
  29. </tr>
  30. <tr>
  31. <td>AlignObjectUp<br>
  32. AlignObjectDown</td>
  33. <td valign="top">Function which aligns an arbitrary object up or down to the next user-supplied power of two. </td>
  34. </tr>
  35. <tr>
  36. <td>GetAlignment<br>
  37. IsAddressAligned<br>
  38. IsObjectAligned&lt;T&gt;<br>
  39. IsAligned&lt;T&gt;<br></td>
  40. <td valign="top">Gets information about alignment. </td>
  41. </tr>
  42. <tr>
  43. <td>AlignedType</td>
  44. <td valign="top">Template which portably allows the re-typing of a class to have a specific alignment. </td>
  45. </tr>
  46. <tr>
  47. <td>AlignedArray</td>
  48. <td valign="top">Template which implements an array of an arbitrary class with a given alignment.</td>
  49. </tr>
  50. <tr>
  51. <td>AlignedObject</td>
  52. <td valign="top">Template which implements an instance of an arbitrary class with a given alignment.</td>
  53. </tr>
  54. <tr>
  55. <td>ReadMisalignedUint16<br>
  56. ReadMisalignedUint32<br>
  57. ReadMisalignedUint64</td>
  58. <td valign="top">Function which safely and portably reads potentially misaligned memory. </td>
  59. </tr>
  60. <tr>
  61. <td>WriteMisalignedUint16<br>
  62. WriteMisalignedUint32<br>
  63. WriteMisalignedUint64</td>
  64. <td valign="top">Function which safely and portably writes potentially misaligned memory. </td>
  65. </tr>
  66. </table>
  67. </blockquote>
  68. <p>We'll discuss some of these briefly.</p>
  69. <h2>EAAlignOf</h2>
  70. <p>EAAlignOf is your basic macro for retrieving the alignment of an object by its type. Recent versions of EABase define EA_ALIGN_OF, so EAAlignOf is currently a duplication of the EABase functionality. </p>
  71. <p>Example usage: </p>
  72. <pre class="code-example">printf(&quot;Alignment of type 'int' is %u.\n&quot;, (unsigned)EAAlignOf(int)); </pre>
  73. <h2>AlignedType</h2>
  74. <p> This class makes an aligned typedef for a given class based on a user-supplied class and alignment. This class exists because of a weakness in VC++ whereby it can only accept __declspec(align) and thus EA_PREFIX_ALIGN usage via an integer literal (e.g. &quot;16&quot;) and not an otherwise equivalent constant integral expression (e.g. sizeof Foo).</p>
  75. <p>Example usage: </p>
  76. <pre class="code-example">const size_t kAlignment = 32; <span class="code-example-comment">// Note that in this case the alignment is defined elsewhere as a non-literal.</span><br>
  77. AlignedType&lt;Widget, kAlignment&gt;::Type widgetAlign128;<br>
  78. widgetAlign128.DoSomething();</pre>
  79. <h2>ReadMisaligned16/32/64</h2>
  80. <p>ReadMisaligned reads an unsigned integer from a possibly non-aligned address. The MIPS processor on the PS2, for example, cannot read a 32-bit value from an unaligned address. This function can be used to make reading such misaligned data portable.. </p>
  81. <p>Example usage: </p>
  82. <pre class="code-example">void DoSomeReading(const char* pData)
  83. {
  84. uint16_t x = ReadMisalignedUint16(pData);
  85. pData += sizeof(uint16_t);
  86. uint32_t y = ReadMisalignedUint32(pData);
  87. pData += sizeof(uint32_t);
  88. uint64_t z = ReadMisalignedUint64(pData);
  89. pData += sizeof(uint64_t);
  90. ...
  91. }</pre>
  92. <hr>
  93. <p>&nbsp;</p>
  94. <p>&nbsp;</p>
  95. <p>&nbsp;</p>
  96. <p>&nbsp;</p>
  97. <p>&nbsp;</p>
  98. <p>&nbsp;</p>
  99. <p>&nbsp;</p>
  100. <p>&nbsp;</p>
  101. <p> </p>
  102. </body></html>