1 /*-------------------------------------------------------------------------
4 * Tuple macros used by both index tuples and heap tuples.
7 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: tupmacs.h,v 1.19 2001/10/28 06:25:59 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 length attlen
97 * and alignment requirement attalign. In practice we don't need the length.
98 * The attalign cases are tested in what is hopefully something like their
99 * frequency of occurrence.
101 #define att_align(cur_offset, attlen, attalign) \
103 ((attalign) == 'i') ? INTALIGN(cur_offset) : \
104 (((attalign) == 'c') ? ((long)(cur_offset)) : \
105 (((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \
107 AssertMacro((attalign) == 's'), \
108 SHORTALIGN(cur_offset) \
113 * att_addlength increments the given offset by the length of the attribute.
114 * attval is only accessed if we are dealing with a varlena attribute.
116 #define att_addlength(cur_offset, attlen, attval) \
120 (cur_offset) + (attlen) \
124 (cur_offset) + VARATT_SIZE(DatumGetPointer(attval)) \
129 * store_att_byval is a partial inverse of fetch_att: store a given Datum
130 * value into a tuple data area at the specified address. However, it only
131 * handles the byval case, because in typical usage the caller needs to
132 * distinguish by-val and by-ref cases anyway, and so a do-it-all macro
133 * wouldn't be convenient.
135 #if SIZEOF_DATUM == 8
137 #define store_att_byval(T,newdatum,attlen) \
142 *(char *) (T) = DatumGetChar(newdatum); \
144 case sizeof(int16): \
145 *(int16 *) (T) = DatumGetInt16(newdatum); \
147 case sizeof(int32): \
148 *(int32 *) (T) = DatumGetInt32(newdatum); \
150 case sizeof(Datum): \
151 *(Datum *) (T) = (newdatum); \
154 elog(ERROR, "store_att_byval: unsupported byval length %d", \
160 #else /* SIZEOF_DATUM != 8 */
162 #define store_att_byval(T,newdatum,attlen) \
167 *(char *) (T) = DatumGetChar(newdatum); \
169 case sizeof(int16): \
170 *(int16 *) (T) = DatumGetInt16(newdatum); \
172 case sizeof(int32): \
173 *(int32 *) (T) = DatumGetInt32(newdatum); \
176 elog(ERROR, "store_att_byval: unsupported byval length %d", \
181 #endif /* SIZEOF_DATUM == 8 */