unpack.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #ifdef HAVE_DIX_CONFIG_H
  2. #include <dix-config.h>
  3. #endif
  4. #ifndef __GLX_unpack_h__
  5. #define __GLX_unpack_h__
  6. /*
  7. * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
  8. * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a
  11. * copy of this software and associated documentation files (the "Software"),
  12. * to deal in the Software without restriction, including without limitation
  13. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. * and/or sell copies of the Software, and to permit persons to whom the
  15. * Software is furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice including the dates of first publication and
  18. * either this permission notice or a reference to
  19. * http://oss.sgi.com/projects/FreeB/
  20. * shall be included in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  23. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  26. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  27. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. *
  30. * Except as contained in this notice, the name of Silicon Graphics, Inc.
  31. * shall not be used in advertising or otherwise to promote the sale, use or
  32. * other dealings in this Software without prior written authorization from
  33. * Silicon Graphics, Inc.
  34. */
  35. #define __GLX_PAD(s) (((s)+3) & (GLuint)~3)
  36. /*
  37. ** Fetch the context-id out of a SingleReq request pointed to by pc.
  38. */
  39. #define __GLX_GET_SINGLE_CONTEXT_TAG(pc) (((xGLXSingleReq*)pc)->contextTag)
  40. #define __GLX_GET_VENDPRIV_CONTEXT_TAG(pc) (((xGLXVendorPrivateReq*)pc)->contextTag)
  41. /*
  42. ** Fetch a double from potentially unaligned memory.
  43. */
  44. #ifdef __GLX_ALIGN64
  45. #define __GLX_MEM_COPY(dst,src,n) memmove(dst,src,n)
  46. #define __GLX_GET_DOUBLE(dst,src) __GLX_MEM_COPY(&dst,src,8)
  47. #else
  48. #define __GLX_GET_DOUBLE(dst,src) (dst) = *((GLdouble*)(src))
  49. #endif
  50. extern void __glXMemInit(void);
  51. extern xGLXSingleReply __glXReply;
  52. #define __GLX_BEGIN_REPLY(size) \
  53. __glXReply.length = __GLX_PAD(size) >> 2; \
  54. __glXReply.type = X_Reply; \
  55. __glXReply.sequenceNumber = client->sequence;
  56. #define __GLX_SEND_HEADER() \
  57. WriteToClient (client, sz_xGLXSingleReply, &__glXReply);
  58. #define __GLX_PUT_RETVAL(a) \
  59. __glXReply.retval = (a);
  60. #define __GLX_PUT_SIZE(a) \
  61. __glXReply.size = (a);
  62. #define __GLX_PUT_RENDERMODE(m) \
  63. __glXReply.pad3 = (m)
  64. /*
  65. ** Get a buffer to hold returned data, with the given alignment. If we have
  66. ** to realloc, allocate size+align, in case the pointer has to be bumped for
  67. ** alignment. The answerBuffer should already be aligned.
  68. **
  69. ** NOTE: the cast (long)res below assumes a long is large enough to hold a
  70. ** pointer.
  71. */
  72. #define __GLX_GET_ANSWER_BUFFER(res,cl,size,align) \
  73. if (size < 0) return BadLength; \
  74. else if ((size) > sizeof(answerBuffer)) { \
  75. int bump; \
  76. if ((cl)->returnBufSize < (size)+(align)) { \
  77. (cl)->returnBuf = (GLbyte*)realloc((cl)->returnBuf, \
  78. (size)+(align)); \
  79. if (!(cl)->returnBuf) { \
  80. return BadAlloc; \
  81. } \
  82. (cl)->returnBufSize = (size)+(align); \
  83. } \
  84. res = (char*)cl->returnBuf; \
  85. bump = (long)(res) % (align); \
  86. if (bump) res += (align) - (bump); \
  87. } else { \
  88. res = (char *)answerBuffer; \
  89. }
  90. #define __GLX_PUT_BYTE() \
  91. *(GLbyte *)&__glXReply.pad3 = *(GLbyte *)answer
  92. #define __GLX_PUT_SHORT() \
  93. *(GLshort *)&__glXReply.pad3 = *(GLshort *)answer
  94. #define __GLX_PUT_INT() \
  95. *(GLint *)&__glXReply.pad3 = *(GLint *)answer
  96. #define __GLX_PUT_FLOAT() \
  97. *(GLfloat *)&__glXReply.pad3 = *(GLfloat *)answer
  98. #define __GLX_PUT_DOUBLE() \
  99. *(GLdouble *)&__glXReply.pad3 = *(GLdouble *)answer
  100. #define __GLX_SEND_BYTE_ARRAY(len) \
  101. WriteToClient(client, __GLX_PAD((len)*__GLX_SIZE_INT8), answer)
  102. #define __GLX_SEND_SHORT_ARRAY(len) \
  103. WriteToClient(client, __GLX_PAD((len)*__GLX_SIZE_INT16), answer)
  104. #define __GLX_SEND_INT_ARRAY(len) \
  105. WriteToClient(client, (len)*__GLX_SIZE_INT32, answer)
  106. #define __GLX_SEND_FLOAT_ARRAY(len) \
  107. WriteToClient(client, (len)*__GLX_SIZE_FLOAT32, answer)
  108. #define __GLX_SEND_DOUBLE_ARRAY(len) \
  109. WriteToClient(client, (len)*__GLX_SIZE_FLOAT64, answer)
  110. #define __GLX_SEND_VOID_ARRAY(len) __GLX_SEND_BYTE_ARRAY(len)
  111. #define __GLX_SEND_UBYTE_ARRAY(len) __GLX_SEND_BYTE_ARRAY(len)
  112. #define __GLX_SEND_USHORT_ARRAY(len) __GLX_SEND_SHORT_ARRAY(len)
  113. #define __GLX_SEND_UINT_ARRAY(len) __GLX_SEND_INT_ARRAY(len)
  114. /*
  115. ** PERFORMANCE NOTE:
  116. ** Machine dependent optimizations abound here; these swapping macros can
  117. ** conceivably be replaced with routines that do the job faster.
  118. */
  119. #define __GLX_DECLARE_SWAP_VARIABLES \
  120. GLbyte sw
  121. #define __GLX_DECLARE_SWAP_ARRAY_VARIABLES \
  122. GLbyte *swapPC; \
  123. GLbyte *swapEnd
  124. #define __GLX_SWAP_INT(pc) \
  125. sw = ((GLbyte *)(pc))[0]; \
  126. ((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[3]; \
  127. ((GLbyte *)(pc))[3] = sw; \
  128. sw = ((GLbyte *)(pc))[1]; \
  129. ((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[2]; \
  130. ((GLbyte *)(pc))[2] = sw;
  131. #define __GLX_SWAP_SHORT(pc) \
  132. sw = ((GLbyte *)(pc))[0]; \
  133. ((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[1]; \
  134. ((GLbyte *)(pc))[1] = sw;
  135. #define __GLX_SWAP_DOUBLE(pc) \
  136. sw = ((GLbyte *)(pc))[0]; \
  137. ((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[7]; \
  138. ((GLbyte *)(pc))[7] = sw; \
  139. sw = ((GLbyte *)(pc))[1]; \
  140. ((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[6]; \
  141. ((GLbyte *)(pc))[6] = sw; \
  142. sw = ((GLbyte *)(pc))[2]; \
  143. ((GLbyte *)(pc))[2] = ((GLbyte *)(pc))[5]; \
  144. ((GLbyte *)(pc))[5] = sw; \
  145. sw = ((GLbyte *)(pc))[3]; \
  146. ((GLbyte *)(pc))[3] = ((GLbyte *)(pc))[4]; \
  147. ((GLbyte *)(pc))[4] = sw;
  148. #define __GLX_SWAP_FLOAT(pc) \
  149. sw = ((GLbyte *)(pc))[0]; \
  150. ((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[3]; \
  151. ((GLbyte *)(pc))[3] = sw; \
  152. sw = ((GLbyte *)(pc))[1]; \
  153. ((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[2]; \
  154. ((GLbyte *)(pc))[2] = sw;
  155. #define __GLX_SWAP_INT_ARRAY(pc, count) \
  156. swapPC = ((GLbyte *)(pc)); \
  157. swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_INT32;\
  158. while (swapPC < swapEnd) { \
  159. __GLX_SWAP_INT(swapPC); \
  160. swapPC += __GLX_SIZE_INT32; \
  161. }
  162. #define __GLX_SWAP_SHORT_ARRAY(pc, count) \
  163. swapPC = ((GLbyte *)(pc)); \
  164. swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_INT16;\
  165. while (swapPC < swapEnd) { \
  166. __GLX_SWAP_SHORT(swapPC); \
  167. swapPC += __GLX_SIZE_INT16; \
  168. }
  169. #define __GLX_SWAP_DOUBLE_ARRAY(pc, count) \
  170. swapPC = ((GLbyte *)(pc)); \
  171. swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_FLOAT64;\
  172. while (swapPC < swapEnd) { \
  173. __GLX_SWAP_DOUBLE(swapPC); \
  174. swapPC += __GLX_SIZE_FLOAT64; \
  175. }
  176. #define __GLX_SWAP_FLOAT_ARRAY(pc, count) \
  177. swapPC = ((GLbyte *)(pc)); \
  178. swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_FLOAT32;\
  179. while (swapPC < swapEnd) { \
  180. __GLX_SWAP_FLOAT(swapPC); \
  181. swapPC += __GLX_SIZE_FLOAT32; \
  182. }
  183. #define __GLX_SWAP_REPLY_HEADER() \
  184. __GLX_SWAP_SHORT(&__glXReply.sequenceNumber); \
  185. __GLX_SWAP_INT(&__glXReply.length);
  186. #define __GLX_SWAP_REPLY_RETVAL() \
  187. __GLX_SWAP_INT(&__glXReply.retval)
  188. #define __GLX_SWAP_REPLY_SIZE() \
  189. __GLX_SWAP_INT(&__glXReply.size)
  190. #endif /* !__GLX_unpack_h__ */