00001 /*------------------------------------------------------------------------- 00002 * 00003 * c.h 00004 * Fundamental C definitions. This is included by every .c file in 00005 * PostgreSQL (via either postgres.h or postgres_fe.h, as appropriate). 00006 * 00007 * Note that the definitions here are not intended to be exposed to clients 00008 * of the frontend interface libraries --- so we don't worry much about 00009 * polluting the namespace with lots of stuff... 00010 * 00011 * 00012 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group 00013 * Portions Copyright (c) 1994, Regents of the University of California 00014 * 00015 * src/include/c.h 00016 * 00017 *------------------------------------------------------------------------- 00018 */ 00019 /* 00020 *---------------------------------------------------------------- 00021 * TABLE OF CONTENTS 00022 * 00023 * When adding stuff to this file, please try to put stuff 00024 * into the relevant section, or add new sections as appropriate. 00025 * 00026 * section description 00027 * ------- ------------------------------------------------ 00028 * 0) pg_config.h and standard system headers 00029 * 1) hacks to cope with non-ANSI C compilers 00030 * 2) bool, true, false, TRUE, FALSE, NULL 00031 * 3) standard system types 00032 * 4) IsValid macros for system types 00033 * 5) offsetof, lengthof, endof, alignment 00034 * 6) assertions 00035 * 7) widely useful macros 00036 * 8) random stuff 00037 * 9) system-specific hacks 00038 * 00039 * NOTE: since this file is included by both frontend and backend modules, it's 00040 * almost certainly wrong to put an "extern" declaration here. typedefs and 00041 * macros are the kind of thing that might go here. 00042 * 00043 *---------------------------------------------------------------- 00044 */ 00045 #ifndef C_H 00046 #define C_H 00047 00048 #include "postgres_ext.h" 00049 00050 /* Must undef pg_config_ext.h symbols before including pg_config.h */ 00051 #undef PG_INT64_TYPE 00052 00053 #include "pg_config.h" 00054 #include "pg_config_manual.h" /* must be after pg_config.h */ 00055 00056 #if !defined(WIN32) && !defined(__CYGWIN__) /* win32 includes further down */ 00057 #include "pg_config_os.h" /* must be before any system header files */ 00058 #endif 00059 00060 #if _MSC_VER >= 1400 || defined(HAVE_CRTDEFS_H) 00061 #define errcode __msvc_errcode 00062 #include <crtdefs.h> 00063 #undef errcode 00064 #endif 00065 00066 /* 00067 * We have to include stdlib.h here because it defines many of these macros 00068 * on some platforms, and we only want our definitions used if stdlib.h doesn't 00069 * have its own. The same goes for stddef and stdarg if present. 00070 */ 00071 00072 #include <stdio.h> 00073 #include <stdlib.h> 00074 #include <string.h> 00075 #include <stddef.h> 00076 #include <stdarg.h> 00077 #ifdef HAVE_STRINGS_H 00078 #include <strings.h> 00079 #endif 00080 #ifdef HAVE_STDINT_H 00081 #include <stdint.h> 00082 #endif 00083 #include <sys/types.h> 00084 00085 #include <errno.h> 00086 #if defined(WIN32) || defined(__CYGWIN__) 00087 #include <fcntl.h> /* ensure O_BINARY is available */ 00088 #endif 00089 00090 #if defined(WIN32) || defined(__CYGWIN__) 00091 /* We have to redefine some system functions after they are included above. */ 00092 #include "pg_config_os.h" 00093 #endif 00094 00095 /* Must be before gettext() games below */ 00096 #include <locale.h> 00097 00098 #define _(x) gettext(x) 00099 00100 #ifdef ENABLE_NLS 00101 #include <libintl.h> 00102 #else 00103 #define gettext(x) (x) 00104 #define dgettext(d,x) (x) 00105 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p)) 00106 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p)) 00107 #endif 00108 00109 /* 00110 * Use this to mark string constants as needing translation at some later 00111 * time, rather than immediately. This is useful for cases where you need 00112 * access to the original string and translated string, and for cases where 00113 * immediate translation is not possible, like when initializing global 00114 * variables. 00115 * http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html 00116 */ 00117 #define gettext_noop(x) (x) 00118 00119 00120 /* ---------------------------------------------------------------- 00121 * Section 1: hacks to cope with non-ANSI C compilers 00122 * 00123 * type prefixes (const, signed, volatile, inline) are handled in pg_config.h. 00124 * ---------------------------------------------------------------- 00125 */ 00126 00127 /* 00128 * CppAsString 00129 * Convert the argument to a string, using the C preprocessor. 00130 * CppConcat 00131 * Concatenate two arguments together, using the C preprocessor. 00132 * 00133 * Note: the standard Autoconf macro AC_C_STRINGIZE actually only checks 00134 * whether #identifier works, but if we have that we likely have ## too. 00135 */ 00136 #if defined(HAVE_STRINGIZE) 00137 00138 #define CppAsString(identifier) #identifier 00139 #define CppConcat(x, y) x##y 00140 #else /* !HAVE_STRINGIZE */ 00141 00142 #define CppAsString(identifier) "identifier" 00143 00144 /* 00145 * CppIdentity -- On Reiser based cpp's this is used to concatenate 00146 * two tokens. That is 00147 * CppIdentity(A)B ==> AB 00148 * We renamed it to _private_CppIdentity because it should not 00149 * be referenced outside this file. On other cpp's it 00150 * produces A B. 00151 */ 00152 #define _priv_CppIdentity(x)x 00153 #define CppConcat(x, y) _priv_CppIdentity(x)y 00154 #endif /* !HAVE_STRINGIZE */ 00155 00156 /* 00157 * dummyret is used to set return values in macros that use ?: to make 00158 * assignments. gcc wants these to be void, other compilers like char 00159 */ 00160 #ifdef __GNUC__ /* GNU cc */ 00161 #define dummyret void 00162 #else 00163 #define dummyret char 00164 #endif 00165 00166 #ifndef __GNUC__ 00167 #define __attribute__(_arg_) 00168 #endif 00169 00170 /* ---------------------------------------------------------------- 00171 * Section 2: bool, true, false, TRUE, FALSE, NULL 00172 * ---------------------------------------------------------------- 00173 */ 00174 00175 /* 00176 * bool 00177 * Boolean value, either true or false. 00178 * 00179 * XXX for C++ compilers, we assume the compiler has a compatible 00180 * built-in definition of bool. 00181 */ 00182 00183 #ifndef __cplusplus 00184 00185 #ifndef bool 00186 typedef char bool; 00187 #endif 00188 00189 #ifndef true 00190 #define true ((bool) 1) 00191 #endif 00192 00193 #ifndef false 00194 #define false ((bool) 0) 00195 #endif 00196 #endif /* not C++ */ 00197 00198 typedef bool *BoolPtr; 00199 00200 #ifndef TRUE 00201 #define TRUE 1 00202 #endif 00203 00204 #ifndef FALSE 00205 #define FALSE 0 00206 #endif 00207 00208 /* 00209 * NULL 00210 * Null pointer. 00211 */ 00212 #ifndef NULL 00213 #define NULL ((void *) 0) 00214 #endif 00215 00216 00217 /* ---------------------------------------------------------------- 00218 * Section 3: standard system types 00219 * ---------------------------------------------------------------- 00220 */ 00221 00222 /* 00223 * Pointer 00224 * Variable holding address of any memory resident object. 00225 * 00226 * XXX Pointer arithmetic is done with this, so it can't be void * 00227 * under "true" ANSI compilers. 00228 */ 00229 typedef char *Pointer; 00230 00231 /* 00232 * intN 00233 * Signed integer, EXACTLY N BITS IN SIZE, 00234 * used for numerical computations and the 00235 * frontend/backend protocol. 00236 */ 00237 #ifndef HAVE_INT8 00238 typedef signed char int8; /* == 8 bits */ 00239 typedef signed short int16; /* == 16 bits */ 00240 typedef signed int int32; /* == 32 bits */ 00241 #endif /* not HAVE_INT8 */ 00242 00243 /* 00244 * uintN 00245 * Unsigned integer, EXACTLY N BITS IN SIZE, 00246 * used for numerical computations and the 00247 * frontend/backend protocol. 00248 */ 00249 #ifndef HAVE_UINT8 00250 typedef unsigned char uint8; /* == 8 bits */ 00251 typedef unsigned short uint16; /* == 16 bits */ 00252 typedef unsigned int uint32; /* == 32 bits */ 00253 #endif /* not HAVE_UINT8 */ 00254 00255 /* 00256 * bitsN 00257 * Unit of bitwise operation, AT LEAST N BITS IN SIZE. 00258 */ 00259 typedef uint8 bits8; /* >= 8 bits */ 00260 typedef uint16 bits16; /* >= 16 bits */ 00261 typedef uint32 bits32; /* >= 32 bits */ 00262 00263 /* 00264 * 64-bit integers 00265 */ 00266 #ifdef HAVE_LONG_INT_64 00267 /* Plain "long int" fits, use it */ 00268 00269 #ifndef HAVE_INT64 00270 typedef long int int64; 00271 #endif 00272 #ifndef HAVE_UINT64 00273 typedef unsigned long int uint64; 00274 #endif 00275 #elif defined(HAVE_LONG_LONG_INT_64) 00276 /* We have working support for "long long int", use that */ 00277 00278 #ifndef HAVE_INT64 00279 typedef long long int int64; 00280 #endif 00281 #ifndef HAVE_UINT64 00282 typedef unsigned long long int uint64; 00283 #endif 00284 #else 00285 /* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */ 00286 #error must have a working 64-bit integer datatype 00287 #endif 00288 00289 /* Decide if we need to decorate 64-bit constants */ 00290 #ifdef HAVE_LL_CONSTANTS 00291 #define INT64CONST(x) ((int64) x##LL) 00292 #define UINT64CONST(x) ((uint64) x##ULL) 00293 #else 00294 #define INT64CONST(x) ((int64) x) 00295 #define UINT64CONST(x) ((uint64) x) 00296 #endif 00297 00298 00299 /* Select timestamp representation (float8 or int64) */ 00300 #ifdef USE_INTEGER_DATETIMES 00301 #define HAVE_INT64_TIMESTAMP 00302 #endif 00303 00304 /* sig_atomic_t is required by ANSI C, but may be missing on old platforms */ 00305 #ifndef HAVE_SIG_ATOMIC_T 00306 typedef int sig_atomic_t; 00307 #endif 00308 00309 /* 00310 * Size 00311 * Size of any memory resident object, as returned by sizeof. 00312 */ 00313 typedef size_t Size; 00314 00315 /* 00316 * Index 00317 * Index into any memory resident array. 00318 * 00319 * Note: 00320 * Indices are non negative. 00321 */ 00322 typedef unsigned int Index; 00323 00324 /* 00325 * Offset 00326 * Offset into any memory resident array. 00327 * 00328 * Note: 00329 * This differs from an Index in that an Index is always 00330 * non negative, whereas Offset may be negative. 00331 */ 00332 typedef signed int Offset; 00333 00334 /* 00335 * Common Postgres datatype names (as used in the catalogs) 00336 */ 00337 typedef float float4; 00338 typedef double float8; 00339 00340 /* 00341 * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId, 00342 * CommandId 00343 */ 00344 00345 /* typedef Oid is in postgres_ext.h */ 00346 00347 /* 00348 * regproc is the type name used in the include/catalog headers, but 00349 * RegProcedure is the preferred name in C code. 00350 */ 00351 typedef Oid regproc; 00352 typedef regproc RegProcedure; 00353 00354 typedef uint32 TransactionId; 00355 00356 typedef uint32 LocalTransactionId; 00357 00358 typedef uint32 SubTransactionId; 00359 00360 #define InvalidSubTransactionId ((SubTransactionId) 0) 00361 #define TopSubTransactionId ((SubTransactionId) 1) 00362 00363 /* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */ 00364 typedef TransactionId MultiXactId; 00365 00366 typedef uint32 MultiXactOffset; 00367 00368 typedef uint32 CommandId; 00369 00370 #define FirstCommandId ((CommandId) 0) 00371 00372 /* 00373 * Array indexing support 00374 */ 00375 #define MAXDIM 6 00376 typedef struct 00377 { 00378 int indx[MAXDIM]; 00379 } IntArray; 00380 00381 /* ---------------- 00382 * Variable-length datatypes all share the 'struct varlena' header. 00383 * 00384 * NOTE: for TOASTable types, this is an oversimplification, since the value 00385 * may be compressed or moved out-of-line. However datatype-specific routines 00386 * are mostly content to deal with de-TOASTed values only, and of course 00387 * client-side routines should never see a TOASTed value. But even in a 00388 * de-TOASTed value, beware of touching vl_len_ directly, as its representation 00389 * is no longer convenient. It's recommended that code always use the VARDATA, 00390 * VARSIZE, and SET_VARSIZE macros instead of relying on direct mentions of 00391 * the struct fields. See postgres.h for details of the TOASTed form. 00392 * ---------------- 00393 */ 00394 struct varlena 00395 { 00396 char vl_len_[4]; /* Do not touch this field directly! */ 00397 char vl_dat[1]; 00398 }; 00399 00400 #define VARHDRSZ ((int32) sizeof(int32)) 00401 00402 /* 00403 * These widely-used datatypes are just a varlena header and the data bytes. 00404 * There is no terminating null or anything like that --- the data length is 00405 * always VARSIZE(ptr) - VARHDRSZ. 00406 */ 00407 typedef struct varlena bytea; 00408 typedef struct varlena text; 00409 typedef struct varlena BpChar; /* blank-padded char, ie SQL char(n) */ 00410 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */ 00411 00412 /* 00413 * Specialized array types. These are physically laid out just the same 00414 * as regular arrays (so that the regular array subscripting code works 00415 * with them). They exist as distinct types mostly for historical reasons: 00416 * they have nonstandard I/O behavior which we don't want to change for fear 00417 * of breaking applications that look at the system catalogs. There is also 00418 * an implementation issue for oidvector: it's part of the primary key for 00419 * pg_proc, and we can't use the normal btree array support routines for that 00420 * without circularity. 00421 */ 00422 typedef struct 00423 { 00424 int32 vl_len_; /* these fields must match ArrayType! */ 00425 int ndim; /* always 1 for int2vector */ 00426 int32 dataoffset; /* always 0 for int2vector */ 00427 Oid elemtype; 00428 int dim1; 00429 int lbound1; 00430 int16 values[1]; /* VARIABLE LENGTH ARRAY */ 00431 } int2vector; /* VARIABLE LENGTH STRUCT */ 00432 00433 typedef struct 00434 { 00435 int32 vl_len_; /* these fields must match ArrayType! */ 00436 int ndim; /* always 1 for oidvector */ 00437 int32 dataoffset; /* always 0 for oidvector */ 00438 Oid elemtype; 00439 int dim1; 00440 int lbound1; 00441 Oid values[1]; /* VARIABLE LENGTH ARRAY */ 00442 } oidvector; /* VARIABLE LENGTH STRUCT */ 00443 00444 /* 00445 * Representation of a Name: effectively just a C string, but null-padded to 00446 * exactly NAMEDATALEN bytes. The use of a struct is historical. 00447 */ 00448 typedef struct nameData 00449 { 00450 char data[NAMEDATALEN]; 00451 } NameData; 00452 typedef NameData *Name; 00453 00454 #define NameStr(name) ((name).data) 00455 00456 /* 00457 * Support macros for escaping strings. escape_backslash should be TRUE 00458 * if generating a non-standard-conforming string. Prefixing a string 00459 * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming. 00460 * Beware of multiple evaluation of the "ch" argument! 00461 */ 00462 #define SQL_STR_DOUBLE(ch, escape_backslash) \ 00463 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash))) 00464 00465 #define ESCAPE_STRING_SYNTAX 'E' 00466 00467 /* ---------------------------------------------------------------- 00468 * Section 4: IsValid macros for system types 00469 * ---------------------------------------------------------------- 00470 */ 00471 /* 00472 * BoolIsValid 00473 * True iff bool is valid. 00474 */ 00475 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true) 00476 00477 /* 00478 * PointerIsValid 00479 * True iff pointer is valid. 00480 */ 00481 #define PointerIsValid(pointer) ((const void*)(pointer) != NULL) 00482 00483 /* 00484 * PointerIsAligned 00485 * True iff pointer is properly aligned to point to the given type. 00486 */ 00487 #define PointerIsAligned(pointer, type) \ 00488 (((intptr_t)(pointer) % (sizeof (type))) == 0) 00489 00490 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid)) 00491 00492 #define RegProcedureIsValid(p) OidIsValid(p) 00493 00494 00495 /* ---------------------------------------------------------------- 00496 * Section 5: offsetof, lengthof, endof, alignment 00497 * ---------------------------------------------------------------- 00498 */ 00499 /* 00500 * offsetof 00501 * Offset of a structure/union field within that structure/union. 00502 * 00503 * XXX This is supposed to be part of stddef.h, but isn't on 00504 * some systems (like SunOS 4). 00505 */ 00506 #ifndef offsetof 00507 #define offsetof(type, field) ((long) &((type *)0)->field) 00508 #endif /* offsetof */ 00509 00510 /* 00511 * lengthof 00512 * Number of elements in an array. 00513 */ 00514 #define lengthof(array) (sizeof (array) / sizeof ((array)[0])) 00515 00516 /* 00517 * endof 00518 * Address of the element one past the last in an array. 00519 */ 00520 #define endof(array) (&(array)[lengthof(array)]) 00521 00522 /* ---------------- 00523 * Alignment macros: align a length or address appropriately for a given type. 00524 * The fooALIGN() macros round up to a multiple of the required alignment, 00525 * while the fooALIGN_DOWN() macros round down. The latter are more useful 00526 * for problems like "how many X-sized structures will fit in a page?". 00527 * 00528 * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2. 00529 * That case seems extremely unlikely to be needed in practice, however. 00530 * ---------------- 00531 */ 00532 00533 #define TYPEALIGN(ALIGNVAL,LEN) \ 00534 (((intptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((intptr_t) ((ALIGNVAL) - 1))) 00535 00536 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN)) 00537 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN)) 00538 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN)) 00539 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN)) 00540 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN)) 00541 /* MAXALIGN covers only built-in types, not buffers */ 00542 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN)) 00543 00544 #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \ 00545 (((intptr_t) (LEN)) & ~((intptr_t) ((ALIGNVAL) - 1))) 00546 00547 #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN)) 00548 #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN)) 00549 #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN)) 00550 #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN)) 00551 #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN)) 00552 00553 /* ---------------------------------------------------------------- 00554 * Section 6: assertions 00555 * ---------------------------------------------------------------- 00556 */ 00557 00558 /* 00559 * USE_ASSERT_CHECKING, if defined, turns on all the assertions. 00560 * - plai 9/5/90 00561 * 00562 * It should _NOT_ be defined in releases or in benchmark copies 00563 */ 00564 00565 /* 00566 * Assert() can be used in both frontend and backend code. In frontend code it 00567 * just calls the standard assert, if it's available. If use of assertions is 00568 * not configured, it does nothing. 00569 */ 00570 #ifndef USE_ASSERT_CHECKING 00571 00572 #define Assert(condition) 00573 #define AssertMacro(condition) ((void)true) 00574 #define AssertArg(condition) 00575 #define AssertState(condition) 00576 00577 #elif defined(FRONTEND) 00578 00579 #include <assert.h> 00580 #define Assert(p) assert(p) 00581 #define AssertMacro(p) ((void) assert(p)) 00582 #define AssertArg(condition) assert(condition) 00583 #define AssertState(condition) assert(condition) 00584 00585 #else /* USE_ASSERT_CHECKING && !FRONTEND */ 00586 00587 /* 00588 * Trap 00589 * Generates an exception if the given condition is true. 00590 */ 00591 #define Trap(condition, errorType) \ 00592 do { \ 00593 if ((assert_enabled) && (condition)) \ 00594 ExceptionalCondition(CppAsString(condition), (errorType), \ 00595 __FILE__, __LINE__); \ 00596 } while (0) 00597 00598 /* 00599 * TrapMacro is the same as Trap but it's intended for use in macros: 00600 * 00601 * #define foo(x) (AssertMacro(x != 0), bar(x)) 00602 * 00603 * Isn't CPP fun? 00604 */ 00605 #define TrapMacro(condition, errorType) \ 00606 ((bool) ((! assert_enabled) || ! (condition) || \ 00607 (ExceptionalCondition(CppAsString(condition), (errorType), \ 00608 __FILE__, __LINE__), 0))) 00609 00610 #define Assert(condition) \ 00611 Trap(!(condition), "FailedAssertion") 00612 00613 #define AssertMacro(condition) \ 00614 ((void) TrapMacro(!(condition), "FailedAssertion")) 00615 00616 #define AssertArg(condition) \ 00617 Trap(!(condition), "BadArgument") 00618 00619 #define AssertState(condition) \ 00620 Trap(!(condition), "BadState") 00621 00622 #endif /* USE_ASSERT_CHECKING && !FRONTEND */ 00623 00624 00625 /* 00626 * Macros to support compile-time assertion checks. 00627 * 00628 * If the "condition" (a compile-time-constant expression) evaluates to false, 00629 * throw a compile error using the "errmessage" (a string literal). 00630 * 00631 * gcc 4.6 and up supports _Static_assert(), but there are bizarre syntactic 00632 * placement restrictions. These macros make it safe to use as a statement 00633 * or in an expression, respectively. 00634 * 00635 * Otherwise we fall back on a kluge that assumes the compiler will complain 00636 * about a negative width for a struct bit-field. This will not include a 00637 * helpful error message, but it beats not getting an error at all. 00638 */ 00639 #ifdef HAVE__STATIC_ASSERT 00640 #define StaticAssertStmt(condition, errmessage) \ 00641 do { _Static_assert(condition, errmessage); } while(0) 00642 #define StaticAssertExpr(condition, errmessage) \ 00643 ({ StaticAssertStmt(condition, errmessage); true; }) 00644 #else /* !HAVE__STATIC_ASSERT */ 00645 #define StaticAssertStmt(condition, errmessage) \ 00646 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; })) 00647 #define StaticAssertExpr(condition, errmessage) \ 00648 StaticAssertStmt(condition, errmessage) 00649 #endif /* HAVE__STATIC_ASSERT */ 00650 00651 00652 /* 00653 * Compile-time checks that a variable (or expression) has the specified type. 00654 * 00655 * AssertVariableIsOfType() can be used as a statement. 00656 * AssertVariableIsOfTypeMacro() is intended for use in macros, eg 00657 * #define foo(x) (AssertVariableIsOfTypeMacro(x, int), bar(x)) 00658 * 00659 * If we don't have __builtin_types_compatible_p, we can still assert that 00660 * the types have the same size. This is far from ideal (especially on 32-bit 00661 * platforms) but it provides at least some coverage. 00662 */ 00663 #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P 00664 #define AssertVariableIsOfType(varname, typename) \ 00665 StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \ 00666 CppAsString(varname) " does not have type " CppAsString(typename)) 00667 #define AssertVariableIsOfTypeMacro(varname, typename) \ 00668 ((void) StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \ 00669 CppAsString(varname) " does not have type " CppAsString(typename))) 00670 #else /* !HAVE__BUILTIN_TYPES_COMPATIBLE_P */ 00671 #define AssertVariableIsOfType(varname, typename) \ 00672 StaticAssertStmt(sizeof(varname) == sizeof(typename), \ 00673 CppAsString(varname) " does not have type " CppAsString(typename)) 00674 #define AssertVariableIsOfTypeMacro(varname, typename) \ 00675 ((void) StaticAssertExpr(sizeof(varname) == sizeof(typename), \ 00676 CppAsString(varname) " does not have type " CppAsString(typename))) 00677 #endif /* HAVE__BUILTIN_TYPES_COMPATIBLE_P */ 00678 00679 00680 /* ---------------------------------------------------------------- 00681 * Section 7: widely useful macros 00682 * ---------------------------------------------------------------- 00683 */ 00684 /* 00685 * Max 00686 * Return the maximum of two numbers. 00687 */ 00688 #define Max(x, y) ((x) > (y) ? (x) : (y)) 00689 00690 /* 00691 * Min 00692 * Return the minimum of two numbers. 00693 */ 00694 #define Min(x, y) ((x) < (y) ? (x) : (y)) 00695 00696 /* 00697 * Abs 00698 * Return the absolute value of the argument. 00699 */ 00700 #define Abs(x) ((x) >= 0 ? (x) : -(x)) 00701 00702 /* 00703 * StrNCpy 00704 * Like standard library function strncpy(), except that result string 00705 * is guaranteed to be null-terminated --- that is, at most N-1 bytes 00706 * of the source string will be kept. 00707 * Also, the macro returns no result (too hard to do that without 00708 * evaluating the arguments multiple times, which seems worse). 00709 * 00710 * BTW: when you need to copy a non-null-terminated string (like a text 00711 * datum) and add a null, do not do it with StrNCpy(..., len+1). That 00712 * might seem to work, but it fetches one byte more than there is in the 00713 * text object. One fine day you'll have a SIGSEGV because there isn't 00714 * another byte before the end of memory. Don't laugh, we've had real 00715 * live bug reports from real live users over exactly this mistake. 00716 * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead. 00717 */ 00718 #define StrNCpy(dst,src,len) \ 00719 do \ 00720 { \ 00721 char * _dst = (dst); \ 00722 Size _len = (len); \ 00723 \ 00724 if (_len > 0) \ 00725 { \ 00726 strncpy(_dst, (src), _len); \ 00727 _dst[_len-1] = '\0'; \ 00728 } \ 00729 } while (0) 00730 00731 00732 /* Get a bit mask of the bits set in non-long aligned addresses */ 00733 #define LONG_ALIGN_MASK (sizeof(long) - 1) 00734 00735 /* 00736 * MemSet 00737 * Exactly the same as standard library function memset(), but considerably 00738 * faster for zeroing small word-aligned structures (such as parsetree nodes). 00739 * This has to be a macro because the main point is to avoid function-call 00740 * overhead. However, we have also found that the loop is faster than 00741 * native libc memset() on some platforms, even those with assembler 00742 * memset() functions. More research needs to be done, perhaps with 00743 * MEMSET_LOOP_LIMIT tests in configure. 00744 */ 00745 #define MemSet(start, val, len) \ 00746 do \ 00747 { \ 00748 /* must be void* because we don't know if it is integer aligned yet */ \ 00749 void *_vstart = (void *) (start); \ 00750 int _val = (val); \ 00751 Size _len = (len); \ 00752 \ 00753 if ((((intptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \ 00754 (_len & LONG_ALIGN_MASK) == 0 && \ 00755 _val == 0 && \ 00756 _len <= MEMSET_LOOP_LIMIT && \ 00757 /* \ 00758 * If MEMSET_LOOP_LIMIT == 0, optimizer should find \ 00759 * the whole "if" false at compile time. \ 00760 */ \ 00761 MEMSET_LOOP_LIMIT != 0) \ 00762 { \ 00763 long *_start = (long *) _vstart; \ 00764 long *_stop = (long *) ((char *) _start + _len); \ 00765 while (_start < _stop) \ 00766 *_start++ = 0; \ 00767 } \ 00768 else \ 00769 memset(_vstart, _val, _len); \ 00770 } while (0) 00771 00772 /* 00773 * MemSetAligned is the same as MemSet except it omits the test to see if 00774 * "start" is word-aligned. This is okay to use if the caller knows a-priori 00775 * that the pointer is suitably aligned (typically, because he just got it 00776 * from palloc(), which always delivers a max-aligned pointer). 00777 */ 00778 #define MemSetAligned(start, val, len) \ 00779 do \ 00780 { \ 00781 long *_start = (long *) (start); \ 00782 int _val = (val); \ 00783 Size _len = (len); \ 00784 \ 00785 if ((_len & LONG_ALIGN_MASK) == 0 && \ 00786 _val == 0 && \ 00787 _len <= MEMSET_LOOP_LIMIT && \ 00788 MEMSET_LOOP_LIMIT != 0) \ 00789 { \ 00790 long *_stop = (long *) ((char *) _start + _len); \ 00791 while (_start < _stop) \ 00792 *_start++ = 0; \ 00793 } \ 00794 else \ 00795 memset(_start, _val, _len); \ 00796 } while (0) 00797 00798 00799 /* 00800 * MemSetTest/MemSetLoop are a variant version that allow all the tests in 00801 * MemSet to be done at compile time in cases where "val" and "len" are 00802 * constants *and* we know the "start" pointer must be word-aligned. 00803 * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use 00804 * MemSetAligned. Beware of multiple evaluations of the arguments when using 00805 * this approach. 00806 */ 00807 #define MemSetTest(val, len) \ 00808 ( ((len) & LONG_ALIGN_MASK) == 0 && \ 00809 (len) <= MEMSET_LOOP_LIMIT && \ 00810 MEMSET_LOOP_LIMIT != 0 && \ 00811 (val) == 0 ) 00812 00813 #define MemSetLoop(start, val, len) \ 00814 do \ 00815 { \ 00816 long * _start = (long *) (start); \ 00817 long * _stop = (long *) ((char *) _start + (Size) (len)); \ 00818 \ 00819 while (_start < _stop) \ 00820 *_start++ = 0; \ 00821 } while (0) 00822 00823 00824 /* 00825 * Mark a point as unreachable in a portable fashion. This should preferably 00826 * be something that the compiler understands, to aid code generation. 00827 * In assert-enabled builds, we prefer abort() for debugging reasons. 00828 */ 00829 #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING) 00830 #define pg_unreachable() __builtin_unreachable() 00831 #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING) 00832 #define pg_unreachable() __assume(0) 00833 #else 00834 #define pg_unreachable() abort() 00835 #endif 00836 00837 00838 /* 00839 * Function inlining support -- Allow modules to define functions that may be 00840 * inlined, if the compiler supports it. 00841 * 00842 * The function bodies must be defined in the module header prefixed by 00843 * STATIC_IF_INLINE, protected by a cpp symbol that the module's .c file must 00844 * define. If the compiler doesn't support inline functions, the function 00845 * definitions are pulled in by the .c file as regular (not inline) symbols. 00846 * 00847 * The header must also declare the functions' prototypes, protected by 00848 * !PG_USE_INLINE. 00849 */ 00850 #ifdef PG_USE_INLINE 00851 #define STATIC_IF_INLINE static inline 00852 #else 00853 #define STATIC_IF_INLINE 00854 #endif /* PG_USE_INLINE */ 00855 00856 /* ---------------------------------------------------------------- 00857 * Section 8: random stuff 00858 * ---------------------------------------------------------------- 00859 */ 00860 00861 /* msb for char */ 00862 #define HIGHBIT (0x80) 00863 #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT) 00864 00865 #define STATUS_OK (0) 00866 #define STATUS_ERROR (-1) 00867 #define STATUS_EOF (-2) 00868 #define STATUS_FOUND (1) 00869 #define STATUS_WAITING (2) 00870 00871 00872 /* 00873 * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only 00874 * used in assert-enabled builds, to avoid compiler warnings about unused 00875 * variables in assert-disabled builds. 00876 */ 00877 #ifdef USE_ASSERT_CHECKING 00878 #define PG_USED_FOR_ASSERTS_ONLY 00879 #else 00880 #define PG_USED_FOR_ASSERTS_ONLY __attribute__((unused)) 00881 #endif 00882 00883 00884 /* gettext domain name mangling */ 00885 00886 /* 00887 * To better support parallel installations of major PostgeSQL 00888 * versions as well as parallel installations of major library soname 00889 * versions, we mangle the gettext domain name by appending those 00890 * version numbers. The coding rule ought to be that whereever the 00891 * domain name is mentioned as a literal, it must be wrapped into 00892 * PG_TEXTDOMAIN(). The macros below do not work on non-literals; but 00893 * that is somewhat intentional because it avoids having to worry 00894 * about multiple states of premangling and postmangling as the values 00895 * are being passed around. 00896 * 00897 * Make sure this matches the installation rules in nls-global.mk. 00898 */ 00899 00900 /* need a second indirection because we want to stringize the macro value, not the name */ 00901 #define CppAsString2(x) CppAsString(x) 00902 00903 #ifdef SO_MAJOR_VERSION 00904 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION) 00905 #else 00906 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION) 00907 #endif 00908 00909 00910 /* ---------------------------------------------------------------- 00911 * Section 9: system-specific hacks 00912 * 00913 * This should be limited to things that absolutely have to be 00914 * included in every source file. The port-specific header file 00915 * is usually a better place for this sort of thing. 00916 * ---------------------------------------------------------------- 00917 */ 00918 00919 /* 00920 * NOTE: this is also used for opening text files. 00921 * WIN32 treats Control-Z as EOF in files opened in text mode. 00922 * Therefore, we open files in binary mode on Win32 so we can read 00923 * literal control-Z. The other affect is that we see CRLF, but 00924 * that is OK because we can already handle those cleanly. 00925 */ 00926 #if defined(WIN32) || defined(__CYGWIN__) 00927 #define PG_BINARY O_BINARY 00928 #define PG_BINARY_A "ab" 00929 #define PG_BINARY_R "rb" 00930 #define PG_BINARY_W "wb" 00931 #else 00932 #define PG_BINARY 0 00933 #define PG_BINARY_A "a" 00934 #define PG_BINARY_R "r" 00935 #define PG_BINARY_W "w" 00936 #endif 00937 00938 /* 00939 * Provide prototypes for routines not present in a particular machine's 00940 * standard C library. 00941 */ 00942 00943 #if !HAVE_DECL_SNPRINTF 00944 extern int 00945 snprintf(char *str, size_t count, const char *fmt,...) 00946 /* This extension allows gcc to check the format string */ 00947 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4))); 00948 #endif 00949 00950 #if !HAVE_DECL_VSNPRINTF 00951 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args); 00952 #endif 00953 00954 #if !defined(HAVE_MEMMOVE) && !defined(memmove) 00955 #define memmove(d, s, c) bcopy(s, d, c) 00956 #endif 00957 00958 /* no special DLL markers on most ports */ 00959 #ifndef PGDLLIMPORT 00960 #define PGDLLIMPORT 00961 #endif 00962 #ifndef PGDLLEXPORT 00963 #define PGDLLEXPORT 00964 #endif 00965 00966 /* 00967 * The following is used as the arg list for signal handlers. Any ports 00968 * that take something other than an int argument should override this in 00969 * their pg_config_os.h file. Note that variable names are required 00970 * because it is used in both the prototypes as well as the definitions. 00971 * Note also the long name. We expect that this won't collide with 00972 * other names causing compiler warnings. 00973 */ 00974 00975 #ifndef SIGNAL_ARGS 00976 #define SIGNAL_ARGS int postgres_signal_arg 00977 #endif 00978 00979 /* 00980 * When there is no sigsetjmp, its functionality is provided by plain 00981 * setjmp. Incidentally, nothing provides setjmp's functionality in 00982 * that case. 00983 */ 00984 #ifndef HAVE_SIGSETJMP 00985 #define sigjmp_buf jmp_buf 00986 #define sigsetjmp(x,y) setjmp(x) 00987 #define siglongjmp longjmp 00988 #endif 00989 00990 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC 00991 extern int fdatasync(int fildes); 00992 #endif 00993 00994 /* If strtoq() exists, rename it to the more standard strtoll() */ 00995 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ) 00996 #define strtoll strtoq 00997 #define HAVE_STRTOLL 1 00998 #endif 00999 01000 /* If strtouq() exists, rename it to the more standard strtoull() */ 01001 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ) 01002 #define strtoull strtouq 01003 #define HAVE_STRTOULL 1 01004 #endif 01005 01006 /* 01007 * We assume if we have these two functions, we have their friends too, and 01008 * can use the wide-character functions. 01009 */ 01010 #if defined(HAVE_WCSTOMBS) && defined(HAVE_TOWLOWER) 01011 #define USE_WIDE_UPPER_LOWER 01012 #endif 01013 01014 /* EXEC_BACKEND defines */ 01015 #ifdef EXEC_BACKEND 01016 #define NON_EXEC_STATIC 01017 #else 01018 #define NON_EXEC_STATIC static 01019 #endif 01020 01021 /* /port compatibility functions */ 01022 #include "port.h" 01023 01024 #endif /* C_H */