1 /*-------------------------------------------------------------------------
4 * Fundamental C definitions. This is included by every .c file in
5 * PostgreSQL (via either postgres.h or postgres_fe.h, as appropriate).
7 * Note that the definitions here are not intended to be exposed to clients
8 * of the frontend interface libraries --- so we don't worry much about
9 * polluting the namespace with lots of stuff...
12 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
17 *-------------------------------------------------------------------------
20 *----------------------------------------------------------------
23 * When adding stuff to this file, please try to put stuff
24 * into the relevant section, or add new sections as appropriate.
27 * ------- ------------------------------------------------
28 * 0) pg_config.h and standard system headers
29 * 1) hacks to cope with non-ANSI C compilers
30 * 2) bool, true, false, TRUE, FALSE, NULL
31 * 3) standard system types
32 * 4) IsValid macros for system types
33 * 5) offsetof, lengthof, endof, alignment
35 * 7) widely useful macros
37 * 9) system-specific hacks
39 * NOTE: since this file is included by both frontend and backend modules, it's
40 * almost certainly wrong to put an "extern" declaration here. typedefs and
41 * macros are the kind of thing that might go here.
43 *----------------------------------------------------------------
48 #include "postgres_ext.h"
50 /* Must undef pg_config_ext.h symbols before including pg_config.h */
53 #include "pg_config.h"
54 #include "pg_config_manual.h" /* must be after pg_config.h */
57 * We always rely on the WIN32 macro being set by our build system,
58 * but _WIN32 is the compiler pre-defined macro. So make sure we define
59 * WIN32 whenever _WIN32 is set, to facilitate standalone building.
61 #if defined(_WIN32) && !defined(WIN32)
65 #if !defined(WIN32) && !defined(__CYGWIN__) /* win32 includes further down */
66 #include "pg_config_os.h" /* must be before any system header files */
69 #if _MSC_VER >= 1400 || defined(HAVE_CRTDEFS_H)
70 #define errcode __msvc_errcode
76 * We have to include stdlib.h here because it defines many of these macros
77 * on some platforms, and we only want our definitions used if stdlib.h doesn't
78 * have its own. The same goes for stddef and stdarg if present.
92 #include <sys/types.h>
95 #if defined(WIN32) || defined(__CYGWIN__)
96 #include <fcntl.h> /* ensure O_BINARY is available */
99 #if defined(WIN32) || defined(__CYGWIN__)
100 /* We have to redefine some system functions after they are included above. */
101 #include "pg_config_os.h"
104 /* Must be before gettext() games below */
107 #define _(x) gettext(x)
112 #define gettext(x) (x)
113 #define dgettext(d,x) (x)
114 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
115 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
119 * Use this to mark string constants as needing translation at some later
120 * time, rather than immediately. This is useful for cases where you need
121 * access to the original string and translated string, and for cases where
122 * immediate translation is not possible, like when initializing global
124 * http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
126 #define gettext_noop(x) (x)
129 /* ----------------------------------------------------------------
130 * Section 1: hacks to cope with non-ANSI C compilers
132 * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
133 * ----------------------------------------------------------------
138 * Convert the argument to a string, using the C preprocessor.
140 * Concatenate two arguments together, using the C preprocessor.
142 * Note: There used to be support here for pre-ANSI C compilers that didn't
143 * support # and ##. Nowadays, these macros are just for clarity and/or
144 * backward compatibility with existing PostgreSQL code.
146 #define CppAsString(identifier) #identifier
147 #define CppConcat(x, y) x##y
150 * dummyret is used to set return values in macros that use ?: to make
151 * assignments. gcc wants these to be void, other compilers like char
153 #ifdef __GNUC__ /* GNU cc */
154 #define dummyret void
156 #define dummyret char
160 #define __attribute__(_arg_)
163 /* ----------------------------------------------------------------
164 * Section 2: bool, true, false, TRUE, FALSE, NULL
165 * ----------------------------------------------------------------
170 * Boolean value, either true or false.
172 * XXX for C++ compilers, we assume the compiler has a compatible
173 * built-in definition of bool.
183 #define true ((bool) 1)
187 #define false ((bool) 0)
191 typedef bool *BoolPtr;
206 #define NULL ((void *) 0)
210 /* ----------------------------------------------------------------
211 * Section 3: standard system types
212 * ----------------------------------------------------------------
217 * Variable holding address of any memory resident object.
219 * XXX Pointer arithmetic is done with this, so it can't be void *
220 * under "true" ANSI compilers.
222 typedef char *Pointer;
226 * Signed integer, EXACTLY N BITS IN SIZE,
227 * used for numerical computations and the
228 * frontend/backend protocol.
231 typedef signed char int8; /* == 8 bits */
232 typedef signed short int16; /* == 16 bits */
233 typedef signed int int32; /* == 32 bits */
234 #endif /* not HAVE_INT8 */
238 * Unsigned integer, EXACTLY N BITS IN SIZE,
239 * used for numerical computations and the
240 * frontend/backend protocol.
243 typedef unsigned char uint8; /* == 8 bits */
244 typedef unsigned short uint16; /* == 16 bits */
245 typedef unsigned int uint32; /* == 32 bits */
246 #endif /* not HAVE_UINT8 */
250 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
252 typedef uint8 bits8; /* >= 8 bits */
253 typedef uint16 bits16; /* >= 16 bits */
254 typedef uint32 bits32; /* >= 32 bits */
259 #ifdef HAVE_LONG_INT_64
260 /* Plain "long int" fits, use it */
263 typedef long int int64;
266 typedef unsigned long int uint64;
268 #elif defined(HAVE_LONG_LONG_INT_64)
269 /* We have working support for "long long int", use that */
272 typedef long long int int64;
275 typedef unsigned long long int uint64;
278 /* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */
279 #error must have a working 64-bit integer datatype
282 /* Decide if we need to decorate 64-bit constants */
283 #ifdef HAVE_LL_CONSTANTS
284 #define INT64CONST(x) ((int64) x##LL)
285 #define UINT64CONST(x) ((uint64) x##ULL)
287 #define INT64CONST(x) ((int64) x)
288 #define UINT64CONST(x) ((uint64) x)
292 /* Select timestamp representation (float8 or int64) */
293 #ifdef USE_INTEGER_DATETIMES
294 #define HAVE_INT64_TIMESTAMP
297 /* sig_atomic_t is required by ANSI C, but may be missing on old platforms */
298 #ifndef HAVE_SIG_ATOMIC_T
299 typedef int sig_atomic_t;
304 * Size of any memory resident object, as returned by sizeof.
310 * Index into any memory resident array.
313 * Indices are non negative.
315 typedef unsigned int Index;
319 * Offset into any memory resident array.
322 * This differs from an Index in that an Index is always
323 * non negative, whereas Offset may be negative.
325 typedef signed int Offset;
328 * Common Postgres datatype names (as used in the catalogs)
330 typedef float float4;
331 typedef double float8;
334 * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
338 /* typedef Oid is in postgres_ext.h */
341 * regproc is the type name used in the include/catalog headers, but
342 * RegProcedure is the preferred name in C code.
345 typedef regproc RegProcedure;
347 typedef uint32 TransactionId;
349 typedef uint32 LocalTransactionId;
351 typedef uint32 SubTransactionId;
353 #define InvalidSubTransactionId ((SubTransactionId) 0)
354 #define TopSubTransactionId ((SubTransactionId) 1)
356 /* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
357 typedef TransactionId MultiXactId;
359 typedef uint32 MultiXactOffset;
361 typedef uint32 CommandId;
363 #define FirstCommandId ((CommandId) 0)
364 #define InvalidCommandId (~(CommandId)0)
367 * Array indexing support
376 * Variable-length datatypes all share the 'struct varlena' header.
378 * NOTE: for TOASTable types, this is an oversimplification, since the value
379 * may be compressed or moved out-of-line. However datatype-specific routines
380 * are mostly content to deal with de-TOASTed values only, and of course
381 * client-side routines should never see a TOASTed value. But even in a
382 * de-TOASTed value, beware of touching vl_len_ directly, as its representation
383 * is no longer convenient. It's recommended that code always use the VARDATA,
384 * VARSIZE, and SET_VARSIZE macros instead of relying on direct mentions of
385 * the struct fields. See postgres.h for details of the TOASTed form.
390 char vl_len_[4]; /* Do not touch this field directly! */
394 #define VARHDRSZ ((int32) sizeof(int32))
397 * These widely-used datatypes are just a varlena header and the data bytes.
398 * There is no terminating null or anything like that --- the data length is
399 * always VARSIZE(ptr) - VARHDRSZ.
401 typedef struct varlena bytea;
402 typedef struct varlena text;
403 typedef struct varlena BpChar; /* blank-padded char, ie SQL char(n) */
404 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
407 * Specialized array types. These are physically laid out just the same
408 * as regular arrays (so that the regular array subscripting code works
409 * with them). They exist as distinct types mostly for historical reasons:
410 * they have nonstandard I/O behavior which we don't want to change for fear
411 * of breaking applications that look at the system catalogs. There is also
412 * an implementation issue for oidvector: it's part of the primary key for
413 * pg_proc, and we can't use the normal btree array support routines for that
414 * without circularity.
418 int32 vl_len_; /* these fields must match ArrayType! */
419 int ndim; /* always 1 for int2vector */
420 int32 dataoffset; /* always 0 for int2vector */
424 int16 values[1]; /* VARIABLE LENGTH ARRAY */
425 } int2vector; /* VARIABLE LENGTH STRUCT */
429 int32 vl_len_; /* these fields must match ArrayType! */
430 int ndim; /* always 1 for oidvector */
431 int32 dataoffset; /* always 0 for oidvector */
435 Oid values[1]; /* VARIABLE LENGTH ARRAY */
436 } oidvector; /* VARIABLE LENGTH STRUCT */
439 * Representation of a Name: effectively just a C string, but null-padded to
440 * exactly NAMEDATALEN bytes. The use of a struct is historical.
442 typedef struct nameData
444 char data[NAMEDATALEN];
446 typedef NameData *Name;
448 #define NameStr(name) ((name).data)
451 * Support macros for escaping strings. escape_backslash should be TRUE
452 * if generating a non-standard-conforming string. Prefixing a string
453 * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
454 * Beware of multiple evaluation of the "ch" argument!
456 #define SQL_STR_DOUBLE(ch, escape_backslash) \
457 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
459 #define ESCAPE_STRING_SYNTAX 'E'
461 /* ----------------------------------------------------------------
462 * Section 4: IsValid macros for system types
463 * ----------------------------------------------------------------
467 * True iff bool is valid.
469 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
473 * True iff pointer is valid.
475 #define PointerIsValid(pointer) ((const void*)(pointer) != NULL)
479 * True iff pointer is properly aligned to point to the given type.
481 #define PointerIsAligned(pointer, type) \
482 (((uintptr_t)(pointer) % (sizeof (type))) == 0)
484 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
486 #define RegProcedureIsValid(p) OidIsValid(p)
489 /* ----------------------------------------------------------------
490 * Section 5: offsetof, lengthof, endof, alignment
491 * ----------------------------------------------------------------
495 * Offset of a structure/union field within that structure/union.
497 * XXX This is supposed to be part of stddef.h, but isn't on
498 * some systems (like SunOS 4).
501 #define offsetof(type, field) ((long) &((type *)0)->field)
502 #endif /* offsetof */
506 * Number of elements in an array.
508 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
512 * Address of the element one past the last in an array.
514 #define endof(array) (&(array)[lengthof(array)])
517 * Alignment macros: align a length or address appropriately for a given type.
518 * The fooALIGN() macros round up to a multiple of the required alignment,
519 * while the fooALIGN_DOWN() macros round down. The latter are more useful
520 * for problems like "how many X-sized structures will fit in a page?".
522 * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
523 * That case seems extremely unlikely to be needed in practice, however.
527 #define TYPEALIGN(ALIGNVAL,LEN) \
528 (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
530 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
531 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
532 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
533 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
534 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
535 /* MAXALIGN covers only built-in types, not buffers */
536 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
538 #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
539 (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
541 #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
542 #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
543 #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
544 #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
545 #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
548 * The above macros will not work with types wider than uintptr_t, like with
549 * uint64 on 32-bit platforms. That's not problem for the usual use where a
550 * pointer or a length is aligned, but for the odd case that you need to
551 * align something (potentially) wider, use TYPEALIGN64.
553 #define TYPEALIGN64(ALIGNVAL,LEN) \
554 (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
556 /* we don't currently need wider versions of the other ALIGN macros */
557 #define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
559 /* ----------------------------------------------------------------
560 * Section 6: assertions
561 * ----------------------------------------------------------------
565 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
568 * It should _NOT_ be defined in releases or in benchmark copies
572 * Assert() can be used in both frontend and backend code. In frontend code it
573 * just calls the standard assert, if it's available. If use of assertions is
574 * not configured, it does nothing.
576 #ifndef USE_ASSERT_CHECKING
578 #define Assert(condition)
579 #define AssertMacro(condition) ((void)true)
580 #define AssertArg(condition)
581 #define AssertState(condition)
582 #define Trap(condition, errorType)
583 #define TrapMacro(condition, errorType) (true)
585 #elif defined(FRONTEND)
588 #define Assert(p) assert(p)
589 #define AssertMacro(p) ((void) assert(p))
590 #define AssertArg(condition) assert(condition)
591 #define AssertState(condition) assert(condition)
592 #else /* USE_ASSERT_CHECKING && !FRONTEND */
596 * Generates an exception if the given condition is true.
598 #define Trap(condition, errorType) \
600 if ((assert_enabled) && (condition)) \
601 ExceptionalCondition(CppAsString(condition), (errorType), \
602 __FILE__, __LINE__); \
606 * TrapMacro is the same as Trap but it's intended for use in macros:
608 * #define foo(x) (AssertMacro(x != 0), bar(x))
612 #define TrapMacro(condition, errorType) \
613 ((bool) ((! assert_enabled) || ! (condition) || \
614 (ExceptionalCondition(CppAsString(condition), (errorType), \
615 __FILE__, __LINE__), 0)))
617 #define Assert(condition) \
618 Trap(!(condition), "FailedAssertion")
620 #define AssertMacro(condition) \
621 ((void) TrapMacro(!(condition), "FailedAssertion"))
623 #define AssertArg(condition) \
624 Trap(!(condition), "BadArgument")
626 #define AssertState(condition) \
627 Trap(!(condition), "BadState")
628 #endif /* USE_ASSERT_CHECKING && !FRONTEND */
632 * Macros to support compile-time assertion checks.
634 * If the "condition" (a compile-time-constant expression) evaluates to false,
635 * throw a compile error using the "errmessage" (a string literal).
637 * gcc 4.6 and up supports _Static_assert(), but there are bizarre syntactic
638 * placement restrictions. These macros make it safe to use as a statement
639 * or in an expression, respectively.
641 * Otherwise we fall back on a kluge that assumes the compiler will complain
642 * about a negative width for a struct bit-field. This will not include a
643 * helpful error message, but it beats not getting an error at all.
645 #ifdef HAVE__STATIC_ASSERT
646 #define StaticAssertStmt(condition, errmessage) \
647 do { _Static_assert(condition, errmessage); } while(0)
648 #define StaticAssertExpr(condition, errmessage) \
649 ({ StaticAssertStmt(condition, errmessage); true; })
650 #else /* !HAVE__STATIC_ASSERT */
651 #define StaticAssertStmt(condition, errmessage) \
652 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
653 #define StaticAssertExpr(condition, errmessage) \
654 StaticAssertStmt(condition, errmessage)
655 #endif /* HAVE__STATIC_ASSERT */
659 * Compile-time checks that a variable (or expression) has the specified type.
661 * AssertVariableIsOfType() can be used as a statement.
662 * AssertVariableIsOfTypeMacro() is intended for use in macros, eg
663 * #define foo(x) (AssertVariableIsOfTypeMacro(x, int), bar(x))
665 * If we don't have __builtin_types_compatible_p, we can still assert that
666 * the types have the same size. This is far from ideal (especially on 32-bit
667 * platforms) but it provides at least some coverage.
669 #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
670 #define AssertVariableIsOfType(varname, typename) \
671 StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \
672 CppAsString(varname) " does not have type " CppAsString(typename))
673 #define AssertVariableIsOfTypeMacro(varname, typename) \
674 ((void) StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
675 CppAsString(varname) " does not have type " CppAsString(typename)))
676 #else /* !HAVE__BUILTIN_TYPES_COMPATIBLE_P */
677 #define AssertVariableIsOfType(varname, typename) \
678 StaticAssertStmt(sizeof(varname) == sizeof(typename), \
679 CppAsString(varname) " does not have type " CppAsString(typename))
680 #define AssertVariableIsOfTypeMacro(varname, typename) \
681 ((void) StaticAssertExpr(sizeof(varname) == sizeof(typename), \
682 CppAsString(varname) " does not have type " CppAsString(typename)))
683 #endif /* HAVE__BUILTIN_TYPES_COMPATIBLE_P */
686 /* ----------------------------------------------------------------
687 * Section 7: widely useful macros
688 * ----------------------------------------------------------------
692 * Return the maximum of two numbers.
694 #define Max(x, y) ((x) > (y) ? (x) : (y))
698 * Return the minimum of two numbers.
700 #define Min(x, y) ((x) < (y) ? (x) : (y))
704 * Return the absolute value of the argument.
706 #define Abs(x) ((x) >= 0 ? (x) : -(x))
710 * Like standard library function strncpy(), except that result string
711 * is guaranteed to be null-terminated --- that is, at most N-1 bytes
712 * of the source string will be kept.
713 * Also, the macro returns no result (too hard to do that without
714 * evaluating the arguments multiple times, which seems worse).
716 * BTW: when you need to copy a non-null-terminated string (like a text
717 * datum) and add a null, do not do it with StrNCpy(..., len+1). That
718 * might seem to work, but it fetches one byte more than there is in the
719 * text object. One fine day you'll have a SIGSEGV because there isn't
720 * another byte before the end of memory. Don't laugh, we've had real
721 * live bug reports from real live users over exactly this mistake.
722 * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
724 #define StrNCpy(dst,src,len) \
727 char * _dst = (dst); \
732 strncpy(_dst, (src), _len); \
733 _dst[_len-1] = '\0'; \
738 /* Get a bit mask of the bits set in non-long aligned addresses */
739 #define LONG_ALIGN_MASK (sizeof(long) - 1)
743 * Exactly the same as standard library function memset(), but considerably
744 * faster for zeroing small word-aligned structures (such as parsetree nodes).
745 * This has to be a macro because the main point is to avoid function-call
746 * overhead. However, we have also found that the loop is faster than
747 * native libc memset() on some platforms, even those with assembler
748 * memset() functions. More research needs to be done, perhaps with
749 * MEMSET_LOOP_LIMIT tests in configure.
751 #define MemSet(start, val, len) \
754 /* must be void* because we don't know if it is integer aligned yet */ \
755 void *_vstart = (void *) (start); \
759 if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
760 (_len & LONG_ALIGN_MASK) == 0 && \
762 _len <= MEMSET_LOOP_LIMIT && \
764 * If MEMSET_LOOP_LIMIT == 0, optimizer should find \
765 * the whole "if" false at compile time. \
767 MEMSET_LOOP_LIMIT != 0) \
769 long *_start = (long *) _vstart; \
770 long *_stop = (long *) ((char *) _start + _len); \
771 while (_start < _stop) \
775 memset(_vstart, _val, _len); \
779 * MemSetAligned is the same as MemSet except it omits the test to see if
780 * "start" is word-aligned. This is okay to use if the caller knows a-priori
781 * that the pointer is suitably aligned (typically, because he just got it
782 * from palloc(), which always delivers a max-aligned pointer).
784 #define MemSetAligned(start, val, len) \
787 long *_start = (long *) (start); \
791 if ((_len & LONG_ALIGN_MASK) == 0 && \
793 _len <= MEMSET_LOOP_LIMIT && \
794 MEMSET_LOOP_LIMIT != 0) \
796 long *_stop = (long *) ((char *) _start + _len); \
797 while (_start < _stop) \
801 memset(_start, _val, _len); \
806 * MemSetTest/MemSetLoop are a variant version that allow all the tests in
807 * MemSet to be done at compile time in cases where "val" and "len" are
808 * constants *and* we know the "start" pointer must be word-aligned.
809 * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use
810 * MemSetAligned. Beware of multiple evaluations of the arguments when using
813 #define MemSetTest(val, len) \
814 ( ((len) & LONG_ALIGN_MASK) == 0 && \
815 (len) <= MEMSET_LOOP_LIMIT && \
816 MEMSET_LOOP_LIMIT != 0 && \
819 #define MemSetLoop(start, val, len) \
822 long * _start = (long *) (start); \
823 long * _stop = (long *) ((char *) _start + (Size) (len)); \
825 while (_start < _stop) \
831 * Mark a point as unreachable in a portable fashion. This should preferably
832 * be something that the compiler understands, to aid code generation.
833 * In assert-enabled builds, we prefer abort() for debugging reasons.
835 #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
836 #define pg_unreachable() __builtin_unreachable()
837 #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
838 #define pg_unreachable() __assume(0)
840 #define pg_unreachable() abort()
845 * Function inlining support -- Allow modules to define functions that may be
846 * inlined, if the compiler supports it.
848 * The function bodies must be defined in the module header prefixed by
849 * STATIC_IF_INLINE, protected by a cpp symbol that the module's .c file must
850 * define. If the compiler doesn't support inline functions, the function
851 * definitions are pulled in by the .c file as regular (not inline) symbols.
853 * The header must also declare the functions' prototypes, protected by
857 #define STATIC_IF_INLINE static inline
859 #define STATIC_IF_INLINE
860 #endif /* PG_USE_INLINE */
862 /* ----------------------------------------------------------------
863 * Section 8: random stuff
864 * ----------------------------------------------------------------
868 #define HIGHBIT (0x80)
869 #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
871 #define STATUS_OK (0)
872 #define STATUS_ERROR (-1)
873 #define STATUS_EOF (-2)
874 #define STATUS_FOUND (1)
875 #define STATUS_WAITING (2)
879 * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
880 * used in assert-enabled builds, to avoid compiler warnings about unused
881 * variables in assert-disabled builds.
883 #ifdef USE_ASSERT_CHECKING
884 #define PG_USED_FOR_ASSERTS_ONLY
886 #define PG_USED_FOR_ASSERTS_ONLY __attribute__((unused))
890 /* gettext domain name mangling */
893 * To better support parallel installations of major PostgeSQL
894 * versions as well as parallel installations of major library soname
895 * versions, we mangle the gettext domain name by appending those
896 * version numbers. The coding rule ought to be that whereever the
897 * domain name is mentioned as a literal, it must be wrapped into
898 * PG_TEXTDOMAIN(). The macros below do not work on non-literals; but
899 * that is somewhat intentional because it avoids having to worry
900 * about multiple states of premangling and postmangling as the values
901 * are being passed around.
903 * Make sure this matches the installation rules in nls-global.mk.
906 /* need a second indirection because we want to stringize the macro value, not the name */
907 #define CppAsString2(x) CppAsString(x)
909 #ifdef SO_MAJOR_VERSION
910 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
912 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
916 /* ----------------------------------------------------------------
917 * Section 9: system-specific hacks
919 * This should be limited to things that absolutely have to be
920 * included in every source file. The port-specific header file
921 * is usually a better place for this sort of thing.
922 * ----------------------------------------------------------------
926 * NOTE: this is also used for opening text files.
927 * WIN32 treats Control-Z as EOF in files opened in text mode.
928 * Therefore, we open files in binary mode on Win32 so we can read
929 * literal control-Z. The other affect is that we see CRLF, but
930 * that is OK because we can already handle those cleanly.
932 #if defined(WIN32) || defined(__CYGWIN__)
933 #define PG_BINARY O_BINARY
934 #define PG_BINARY_A "ab"
935 #define PG_BINARY_R "rb"
936 #define PG_BINARY_W "wb"
939 #define PG_BINARY_A "a"
940 #define PG_BINARY_R "r"
941 #define PG_BINARY_W "w"
945 * Provide prototypes for routines not present in a particular machine's
946 * standard C library.
949 #if !HAVE_DECL_SNPRINTF
951 snprintf(char *str, size_t count, const char *fmt,...)
952 /* This extension allows gcc to check the format string */
953 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
956 #if !HAVE_DECL_VSNPRINTF
957 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
960 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
961 #define memmove(d, s, c) bcopy(s, d, c)
964 /* no special DLL markers on most ports */
973 * The following is used as the arg list for signal handlers. Any ports
974 * that take something other than an int argument should override this in
975 * their pg_config_os.h file. Note that variable names are required
976 * because it is used in both the prototypes as well as the definitions.
977 * Note also the long name. We expect that this won't collide with
978 * other names causing compiler warnings.
982 #define SIGNAL_ARGS int postgres_signal_arg
986 * When there is no sigsetjmp, its functionality is provided by plain
987 * setjmp. Incidentally, nothing provides setjmp's functionality in
990 #ifndef HAVE_SIGSETJMP
991 #define sigjmp_buf jmp_buf
992 #define sigsetjmp(x,y) setjmp(x)
993 #define siglongjmp longjmp
996 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
997 extern int fdatasync(int fildes);
1000 /* If strtoq() exists, rename it to the more standard strtoll() */
1001 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
1002 #define strtoll strtoq
1003 #define HAVE_STRTOLL 1
1006 /* If strtouq() exists, rename it to the more standard strtoull() */
1007 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
1008 #define strtoull strtouq
1009 #define HAVE_STRTOULL 1
1013 * We assume if we have these two functions, we have their friends too, and
1014 * can use the wide-character functions.
1016 #if defined(HAVE_WCSTOMBS) && defined(HAVE_TOWLOWER)
1017 #define USE_WIDE_UPPER_LOWER
1020 /* EXEC_BACKEND defines */
1022 #define NON_EXEC_STATIC
1024 #define NON_EXEC_STATIC static
1027 /* /port compatibility functions */