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