53 #include "pg_config.h"
61 #if defined(_WIN32) && !defined(WIN32)
65 #if !defined(WIN32) && !defined(__CYGWIN__)
66 #include "pg_config_os.h"
69 #if _MSC_VER >= 1400 || defined(HAVE_CRTDEFS_H)
70 #define errcode __msvc_errcode
92 #include <sys/types.h>
95 #if defined(WIN32) || defined(__CYGWIN__)
99 #if defined(WIN32) || defined(__CYGWIN__)
101 #include "pg_config_os.h"
112 #ifdef PG_FORCE_DISABLE_INLINE
120 #define _(x) gettext(x)
125 #define gettext(x) (x)
126 #define dgettext(d,x) (x)
127 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
128 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
139 #define gettext_noop(x) (x)
159 #define CppAsString(identifier) #identifier
160 #define CppConcat(x, y) x##y
167 #define dummyret void
169 #define dummyret char
173 #ifdef HAVE_FUNCNAME__FUNC
174 #define PG_FUNCNAME_MACRO __func__
176 #ifdef HAVE_FUNCNAME__FUNCTION
177 #define PG_FUNCNAME_MACRO __FUNCTION__
179 #define PG_FUNCNAME_MACRO NULL
203 #define true ((bool) 1)
207 #define false ((bool) 0)
226 #define NULL ((void *) 0)
279 #ifdef HAVE_LONG_INT_64
283 typedef long int int64;
286 typedef unsigned long int uint64;
288 #elif defined(HAVE_LONG_LONG_INT_64)
292 typedef long long int int64;
295 typedef unsigned long long int uint64;
299 #error must have a working 64-bit integer datatype
303 #ifdef HAVE_LL_CONSTANTS
304 #define INT64CONST(x) ((int64) x##LL)
305 #define UINT64CONST(x) ((uint64) x##ULL)
307 #define INT64CONST(x) ((int64) x)
308 #define UINT64CONST(x) ((uint64) x)
312 #define INT64_FORMAT "%" INT64_MODIFIER "d"
313 #define UINT64_FORMAT "%" INT64_MODIFIER "u"
320 #if defined(PG_INT128_TYPE)
322 typedef PG_INT128_TYPE int128;
323 typedef unsigned PG_INT128_TYPE uint128;
330 #define PG_INT8_MIN (-0x7F-1)
331 #define PG_INT8_MAX (0x7F)
332 #define PG_UINT8_MAX (0xFF)
333 #define PG_INT16_MIN (-0x7FFF-1)
334 #define PG_INT16_MAX (0x7FFF)
335 #define PG_UINT16_MAX (0xFFFF)
336 #define PG_INT32_MIN (-0x7FFFFFFF-1)
337 #define PG_INT32_MAX (0x7FFFFFFF)
338 #define PG_UINT32_MAX (0xFFFFFFFF)
339 #define PG_INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
340 #define PG_INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
341 #define PG_UINT64_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
344 #ifdef USE_INTEGER_DATETIMES
345 #define HAVE_INT64_TIMESTAMP
399 #define InvalidSubTransactionId ((SubTransactionId) 0)
400 #define TopSubTransactionId ((SubTransactionId) 1)
409 #define FirstCommandId ((CommandId) 0)
410 #define InvalidCommandId (~(CommandId)0)
440 #define VARHDRSZ ((int32) sizeof(int32))
494 #define NameStr(name) ((name).data)
502 #define SQL_STR_DOUBLE(ch, escape_backslash) \
503 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
505 #define ESCAPE_STRING_SYNTAX 'E'
515 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
521 #define PointerIsValid(pointer) ((const void*)(pointer) != NULL)
527 #define PointerIsAligned(pointer, type) \
528 (((uintptr_t)(pointer) % (sizeof (type))) == 0)
530 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
532 #define RegProcedureIsValid(p) OidIsValid(p)
547 #define offsetof(type, field) ((long) &((type *)0)->field)
554 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
560 #define endof(array) (&(array)[lengthof(array)])
573 #define TYPEALIGN(ALIGNVAL,LEN) \
574 (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
576 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
577 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
578 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
579 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
580 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
582 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
583 #define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
585 #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
586 (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
588 #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
589 #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
590 #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
591 #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
592 #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
600 #define TYPEALIGN64(ALIGNVAL,LEN) \
601 (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
604 #define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
619 #define pg_attribute_unused() __attribute__((unused))
621 #define pg_attribute_unused()
625 #if defined(__GNUC__) || defined(__IBMC__)
626 #define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
627 #define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
629 #define pg_attribute_format_arg(a)
630 #define pg_attribute_printf(f,a)
634 #if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
635 #define pg_attribute_aligned(a) __attribute__((aligned(a)))
636 #define pg_attribute_noreturn() __attribute__((noreturn))
637 #define pg_attribute_packed() __attribute__((packed))
638 #define HAVE_PG_ATTRIBUTE_NORETURN 1
645 #define pg_attribute_noreturn()
665 #ifndef USE_ASSERT_CHECKING
667 #define Assert(condition) ((void)true)
668 #define AssertMacro(condition) ((void)true)
669 #define AssertArg(condition) ((void)true)
670 #define AssertState(condition) ((void)true)
671 #define AssertPointerAlignment(ptr, bndr) ((void)true)
672 #define Trap(condition, errorType) ((void)true)
673 #define TrapMacro(condition, errorType) (true)
675 #elif defined(FRONTEND)
678 #define Assert(p) assert(p)
679 #define AssertMacro(p) ((void) assert(p))
680 #define AssertArg(condition) assert(condition)
681 #define AssertState(condition) assert(condition)
682 #define AssertPointerAlignment(ptr, bndr) ((void)true)
689 #define Trap(condition, errorType) \
692 ExceptionalCondition(CppAsString(condition), (errorType), \
693 __FILE__, __LINE__); \
703 #define TrapMacro(condition, errorType) \
704 ((bool) (! (condition) || \
705 (ExceptionalCondition(CppAsString(condition), (errorType), \
706 __FILE__, __LINE__), 0)))
708 #define Assert(condition) \
709 Trap(!(condition), "FailedAssertion")
711 #define AssertMacro(condition) \
712 ((void) TrapMacro(!(condition), "FailedAssertion"))
714 #define AssertArg(condition) \
715 Trap(!(condition), "BadArgument")
717 #define AssertState(condition) \
718 Trap(!(condition), "BadState")
723 #define AssertPointerAlignment(ptr, bndr) \
724 Trap(TYPEALIGN(bndr, (uintptr_t)(ptr)) != (uintptr_t)(ptr), \
743 #ifdef HAVE__STATIC_ASSERT
744 #define StaticAssertStmt(condition, errmessage) \
745 do { _Static_assert(condition, errmessage); } while(0)
746 #define StaticAssertExpr(condition, errmessage) \
747 ({ StaticAssertStmt(condition, errmessage); true; })
749 #define StaticAssertStmt(condition, errmessage) \
750 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
751 #define StaticAssertExpr(condition, errmessage) \
752 StaticAssertStmt(condition, errmessage)
767 #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
768 #define AssertVariableIsOfType(varname, typename) \
769 StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \
770 CppAsString(varname) " does not have type " CppAsString(typename))
771 #define AssertVariableIsOfTypeMacro(varname, typename) \
772 ((void) StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
773 CppAsString(varname) " does not have type " CppAsString(typename)))
775 #define AssertVariableIsOfType(varname, typename) \
776 StaticAssertStmt(sizeof(varname) == sizeof(typename), \
777 CppAsString(varname) " does not have type " CppAsString(typename))
778 #define AssertVariableIsOfTypeMacro(varname, typename) \
779 ((void) StaticAssertExpr(sizeof(varname) == sizeof(typename), \
780 CppAsString(varname) " does not have type " CppAsString(typename)))
792 #define Max(x, y) ((x) > (y) ? (x) : (y))
798 #define Min(x, y) ((x) < (y) ? (x) : (y))
804 #define Abs(x) ((x) >= 0 ? (x) : -(x))
822 #define StrNCpy(dst,src,len) \
825 char * _dst = (dst); \
830 strncpy(_dst, (src), _len); \
831 _dst[_len-1] = '\0'; \
837 #define LONG_ALIGN_MASK (sizeof(long) - 1)
849 #define MemSet(start, val, len) \
853 void *_vstart = (void *) (start); \
857 if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
858 (_len & LONG_ALIGN_MASK) == 0 && \
860 _len <= MEMSET_LOOP_LIMIT && \
865 MEMSET_LOOP_LIMIT != 0) \
867 long *_start = (long *) _vstart; \
868 long *_stop = (long *) ((char *) _start + _len); \
869 while (_start < _stop) \
873 memset(_vstart, _val, _len); \
882 #define MemSetAligned(start, val, len) \
885 long *_start = (long *) (start); \
889 if ((_len & LONG_ALIGN_MASK) == 0 && \
891 _len <= MEMSET_LOOP_LIMIT && \
892 MEMSET_LOOP_LIMIT != 0) \
894 long *_stop = (long *) ((char *) _start + _len); \
895 while (_start < _stop) \
899 memset(_start, _val, _len); \
911 #define MemSetTest(val, len) \
912 ( ((len) & LONG_ALIGN_MASK) == 0 && \
913 (len) <= MEMSET_LOOP_LIMIT && \
914 MEMSET_LOOP_LIMIT != 0 && \
917 #define MemSetLoop(start, val, len) \
920 long * _start = (long *) (start); \
921 long * _stop = (long *) ((char *) _start + (Size) (len)); \
923 while (_start < _stop) \
933 #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
934 #define pg_unreachable() __builtin_unreachable()
935 #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
936 #define pg_unreachable() __assume(0)
938 #define pg_unreachable() abort()
948 #define HIGHBIT (0x80)
949 #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
951 #define STATUS_OK (0)
952 #define STATUS_ERROR (-1)
953 #define STATUS_EOF (-2)
954 #define STATUS_FOUND (1)
955 #define STATUS_WAITING (2)
963 #ifdef USE_ASSERT_CHECKING
964 #define PG_USED_FOR_ASSERTS_ONLY
966 #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
987 #define CppAsString2(x) CppAsString(x)
989 #ifdef SO_MAJOR_VERSION
990 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
992 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1012 #if defined(WIN32) || defined(__CYGWIN__)
1013 #define PG_BINARY O_BINARY
1014 #define PG_BINARY_A "ab"
1015 #define PG_BINARY_R "rb"
1016 #define PG_BINARY_W "wb"
1019 #define PG_BINARY_A "a"
1020 #define PG_BINARY_R "r"
1021 #define PG_BINARY_W "w"
1029 #if !HAVE_DECL_SNPRINTF
1033 #if !HAVE_DECL_VSNPRINTF
1034 extern int vsnprintf(
char *str,
size_t count,
const char *fmt, va_list
args);
1037 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
1038 #define memmove(d, s, c) bcopy(s, d, c)
1059 #define SIGNAL_ARGS int postgres_signal_arg
1068 #define sigjmp_buf jmp_buf
1069 #define sigsetjmp(x,y) setjmp(x)
1070 #define siglongjmp longjmp
1073 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
1074 extern int fdatasync(
int fildes);
1078 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
1079 #define strtoll strtoq
1080 #define HAVE_STRTOLL 1
1084 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
1085 #define strtoull strtouq
1086 #define HAVE_STRTOULL 1
1093 #if defined(HAVE_WCSTOMBS) && defined(HAVE_TOWLOWER)
1094 #define USE_WIDE_UPPER_LOWER
1099 #define NON_EXEC_STATIC
1101 #define NON_EXEC_STATIC static
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define pg_attribute_printf(f, a)
int int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
uint32 LocalTransactionId
char vl_dat[FLEXIBLE_ARRAY_MEMBER]
TransactionId MultiXactId
static Datum values[MAXATTR]