1 /*-------------------------------------------------------------------------
4 * Tuple macros used by both index tuples and heap tuples.
7 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/include/access/tupmacs.h,v 1.28 2005/05/06 17:24:55 tgl Exp $
12 *-------------------------------------------------------------------------
19 * check to see if the ATT'th bit of an array of 8-bit bytes is set.
21 #define att_isnull(ATT, BITS) (!((BITS)[(ATT) >> 3] & (1 << ((ATT) & 0x07))))
24 * Given a Form_pg_attribute and a pointer into a tuple's data area,
25 * return the correct value or pointer.
27 * We return a Datum value in all cases. If the attribute has "byval" false,
28 * we return the same pointer into the tuple data area that we're passed.
29 * Otherwise, we return the correct number of bytes fetched from the data
30 * area and extended to Datum form.
32 * On machines where Datum is 8 bytes, we support fetching 8-byte byval
33 * attributes; otherwise, only 1, 2, and 4-byte values are supported.
35 * Note that T must already be properly aligned for this to work correctly.
37 #define fetchatt(A,T) fetch_att(T, (A)->attbyval, (A)->attlen)
40 * Same, but work from byval/len parameters rather than Form_pg_attribute.
44 #define fetch_att(T,attbyval,attlen) \
48 (attlen) == (int) sizeof(Datum) ? \
52 (attlen) == (int) sizeof(int32) ? \
53 Int32GetDatum(*((int32 *)(T))) \
56 (attlen) == (int) sizeof(int16) ? \
57 Int16GetDatum(*((int16 *)(T))) \
60 AssertMacro((attlen) == 1), \
61 CharGetDatum(*((char *)(T))) \
67 PointerGetDatum((char *) (T)) \
70 #else /* SIZEOF_DATUM != 8 */
72 #define fetch_att(T,attbyval,attlen) \
76 (attlen) == (int) sizeof(int32) ? \
77 Int32GetDatum(*((int32 *)(T))) \
80 (attlen) == (int) sizeof(int16) ? \
81 Int16GetDatum(*((int16 *)(T))) \
84 AssertMacro((attlen) == 1), \
85 CharGetDatum(*((char *)(T))) \
90 PointerGetDatum((char *) (T)) \
92 #endif /* SIZEOF_DATUM == 8 */
95 * att_align aligns the given offset as needed for a datum of alignment
96 * requirement attalign. The cases are tested in what is hopefully something
97 * like their frequency of occurrence.
99 #define att_align(cur_offset, attalign) \
101 ((attalign) == 'i') ? INTALIGN(cur_offset) : \
102 (((attalign) == 'c') ? ((long)(cur_offset)) : \
103 (((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \
105 AssertMacro((attalign) == 's'), \
106 SHORTALIGN(cur_offset) \
111 * att_addlength increments the given offset by the length of the attribute.
112 * attval is only accessed if we are dealing with a variable-length attribute.
114 #define att_addlength(cur_offset, attlen, attval) \
118 (cur_offset) + (attlen) \
120 : (((attlen) == -1) ? \
122 (cur_offset) + VARATT_SIZE(DatumGetPointer(attval)) \
126 AssertMacro((attlen) == -2), \
127 (cur_offset) + (strlen(DatumGetCString(attval)) + 1) \
132 * store_att_byval is a partial inverse of fetch_att: store a given Datum
133 * value into a tuple data area at the specified address. However, it only
134 * handles the byval case, because in typical usage the caller needs to
135 * distinguish by-val and by-ref cases anyway, and so a do-it-all macro
136 * wouldn't be convenient.
138 #if SIZEOF_DATUM == 8
140 #define store_att_byval(T,newdatum,attlen) \
145 *(char *) (T) = DatumGetChar(newdatum); \
147 case sizeof(int16): \
148 *(int16 *) (T) = DatumGetInt16(newdatum); \
150 case sizeof(int32): \
151 *(int32 *) (T) = DatumGetInt32(newdatum); \
153 case sizeof(Datum): \
154 *(Datum *) (T) = (newdatum); \
157 elog(ERROR, "unsupported byval length: %d", \
163 #else /* SIZEOF_DATUM != 8 */
165 #define store_att_byval(T,newdatum,attlen) \
170 *(char *) (T) = DatumGetChar(newdatum); \
172 case sizeof(int16): \
173 *(int16 *) (T) = DatumGetInt16(newdatum); \
175 case sizeof(int32): \
176 *(int32 *) (T) = DatumGetInt32(newdatum); \
179 elog(ERROR, "unsupported byval length: %d", \
184 #endif /* SIZEOF_DATUM == 8 */