2
0

varbit.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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-2022, 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. #include <limits.h>
  18. #include "fmgr.h"
  19. /*
  20. * Modeled on struct varlena from postgres.h, but data type is bits8.
  21. *
  22. * Caution: if bit_len is not a multiple of BITS_PER_BYTE, the low-order
  23. * bits of the last byte of bit_dat[] are unused and MUST be zeroes.
  24. * (This allows bit_cmp() to not bother masking the last byte.)
  25. * Also, there should not be any excess bytes counted in the header length.
  26. */
  27. typedef struct
  28. {
  29. int32 vl_len_; /* varlena header (do not touch directly!) */
  30. int32 bit_len; /* number of valid bits */
  31. bits8 bit_dat[FLEXIBLE_ARRAY_MEMBER]; /* bit string, most sig. byte
  32. * first */
  33. } VarBit;
  34. /*
  35. * fmgr interface macros
  36. *
  37. * BIT and BIT VARYING are toastable varlena types. They are the same
  38. * as far as representation goes, so we just have one set of macros.
  39. */
  40. #define DatumGetVarBitP(X) ((VarBit *) PG_DETOAST_DATUM(X))
  41. #define DatumGetVarBitPCopy(X) ((VarBit *) PG_DETOAST_DATUM_COPY(X))
  42. #define VarBitPGetDatum(X) PointerGetDatum(X)
  43. #define PG_GETARG_VARBIT_P(n) DatumGetVarBitP(PG_GETARG_DATUM(n))
  44. #define PG_GETARG_VARBIT_P_COPY(n) DatumGetVarBitPCopy(PG_GETARG_DATUM(n))
  45. #define PG_RETURN_VARBIT_P(x) return VarBitPGetDatum(x)
  46. /* Header overhead *in addition to* VARHDRSZ */
  47. #define VARBITHDRSZ sizeof(int32)
  48. /* Number of bits in this bit string */
  49. #define VARBITLEN(PTR) (((VarBit *) (PTR))->bit_len)
  50. /* Pointer to the first byte containing bit string data */
  51. #define VARBITS(PTR) (((VarBit *) (PTR))->bit_dat)
  52. /* Number of bytes in the data section of a bit string */
  53. #define VARBITBYTES(PTR) (VARSIZE(PTR) - VARHDRSZ - VARBITHDRSZ)
  54. /* Padding of the bit string at the end (in bits) */
  55. #define VARBITPAD(PTR) (VARBITBYTES(PTR)*BITS_PER_BYTE - VARBITLEN(PTR))
  56. /* Number of bytes needed to store a bit string of a given length */
  57. #define VARBITTOTALLEN(BITLEN) (((BITLEN) + BITS_PER_BYTE-1)/BITS_PER_BYTE + \
  58. VARHDRSZ + VARBITHDRSZ)
  59. /*
  60. * Maximum number of bits. Several code sites assume no overflow from
  61. * computing bitlen + X; VARBITTOTALLEN() has the largest such X.
  62. */
  63. #define VARBITMAXLEN (INT_MAX - BITS_PER_BYTE + 1)
  64. /* pointer beyond the end of the bit string (like end() in STL containers) */
  65. #define VARBITEND(PTR) (((bits8 *) (PTR)) + VARSIZE(PTR))
  66. /* Mask that will cover exactly one byte, i.e. BITS_PER_BYTE bits */
  67. #define BITMASK 0xFF
  68. #endif