/src/postgres/src/include/utils/varbit.h
Line | Count | Source (jump to first uncovered line) |
1 | | /*------------------------------------------------------------------------- |
2 | | * |
3 | | * varbit.h |
4 | | * Functions for the SQL datatypes BIT() and BIT VARYING(). |
5 | | * |
6 | | * Code originally contributed by Adriaan Joubert. |
7 | | * |
8 | | * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
9 | | * Portions Copyright (c) 1994, Regents of the University of California |
10 | | * |
11 | | * src/include/utils/varbit.h |
12 | | * |
13 | | *------------------------------------------------------------------------- |
14 | | */ |
15 | | #ifndef VARBIT_H |
16 | | #define VARBIT_H |
17 | | |
18 | | #include <limits.h> |
19 | | |
20 | | #include "fmgr.h" |
21 | | |
22 | | /* |
23 | | * Modeled on struct varlena from postgres.h, but data type is bits8. |
24 | | * |
25 | | * Caution: if bit_len is not a multiple of BITS_PER_BYTE, the low-order |
26 | | * bits of the last byte of bit_dat[] are unused and MUST be zeroes. |
27 | | * (This allows bit_cmp() to not bother masking the last byte.) |
28 | | * Also, there should not be any excess bytes counted in the header length. |
29 | | */ |
30 | | typedef struct |
31 | | { |
32 | | int32 vl_len_; /* varlena header (do not touch directly!) */ |
33 | | int32 bit_len; /* number of valid bits */ |
34 | | bits8 bit_dat[FLEXIBLE_ARRAY_MEMBER]; /* bit string, most sig. byte |
35 | | * first */ |
36 | | } VarBit; |
37 | | |
38 | | /* |
39 | | * fmgr interface macros |
40 | | * |
41 | | * BIT and BIT VARYING are toastable varlena types. They are the same |
42 | | * as far as representation goes, so we just have one set of macros. |
43 | | */ |
44 | | static inline VarBit * |
45 | | DatumGetVarBitP(Datum X) |
46 | 0 | { |
47 | 0 | return (VarBit *) PG_DETOAST_DATUM(X); |
48 | 0 | } |
49 | | |
50 | | static inline VarBit * |
51 | | DatumGetVarBitPCopy(Datum X) |
52 | 0 | { |
53 | 0 | return (VarBit *) PG_DETOAST_DATUM_COPY(X); |
54 | 0 | } |
55 | | |
56 | | static inline Datum |
57 | | VarBitPGetDatum(const VarBit *X) |
58 | 0 | { |
59 | 0 | return PointerGetDatum(X); |
60 | 0 | } |
61 | | |
62 | 0 | #define PG_GETARG_VARBIT_P(n) DatumGetVarBitP(PG_GETARG_DATUM(n)) |
63 | | #define PG_GETARG_VARBIT_P_COPY(n) DatumGetVarBitPCopy(PG_GETARG_DATUM(n)) |
64 | 0 | #define PG_RETURN_VARBIT_P(x) return VarBitPGetDatum(x) |
65 | | |
66 | | /* Header overhead *in addition to* VARHDRSZ */ |
67 | 0 | #define VARBITHDRSZ sizeof(int32) |
68 | | /* Number of bits in this bit string */ |
69 | 0 | #define VARBITLEN(PTR) (((VarBit *) (PTR))->bit_len) |
70 | | /* Pointer to the first byte containing bit string data */ |
71 | 0 | #define VARBITS(PTR) (((VarBit *) (PTR))->bit_dat) |
72 | | /* Number of bytes in the data section of a bit string */ |
73 | 0 | #define VARBITBYTES(PTR) (VARSIZE(PTR) - VARHDRSZ - VARBITHDRSZ) |
74 | | /* Padding of the bit string at the end (in bits) */ |
75 | 0 | #define VARBITPAD(PTR) (VARBITBYTES(PTR)*BITS_PER_BYTE - VARBITLEN(PTR)) |
76 | | /* Number of bytes needed to store a bit string of a given length */ |
77 | 0 | #define VARBITTOTALLEN(BITLEN) (((BITLEN) + BITS_PER_BYTE-1)/BITS_PER_BYTE + \ |
78 | 0 | VARHDRSZ + VARBITHDRSZ) |
79 | | /* |
80 | | * Maximum number of bits. Several code sites assume no overflow from |
81 | | * computing bitlen + X; VARBITTOTALLEN() has the largest such X. |
82 | | */ |
83 | 0 | #define VARBITMAXLEN (INT_MAX - BITS_PER_BYTE + 1) |
84 | | /* pointer beyond the end of the bit string (like end() in STL containers) */ |
85 | 0 | #define VARBITEND(PTR) (((bits8 *) (PTR)) + VARSIZE(PTR)) |
86 | | /* Mask that will cover exactly one byte, i.e. BITS_PER_BYTE bits */ |
87 | 0 | #define BITMASK 0xFF |
88 | | |
89 | | #endif |