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