PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
c.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * c.h
4  * Fundamental C definitions. This is included by every .c file in
5  * PostgreSQL (via either postgres.h or postgres_fe.h, as appropriate).
6  *
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...
10  *
11  *
12  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * src/include/c.h
16  *
17  *-------------------------------------------------------------------------
18  */
19 /*
20  *----------------------------------------------------------------
21  * TABLE OF CONTENTS
22  *
23  * When adding stuff to this file, please try to put stuff
24  * into the relevant section, or add new sections as appropriate.
25  *
26  * section description
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
34  * 6) assertions
35  * 7) widely useful macros
36  * 8) random stuff
37  * 9) system-specific hacks
38  *
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.
42  *
43  *----------------------------------------------------------------
44  */
45 #ifndef C_H
46 #define C_H
47 
48 #include "postgres_ext.h"
49 
50 /* Must undef pg_config_ext.h symbols before including pg_config.h */
51 #undef PG_INT64_TYPE
52 
53 #include "pg_config.h"
54 #include "pg_config_manual.h" /* must be after pg_config.h */
55 
56 /*
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.
60  */
61 #if defined(_WIN32) && !defined(WIN32)
62 #define WIN32
63 #endif
64 
65 #if !defined(WIN32) && !defined(__CYGWIN__) /* win32 includes further down */
66 #include "pg_config_os.h" /* must be before any system header files */
67 #endif
68 
69 #if _MSC_VER >= 1400 || defined(HAVE_CRTDEFS_H)
70 #define errcode __msvc_errcode
71 #include <crtdefs.h>
72 #undef errcode
73 #endif
74 
75 /*
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.
79  */
80 
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <stddef.h>
85 #include <stdarg.h>
86 #ifdef HAVE_STRINGS_H
87 #include <strings.h>
88 #endif
89 #ifdef HAVE_STDINT_H
90 #include <stdint.h>
91 #endif
92 #include <sys/types.h>
93 
94 #include <errno.h>
95 #if defined(WIN32) || defined(__CYGWIN__)
96 #include <fcntl.h> /* ensure O_BINARY is available */
97 #endif
98 
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"
102 #endif
103 
104 /*
105  * Force disable inlining if PG_FORCE_DISABLE_INLINE is defined. This is used
106  * to work around compiler bugs and might also be useful for investigatory
107  * purposes by defining the symbol in the platform's header..
108  *
109  * This is done early (in slightly the wrong section) as functionality later
110  * in this file might want to rely on inline functions.
111  */
112 #ifdef PG_FORCE_DISABLE_INLINE
113 #undef inline
114 #define inline
115 #endif
116 
117 /* Must be before gettext() games below */
118 #include <locale.h>
119 
120 #define _(x) gettext(x)
121 
122 #ifdef ENABLE_NLS
123 #include <libintl.h>
124 #else
125 #define gettext(x) (x)
126 #define dgettext(d,x) (x)
127 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
128 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
129 #endif
130 
131 /*
132  * Use this to mark string constants as needing translation at some later
133  * time, rather than immediately. This is useful for cases where you need
134  * access to the original string and translated string, and for cases where
135  * immediate translation is not possible, like when initializing global
136  * variables.
137  * http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
138  */
139 #define gettext_noop(x) (x)
140 
141 
142 /* ----------------------------------------------------------------
143  * Section 1: hacks to cope with non-ANSI C compilers
144  *
145  * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
146  * ----------------------------------------------------------------
147  */
148 
149 /*
150  * CppAsString
151  * Convert the argument to a string, using the C preprocessor.
152  * CppAsString2
153  * Convert the argument to a string, after one round of macro expansion.
154  * CppConcat
155  * Concatenate two arguments together, using the C preprocessor.
156  *
157  * Note: There used to be support here for pre-ANSI C compilers that didn't
158  * support # and ##. Nowadays, these macros are just for clarity and/or
159  * backward compatibility with existing PostgreSQL code.
160  */
161 #define CppAsString(identifier) #identifier
162 #define CppAsString2(x) CppAsString(x)
163 #define CppConcat(x, y) x##y
164 
165 /*
166  * dummyret is used to set return values in macros that use ?: to make
167  * assignments. gcc wants these to be void, other compilers like char
168  */
169 #ifdef __GNUC__ /* GNU cc */
170 #define dummyret void
171 #else
172 #define dummyret char
173 #endif
174 
175 /* Which __func__ symbol do we have, if any? */
176 #ifdef HAVE_FUNCNAME__FUNC
177 #define PG_FUNCNAME_MACRO __func__
178 #else
179 #ifdef HAVE_FUNCNAME__FUNCTION
180 #define PG_FUNCNAME_MACRO __FUNCTION__
181 #else
182 #define PG_FUNCNAME_MACRO NULL
183 #endif
184 #endif
185 
186 /* ----------------------------------------------------------------
187  * Section 2: bool, true, false, TRUE, FALSE, NULL
188  * ----------------------------------------------------------------
189  */
190 
191 /*
192  * bool
193  * Boolean value, either true or false.
194  *
195  * XXX for C++ compilers, we assume the compiler has a compatible
196  * built-in definition of bool.
197  */
198 
199 #ifndef __cplusplus
200 
201 #ifndef bool
202 typedef char bool;
203 #endif
204 
205 #ifndef true
206 #define true ((bool) 1)
207 #endif
208 
209 #ifndef false
210 #define false ((bool) 0)
211 #endif
212 #endif /* not C++ */
213 
214 typedef bool *BoolPtr;
215 
216 #ifndef TRUE
217 #define TRUE 1
218 #endif
219 
220 #ifndef FALSE
221 #define FALSE 0
222 #endif
223 
224 /*
225  * NULL
226  * Null pointer.
227  */
228 #ifndef NULL
229 #define NULL ((void *) 0)
230 #endif
231 
232 
233 /* ----------------------------------------------------------------
234  * Section 3: standard system types
235  * ----------------------------------------------------------------
236  */
237 
238 /*
239  * Pointer
240  * Variable holding address of any memory resident object.
241  *
242  * XXX Pointer arithmetic is done with this, so it can't be void *
243  * under "true" ANSI compilers.
244  */
245 typedef char *Pointer;
246 
247 /*
248  * intN
249  * Signed integer, EXACTLY N BITS IN SIZE,
250  * used for numerical computations and the
251  * frontend/backend protocol.
252  */
253 #ifndef HAVE_INT8
254 typedef signed char int8; /* == 8 bits */
255 typedef signed short int16; /* == 16 bits */
256 typedef signed int int32; /* == 32 bits */
257 #endif /* not HAVE_INT8 */
258 
259 /*
260  * uintN
261  * Unsigned integer, EXACTLY N BITS IN SIZE,
262  * used for numerical computations and the
263  * frontend/backend protocol.
264  */
265 #ifndef HAVE_UINT8
266 typedef unsigned char uint8; /* == 8 bits */
267 typedef unsigned short uint16; /* == 16 bits */
268 typedef unsigned int uint32; /* == 32 bits */
269 #endif /* not HAVE_UINT8 */
270 
271 /*
272  * bitsN
273  * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
274  */
275 typedef uint8 bits8; /* >= 8 bits */
276 typedef uint16 bits16; /* >= 16 bits */
277 typedef uint32 bits32; /* >= 32 bits */
278 
279 /*
280  * 64-bit integers
281  */
282 #ifdef HAVE_LONG_INT_64
283 /* Plain "long int" fits, use it */
284 
285 #ifndef HAVE_INT64
286 typedef long int int64;
287 #endif
288 #ifndef HAVE_UINT64
289 typedef unsigned long int uint64;
290 #endif
291 #elif defined(HAVE_LONG_LONG_INT_64)
292 /* We have working support for "long long int", use that */
293 
294 #ifndef HAVE_INT64
295 typedef long long int int64;
296 #endif
297 #ifndef HAVE_UINT64
298 typedef unsigned long long int uint64;
299 #endif
300 #else
301 /* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */
302 #error must have a working 64-bit integer datatype
303 #endif
304 
305 /* Decide if we need to decorate 64-bit constants */
306 #ifdef HAVE_LL_CONSTANTS
307 #define INT64CONST(x) ((int64) x##LL)
308 #define UINT64CONST(x) ((uint64) x##ULL)
309 #else
310 #define INT64CONST(x) ((int64) x)
311 #define UINT64CONST(x) ((uint64) x)
312 #endif
313 
314 /* snprintf format strings to use for 64-bit integers */
315 #define INT64_FORMAT "%" INT64_MODIFIER "d"
316 #define UINT64_FORMAT "%" INT64_MODIFIER "u"
317 
318 /*
319  * 128-bit signed and unsigned integers
320  * There currently is only a limited support for the type. E.g. 128bit
321  * literals and snprintf are not supported; but math is.
322  */
323 #if defined(PG_INT128_TYPE)
324 #define HAVE_INT128
325 typedef PG_INT128_TYPE int128;
326 typedef unsigned PG_INT128_TYPE uint128;
327 #endif
328 
329 /*
330  * stdint.h limits aren't guaranteed to be present and aren't guaranteed to
331  * have compatible types with our fixed width types. So just define our own.
332  */
333 #define PG_INT8_MIN (-0x7F-1)
334 #define PG_INT8_MAX (0x7F)
335 #define PG_UINT8_MAX (0xFF)
336 #define PG_INT16_MIN (-0x7FFF-1)
337 #define PG_INT16_MAX (0x7FFF)
338 #define PG_UINT16_MAX (0xFFFF)
339 #define PG_INT32_MIN (-0x7FFFFFFF-1)
340 #define PG_INT32_MAX (0x7FFFFFFF)
341 #define PG_UINT32_MAX (0xFFFFFFFF)
342 #define PG_INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
343 #define PG_INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
344 #define PG_UINT64_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
345 
346 /*
347  * We now always use int64 timestamps, but keep this symbol defined for the
348  * benefit of external code that might test it.
349  */
350 #define HAVE_INT64_TIMESTAMP
351 
352 /*
353  * Size
354  * Size of any memory resident object, as returned by sizeof.
355  */
356 typedef size_t Size;
357 
358 /*
359  * Index
360  * Index into any memory resident array.
361  *
362  * Note:
363  * Indices are non negative.
364  */
365 typedef unsigned int Index;
366 
367 /*
368  * Offset
369  * Offset into any memory resident array.
370  *
371  * Note:
372  * This differs from an Index in that an Index is always
373  * non negative, whereas Offset may be negative.
374  */
375 typedef signed int Offset;
376 
377 /*
378  * Common Postgres datatype names (as used in the catalogs)
379  */
380 typedef float float4;
381 typedef double float8;
382 
383 /*
384  * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
385  * CommandId
386  */
387 
388 /* typedef Oid is in postgres_ext.h */
389 
390 /*
391  * regproc is the type name used in the include/catalog headers, but
392  * RegProcedure is the preferred name in C code.
393  */
394 typedef Oid regproc;
396 
398 
400 
402 
403 #define InvalidSubTransactionId ((SubTransactionId) 0)
404 #define TopSubTransactionId ((SubTransactionId) 1)
405 
406 /* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
408 
410 
412 
413 #define FirstCommandId ((CommandId) 0)
414 #define InvalidCommandId (~(CommandId)0)
415 
416 /*
417  * Array indexing support
418  */
419 #define MAXDIM 6
420 typedef struct
421 {
422  int indx[MAXDIM];
423 } IntArray;
424 
425 /* ----------------
426  * Variable-length datatypes all share the 'struct varlena' header.
427  *
428  * NOTE: for TOASTable types, this is an oversimplification, since the value
429  * may be compressed or moved out-of-line. However datatype-specific routines
430  * are mostly content to deal with de-TOASTed values only, and of course
431  * client-side routines should never see a TOASTed value. But even in a
432  * de-TOASTed value, beware of touching vl_len_ directly, as its
433  * representation is no longer convenient. It's recommended that code always
434  * use macros VARDATA_ANY, VARSIZE_ANY, VARSIZE_ANY_EXHDR, VARDATA, VARSIZE,
435  * and SET_VARSIZE instead of relying on direct mentions of the struct fields.
436  * See postgres.h for details of the TOASTed form.
437  * ----------------
438  */
439 struct varlena
440 {
441  char vl_len_[4]; /* Do not touch this field directly! */
442  char vl_dat[FLEXIBLE_ARRAY_MEMBER]; /* Data content is here */
443 };
444 
445 #define VARHDRSZ ((int32) sizeof(int32))
446 
447 /*
448  * These widely-used datatypes are just a varlena header and the data bytes.
449  * There is no terminating null or anything like that --- the data length is
450  * always VARSIZE_ANY_EXHDR(ptr).
451  */
452 typedef struct varlena bytea;
453 typedef struct varlena text;
454 typedef struct varlena BpChar; /* blank-padded char, ie SQL char(n) */
455 typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
456 
457 /*
458  * Specialized array types. These are physically laid out just the same
459  * as regular arrays (so that the regular array subscripting code works
460  * with them). They exist as distinct types mostly for historical reasons:
461  * they have nonstandard I/O behavior which we don't want to change for fear
462  * of breaking applications that look at the system catalogs. There is also
463  * an implementation issue for oidvector: it's part of the primary key for
464  * pg_proc, and we can't use the normal btree array support routines for that
465  * without circularity.
466  */
467 typedef struct
468 {
469  int32 vl_len_; /* these fields must match ArrayType! */
470  int ndim; /* always 1 for int2vector */
471  int32 dataoffset; /* always 0 for int2vector */
473  int dim1;
474  int lbound1;
475  int16 values[FLEXIBLE_ARRAY_MEMBER];
476 } int2vector;
477 
478 typedef struct
479 {
480  int32 vl_len_; /* these fields must match ArrayType! */
481  int ndim; /* always 1 for oidvector */
482  int32 dataoffset; /* always 0 for oidvector */
484  int dim1;
485  int lbound1;
486  Oid values[FLEXIBLE_ARRAY_MEMBER];
487 } oidvector;
488 
489 /*
490  * Representation of a Name: effectively just a C string, but null-padded to
491  * exactly NAMEDATALEN bytes. The use of a struct is historical.
492  */
493 typedef struct nameData
494 {
496 } NameData;
497 typedef NameData *Name;
498 
499 #define NameStr(name) ((name).data)
500 
501 /*
502  * Support macros for escaping strings. escape_backslash should be TRUE
503  * if generating a non-standard-conforming string. Prefixing a string
504  * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
505  * Beware of multiple evaluation of the "ch" argument!
506  */
507 #define SQL_STR_DOUBLE(ch, escape_backslash) \
508  ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
509 
510 #define ESCAPE_STRING_SYNTAX 'E'
511 
512 /* ----------------------------------------------------------------
513  * Section 4: IsValid macros for system types
514  * ----------------------------------------------------------------
515  */
516 /*
517  * BoolIsValid
518  * True iff bool is valid.
519  */
520 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
521 
522 /*
523  * PointerIsValid
524  * True iff pointer is valid.
525  */
526 #define PointerIsValid(pointer) ((const void*)(pointer) != NULL)
527 
528 /*
529  * PointerIsAligned
530  * True iff pointer is properly aligned to point to the given type.
531  */
532 #define PointerIsAligned(pointer, type) \
533  (((uintptr_t)(pointer) % (sizeof (type))) == 0)
534 
535 #define OffsetToPointer(base, offset) \
536  ((void *)((char *) base + offset))
537 
538 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
539 
540 #define RegProcedureIsValid(p) OidIsValid(p)
541 
542 
543 /* ----------------------------------------------------------------
544  * Section 5: offsetof, lengthof, endof, alignment
545  * ----------------------------------------------------------------
546  */
547 /*
548  * offsetof
549  * Offset of a structure/union field within that structure/union.
550  *
551  * XXX This is supposed to be part of stddef.h, but isn't on
552  * some systems (like SunOS 4).
553  */
554 #ifndef offsetof
555 #define offsetof(type, field) ((long) &((type *)0)->field)
556 #endif /* offsetof */
557 
558 /*
559  * lengthof
560  * Number of elements in an array.
561  */
562 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
563 
564 /*
565  * endof
566  * Address of the element one past the last in an array.
567  */
568 #define endof(array) (&(array)[lengthof(array)])
569 
570 /* ----------------
571  * Alignment macros: align a length or address appropriately for a given type.
572  * The fooALIGN() macros round up to a multiple of the required alignment,
573  * while the fooALIGN_DOWN() macros round down. The latter are more useful
574  * for problems like "how many X-sized structures will fit in a page?".
575  *
576  * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
577  * That case seems extremely unlikely to be needed in practice, however.
578  * ----------------
579  */
580 
581 #define TYPEALIGN(ALIGNVAL,LEN) \
582  (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
583 
584 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
585 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
586 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
587 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
588 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
589 /* MAXALIGN covers only built-in types, not buffers */
590 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
591 #define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
592 
593 #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
594  (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
595 
596 #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
597 #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
598 #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
599 #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
600 #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
601 #define BUFFERALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_BUFFER, (LEN))
602 
603 /*
604  * The above macros will not work with types wider than uintptr_t, like with
605  * uint64 on 32-bit platforms. That's not problem for the usual use where a
606  * pointer or a length is aligned, but for the odd case that you need to
607  * align something (potentially) wider, use TYPEALIGN64.
608  */
609 #define TYPEALIGN64(ALIGNVAL,LEN) \
610  (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
611 
612 /* we don't currently need wider versions of the other ALIGN macros */
613 #define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
614 
615 /* ----------------
616  * Attribute macros
617  *
618  * GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
619  * GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
620  * Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
621  * XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/function_attributes.html
622  * XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/type_attrib.html
623  * ----------------
624  */
625 
626 /* only GCC supports the unused attribute */
627 #ifdef __GNUC__
628 #define pg_attribute_unused() __attribute__((unused))
629 #else
630 #define pg_attribute_unused()
631 #endif
632 
633 /* GCC and XLC support format attributes */
634 #if defined(__GNUC__) || defined(__IBMC__)
635 #define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
636 #define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
637 #else
638 #define pg_attribute_format_arg(a)
639 #define pg_attribute_printf(f,a)
640 #endif
641 
642 /* GCC, Sunpro and XLC support aligned, packed and noreturn */
643 #if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
644 #define pg_attribute_aligned(a) __attribute__((aligned(a)))
645 #define pg_attribute_noreturn() __attribute__((noreturn))
646 #define pg_attribute_packed() __attribute__((packed))
647 #define HAVE_PG_ATTRIBUTE_NORETURN 1
648 #else
649 /*
650  * NB: aligned and packed are not given default definitions because they
651  * affect code functionality; they *must* be implemented by the compiler
652  * if they are to be used.
653  */
654 #define pg_attribute_noreturn()
655 #endif
656 
657 /* ----------------------------------------------------------------
658  * Section 6: assertions
659  * ----------------------------------------------------------------
660  */
661 
662 /*
663  * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
664  * - plai 9/5/90
665  *
666  * It should _NOT_ be defined in releases or in benchmark copies
667  */
668 
669 /*
670  * Assert() can be used in both frontend and backend code. In frontend code it
671  * just calls the standard assert, if it's available. If use of assertions is
672  * not configured, it does nothing.
673  */
674 #ifndef USE_ASSERT_CHECKING
675 
676 #define Assert(condition) ((void)true)
677 #define AssertMacro(condition) ((void)true)
678 #define AssertArg(condition) ((void)true)
679 #define AssertState(condition) ((void)true)
680 #define AssertPointerAlignment(ptr, bndr) ((void)true)
681 #define Trap(condition, errorType) ((void)true)
682 #define TrapMacro(condition, errorType) (true)
683 
684 #elif defined(FRONTEND)
685 
686 #include <assert.h>
687 #define Assert(p) assert(p)
688 #define AssertMacro(p) ((void) assert(p))
689 #define AssertArg(condition) assert(condition)
690 #define AssertState(condition) assert(condition)
691 #define AssertPointerAlignment(ptr, bndr) ((void)true)
692 #else /* USE_ASSERT_CHECKING && !FRONTEND */
693 
694 /*
695  * Trap
696  * Generates an exception if the given condition is true.
697  */
698 #define Trap(condition, errorType) \
699  do { \
700  if (condition) \
701  ExceptionalCondition(CppAsString(condition), (errorType), \
702  __FILE__, __LINE__); \
703  } while (0)
704 
705 /*
706  * TrapMacro is the same as Trap but it's intended for use in macros:
707  *
708  * #define foo(x) (AssertMacro(x != 0), bar(x))
709  *
710  * Isn't CPP fun?
711  */
712 #define TrapMacro(condition, errorType) \
713  ((bool) (! (condition) || \
714  (ExceptionalCondition(CppAsString(condition), (errorType), \
715  __FILE__, __LINE__), 0)))
716 
717 #define Assert(condition) \
718  Trap(!(condition), "FailedAssertion")
719 
720 #define AssertMacro(condition) \
721  ((void) TrapMacro(!(condition), "FailedAssertion"))
722 
723 #define AssertArg(condition) \
724  Trap(!(condition), "BadArgument")
725 
726 #define AssertState(condition) \
727  Trap(!(condition), "BadState")
728 
729 /*
730  * Check that `ptr' is `bndr' aligned.
731  */
732 #define AssertPointerAlignment(ptr, bndr) \
733  Trap(TYPEALIGN(bndr, (uintptr_t)(ptr)) != (uintptr_t)(ptr), \
734  "UnalignedPointer")
735 
736 #endif /* USE_ASSERT_CHECKING && !FRONTEND */
737 
738 /*
739  * Macros to support compile-time assertion checks.
740  *
741  * If the "condition" (a compile-time-constant expression) evaluates to false,
742  * throw a compile error using the "errmessage" (a string literal).
743  *
744  * gcc 4.6 and up supports _Static_assert(), but there are bizarre syntactic
745  * placement restrictions. These macros make it safe to use as a statement
746  * or in an expression, respectively.
747  *
748  * Otherwise we fall back on a kluge that assumes the compiler will complain
749  * about a negative width for a struct bit-field. This will not include a
750  * helpful error message, but it beats not getting an error at all.
751  */
752 #ifdef HAVE__STATIC_ASSERT
753 #define StaticAssertStmt(condition, errmessage) \
754  do { _Static_assert(condition, errmessage); } while(0)
755 #define StaticAssertExpr(condition, errmessage) \
756  ({ StaticAssertStmt(condition, errmessage); true; })
757 #else /* !HAVE__STATIC_ASSERT */
758 #define StaticAssertStmt(condition, errmessage) \
759  ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
760 #define StaticAssertExpr(condition, errmessage) \
761  StaticAssertStmt(condition, errmessage)
762 #endif /* HAVE__STATIC_ASSERT */
763 
764 
765 /*
766  * Compile-time checks that a variable (or expression) has the specified type.
767  *
768  * AssertVariableIsOfType() can be used as a statement.
769  * AssertVariableIsOfTypeMacro() is intended for use in macros, eg
770  * #define foo(x) (AssertVariableIsOfTypeMacro(x, int), bar(x))
771  *
772  * If we don't have __builtin_types_compatible_p, we can still assert that
773  * the types have the same size. This is far from ideal (especially on 32-bit
774  * platforms) but it provides at least some coverage.
775  */
776 #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
777 #define AssertVariableIsOfType(varname, typename) \
778  StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \
779  CppAsString(varname) " does not have type " CppAsString(typename))
780 #define AssertVariableIsOfTypeMacro(varname, typename) \
781  ((void) StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
782  CppAsString(varname) " does not have type " CppAsString(typename)))
783 #else /* !HAVE__BUILTIN_TYPES_COMPATIBLE_P */
784 #define AssertVariableIsOfType(varname, typename) \
785  StaticAssertStmt(sizeof(varname) == sizeof(typename), \
786  CppAsString(varname) " does not have type " CppAsString(typename))
787 #define AssertVariableIsOfTypeMacro(varname, typename) \
788  ((void) StaticAssertExpr(sizeof(varname) == sizeof(typename), \
789  CppAsString(varname) " does not have type " CppAsString(typename)))
790 #endif /* HAVE__BUILTIN_TYPES_COMPATIBLE_P */
791 
792 
793 /* ----------------------------------------------------------------
794  * Section 7: widely useful macros
795  * ----------------------------------------------------------------
796  */
797 /*
798  * Max
799  * Return the maximum of two numbers.
800  */
801 #define Max(x, y) ((x) > (y) ? (x) : (y))
802 
803 /*
804  * Min
805  * Return the minimum of two numbers.
806  */
807 #define Min(x, y) ((x) < (y) ? (x) : (y))
808 
809 /*
810  * Abs
811  * Return the absolute value of the argument.
812  */
813 #define Abs(x) ((x) >= 0 ? (x) : -(x))
814 
815 /*
816  * StrNCpy
817  * Like standard library function strncpy(), except that result string
818  * is guaranteed to be null-terminated --- that is, at most N-1 bytes
819  * of the source string will be kept.
820  * Also, the macro returns no result (too hard to do that without
821  * evaluating the arguments multiple times, which seems worse).
822  *
823  * BTW: when you need to copy a non-null-terminated string (like a text
824  * datum) and add a null, do not do it with StrNCpy(..., len+1). That
825  * might seem to work, but it fetches one byte more than there is in the
826  * text object. One fine day you'll have a SIGSEGV because there isn't
827  * another byte before the end of memory. Don't laugh, we've had real
828  * live bug reports from real live users over exactly this mistake.
829  * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
830  */
831 #define StrNCpy(dst,src,len) \
832  do \
833  { \
834  char * _dst = (dst); \
835  Size _len = (len); \
836 \
837  if (_len > 0) \
838  { \
839  strncpy(_dst, (src), _len); \
840  _dst[_len-1] = '\0'; \
841  } \
842  } while (0)
843 
844 
845 /* Get a bit mask of the bits set in non-long aligned addresses */
846 #define LONG_ALIGN_MASK (sizeof(long) - 1)
847 
848 /*
849  * MemSet
850  * Exactly the same as standard library function memset(), but considerably
851  * faster for zeroing small word-aligned structures (such as parsetree nodes).
852  * This has to be a macro because the main point is to avoid function-call
853  * overhead. However, we have also found that the loop is faster than
854  * native libc memset() on some platforms, even those with assembler
855  * memset() functions. More research needs to be done, perhaps with
856  * MEMSET_LOOP_LIMIT tests in configure.
857  */
858 #define MemSet(start, val, len) \
859  do \
860  { \
861  /* must be void* because we don't know if it is integer aligned yet */ \
862  void *_vstart = (void *) (start); \
863  int _val = (val); \
864  Size _len = (len); \
865 \
866  if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
867  (_len & LONG_ALIGN_MASK) == 0 && \
868  _val == 0 && \
869  _len <= MEMSET_LOOP_LIMIT && \
870  /* \
871  * If MEMSET_LOOP_LIMIT == 0, optimizer should find \
872  * the whole "if" false at compile time. \
873  */ \
874  MEMSET_LOOP_LIMIT != 0) \
875  { \
876  long *_start = (long *) _vstart; \
877  long *_stop = (long *) ((char *) _start + _len); \
878  while (_start < _stop) \
879  *_start++ = 0; \
880  } \
881  else \
882  memset(_vstart, _val, _len); \
883  } while (0)
884 
885 /*
886  * MemSetAligned is the same as MemSet except it omits the test to see if
887  * "start" is word-aligned. This is okay to use if the caller knows a-priori
888  * that the pointer is suitably aligned (typically, because he just got it
889  * from palloc(), which always delivers a max-aligned pointer).
890  */
891 #define MemSetAligned(start, val, len) \
892  do \
893  { \
894  long *_start = (long *) (start); \
895  int _val = (val); \
896  Size _len = (len); \
897 \
898  if ((_len & LONG_ALIGN_MASK) == 0 && \
899  _val == 0 && \
900  _len <= MEMSET_LOOP_LIMIT && \
901  MEMSET_LOOP_LIMIT != 0) \
902  { \
903  long *_stop = (long *) ((char *) _start + _len); \
904  while (_start < _stop) \
905  *_start++ = 0; \
906  } \
907  else \
908  memset(_start, _val, _len); \
909  } while (0)
910 
911 
912 /*
913  * MemSetTest/MemSetLoop are a variant version that allow all the tests in
914  * MemSet to be done at compile time in cases where "val" and "len" are
915  * constants *and* we know the "start" pointer must be word-aligned.
916  * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use
917  * MemSetAligned. Beware of multiple evaluations of the arguments when using
918  * this approach.
919  */
920 #define MemSetTest(val, len) \
921  ( ((len) & LONG_ALIGN_MASK) == 0 && \
922  (len) <= MEMSET_LOOP_LIMIT && \
923  MEMSET_LOOP_LIMIT != 0 && \
924  (val) == 0 )
925 
926 #define MemSetLoop(start, val, len) \
927  do \
928  { \
929  long * _start = (long *) (start); \
930  long * _stop = (long *) ((char *) _start + (Size) (len)); \
931  \
932  while (_start < _stop) \
933  *_start++ = 0; \
934  } while (0)
935 
936 
937 /*
938  * Mark a point as unreachable in a portable fashion. This should preferably
939  * be something that the compiler understands, to aid code generation.
940  * In assert-enabled builds, we prefer abort() for debugging reasons.
941  */
942 #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
943 #define pg_unreachable() __builtin_unreachable()
944 #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
945 #define pg_unreachable() __assume(0)
946 #else
947 #define pg_unreachable() abort()
948 #endif
949 
950 
951 /*
952  * Hints to the compiler about the likelihood of a branch. Both likely() and
953  * unlikely() return the boolean value of the contained expression.
954  *
955  * These should only be used sparingly, in very hot code paths. It's very easy
956  * to mis-estimate likelihoods.
957  */
958 #if __GNUC__ >= 3
959 #define likely(x) __builtin_expect((x) != 0, 1)
960 #define unlikely(x) __builtin_expect((x) != 0, 0)
961 #else
962 #define likely(x) ((x) != 0)
963 #define unlikely(x) ((x) != 0)
964 #endif
965 
966 
967 /* ----------------------------------------------------------------
968  * Section 8: random stuff
969  * ----------------------------------------------------------------
970  */
971 
972 /* msb for char */
973 #define HIGHBIT (0x80)
974 #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
975 
976 #define STATUS_OK (0)
977 #define STATUS_ERROR (-1)
978 #define STATUS_EOF (-2)
979 #define STATUS_FOUND (1)
980 #define STATUS_WAITING (2)
981 
982 
983 /*
984  * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
985  * used in assert-enabled builds, to avoid compiler warnings about unused
986  * variables in assert-disabled builds.
987  */
988 #ifdef USE_ASSERT_CHECKING
989 #define PG_USED_FOR_ASSERTS_ONLY
990 #else
991 #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
992 #endif
993 
994 
995 /* gettext domain name mangling */
996 
997 /*
998  * To better support parallel installations of major PostgreSQL
999  * versions as well as parallel installations of major library soname
1000  * versions, we mangle the gettext domain name by appending those
1001  * version numbers. The coding rule ought to be that wherever the
1002  * domain name is mentioned as a literal, it must be wrapped into
1003  * PG_TEXTDOMAIN(). The macros below do not work on non-literals; but
1004  * that is somewhat intentional because it avoids having to worry
1005  * about multiple states of premangling and postmangling as the values
1006  * are being passed around.
1007  *
1008  * Make sure this matches the installation rules in nls-global.mk.
1009  */
1010 #ifdef SO_MAJOR_VERSION
1011 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
1012 #else
1013 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1014 #endif
1015 
1016 
1017 /* ----------------------------------------------------------------
1018  * Section 9: system-specific hacks
1019  *
1020  * This should be limited to things that absolutely have to be
1021  * included in every source file. The port-specific header file
1022  * is usually a better place for this sort of thing.
1023  * ----------------------------------------------------------------
1024  */
1025 
1026 /*
1027  * NOTE: this is also used for opening text files.
1028  * WIN32 treats Control-Z as EOF in files opened in text mode.
1029  * Therefore, we open files in binary mode on Win32 so we can read
1030  * literal control-Z. The other affect is that we see CRLF, but
1031  * that is OK because we can already handle those cleanly.
1032  */
1033 #if defined(WIN32) || defined(__CYGWIN__)
1034 #define PG_BINARY O_BINARY
1035 #define PG_BINARY_A "ab"
1036 #define PG_BINARY_R "rb"
1037 #define PG_BINARY_W "wb"
1038 #else
1039 #define PG_BINARY 0
1040 #define PG_BINARY_A "a"
1041 #define PG_BINARY_R "r"
1042 #define PG_BINARY_W "w"
1043 #endif
1044 
1045 /*
1046  * Provide prototypes for routines not present in a particular machine's
1047  * standard C library.
1048  */
1049 
1050 #if !HAVE_DECL_SNPRINTF
1051 extern int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3, 4);
1052 #endif
1053 
1054 #if !HAVE_DECL_VSNPRINTF
1055 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
1056 #endif
1057 
1058 #if !defined(HAVE_MEMMOVE) && !defined(memmove)
1059 #define memmove(d, s, c) bcopy(s, d, c)
1060 #endif
1061 
1062 /* no special DLL markers on most ports */
1063 #ifndef PGDLLIMPORT
1064 #define PGDLLIMPORT
1065 #endif
1066 #ifndef PGDLLEXPORT
1067 #define PGDLLEXPORT
1068 #endif
1069 
1070 /*
1071  * The following is used as the arg list for signal handlers. Any ports
1072  * that take something other than an int argument should override this in
1073  * their pg_config_os.h file. Note that variable names are required
1074  * because it is used in both the prototypes as well as the definitions.
1075  * Note also the long name. We expect that this won't collide with
1076  * other names causing compiler warnings.
1077  */
1078 
1079 #ifndef SIGNAL_ARGS
1080 #define SIGNAL_ARGS int postgres_signal_arg
1081 #endif
1082 
1083 /*
1084  * When there is no sigsetjmp, its functionality is provided by plain
1085  * setjmp. Incidentally, nothing provides setjmp's functionality in
1086  * that case. We now support the case only on Windows.
1087  */
1088 #ifdef WIN32
1089 #define sigjmp_buf jmp_buf
1090 #define sigsetjmp(x,y) setjmp(x)
1091 #define siglongjmp longjmp
1092 #endif
1093 
1094 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
1095 extern int fdatasync(int fildes);
1096 #endif
1097 
1098 /* If strtoq() exists, rename it to the more standard strtoll() */
1099 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
1100 #define strtoll strtoq
1101 #define HAVE_STRTOLL 1
1102 #endif
1103 
1104 /* If strtouq() exists, rename it to the more standard strtoull() */
1105 #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
1106 #define strtoull strtouq
1107 #define HAVE_STRTOULL 1
1108 #endif
1109 
1110 /*
1111  * We assume if we have these two functions, we have their friends too, and
1112  * can use the wide-character functions.
1113  */
1114 #if defined(HAVE_WCSTOMBS) && defined(HAVE_TOWLOWER)
1115 #define USE_WIDE_UPPER_LOWER
1116 #endif
1117 
1118 /* EXEC_BACKEND defines */
1119 #ifdef EXEC_BACKEND
1120 #define NON_EXEC_STATIC
1121 #else
1122 #define NON_EXEC_STATIC static
1123 #endif
1124 
1125 /* /port compatibility functions */
1126 #include "port.h"
1127 
1128 #endif /* C_H */
signed short int16
Definition: c.h:255
uint32 CommandId
Definition: c.h:411
int lbound1
Definition: c.h:474
Definition: c.h:478
char data[NAMEDATALEN]
Definition: c.h:495
uint32 MultiXactOffset
Definition: c.h:409
uint32 TransactionId
Definition: c.h:397
#define MAXDIM
Definition: c.h:419
uint16 bits16
Definition: c.h:276
regproc RegProcedure
Definition: c.h:395
unsigned char uint8
Definition: c.h:266
int32 dataoffset
Definition: c.h:482
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
Oid elemtype
Definition: c.h:472
uint32 SubTransactionId
Definition: c.h:401
unsigned int Oid
Definition: postgres_ext.h:31
char bool
Definition: c.h:202
signed int int32
Definition: c.h:256
#define pg_attribute_printf(f, a)
Definition: c.h:639
char vl_len_[4]
Definition: c.h:441
int int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
#define NAMEDATALEN
unsigned short uint16
Definition: c.h:267
char * Pointer
Definition: c.h:245
int32 dataoffset
Definition: c.h:471
double float8
Definition: c.h:381
int32 vl_len_
Definition: c.h:480
Definition: c.h:493
int dim1
Definition: c.h:484
int ndim
Definition: c.h:470
unsigned int uint32
Definition: c.h:268
uint32 LocalTransactionId
Definition: c.h:399
char vl_dat[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:442
signed char int8
Definition: c.h:254
float float4
Definition: c.h:380
uint8 bits8
Definition: c.h:275
Definition: c.h:467
int dim1
Definition: c.h:473
unsigned int Index
Definition: c.h:365
TransactionId MultiXactId
Definition: c.h:407
uint32 bits32
Definition: c.h:277
int32 vl_len_
Definition: c.h:469
signed int Offset
Definition: c.h:375
size_t Size
Definition: c.h:356
int lbound1
Definition: c.h:485
Oid regproc
Definition: c.h:394
static Datum values[MAXATTR]
Definition: bootstrap.c:163
Definition: c.h:420
Oid elemtype
Definition: c.h:483
bool * BoolPtr
Definition: c.h:214
struct nameData NameData
Definition: c.h:439
NameData * Name
Definition: c.h:497
int ndim
Definition: c.h:481