varbit.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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-2016, 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. typedef struct
  23. {
  24. int32 vl_len_; /* varlena header (do not touch directly!) */
  25. int32 bit_len; /* number of valid bits */
  26. bits8 bit_dat[FLEXIBLE_ARRAY_MEMBER]; /* bit string, most sig. byte
  27. * first */
  28. } VarBit;
  29. /*
  30. * fmgr interface macros
  31. *
  32. * BIT and BIT VARYING are toastable varlena types. They are the same
  33. * as far as representation goes, so we just have one set of macros.
  34. */
  35. #define DatumGetVarBitP(X) ((VarBit *) PG_DETOAST_DATUM(X))
  36. #define DatumGetVarBitPCopy(X) ((VarBit *) PG_DETOAST_DATUM_COPY(X))
  37. #define VarBitPGetDatum(X) PointerGetDatum(X)
  38. #define PG_GETARG_VARBIT_P(n) DatumGetVarBitP(PG_GETARG_DATUM(n))
  39. #define PG_GETARG_VARBIT_P_COPY(n) DatumGetVarBitPCopy(PG_GETARG_DATUM(n))
  40. #define PG_RETURN_VARBIT_P(x) return VarBitPGetDatum(x)
  41. /* Header overhead *in addition to* VARHDRSZ */
  42. #define VARBITHDRSZ sizeof(int32)
  43. /* Number of bits in this bit string */
  44. #define VARBITLEN(PTR) (((VarBit *) (PTR))->bit_len)
  45. /* Pointer to the first byte containing bit string data */
  46. #define VARBITS(PTR) (((VarBit *) (PTR))->bit_dat)
  47. /* Number of bytes in the data section of a bit string */
  48. #define VARBITBYTES(PTR) (VARSIZE(PTR) - VARHDRSZ - VARBITHDRSZ)
  49. /* Padding of the bit string at the end (in bits) */
  50. #define VARBITPAD(PTR) (VARBITBYTES(PTR)*BITS_PER_BYTE - VARBITLEN(PTR))
  51. /* Number of bytes needed to store a bit string of a given length */
  52. #define VARBITTOTALLEN(BITLEN) (((BITLEN) + BITS_PER_BYTE-1)/BITS_PER_BYTE + \
  53. VARHDRSZ + VARBITHDRSZ)
  54. /*
  55. * Maximum number of bits. Several code sites assume no overflow from
  56. * computing bitlen + X; VARBITTOTALLEN() has the largest such X.
  57. */
  58. #define VARBITMAXLEN (INT_MAX - BITS_PER_BYTE + 1)
  59. /* pointer beyond the end of the bit string (like end() in STL containers) */
  60. #define VARBITEND(PTR) (((bits8 *) (PTR)) + VARSIZE(PTR))
  61. /* Mask that will cover exactly one byte, i.e. BITS_PER_BYTE bits */
  62. #define BITMASK 0xFF
  63. extern Datum bit_in(PG_FUNCTION_ARGS);
  64. extern Datum bit_out(PG_FUNCTION_ARGS);
  65. extern Datum bit_recv(PG_FUNCTION_ARGS);
  66. extern Datum bit_send(PG_FUNCTION_ARGS);
  67. extern Datum bittypmodin(PG_FUNCTION_ARGS);
  68. extern Datum bittypmodout(PG_FUNCTION_ARGS);
  69. extern Datum varbit_in(PG_FUNCTION_ARGS);
  70. extern Datum varbit_out(PG_FUNCTION_ARGS);
  71. extern Datum varbit_recv(PG_FUNCTION_ARGS);
  72. extern Datum varbit_send(PG_FUNCTION_ARGS);
  73. extern Datum varbittypmodin(PG_FUNCTION_ARGS);
  74. extern Datum varbittypmodout(PG_FUNCTION_ARGS);
  75. extern Datum bit(PG_FUNCTION_ARGS);
  76. extern Datum varbit_transform(PG_FUNCTION_ARGS);
  77. extern Datum varbit(PG_FUNCTION_ARGS);
  78. extern Datum biteq(PG_FUNCTION_ARGS);
  79. extern Datum bitne(PG_FUNCTION_ARGS);
  80. extern Datum bitlt(PG_FUNCTION_ARGS);
  81. extern Datum bitle(PG_FUNCTION_ARGS);
  82. extern Datum bitgt(PG_FUNCTION_ARGS);
  83. extern Datum bitge(PG_FUNCTION_ARGS);
  84. extern Datum bitcmp(PG_FUNCTION_ARGS);
  85. /* avoid the names bitand and bitor, since they are C++ keywords */
  86. extern Datum bit_and(PG_FUNCTION_ARGS);
  87. extern Datum bit_or(PG_FUNCTION_ARGS);
  88. extern Datum bitxor(PG_FUNCTION_ARGS);
  89. extern Datum bitnot(PG_FUNCTION_ARGS);
  90. extern Datum bitshiftleft(PG_FUNCTION_ARGS);
  91. extern Datum bitshiftright(PG_FUNCTION_ARGS);
  92. extern Datum bitcat(PG_FUNCTION_ARGS);
  93. extern Datum bitsubstr(PG_FUNCTION_ARGS);
  94. extern Datum bitsubstr_no_len(PG_FUNCTION_ARGS);
  95. extern Datum bitoverlay(PG_FUNCTION_ARGS);
  96. extern Datum bitoverlay_no_len(PG_FUNCTION_ARGS);
  97. extern Datum bitlength(PG_FUNCTION_ARGS);
  98. extern Datum bitoctetlength(PG_FUNCTION_ARGS);
  99. extern Datum bitfromint4(PG_FUNCTION_ARGS);
  100. extern Datum bittoint4(PG_FUNCTION_ARGS);
  101. extern Datum bitfromint8(PG_FUNCTION_ARGS);
  102. extern Datum bittoint8(PG_FUNCTION_ARGS);
  103. extern Datum bitposition(PG_FUNCTION_ARGS);
  104. extern Datum bitsetbit(PG_FUNCTION_ARGS);
  105. extern Datum bitgetbit(PG_FUNCTION_ARGS);
  106. #endif