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