diff options
author | Pavan Deolasee | 2015-05-05 09:19:18 +0000 |
---|---|---|
committer | Pavan Deolasee | 2015-05-05 09:19:18 +0000 |
commit | 73fa25c67cbfa24c03e28c96bf356f2592671730 (patch) | |
tree | 10ded7e26abd78d93658cb72fc5cb9d4672eff2a /src/include/c.h | |
parent | da4d108859bcd7a308ca75aba54281e32968822c (diff) | |
parent | 4a9ab6d8619817f9e3989c99b65140e19041dab7 (diff) |
Merge branch 'XL_MASTER_MERGE_9_4' into XL_NEW_MASTER
Conflicts:
src/test/regress/expected/aggregates.out
src/test/regress/expected/create_index.out
src/test/regress/expected/inherit.out
src/test/regress/expected/join.out
src/test/regress/expected/window.out
src/test/regress/expected/with.out
Diffstat (limited to 'src/include/c.h')
-rw-r--r-- | src/include/c.h | 268 |
1 files changed, 218 insertions, 50 deletions
diff --git a/src/include/c.h b/src/include/c.h index 8b3c5e5996..df22d50d4e 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -9,7 +9,7 @@ * polluting the namespace with lots of stuff... * * - * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group + * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * src/include/c.h @@ -31,12 +31,13 @@ * 3) standard system types * 4) IsValid macros for system types * 5) offsetof, lengthof, endof, alignment - * 6) widely useful macros - * 7) random stuff - * 8) system-specific hacks + * 6) assertions + * 7) widely useful macros + * 8) random stuff + * 9) system-specific hacks * * NOTE: since this file is included by both frontend and backend modules, it's - * almost certainly wrong to put an "extern" declaration here. typedefs and + * almost certainly wrong to put an "extern" declaration here. typedefs and * macros are the kind of thing that might go here. * *---------------------------------------------------------------- @@ -44,19 +45,26 @@ #ifndef C_H #define C_H -/* - * We have to include stdlib.h here because it defines many of these macros - * on some platforms, and we only want our definitions used if stdlib.h doesn't - * have its own. The same goes for stddef and stdarg if present. - */ +#include "postgres_ext.h" + +/* Must undef pg_config_ext.h symbols before including pg_config.h */ +#undef PG_INT64_TYPE #include "pg_config.h" #include "pg_config_manual.h" /* must be after pg_config.h */ -#if !defined(WIN32) && !defined(__CYGWIN__) /* win32 will include further - * down */ + +/* + * We always rely on the WIN32 macro being set by our build system, + * but _WIN32 is the compiler pre-defined macro. So make sure we define + * WIN32 whenever _WIN32 is set, to facilitate standalone building. + */ +#if defined(_WIN32) && !defined(WIN32) +#define WIN32 +#endif + +#if !defined(WIN32) && !defined(__CYGWIN__) /* win32 includes further down */ #include "pg_config_os.h" /* must be before any system header files */ #endif -#include "postgres_ext.h" #if _MSC_VER >= 1400 || defined(HAVE_CRTDEFS_H) #define errcode __msvc_errcode @@ -64,6 +72,12 @@ #undef errcode #endif +/* + * We have to include stdlib.h here because it defines many of these macros + * on some platforms, and we only want our definitions used if stdlib.h doesn't + * have its own. The same goes for stddef and stdarg if present. + */ + #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -103,7 +117,7 @@ /* * Use this to mark string constants as needing translation at some later - * time, rather than immediately. This is useful for cases where you need + * time, rather than immediately. This is useful for cases where you need * access to the original string and translated string, and for cases where * immediate translation is not possible, like when initializing global * variables. @@ -125,28 +139,12 @@ * CppConcat * Concatenate two arguments together, using the C preprocessor. * - * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks - * whether #identifier works, but if we have that we likely have ## too. + * Note: There used to be support here for pre-ANSI C compilers that didn't + * support # and ##. Nowadays, these macros are just for clarity and/or + * backward compatibility with existing PostgreSQL code. */ -#if defined(HAVE_STRINGIZE) - #define CppAsString(identifier) #identifier #define CppConcat(x, y) x##y -#else /* !HAVE_STRINGIZE */ - -#define CppAsString(identifier) "identifier" - -/* - * CppIdentity -- On Reiser based cpp's this is used to concatenate - * two tokens. That is - * CppIdentity(A)B ==> AB - * We renamed it to _private_CppIdentity because it should not - * be referenced outside this file. On other cpp's it - * produces A B. - */ -#define _priv_CppIdentity(x)x -#define CppConcat(x, y) _priv_CppIdentity(x)y -#endif /* !HAVE_STRINGIZE */ /* * dummyret is used to set return values in macros that use ?: to make @@ -329,8 +327,6 @@ typedef signed int Offset; /* * Common Postgres datatype names (as used in the catalogs) */ -typedef int16 int2; -typedef int32 int4; typedef float float4; typedef double float8; @@ -365,6 +361,7 @@ typedef uint32 MultiXactOffset; typedef uint32 CommandId; #define FirstCommandId ((CommandId) 0) +#define InvalidCommandId (~(CommandId)0) /* * Array indexing support @@ -379,7 +376,7 @@ typedef struct * Variable-length datatypes all share the 'struct varlena' header. * * NOTE: for TOASTable types, this is an oversimplification, since the value - * may be compressed or moved out-of-line. However datatype-specific routines + * may be compressed or moved out-of-line. However datatype-specific routines * are mostly content to deal with de-TOASTed values only, and of course * client-side routines should never see a TOASTed value. But even in a * de-TOASTed value, beware of touching vl_len_ directly, as its representation @@ -409,7 +406,7 @@ typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */ /* * Specialized array types. These are physically laid out just the same * as regular arrays (so that the regular array subscripting code works - * with them). They exist as distinct types mostly for historical reasons: + * with them). They exist as distinct types mostly for historical reasons: * they have nonstandard I/O behavior which we don't want to change for fear * of breaking applications that look at the system catalogs. There is also * an implementation issue for oidvector: it's part of the primary key for @@ -424,7 +421,7 @@ typedef struct Oid elemtype; int dim1; int lbound1; - int2 values[1]; /* VARIABLE LENGTH ARRAY */ + int16 values[1]; /* VARIABLE LENGTH ARRAY */ } int2vector; /* VARIABLE LENGTH STRUCT */ typedef struct @@ -452,7 +449,7 @@ typedef NameData *Name; /* * Support macros for escaping strings. escape_backslash should be TRUE - * if generating a non-standard-conforming string. Prefixing a string + * if generating a non-standard-conforming string. Prefixing a string * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming. * Beware of multiple evaluation of the "ch" argument! */ @@ -482,7 +479,7 @@ typedef NameData *Name; * True iff pointer is properly aligned to point to the given type. */ #define PointerIsAligned(pointer, type) \ - (((intptr_t)(pointer) % (sizeof (type))) == 0) + (((uintptr_t)(pointer) % (sizeof (type))) == 0) #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid)) @@ -528,7 +525,7 @@ typedef NameData *Name; */ #define TYPEALIGN(ALIGNVAL,LEN) \ - (((intptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((intptr_t) ((ALIGNVAL) - 1))) + (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1))) #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN)) #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN)) @@ -539,7 +536,7 @@ typedef NameData *Name; #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN)) #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \ - (((intptr_t) (LEN)) & ~((intptr_t) ((ALIGNVAL) - 1))) + (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1))) #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN)) #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN)) @@ -547,8 +544,147 @@ typedef NameData *Name; #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN)) #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN)) +/* + * The above macros will not work with types wider than uintptr_t, like with + * uint64 on 32-bit platforms. That's not problem for the usual use where a + * pointer or a length is aligned, but for the odd case that you need to + * align something (potentially) wider, use TYPEALIGN64. + */ +#define TYPEALIGN64(ALIGNVAL,LEN) \ + (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1))) + +/* we don't currently need wider versions of the other ALIGN macros */ +#define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN)) + /* ---------------------------------------------------------------- - * Section 6: widely useful macros + * Section 6: assertions + * ---------------------------------------------------------------- + */ + +/* + * USE_ASSERT_CHECKING, if defined, turns on all the assertions. + * - plai 9/5/90 + * + * It should _NOT_ be defined in releases or in benchmark copies + */ + +/* + * Assert() can be used in both frontend and backend code. In frontend code it + * just calls the standard assert, if it's available. If use of assertions is + * not configured, it does nothing. + */ +#ifndef USE_ASSERT_CHECKING + +#define Assert(condition) +#define AssertMacro(condition) ((void)true) +#define AssertArg(condition) +#define AssertState(condition) +#define Trap(condition, errorType) +#define TrapMacro(condition, errorType) (true) + +#elif defined(FRONTEND) + +#include <assert.h> +#define Assert(p) assert(p) +#define AssertMacro(p) ((void) assert(p)) +#define AssertArg(condition) assert(condition) +#define AssertState(condition) assert(condition) +#else /* USE_ASSERT_CHECKING && !FRONTEND */ + +/* + * Trap + * Generates an exception if the given condition is true. + */ +#define Trap(condition, errorType) \ + do { \ + if ((assert_enabled) && (condition)) \ + ExceptionalCondition(CppAsString(condition), (errorType), \ + __FILE__, __LINE__); \ + } while (0) + +/* + * TrapMacro is the same as Trap but it's intended for use in macros: + * + * #define foo(x) (AssertMacro(x != 0), bar(x)) + * + * Isn't CPP fun? + */ +#define TrapMacro(condition, errorType) \ + ((bool) ((! assert_enabled) || ! (condition) || \ + (ExceptionalCondition(CppAsString(condition), (errorType), \ + __FILE__, __LINE__), 0))) + +#define Assert(condition) \ + Trap(!(condition), "FailedAssertion") + +#define AssertMacro(condition) \ + ((void) TrapMacro(!(condition), "FailedAssertion")) + +#define AssertArg(condition) \ + Trap(!(condition), "BadArgument") + +#define AssertState(condition) \ + Trap(!(condition), "BadState") +#endif /* USE_ASSERT_CHECKING && !FRONTEND */ + + +/* + * Macros to support compile-time assertion checks. + * + * If the "condition" (a compile-time-constant expression) evaluates to false, + * throw a compile error using the "errmessage" (a string literal). + * + * gcc 4.6 and up supports _Static_assert(), but there are bizarre syntactic + * placement restrictions. These macros make it safe to use as a statement + * or in an expression, respectively. + * + * Otherwise we fall back on a kluge that assumes the compiler will complain + * about a negative width for a struct bit-field. This will not include a + * helpful error message, but it beats not getting an error at all. + */ +#ifdef HAVE__STATIC_ASSERT +#define StaticAssertStmt(condition, errmessage) \ + do { _Static_assert(condition, errmessage); } while(0) +#define StaticAssertExpr(condition, errmessage) \ + ({ StaticAssertStmt(condition, errmessage); true; }) +#else /* !HAVE__STATIC_ASSERT */ +#define StaticAssertStmt(condition, errmessage) \ + ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; })) +#define StaticAssertExpr(condition, errmessage) \ + StaticAssertStmt(condition, errmessage) +#endif /* HAVE__STATIC_ASSERT */ + + +/* + * Compile-time checks that a variable (or expression) has the specified type. + * + * AssertVariableIsOfType() can be used as a statement. + * AssertVariableIsOfTypeMacro() is intended for use in macros, eg + * #define foo(x) (AssertVariableIsOfTypeMacro(x, int), bar(x)) + * + * If we don't have __builtin_types_compatible_p, we can still assert that + * the types have the same size. This is far from ideal (especially on 32-bit + * platforms) but it provides at least some coverage. + */ +#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P +#define AssertVariableIsOfType(varname, typename) \ + StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \ + CppAsString(varname) " does not have type " CppAsString(typename)) +#define AssertVariableIsOfTypeMacro(varname, typename) \ + ((void) StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \ + CppAsString(varname) " does not have type " CppAsString(typename))) +#else /* !HAVE__BUILTIN_TYPES_COMPATIBLE_P */ +#define AssertVariableIsOfType(varname, typename) \ + StaticAssertStmt(sizeof(varname) == sizeof(typename), \ + CppAsString(varname) " does not have type " CppAsString(typename)) +#define AssertVariableIsOfTypeMacro(varname, typename) \ + ((void) StaticAssertExpr(sizeof(varname) == sizeof(typename), \ + CppAsString(varname) " does not have type " CppAsString(typename))) +#endif /* HAVE__BUILTIN_TYPES_COMPATIBLE_P */ + + +/* ---------------------------------------------------------------- + * Section 7: widely useful macros * ---------------------------------------------------------------- */ /* @@ -581,7 +717,7 @@ typedef NameData *Name; * datum) and add a null, do not do it with StrNCpy(..., len+1). That * might seem to work, but it fetches one byte more than there is in the * text object. One fine day you'll have a SIGSEGV because there isn't - * another byte before the end of memory. Don't laugh, we've had real + * another byte before the end of memory. Don't laugh, we've had real * live bug reports from real live users over exactly this mistake. * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead. */ @@ -607,7 +743,7 @@ typedef NameData *Name; * Exactly the same as standard library function memset(), but considerably * faster for zeroing small word-aligned structures (such as parsetree nodes). * This has to be a macro because the main point is to avoid function-call - * overhead. However, we have also found that the loop is faster than + * overhead. However, we have also found that the loop is faster than * native libc memset() on some platforms, even those with assembler * memset() functions. More research needs to be done, perhaps with * MEMSET_LOOP_LIMIT tests in configure. @@ -620,7 +756,7 @@ typedef NameData *Name; int _val = (val); \ Size _len = (len); \ \ - if ((((intptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \ + if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \ (_len & LONG_ALIGN_MASK) == 0 && \ _val == 0 && \ _len <= MEMSET_LOOP_LIMIT && \ @@ -691,8 +827,40 @@ typedef NameData *Name; } while (0) +/* + * Mark a point as unreachable in a portable fashion. This should preferably + * be something that the compiler understands, to aid code generation. + * In assert-enabled builds, we prefer abort() for debugging reasons. + */ +#if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING) +#define pg_unreachable() __builtin_unreachable() +#elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING) +#define pg_unreachable() __assume(0) +#else +#define pg_unreachable() abort() +#endif + + +/* + * Function inlining support -- Allow modules to define functions that may be + * inlined, if the compiler supports it. + * + * The function bodies must be defined in the module header prefixed by + * STATIC_IF_INLINE, protected by a cpp symbol that the module's .c file must + * define. If the compiler doesn't support inline functions, the function + * definitions are pulled in by the .c file as regular (not inline) symbols. + * + * The header must also declare the functions' prototypes, protected by + * !PG_USE_INLINE. + */ +#ifdef PG_USE_INLINE +#define STATIC_IF_INLINE static inline +#else +#define STATIC_IF_INLINE +#endif /* PG_USE_INLINE */ + /* ---------------------------------------------------------------- - * Section 7: random stuff + * Section 8: random stuff * ---------------------------------------------------------------- */ @@ -746,10 +914,10 @@ typedef NameData *Name; /* ---------------------------------------------------------------- - * Section 8: system-specific hacks + * Section 9: system-specific hacks * * This should be limited to things that absolutely have to be - * included in every source file. The port-specific header file + * included in every source file. The port-specific header file * is usually a better place for this sort of thing. * ---------------------------------------------------------------- */ @@ -758,7 +926,7 @@ typedef NameData *Name; * NOTE: this is also used for opening text files. * WIN32 treats Control-Z as EOF in files opened in text mode. * Therefore, we open files in binary mode on Win32 so we can read - * literal control-Z. The other affect is that we see CRLF, but + * literal control-Z. The other affect is that we see CRLF, but * that is OK because we can already handle those cleanly. */ #if defined(WIN32) || defined(__CYGWIN__) |