]> granicus.if.org Git - postgresql/blob - src/include/access/tupmacs.h
Add:
[postgresql] / src / include / access / tupmacs.h
1 /*-------------------------------------------------------------------------
2  *
3  * tupmacs.h
4  *        Tuple macros used by both index tuples and heap tuples.
5  *
6  *
7  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: tupmacs.h,v 1.13 2000/01/26 05:57:51 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef TUPMACS_H
15 #define TUPMACS_H
16
17 #include "utils/memutils.h"
18
19 /*
20  * check to see if the ATT'th bit of an array of 8-bit bytes is set.
21  */
22 #define att_isnull(ATT, BITS) (!((BITS)[(ATT) >> 3] & (1 << ((ATT) & 0x07))))
23
24 /*
25  * given a Form_pg_attribute and a pointer into a tuple's data
26  * area, return the correct value or pointer.
27  *
28  * We return a 4 byte (char *) value in all cases.      If the attribute has
29  * "byval" false or has variable length, we return the same pointer
30  * into the tuple data area that we're passed.  Otherwise, we return
31  * the 1, 2, or 4 bytes pointed to by it, properly extended to 4
32  * bytes, depending on the length of the attribute.
33  *
34  * note that T must already be properly LONGALIGN/SHORTALIGN'd for
35  * this to work correctly.
36  *
37  * the double-cast is to stop gcc from (correctly) complaining about
38  * casting integer types with size < sizeof(char *) to (char *).
39  * sign-extension may get weird if you use an integer type that
40  * isn't the same size as (char *) for the first cast.  (on the other
41  * hand, it's safe to use another type for the (foo *)(T).)
42  *
43  * attbyval seems to be fairly redundant.  We have to return a pointer if
44  * the value is longer than 4 bytes or has variable length; returning the
45  * value would be useless.      In fact, for at least the variable length case,
46  * the caller assumes we return a pointer regardless of attbyval.
47  * I would eliminate attbyval altogether, but I don't know how.  -BRYANH.
48  */
49 #define fetchatt(A, T) \
50 ( \
51         (*(A))->attbyval && (*(A))->attlen != -1 ? \
52         ( \
53                 (*(A))->attlen > sizeof(int16) ? \
54                 ( \
55                         (char *) (long) *((int32 *)(T)) \
56                 ) \
57                 : \
58                 ( \
59                         (*(A))->attlen < sizeof(int16) ? \
60                                 (char *) (long) *((char *)(T)) \
61                         : \
62                                 (char *) (long) *((int16 *)(T))) \
63                 ) \
64         : \
65         (char *) (T) \
66 )
67
68 /* att_align aligns the given offset as needed for a datum of length attlen
69  * and alignment requirement attalign.  In practice we don't need the length.
70  * The attalign cases are tested in what is hopefully something like their
71  * frequency of occurrence.
72  */
73 #define att_align(cur_offset, attlen, attalign) \
74 ( \
75         ((attalign) == 'i') ? INTALIGN(cur_offset) : \
76          (((attalign) == 'c') ? ((long)(cur_offset)) : \
77           (((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \
78                 ( \
79                         AssertMacro((attalign) == 's'), \
80                         SHORTALIGN(cur_offset) \
81                 ))) \
82 )
83
84 #define att_addlength(cur_offset, attlen, attval) \
85 ( \
86         ((attlen) != -1) ? \
87         ( \
88                 (cur_offset) + (attlen) \
89         ) \
90         : \
91         ( \
92                 (cur_offset) + VARATT_SIZE(DatumGetPointer(attval)) \
93         ) \
94 )
95
96 #endif