1 /*-------------------------------------------------------------------------
4 * Tuple macros used by both index tuples and heap tuples.
7 * Portions Copyright (c) 1996-2004, 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.26 2004/08/29 04:13:04 momjian Exp $
12 *-------------------------------------------------------------------------
17 #include "utils/memutils.h"
20 * check to see if the ATT'th bit of an array of 8-bit bytes is set.
22 #define att_isnull(ATT, BITS) (!((BITS)[(ATT) >> 3] & (1 << ((ATT) & 0x07))))
25 * Given a Form_pg_attribute and a pointer into a tuple's data area,
26 * return the correct value or pointer.
28 * We return a Datum value in all cases. If the attribute has "byval" false,
29 * we return the same pointer into the tuple data area that we're passed.
30 * Otherwise, we return the correct number of bytes fetched from the data
31 * area and extended to Datum form.
33 * On machines where Datum is 8 bytes, we support fetching 8-byte byval
34 * attributes; otherwise, only 1, 2, and 4-byte values are supported.
36 * Note that T must already be properly aligned for this to work correctly.
38 #define fetchatt(A,T) fetch_att(T, (A)->attbyval, (A)->attlen)
41 * Same, but work from byval/len parameters rather than Form_pg_attribute.
45 #define fetch_att(T,attbyval,attlen) \
49 (attlen) == (int) sizeof(Datum) ? \
53 (attlen) == (int) sizeof(int32) ? \
54 Int32GetDatum(*((int32 *)(T))) \
57 (attlen) == (int) sizeof(int16) ? \
58 Int16GetDatum(*((int16 *)(T))) \
61 AssertMacro((attlen) == 1), \
62 CharGetDatum(*((char *)(T))) \
68 PointerGetDatum((char *) (T)) \
71 #else /* SIZEOF_DATUM != 8 */
73 #define fetch_att(T,attbyval,attlen) \
77 (attlen) == (int) sizeof(int32) ? \
78 Int32GetDatum(*((int32 *)(T))) \
81 (attlen) == (int) sizeof(int16) ? \
82 Int16GetDatum(*((int16 *)(T))) \
85 AssertMacro((attlen) == 1), \
86 CharGetDatum(*((char *)(T))) \
91 PointerGetDatum((char *) (T)) \
93 #endif /* SIZEOF_DATUM == 8 */
96 * att_align aligns the given offset as needed for a datum of alignment
97 * requirement attalign. The cases are tested in what is hopefully something
98 * like their frequency of occurrence.
100 #define att_align(cur_offset, attalign) \
102 ((attalign) == 'i') ? INTALIGN(cur_offset) : \
103 (((attalign) == 'c') ? ((long)(cur_offset)) : \
104 (((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \
106 AssertMacro((attalign) == 's'), \
107 SHORTALIGN(cur_offset) \
112 * att_addlength increments the given offset by the length of the attribute.
113 * attval is only accessed if we are dealing with a variable-length attribute.
115 #define att_addlength(cur_offset, attlen, attval) \
119 (cur_offset) + (attlen) \
121 : (((attlen) == -1) ? \
123 (cur_offset) + VARATT_SIZE(DatumGetPointer(attval)) \
127 AssertMacro((attlen) == -2), \
128 (cur_offset) + (strlen(DatumGetCString(attval)) + 1) \
133 * store_att_byval is a partial inverse of fetch_att: store a given Datum
134 * value into a tuple data area at the specified address. However, it only
135 * handles the byval case, because in typical usage the caller needs to
136 * distinguish by-val and by-ref cases anyway, and so a do-it-all macro
137 * wouldn't be convenient.
139 #if SIZEOF_DATUM == 8
141 #define store_att_byval(T,newdatum,attlen) \
146 *(char *) (T) = DatumGetChar(newdatum); \
148 case sizeof(int16): \
149 *(int16 *) (T) = DatumGetInt16(newdatum); \
151 case sizeof(int32): \
152 *(int32 *) (T) = DatumGetInt32(newdatum); \
154 case sizeof(Datum): \
155 *(Datum *) (T) = (newdatum); \
158 elog(ERROR, "unsupported byval length: %d", \
164 #else /* SIZEOF_DATUM != 8 */
166 #define store_att_byval(T,newdatum,attlen) \
171 *(char *) (T) = DatumGetChar(newdatum); \
173 case sizeof(int16): \
174 *(int16 *) (T) = DatumGetInt16(newdatum); \
176 case sizeof(int32): \
177 *(int32 *) (T) = DatumGetInt32(newdatum); \
180 elog(ERROR, "unsupported byval length: %d", \
185 #endif /* SIZEOF_DATUM == 8 */