54 #include "pg_config.h"
56 #include "pg_config_os.h"
68 #include <sys/types.h>
70 #if defined(WIN32) || defined(__CYGWIN__)
91 #ifdef PG_FORCE_DISABLE_INLINE
112 #ifndef __has_attribute
113 #define __has_attribute(attribute) 0
118 #define pg_attribute_unused() __attribute__((unused))
120 #define pg_attribute_unused()
130 #define pg_nodiscard __attribute__((warn_unused_result))
141 #if __clang_major__ >= 7 || __GNUC__ >= 8
142 #define pg_attribute_no_sanitize_alignment() __attribute__((no_sanitize("alignment")))
144 #define pg_attribute_no_sanitize_alignment()
152 #if __has_attribute (nonnull)
153 #define pg_attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
155 #define pg_attribute_nonnull(...)
163 #ifdef USE_ASSERT_CHECKING
164 #define PG_USED_FOR_ASSERTS_ONLY
166 #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
170 #if defined(__GNUC__) || defined(__IBMC__)
171 #define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
172 #define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
174 #define pg_attribute_format_arg(a)
175 #define pg_attribute_printf(f,a)
179 #if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
180 #define pg_attribute_aligned(a) __attribute__((aligned(a)))
181 #define pg_attribute_noreturn() __attribute__((noreturn))
182 #define pg_attribute_packed() __attribute__((packed))
183 #define HAVE_PG_ATTRIBUTE_NORETURN 1
184 #elif defined(_MSC_VER)
193 #define pg_attribute_aligned(a) __declspec(align(a))
194 #define pg_attribute_noreturn()
201 #define pg_attribute_noreturn()
210 #if (defined(__GNUC__) && __GNUC__ > 3 && defined(__OPTIMIZE__)) || defined(__SUNPRO_C) || defined(__IBMC__)
212 #define pg_attribute_always_inline __attribute__((always_inline)) inline
213 #elif defined(_MSC_VER)
215 #define pg_attribute_always_inline __forceinline
218 #define pg_attribute_always_inline inline
228 #if (defined(__GNUC__) && __GNUC__ > 2) || defined(__SUNPRO_C) || defined(__IBMC__)
229 #define pg_noinline __attribute__((noinline))
231 #elif defined(_MSC_VER)
232 #define pg_noinline __declspec(noinline)
246 #if defined(__MINGW64__) && __GNUC__ == 8 && __GNUC_MINOR__ == 1
248 #define pg_attribute_cold
249 #define pg_attribute_hot
256 #if __has_attribute (cold)
257 #define pg_attribute_cold __attribute__((cold))
259 #define pg_attribute_cold
262 #if __has_attribute (hot)
263 #define pg_attribute_hot __attribute__((hot))
265 #define pg_attribute_hot
275 #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
276 #define pg_unreachable() __builtin_unreachable()
277 #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
278 #define pg_unreachable() __assume(0)
280 #define pg_unreachable() abort()
291 #define likely(x) __builtin_expect((x) != 0, 1)
292 #define unlikely(x) __builtin_expect((x) != 0, 0)
294 #define likely(x) ((x) != 0)
295 #define unlikely(x) ((x) != 0)
310 #define CppAsString(identifier) #identifier
311 #define CppAsString2(x) CppAsString(x)
312 #define CppConcat(x, y) x##y
334 #define EXPAND(args) args
335 #define VA_ARGS_NARGS(...) \
336 VA_ARGS_NARGS_ EXPAND((__VA_ARGS__, \
338 59,58,57,56,55,54,53,52,51,50, \
339 49,48,47,46,45,44,43,42,41,40, \
340 39,38,37,36,35,34,33,32,31,30, \
341 29,28,27,26,25,24,23,22,21,20, \
342 19,18,17,16,15,14,13,12,11,10, \
343 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
346 #define VA_ARGS_NARGS(...) \
347 VA_ARGS_NARGS_(__VA_ARGS__, \
349 59,58,57,56,55,54,53,52,51,50, \
350 49,48,47,46,45,44,43,42,41,40, \
351 39,38,37,36,35,34,33,32,31,30, \
352 29,28,27,26,25,24,23,22,21,20, \
353 19,18,17,16,15,14,13,12,11,10, \
354 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
357 #define VA_ARGS_NARGS_( \
358 _01,_02,_03,_04,_05,_06,_07,_08,_09,_10, \
359 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
360 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
361 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
362 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
363 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
364 _61,_62,_63, N, ...) \
382 #define FLEXIBLE_ARRAY_MEMBER
409 #define HAVE_PRAGMA_GCC_SYSTEM_HEADER 1
435 #ifdef PG_USE_STDBOOL
444 #define true ((bool) 1)
448 #define false ((bool) 0)
504 #ifdef HAVE_LONG_INT_64
508 typedef long int int64;
511 typedef unsigned long int uint64;
513 #define INT64CONST(x) (x##L)
514 #define UINT64CONST(x) (x##UL)
515 #elif defined(HAVE_LONG_LONG_INT_64)
519 typedef long long int int64;
522 typedef unsigned long long int uint64;
524 #define INT64CONST(x) (x##LL)
525 #define UINT64CONST(x) (x##ULL)
528 #error must have a working 64-bit integer datatype
532 #define INT64_FORMAT "%" INT64_MODIFIER "d"
533 #define UINT64_FORMAT "%" INT64_MODIFIER "u"
543 #if defined(PG_INT128_TYPE)
544 #if defined(pg_attribute_aligned) || ALIGNOF_PG_INT128_TYPE <= MAXIMUM_ALIGNOF
545 #define HAVE_INT128 1
547 typedef PG_INT128_TYPE int128
548 #if defined(pg_attribute_aligned)
549 pg_attribute_aligned(MAXIMUM_ALIGNOF)
553 typedef unsigned PG_INT128_TYPE uint128
554 #if defined(pg_attribute_aligned)
555 pg_attribute_aligned(MAXIMUM_ALIGNOF)
566 #define PG_INT8_MIN (-0x7F-1)
567 #define PG_INT8_MAX (0x7F)
568 #define PG_UINT8_MAX (0xFF)
569 #define PG_INT16_MIN (-0x7FFF-1)
570 #define PG_INT16_MAX (0x7FFF)
571 #define PG_UINT16_MAX (0xFFFF)
572 #define PG_INT32_MIN (-0x7FFFFFFF-1)
573 #define PG_INT32_MAX (0x7FFFFFFF)
574 #define PG_UINT32_MAX (0xFFFFFFFFU)
575 #define PG_INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
576 #define PG_INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
577 #define PG_UINT64_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
583 #define HAVE_INT64_TIMESTAMP
616 #ifdef USE_FLOAT8_BYVAL
617 #define FLOAT8PASSBYVAL true
619 #define FLOAT8PASSBYVAL false
642 #define InvalidSubTransactionId ((SubTransactionId) 0)
643 #define TopSubTransactionId ((SubTransactionId) 1)
652 #define FirstCommandId ((CommandId) 0)
653 #define InvalidCommandId (~(CommandId)0)
676 #define VARHDRSZ ((int32) sizeof(int32))
730 #define NameStr(name) ((name).data)
741 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
747 #define PointerIsValid(pointer) ((const void*)(pointer) != NULL)
753 #define PointerIsAligned(pointer, type) \
754 (((uintptr_t)(pointer) % (sizeof (type))) == 0)
756 #define OffsetToPointer(base, offset) \
757 ((void *)((char *) base + offset))
759 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
761 #define RegProcedureIsValid(p) OidIsValid(p)
772 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
788 #define TYPEALIGN(ALIGNVAL,LEN) \
789 (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
791 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
792 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
793 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
794 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
795 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
797 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
798 #define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
800 #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
801 (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
803 #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
804 #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
805 #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
806 #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
807 #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
808 #define BUFFERALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_BUFFER, (LEN))
816 #define TYPEALIGN64(ALIGNVAL,LEN) \
817 (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
820 #define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
840 #ifndef USE_ASSERT_CHECKING
842 #define Assert(condition) ((void)true)
843 #define AssertMacro(condition) ((void)true)
845 #elif defined(FRONTEND)
848 #define Assert(p) assert(p)
849 #define AssertMacro(p) ((void) assert(p))
857 #define Assert(condition) \
860 ExceptionalCondition(#condition, __FILE__, __LINE__); \
869 #define AssertMacro(condition) \
870 ((void) ((condition) || \
871 (ExceptionalCondition(#condition, __FILE__, __LINE__), 0)))
878 #define AssertPointerAlignment(ptr, bndr) \
879 Assert(TYPEALIGN(bndr, (uintptr_t)(ptr)) == (uintptr_t)(ptr))
912 #ifdef HAVE__STATIC_ASSERT
913 #define StaticAssertDecl(condition, errmessage) \
914 _Static_assert(condition, errmessage)
915 #define StaticAssertStmt(condition, errmessage) \
916 do { _Static_assert(condition, errmessage); } while(0)
917 #define StaticAssertExpr(condition, errmessage) \
918 ((void) ({ StaticAssertStmt(condition, errmessage); true; }))
920 #define StaticAssertDecl(condition, errmessage) \
921 extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
922 #define StaticAssertStmt(condition, errmessage) \
923 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
924 #define StaticAssertExpr(condition, errmessage) \
925 StaticAssertStmt(condition, errmessage)
928 #if defined(__cpp_static_assert) && __cpp_static_assert >= 200410
929 #define StaticAssertDecl(condition, errmessage) \
930 static_assert(condition, errmessage)
931 #define StaticAssertStmt(condition, errmessage) \
932 static_assert(condition, errmessage)
933 #define StaticAssertExpr(condition, errmessage) \
934 ({ static_assert(condition, errmessage); })
936 #define StaticAssertDecl(condition, errmessage) \
937 extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
938 #define StaticAssertStmt(condition, errmessage) \
939 do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
940 #define StaticAssertExpr(condition, errmessage) \
941 ((void) ({ StaticAssertStmt(condition, errmessage); }))
957 #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
958 #define AssertVariableIsOfType(varname, typename) \
959 StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \
960 CppAsString(varname) " does not have type " CppAsString(typename))
961 #define AssertVariableIsOfTypeMacro(varname, typename) \
962 (StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
963 CppAsString(varname) " does not have type " CppAsString(typename)))
965 #define AssertVariableIsOfType(varname, typename) \
966 StaticAssertStmt(sizeof(varname) == sizeof(typename), \
967 CppAsString(varname) " does not have type " CppAsString(typename))
968 #define AssertVariableIsOfTypeMacro(varname, typename) \
969 (StaticAssertExpr(sizeof(varname) == sizeof(typename), \
970 CppAsString(varname) " does not have type " CppAsString(typename)))
982 #define Max(x, y) ((x) > (y) ? (x) : (y))
988 #define Min(x, y) ((x) < (y) ? (x) : (y))
992 #define LONG_ALIGN_MASK (sizeof(long) - 1)
1004 #define MemSet(start, val, len) \
1008 void *_vstart = (void *) (start); \
1010 Size _len = (len); \
1012 if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
1013 (_len & LONG_ALIGN_MASK) == 0 && \
1015 _len <= MEMSET_LOOP_LIMIT && \
1020 MEMSET_LOOP_LIMIT != 0) \
1022 long *_start = (long *) _vstart; \
1023 long *_stop = (long *) ((char *) _start + _len); \
1024 while (_start < _stop) \
1028 memset(_vstart, _val, _len); \
1037 #define MemSetAligned(start, val, len) \
1040 long *_start = (long *) (start); \
1042 Size _len = (len); \
1044 if ((_len & LONG_ALIGN_MASK) == 0 && \
1046 _len <= MEMSET_LOOP_LIMIT && \
1047 MEMSET_LOOP_LIMIT != 0) \
1049 long *_stop = (long *) ((char *) _start + _len); \
1050 while (_start < _stop) \
1054 memset(_start, _val, _len); \
1066 #define MemSetTest(val, len) \
1067 ( ((len) & LONG_ALIGN_MASK) == 0 && \
1068 (len) <= MEMSET_LOOP_LIMIT && \
1069 MEMSET_LOOP_LIMIT != 0 && \
1072 #define MemSetLoop(start, val, len) \
1075 long * _start = (long *) (start); \
1076 long * _stop = (long *) ((char *) _start + (Size) (len)); \
1078 while (_start < _stop) \
1093 #define FLOAT4_FITS_IN_INT16(num) \
1094 ((num) >= (float4) PG_INT16_MIN && (num) < -((float4) PG_INT16_MIN))
1095 #define FLOAT4_FITS_IN_INT32(num) \
1096 ((num) >= (float4) PG_INT32_MIN && (num) < -((float4) PG_INT32_MIN))
1097 #define FLOAT4_FITS_IN_INT64(num) \
1098 ((num) >= (float4) PG_INT64_MIN && (num) < -((float4) PG_INT64_MIN))
1099 #define FLOAT8_FITS_IN_INT16(num) \
1100 ((num) >= (float8) PG_INT16_MIN && (num) < -((float8) PG_INT16_MIN))
1101 #define FLOAT8_FITS_IN_INT32(num) \
1102 ((num) >= (float8) PG_INT32_MIN && (num) < -((float8) PG_INT32_MIN))
1103 #define FLOAT8_FITS_IN_INT64(num) \
1104 ((num) >= (float8) PG_INT64_MIN && (num) < -((float8) PG_INT64_MIN))
1117 #define INVERT_COMPARE_RESULT(var) \
1118 ((var) = ((var) < 0) ? 1 : -(var))
1147 #define HIGHBIT (0x80)
1148 #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
1156 #define SQL_STR_DOUBLE(ch, escape_backslash) \
1157 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
1159 #define ESCAPE_STRING_SYNTAX 'E'
1162 #define STATUS_OK (0)
1163 #define STATUS_ERROR (-1)
1164 #define STATUS_EOF (-2)
1172 #define gettext(x) (x)
1173 #define dgettext(d,x) (x)
1174 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
1175 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
1178 #define _(x) gettext(x)
1189 #define gettext_noop(x) (x)
1204 #ifdef SO_MAJOR_VERSION
1205 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
1207 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1225 #if defined(HAVE__BUILTIN_TYPES_COMPATIBLE_P)
1226 #define unconstify(underlying_type, expr) \
1227 (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), const underlying_type), \
1229 (underlying_type) (expr))
1230 #define unvolatize(underlying_type, expr) \
1231 (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), volatile underlying_type), \
1233 (underlying_type) (expr))
1235 #define unconstify(underlying_type, expr) \
1236 ((underlying_type) (expr))
1237 #define unvolatize(underlying_type, expr) \
1238 ((underlying_type) (expr))
1257 #if defined(WIN32) || defined(__CYGWIN__)
1258 #define PG_BINARY O_BINARY
1259 #define PG_BINARY_A "ab"
1260 #define PG_BINARY_R "rb"
1261 #define PG_BINARY_W "wb"
1264 #define PG_BINARY_A "a"
1265 #define PG_BINARY_R "r"
1266 #define PG_BINARY_W "w"
1274 #if !HAVE_DECL_FDATASYNC
1283 #ifdef HAVE_LONG_INT_64
1284 #define strtoi64(str, endptr, base) ((int64) strtol(str, endptr, base))
1285 #define strtou64(str, endptr, base) ((uint64) strtoul(str, endptr, base))
1287 #define strtoi64(str, endptr, base) ((int64) strtoll(str, endptr, base))
1288 #define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
1294 #ifdef HAVE_LONG_INT_64
1295 #define i64abs(i) labs(i)
1297 #define i64abs(i) llabs(i)
1318 #ifdef HAVE_VISIBILITY_ATTRIBUTE
1319 #define PGDLLEXPORT __attribute__((visibility("default")))
1335 #define SIGNAL_ARGS int postgres_signal_arg
1346 typedef intptr_t sigjmp_buf[5];
1347 #define sigsetjmp(x,y) __builtin_setjmp(x)
1348 #define siglongjmp __builtin_longjmp
1350 #define sigjmp_buf jmp_buf
1351 #define sigsetjmp(x,y) setjmp(x)
1352 #define siglongjmp longjmp
static Datum values[MAXATTR]
union PGAlignedBlock PGAlignedBlock
union PGAlignedXLogBlock PGAlignedXLogBlock
#define pg_attribute_noreturn()
TransactionId MultiXactId
#define FLEXIBLE_ARRAY_MEMBER
uint32 LocalTransactionId
int fdatasync(int fildes)
void ExceptionalCondition(const char *conditionName, const char *fileName, int lineNumber) pg_attribute_noreturn()
void(* pg_funcptr_t)(void)
char vl_dat[FLEXIBLE_ARRAY_MEMBER]