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-2013, 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 */
56 #if !defined(WIN32) && !defined(__CYGWIN__) /* win32 includes further down */
57 #include "pg_config_os.h" /* must be before any system header files */
60 #if _MSC_VER >= 1400 || defined(HAVE_CRTDEFS_H)
61 #define errcode __msvc_errcode
67 * We have to include stdlib.h here because it defines many of these macros
68 * on some platforms, and we only want our definitions used if stdlib.h doesn't
69 * have its own. The same goes for stddef and stdarg if present.
83 #include <sys/types.h>
86 #if defined(WIN32) || defined(__CYGWIN__)
87 #include <fcntl.h> /* ensure O_BINARY is available */
90 #if defined(WIN32) || defined(__CYGWIN__)
91 /* We have to redefine some system functions after they are included above. */
92 #include "pg_config_os.h"
95 /* Must be before gettext() games below */
98 #define _(x) gettext(x)
103 #define gettext(x) (x)
104 #define dgettext(d,x) (x)
105 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
106 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
110 * Use this to mark string constants as needing translation at some later
111 * time, rather than immediately. This is useful for cases where you need
112 * access to the original string and translated string, and for cases where
113 * immediate translation is not possible, like when initializing global
115 * http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
117 #define gettext_noop(x) (x)
120 /* ----------------------------------------------------------------
121 * Section 1: hacks to cope with non-ANSI C compilers
123 * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
124 * ----------------------------------------------------------------
129 * Convert the argument to a string, using the C preprocessor.
131 * Concatenate two arguments together, using the C preprocessor.
133 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks
134 * whether #identifier works, but if we have that we likely have ## too.
136 #if defined(HAVE_STRINGIZE)
138 #define CppAsString(identifier) #identifier
139 #define CppConcat(x, y) x##y
140 #else /* !HAVE_STRINGIZE */
142 #define CppAsString(identifier) "identifier"
145 * CppIdentity -- On Reiser based cpp's this is used to concatenate
146 * two tokens. That is
147 * CppIdentity(A)B ==> AB
148 * We renamed it to _private_CppIdentity because it should not
149 * be referenced outside this file. On other cpp's it
152 #define _priv_CppIdentity(x)x
153 #define CppConcat(x, y) _priv_CppIdentity(x)y
154 #endif /* !HAVE_STRINGIZE */
157 * dummyret is used to set return values in macros that use ?: to make
158 * assignments. gcc wants these to be void, other compilers like char
160 #ifdef __GNUC__ /* GNU cc */
161 #define dummyret void
163 #define dummyret char
167 #define __attribute__(_arg_)
170 /* ----------------------------------------------------------------
171 * Section 2: bool, true, false, TRUE, FALSE, NULL
172 * ----------------------------------------------------------------
177 * Boolean value, either true or false.
179 * XXX for C++ compilers, we assume the compiler has a compatible
180 * built-in definition of bool.
190 #define true ((bool) 1)
194 #define false ((bool) 0)
198 typedef bool *BoolPtr;
213 #define NULL ((void *) 0)
217 /* ----------------------------------------------------------------
218 * Section 3: standard system types
219 * ----------------------------------------------------------------
224 * Variable holding address of any memory resident object.
226 * XXX Pointer arithmetic is done with this, so it can't be void *
227 * under "true" ANSI compilers.
229 typedef char *Pointer;
233 * Signed integer, EXACTLY N BITS IN SIZE,
234 * used for numerical computations and the
235 * frontend/backend protocol.
238 typedef signed char int8; /* == 8 bits */
239 typedef signed short int16; /* == 16 bits */
240 typedef signed int int32; /* == 32 bits */
241 #endif /* not HAVE_INT8 */
245 * Unsigned integer, EXACTLY N BITS IN SIZE,
246 * used for numerical computations and the
247 * frontend/backend protocol.
250 typedef unsigned char uint8; /* == 8 bits */
251 typedef unsigned short uint16; /* == 16 bits */
252 typedef unsigned int uint32; /* == 32 bits */
253 #endif /* not HAVE_UINT8 */
257 * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
259 typedef uint8 bits8; /* >= 8 bits */
260 typedef uint16 bits16; /* >= 16 bits */
261 typedef uint32 bits32; /* >= 32 bits */
266 #ifdef HAVE_LONG_INT_64
267 /* Plain "long int" fits, use it */
270 typedef long int int64;
273 typedef unsigned long int uint64;
275 #elif defined(HAVE_LONG_LONG_INT_64)
276 /* We have working support for "long long int", use that */
279 typedef long long int int64;
282 typedef unsigned long long int uint64;
285 /* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */
286 #error must have a working 64-bit integer datatype
289 /* Decide if we need to decorate 64-bit constants */
290 #ifdef HAVE_LL_CONSTANTS
291 #define INT64CONST(x) ((int64) x##LL)
292 #define UINT64CONST(x) ((uint64) x##ULL)
294 #define INT64CONST(x) ((int64) x)
295 #define UINT64CONST(x) ((uint64) x)
299 /* Select timestamp representation (float8 or int64) */
300 #ifdef USE_INTEGER_DATETIMES
301 #define HAVE_INT64_TIMESTAMP
304 /* sig_atomic_t is required by ANSI C, but may be missing on old platforms */
305 #ifndef HAVE_SIG_ATOMIC_T
306 typedef int sig_atomic_t;
311 * Size of any memory resident object, as returned by sizeof.
317 * Index into any memory resident array.
320 * Indices are non negative.
322 typedef unsigned int Index;
326 * Offset into any memory resident array.
329 * This differs from an Index in that an Index is always
330 * non negative, whereas Offset may be negative.
332 typedef signed int Offset;
335 * Common Postgres datatype names (as used in the catalogs)
337 typedef float float4;
338 typedef double float8;
341 * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
345 /* typedef Oid is in postgres_ext.h */
348 * regproc is the type name used in the include/catalog headers, but
349 * RegProcedure is the preferred name in C code.
352 typedef regproc RegProcedure;
354 typedef uint32 TransactionId;
356 typedef uint32 LocalTransactionId;
358 typedef uint32 SubTransactionId;
360 #define InvalidSubTransactionId ((SubTransactionId) 0)
361 #define TopSubTransactionId ((SubTransactionId) 1)
363 /* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
364 typedef TransactionId MultiXactId;
366 typedef uint32 MultiXactOffset;
368 typedef uint32 CommandId;
370 #define FirstCommandId ((CommandId) 0)
371 #define InvalidCommandId (~(CommandId)0)
374 * Array indexing support
383 * Variable-length datatypes all share the 'struct varlena' header.
385 * NOTE: for TOASTable types, this is an oversimplification, since the value
386 * may be compressed or moved out-of-line. However datatype-specific routines
387 * are mostly content to deal with de-TOASTed values only, and of course
388 * client-side routines should never see a TOASTed value. But even in a
389 * de-TOASTed value, beware of touching vl_len_ directly, as its representation
390 * is no longer convenient. It's recommended that code always use the VARDATA,
391 * VARSIZE, and SET_VARSIZE macros instead of relying on direct mentions of
392 * the struct fields. See postgres.h for details of the TOASTed form.
397 char vl_len_[4]; /* Do not touch this field directly! */
401 #define VARHDRSZ ((int32) sizeof(int32))
404 * These widely-used datatypes are just a varlena header and the data bytes.
405 * There is no terminating null or anything like that --- the data length is
406 * always VARSIZE(ptr) - VARHDRSZ.
408 typedef struct varlena bytea;
409 typedef struct varlena text;
410 typedef struct varlena BpChar; /* blank-padded char, ie SQL char(n) */
411 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
414 * Specialized array types. These are physically laid out just the same
415 * as regular arrays (so that the regular array subscripting code works
416 * with them). They exist as distinct types mostly for historical reasons:
417 * they have nonstandard I/O behavior which we don't want to change for fear
418 * of breaking applications that look at the system catalogs. There is also
419 * an implementation issue for oidvector: it's part of the primary key for
420 * pg_proc, and we can't use the normal btree array support routines for that
421 * without circularity.
425 int32 vl_len_; /* these fields must match ArrayType! */
426 int ndim; /* always 1 for int2vector */
427 int32 dataoffset; /* always 0 for int2vector */
431 int16 values[1]; /* VARIABLE LENGTH ARRAY */
432 } int2vector; /* VARIABLE LENGTH STRUCT */
436 int32 vl_len_; /* these fields must match ArrayType! */
437 int ndim; /* always 1 for oidvector */
438 int32 dataoffset; /* always 0 for oidvector */
442 Oid values[1]; /* VARIABLE LENGTH ARRAY */
443 } oidvector; /* VARIABLE LENGTH STRUCT */
446 * Representation of a Name: effectively just a C string, but null-padded to
447 * exactly NAMEDATALEN bytes. The use of a struct is historical.
449 typedef struct nameData
451 char data[NAMEDATALEN];
453 typedef NameData *Name;
455 #define NameStr(name) ((name).data)
458 * Support macros for escaping strings. escape_backslash should be TRUE
459 * if generating a non-standard-conforming string. Prefixing a string
460 * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
461 * Beware of multiple evaluation of the "ch" argument!
463 #define SQL_STR_DOUBLE(ch, escape_backslash) \
464 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
466 #define ESCAPE_STRING_SYNTAX 'E'
468 /* ----------------------------------------------------------------
469 * Section 4: IsValid macros for system types
470 * ----------------------------------------------------------------
474 * True iff bool is valid.
476 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
480 * True iff pointer is valid.
482 #define PointerIsValid(pointer) ((const void*)(pointer) != NULL)
486 * True iff pointer is properly aligned to point to the given type.
488 #define PointerIsAligned(pointer, type) \
489 (((intptr_t)(pointer) % (sizeof (type))) == 0)
491 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
493 #define RegProcedureIsValid(p) OidIsValid(p)
496 /* ----------------------------------------------------------------
497 * Section 5: offsetof, lengthof, endof, alignment
498 * ----------------------------------------------------------------
502 * Offset of a structure/union field within that structure/union.
504 * XXX This is supposed to be part of stddef.h, but isn't on
505 * some systems (like SunOS 4).
508 #define offsetof(type, field) ((long) &((type *)0)->field)
509 #endif /* offsetof */
513 * Number of elements in an array.
515 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
519 * Address of the element one past the last in an array.
521 #define endof(array) (&(array)[lengthof(array)])
524 * Alignment macros: align a length or address appropriately for a given type.
525 * The fooALIGN() macros round up to a multiple of the required alignment,
526 * while the fooALIGN_DOWN() macros round down. The latter are more useful
527 * for problems like "how many X-sized structures will fit in a page?".
529 * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
530 * That case seems extremely unlikely to be needed in practice, however.
534 #define TYPEALIGN(ALIGNVAL,LEN) \
535 (((intptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((intptr_t) ((ALIGNVAL) - 1)))
537 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
538 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
539 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
540 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
541 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
542 /* MAXALIGN covers only built-in types, not buffers */
543 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
545 #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
546 (((intptr_t) (LEN)) & ~((intptr_t) ((ALIGNVAL) - 1)))
548 #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
549 #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
550 #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
551 #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
552 #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
554 /* ----------------------------------------------------------------
555 * Section 6: assertions
556 * ----------------------------------------------------------------
560 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
563 * It should _NOT_ be defined in releases or in benchmark copies
567 * Assert() can be used in both frontend and backend code. In frontend code it
568 * just calls the standard assert, if it's available. If use of assertions is
569 * not configured, it does nothing.
571 #ifndef USE_ASSERT_CHECKING
573 #define Assert(condition)
574 #define AssertMacro(condition) ((void)true)
575 #define AssertArg(condition)
576 #define AssertState(condition)
577 #define Trap(condition, errorType)
578 #define TrapMacro(condition, errorType) (true)
580 #elif defined(FRONTEND)
583 #define Assert(p) assert(p)
584 #define AssertMacro(p) ((void) assert(p))
585 #define AssertArg(condition) assert(condition)
586 #define AssertState(condition) assert(condition)
587 #else /* USE_ASSERT_CHECKING && !FRONTEND */
591 * Generates an exception if the given condition is true.
593 #define Trap(condition, errorType) \
595 if ((assert_enabled) && (condition)) \
596 ExceptionalCondition(CppAsString(condition), (errorType), \
597 __FILE__, __LINE__); \
601 * TrapMacro is the same as Trap but it's intended for use in macros:
603 * #define foo(x) (AssertMacro(x != 0), bar(x))
607 #define TrapMacro(condition, errorType) \
608 ((bool) ((! assert_enabled) || ! (condition) || \
609 (ExceptionalCondition(CppAsString(condition), (errorType), \
610 __FILE__, __LINE__), 0)))
612 #define Assert(condition) \
613 Trap(!(condition), "FailedAssertion")
615 #define AssertMacro(condition) \
616 ((void) TrapMacro(!(condition), "FailedAssertion"))
618 #define AssertArg(condition) \
619 Trap(!(condition), "BadArgument")
621 #define AssertState(condition) \
622 Trap(!(condition), "BadState")
623 #endif /* USE_ASSERT_CHECKING && !FRONTEND */
627 * Macros to support compile-time assertion checks.
629 * If the "condition" (a compile-time-constant expression) evaluates to false,
630 * throw a compile error using the "errmessage" (a string literal).
632 * gcc 4.6 and up supports _Static_assert(), but there are bizarre syntactic
633 * placement restrictions. These macros make it safe to use as a statement
634 * or in an expression, respectively.
636 * Otherwise we fall back on a kluge that assumes the compiler will complain
637 * about a negative width for a struct bit-field. This will not include a
638 * helpful error message, but it beats not getting an error at all.
640 #ifdef HAVE__STATIC_ASSERT
641 #define StaticAssertStmt(condition, errmessage) \
642 do { _Static_assert(condition, errmessage); } while(0)
643 #define StaticAssertExpr(condition, errmessage) \
644 ({ StaticAssertStmt(condition, errmessage); true; })
645 #else /* !HAVE__STATIC_ASSERT */
646 #define StaticAssertStmt(condition, errmessage) \
647 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
648 #define StaticAssertExpr(condition, errmessage) \
649 StaticAssertStmt(condition, errmessage)
650 #endif /* HAVE__STATIC_ASSERT */
654 * Compile-time checks that a variable (or expression) has the specified type.
656 * AssertVariableIsOfType() can be used as a statement.
657 * AssertVariableIsOfTypeMacro() is intended for use in macros, eg
658 * #define foo(x) (AssertVariableIsOfTypeMacro(x, int), bar(x))
660 * If we don't have __builtin_types_compatible_p, we can still assert that
661 * the types have the same size. This is far from ideal (especially on 32-bit
662 * platforms) but it provides at least some coverage.
664 #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
665 #define AssertVariableIsOfType(varname, typename) \
666 StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \
667 CppAsString(varname) " does not have type " CppAsString(typename))
668 #define AssertVariableIsOfTypeMacro(varname, typename) \
669 ((void) StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
670 CppAsString(varname) " does not have type " CppAsString(typename)))
671 #else /* !HAVE__BUILTIN_TYPES_COMPATIBLE_P */
672 #define AssertVariableIsOfType(varname, typename) \
673 StaticAssertStmt(sizeof(varname) == sizeof(typename), \
674 CppAsString(varname) " does not have type " CppAsString(typename))
675 #define AssertVariableIsOfTypeMacro(varname, typename) \
676 ((void) StaticAssertExpr(sizeof(varname) == sizeof(typename), \
677 CppAsString(varname) " does not have type " CppAsString(typename)))
678 #endif /* HAVE__BUILTIN_TYPES_COMPATIBLE_P */
681 /* ----------------------------------------------------------------
682 * Section 7: widely useful macros
683 * ----------------------------------------------------------------
687 * Return the maximum of two numbers.
689 #define Max(x, y) ((x) > (y) ? (x) : (y))
693 * Return the minimum of two numbers.
695 #define Min(x, y) ((x) < (y) ? (x) : (y))
699 * Return the absolute value of the argument.
701 #define Abs(x) ((x) >= 0 ? (x) : -(x))
705 * Like standard library function strncpy(), except that result string
706 * is guaranteed to be null-terminated --- that is, at most N-1 bytes
707 * of the source string will be kept.
708 * Also, the macro returns no result (too hard to do that without
709 * evaluating the arguments multiple times, which seems worse).
711 * BTW: when you need to copy a non-null-terminated string (like a text
712 * datum) and add a null, do not do it with StrNCpy(..., len+1). That
713 * might seem to work, but it fetches one byte more than there is in the
714 * text object. One fine day you'll have a SIGSEGV because there isn't
715 * another byte before the end of memory. Don't laugh, we've had real
716 * live bug reports from real live users over exactly this mistake.
717 * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
719 #define StrNCpy(dst,src,len) \
722 char * _dst = (dst); \
727 strncpy(_dst, (src), _len); \
728 _dst[_len-1] = '\0'; \
733 /* Get a bit mask of the bits set in non-long aligned addresses */
734 #define LONG_ALIGN_MASK (sizeof(long) - 1)
738 * Exactly the same as standard library function memset(), but considerably
739 * faster for zeroing small word-aligned structures (such as parsetree nodes).
740 * This has to be a macro because the main point is to avoid function-call
741 * overhead. However, we have also found that the loop is faster than
742 * native libc memset() on some platforms, even those with assembler
743 * memset() functions. More research needs to be done, perhaps with
744 * MEMSET_LOOP_LIMIT tests in configure.
746 #define MemSet(start, val, len) \
749 /* must be void* because we don't know if it is integer aligned yet */ \
750 void *_vstart = (void *) (start); \
754 if ((((intptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
755 (_len & LONG_ALIGN_MASK) == 0 && \
757 _len <= MEMSET_LOOP_LIMIT && \
759 * If MEMSET_LOOP_LIMIT == 0, optimizer should find \
760 * the whole "if" false at compile time. \
762 MEMSET_LOOP_LIMIT != 0) \
764 long *_start = (long *) _vstart; \
765 long *_stop = (long *) ((char *) _start + _len); \
766 while (_start < _stop) \
770 memset(_vstart, _val, _len); \
774 * MemSetAligned is the same as MemSet except it omits the test to see if
775 * "start" is word-aligned. This is okay to use if the caller knows a-priori
776 * that the pointer is suitably aligned (typically, because he just got it
777 * from palloc(), which always delivers a max-aligned pointer).
779 #define MemSetAligned(start, val, len) \
782 long *_start = (long *) (start); \
786 if ((_len & LONG_ALIGN_MASK) == 0 && \
788 _len <= MEMSET_LOOP_LIMIT && \
789 MEMSET_LOOP_LIMIT != 0) \
791 long *_stop = (long *) ((char *) _start + _len); \
792 while (_start < _stop) \
796 memset(_start, _val, _len); \
801 * MemSetTest/MemSetLoop are a variant version that allow all the tests in
802 * MemSet to be done at compile time in cases where "val" and "len" are
803 * constants *and* we know the "start" pointer must be word-aligned.
804 * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use
805 * MemSetAligned. Beware of multiple evaluations of the arguments when using
808 #define MemSetTest(val, len) \
809 ( ((len) & LONG_ALIGN_MASK) == 0 && \
810 (len) <= MEMSET_LOOP_LIMIT && \
811 MEMSET_LOOP_LIMIT != 0 && \
814 #define MemSetLoop(start, val, len) \
817 long * _start = (long *) (start); \
818 long * _stop = (long *) ((char *) _start + (Size) (len)); \
820 while (_start < _stop) \
826 * Mark a point as unreachable in a portable fashion. This should preferably
827 * be something that the compiler understands, to aid code generation.
828 * In assert-enabled builds, we prefer abort() for debugging reasons.
830 #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
831 #define pg_unreachable() __builtin_unreachable()
832 #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
833 #define pg_unreachable() __assume(0)
835 #define pg_unreachable() abort()
840 * Function inlining support -- Allow modules to define functions that may be
841 * inlined, if the compiler supports it.
843 * The function bodies must be defined in the module header prefixed by
844 * STATIC_IF_INLINE, protected by a cpp symbol that the module's .c file must
845 * define. If the compiler doesn't support inline functions, the function
846 * definitions are pulled in by the .c file as regular (not inline) symbols.
848 * The header must also declare the functions' prototypes, protected by
852 #define STATIC_IF_INLINE static inline
854 #define STATIC_IF_INLINE
855 #endif /* PG_USE_INLINE */
857 /* ----------------------------------------------------------------
858 * Section 8: random stuff
859 * ----------------------------------------------------------------
863 #define HIGHBIT (0x80)
864 #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
866 #define STATUS_OK (0)
867 #define STATUS_ERROR (-1)
868 #define STATUS_EOF (-2)
869 #define STATUS_FOUND (1)
870 #define STATUS_WAITING (2)
874 * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
875 * used in assert-enabled builds, to avoid compiler warnings about unused
876 * variables in assert-disabled builds.
878 #ifdef USE_ASSERT_CHECKING
879 #define PG_USED_FOR_ASSERTS_ONLY
881 #define PG_USED_FOR_ASSERTS_ONLY __attribute__((unused))
885 /* gettext domain name mangling */
888 * To better support parallel installations of major PostgeSQL
889 * versions as well as parallel installations of major library soname
890 * versions, we mangle the gettext domain name by appending those
891 * version numbers. The coding rule ought to be that whereever the
892 * domain name is mentioned as a literal, it must be wrapped into
893 * PG_TEXTDOMAIN(). The macros below do not work on non-literals; but
894 * that is somewhat intentional because it avoids having to worry
895 * about multiple states of premangling and postmangling as the values
896 * are being passed around.
898 * Make sure this matches the installation rules in nls-global.mk.
901 /* need a second indirection because we want to stringize the macro value, not the name */
902 #define CppAsString2(x) CppAsString(x)
904 #ifdef SO_MAJOR_VERSION
905 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
907 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
911 /* ----------------------------------------------------------------
912 * Section 9: system-specific hacks
914 * This should be limited to things that absolutely have to be
915 * included in every source file. The port-specific header file
916 * is usually a better place for this sort of thing.
917 * ----------------------------------------------------------------
921 * NOTE: this is also used for opening text files.
922 * WIN32 treats Control-Z as EOF in files opened in text mode.
923 * Therefore, we open files in binary mode on Win32 so we can read
924 * literal control-Z. The other affect is that we see CRLF, but
925 * that is OK because we can already handle those cleanly.
927 #if defined(WIN32) || defined(__CYGWIN__)
928 #define PG_BINARY O_BINARY
929 #define PG_BINARY_A "ab"
930 #define PG_BINARY_R "rb"
931 #define PG_BINARY_W "wb"
934 #define PG_BINARY_A "a"
935 #define PG_BINARY_R "r"
936 #define PG_BINARY_W "w"
940 * Provide prototypes for routines not present in a particular machine's
941 * standard C library.
944 #if !HAVE_DECL_SNPRINTF
946 snprintf(char *str, size_t count, const char *fmt,...)
947 /* This extension allows gcc to check the format string */
948 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
951 #if !HAVE_DECL_VSNPRINTF
952 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
955 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
956 #define memmove(d, s, c) bcopy(s, d, c)
959 /* no special DLL markers on most ports */
968 * The following is used as the arg list for signal handlers. Any ports
969 * that take something other than an int argument should override this in
970 * their pg_config_os.h file. Note that variable names are required
971 * because it is used in both the prototypes as well as the definitions.
972 * Note also the long name. We expect that this won't collide with
973 * other names causing compiler warnings.
977 #define SIGNAL_ARGS int postgres_signal_arg
981 * When there is no sigsetjmp, its functionality is provided by plain
982 * setjmp. Incidentally, nothing provides setjmp's functionality in
985 #ifndef HAVE_SIGSETJMP
986 #define sigjmp_buf jmp_buf
987 #define sigsetjmp(x,y) setjmp(x)
988 #define siglongjmp longjmp
991 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
992 extern int fdatasync(int fildes);
995 /* If strtoq() exists, rename it to the more standard strtoll() */
996 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
997 #define strtoll strtoq
998 #define HAVE_STRTOLL 1
1001 /* If strtouq() exists, rename it to the more standard strtoull() */
1002 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
1003 #define strtoull strtouq
1004 #define HAVE_STRTOULL 1
1008 * We assume if we have these two functions, we have their friends too, and
1009 * can use the wide-character functions.
1011 #if defined(HAVE_WCSTOMBS) && defined(HAVE_TOWLOWER)
1012 #define USE_WIDE_UPPER_LOWER
1015 /* EXEC_BACKEND defines */
1017 #define NON_EXEC_STATIC
1019 #define NON_EXEC_STATIC static
1022 /* /port compatibility functions */