tupmacs.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*-------------------------------------------------------------------------
  2. *
  3. * tupmacs.h
  4. * Tuple macros used by both index tuples and heap tuples.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/access/tupmacs.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef TUPMACS_H
  15. #define TUPMACS_H
  16. /*
  17. * check to see if the ATT'th bit of an array of 8-bit bytes is set.
  18. */
  19. #define att_isnull(ATT, BITS) (!((BITS)[(ATT) >> 3] & (1 << ((ATT) & 0x07))))
  20. /*
  21. * Given a Form_pg_attribute and a pointer into a tuple's data area,
  22. * return the correct value or pointer.
  23. *
  24. * We return a Datum value in all cases. If the attribute has "byval" false,
  25. * we return the same pointer into the tuple data area that we're passed.
  26. * Otherwise, we return the correct number of bytes fetched from the data
  27. * area and extended to Datum form.
  28. *
  29. * On machines where Datum is 8 bytes, we support fetching 8-byte byval
  30. * attributes; otherwise, only 1, 2, and 4-byte values are supported.
  31. *
  32. * Note that T must already be properly aligned for this to work correctly.
  33. */
  34. #define fetchatt(A,T) fetch_att(T, (A)->attbyval, (A)->attlen)
  35. /*
  36. * Same, but work from byval/len parameters rather than Form_pg_attribute.
  37. */
  38. #if SIZEOF_DATUM == 8
  39. #define fetch_att(T,attbyval,attlen) \
  40. ( \
  41. (attbyval) ? \
  42. ( \
  43. (attlen) == (int) sizeof(Datum) ? \
  44. *((Datum *)(T)) \
  45. : \
  46. ( \
  47. (attlen) == (int) sizeof(int32) ? \
  48. Int32GetDatum(*((int32 *)(T))) \
  49. : \
  50. ( \
  51. (attlen) == (int) sizeof(int16) ? \
  52. Int16GetDatum(*((int16 *)(T))) \
  53. : \
  54. ( \
  55. AssertMacro((attlen) == 1), \
  56. CharGetDatum(*((char *)(T))) \
  57. ) \
  58. ) \
  59. ) \
  60. ) \
  61. : \
  62. PointerGetDatum((char *) (T)) \
  63. )
  64. #else /* SIZEOF_DATUM != 8 */
  65. #define fetch_att(T,attbyval,attlen) \
  66. ( \
  67. (attbyval) ? \
  68. ( \
  69. (attlen) == (int) sizeof(int32) ? \
  70. Int32GetDatum(*((int32 *)(T))) \
  71. : \
  72. ( \
  73. (attlen) == (int) sizeof(int16) ? \
  74. Int16GetDatum(*((int16 *)(T))) \
  75. : \
  76. ( \
  77. AssertMacro((attlen) == 1), \
  78. CharGetDatum(*((char *)(T))) \
  79. ) \
  80. ) \
  81. ) \
  82. : \
  83. PointerGetDatum((char *) (T)) \
  84. )
  85. #endif /* SIZEOF_DATUM == 8 */
  86. /*
  87. * att_align_datum aligns the given offset as needed for a datum of alignment
  88. * requirement attalign and typlen attlen. attdatum is the Datum variable
  89. * we intend to pack into a tuple (it's only accessed if we are dealing with
  90. * a varlena type). Note that this assumes the Datum will be stored as-is;
  91. * callers that are intending to convert non-short varlena datums to short
  92. * format have to account for that themselves.
  93. */
  94. #define att_align_datum(cur_offset, attalign, attlen, attdatum) \
  95. ( \
  96. ((attlen) == -1 && VARATT_IS_SHORT(DatumGetPointer(attdatum))) ? \
  97. (uintptr_t) (cur_offset) : \
  98. att_align_nominal(cur_offset, attalign) \
  99. )
  100. /*
  101. * att_align_pointer performs the same calculation as att_align_datum,
  102. * but is used when walking a tuple. attptr is the current actual data
  103. * pointer; when accessing a varlena field we have to "peek" to see if we
  104. * are looking at a pad byte or the first byte of a 1-byte-header datum.
  105. * (A zero byte must be either a pad byte, or the first byte of a correctly
  106. * aligned 4-byte length word; in either case we can align safely. A non-zero
  107. * byte must be either a 1-byte length word, or the first byte of a correctly
  108. * aligned 4-byte length word; in either case we need not align.)
  109. *
  110. * Note: some callers pass a "char *" pointer for cur_offset. This is
  111. * a bit of a hack but should work all right as long as uintptr_t is the
  112. * correct width.
  113. */
  114. #define att_align_pointer(cur_offset, attalign, attlen, attptr) \
  115. ( \
  116. ((attlen) == -1 && VARATT_NOT_PAD_BYTE(attptr)) ? \
  117. (uintptr_t) (cur_offset) : \
  118. att_align_nominal(cur_offset, attalign) \
  119. )
  120. /*
  121. * att_align_nominal aligns the given offset as needed for a datum of alignment
  122. * requirement attalign, ignoring any consideration of packed varlena datums.
  123. * There are three main use cases for using this macro directly:
  124. * * we know that the att in question is not varlena (attlen != -1);
  125. * in this case it is cheaper than the above macros and just as good.
  126. * * we need to estimate alignment padding cost abstractly, ie without
  127. * reference to a real tuple. We must assume the worst case that
  128. * all varlenas are aligned.
  129. * * within arrays, we unconditionally align varlenas (XXX this should be
  130. * revisited, probably).
  131. *
  132. * The attalign cases are tested in what is hopefully something like their
  133. * frequency of occurrence.
  134. */
  135. #define att_align_nominal(cur_offset, attalign) \
  136. ( \
  137. ((attalign) == 'i') ? INTALIGN(cur_offset) : \
  138. (((attalign) == 'c') ? (uintptr_t) (cur_offset) : \
  139. (((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \
  140. ( \
  141. AssertMacro((attalign) == 's'), \
  142. SHORTALIGN(cur_offset) \
  143. ))) \
  144. )
  145. /*
  146. * att_addlength_datum increments the given offset by the space needed for
  147. * the given Datum variable. attdatum is only accessed if we are dealing
  148. * with a variable-length attribute.
  149. */
  150. #define att_addlength_datum(cur_offset, attlen, attdatum) \
  151. att_addlength_pointer(cur_offset, attlen, DatumGetPointer(attdatum))
  152. /*
  153. * att_addlength_pointer performs the same calculation as att_addlength_datum,
  154. * but is used when walking a tuple --- attptr is the pointer to the field
  155. * within the tuple.
  156. *
  157. * Note: some callers pass a "char *" pointer for cur_offset. This is
  158. * actually perfectly OK, but probably should be cleaned up along with
  159. * the same practice for att_align_pointer.
  160. */
  161. #define att_addlength_pointer(cur_offset, attlen, attptr) \
  162. ( \
  163. ((attlen) > 0) ? \
  164. ( \
  165. (cur_offset) + (attlen) \
  166. ) \
  167. : (((attlen) == -1) ? \
  168. ( \
  169. (cur_offset) + VARSIZE_ANY(attptr) \
  170. ) \
  171. : \
  172. ( \
  173. AssertMacro((attlen) == -2), \
  174. (cur_offset) + (strlen((char *) (attptr)) + 1) \
  175. )) \
  176. )
  177. /*
  178. * store_att_byval is a partial inverse of fetch_att: store a given Datum
  179. * value into a tuple data area at the specified address. However, it only
  180. * handles the byval case, because in typical usage the caller needs to
  181. * distinguish by-val and by-ref cases anyway, and so a do-it-all macro
  182. * wouldn't be convenient.
  183. */
  184. #if SIZEOF_DATUM == 8
  185. #define store_att_byval(T,newdatum,attlen) \
  186. do { \
  187. switch (attlen) \
  188. { \
  189. case sizeof(char): \
  190. *(char *) (T) = DatumGetChar(newdatum); \
  191. break; \
  192. case sizeof(int16): \
  193. *(int16 *) (T) = DatumGetInt16(newdatum); \
  194. break; \
  195. case sizeof(int32): \
  196. *(int32 *) (T) = DatumGetInt32(newdatum); \
  197. break; \
  198. case sizeof(Datum): \
  199. *(Datum *) (T) = (newdatum); \
  200. break; \
  201. default: \
  202. elog(ERROR, "unsupported byval length: %d", \
  203. (int) (attlen)); \
  204. break; \
  205. } \
  206. } while (0)
  207. #else /* SIZEOF_DATUM != 8 */
  208. #define store_att_byval(T,newdatum,attlen) \
  209. do { \
  210. switch (attlen) \
  211. { \
  212. case sizeof(char): \
  213. *(char *) (T) = DatumGetChar(newdatum); \
  214. break; \
  215. case sizeof(int16): \
  216. *(int16 *) (T) = DatumGetInt16(newdatum); \
  217. break; \
  218. case sizeof(int32): \
  219. *(int32 *) (T) = DatumGetInt32(newdatum); \
  220. break; \
  221. default: \
  222. elog(ERROR, "unsupported byval length: %d", \
  223. (int) (attlen)); \
  224. break; \
  225. } \
  226. } while (0)
  227. #endif /* SIZEOF_DATUM == 8 */
  228. #endif