PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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/* IWYU pragma: always_keep */
20/*
21 *----------------------------------------------------------------
22 * TABLE OF CONTENTS
23 *
24 * When adding stuff to this file, please try to put stuff
25 * into the relevant section, or add new sections as appropriate.
26 *
27 * section description
28 * ------- ------------------------------------------------
29 * 0) pg_config.h and standard system headers
30 * 1) compiler characteristics
31 * 2) bool, true, false
32 * 3) standard system types
33 * 4) IsValid macros for system types
34 * 5) lengthof, alignment
35 * 6) assertions
36 * 7) widely useful macros
37 * 8) random stuff
38 * 9) system-specific hacks
39 *
40 * NOTE: since this file is included by both frontend and backend modules,
41 * it's usually wrong to put an "extern" declaration here, unless it's
42 * ifdef'd so that it's seen in only one case or the other.
43 * typedefs and macros are the kind of thing that might go here.
44 *
45 *----------------------------------------------------------------
46 */
47#ifndef C_H
48#define C_H
49
50/* IWYU pragma: begin_exports */
51
52/*
53 * These headers must be included before any system headers, because on some
54 * platforms they affect the behavior of the system headers (for example, by
55 * defining _FILE_OFFSET_BITS).
56 */
57#include "pg_config.h"
58#include "pg_config_manual.h" /* must be after pg_config.h */
59#include "pg_config_os.h" /* config from include/port/PORTNAME.h */
60
61/* System header files that should be available everywhere in Postgres */
62#include <assert.h>
63#include <inttypes.h>
64#include <stdalign.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <stddef.h>
69#include <stdarg.h>
70#ifdef HAVE_STRINGS_H
71#include <strings.h>
72#endif
73#include <stdint.h>
74#include <sys/types.h>
75#include <errno.h>
76#if defined(WIN32) || defined(__CYGWIN__)
77#include <fcntl.h> /* ensure O_BINARY is available */
78#endif
79#include <locale.h>
80#ifdef HAVE_XLOCALE_H
81#include <xlocale.h>
82#endif
83#ifdef ENABLE_NLS
84#include <libintl.h>
85#endif
86
87#ifdef __cplusplus
88extern "C++"
89{
90/* This header is used in the definition of various C++ things below. */
91#include <type_traits>
92}
93#endif
94
95 /* Pull in fundamental symbols that we also expose to applications */
96#include "postgres_ext.h"
97
98/* Define before including zlib.h to add const decorations to zlib API. */
99#ifdef HAVE_LIBZ
100#define ZLIB_CONST
101#endif
102
103
104/* ----------------------------------------------------------------
105 * Section 1: compiler characteristics
106 * ----------------------------------------------------------------
107 */
108
109/*
110 * Disable "inline" if PG_FORCE_DISABLE_INLINE is defined.
111 * This is used to work around compiler bugs and might also be useful for
112 * investigatory purposes.
113 */
114#ifdef PG_FORCE_DISABLE_INLINE
115#undef inline
116#define inline
117#endif
118
119/*
120 * Attribute macros
121 *
122 * C23: https://en.cppreference.com/w/c/language/attributes.html
123 * C++: https://en.cppreference.com/w/cpp/language/attributes.html
124 * GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
125 * GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
126 * Clang: https://clang.llvm.org/docs/AttributeReference.html
127 */
128
129/*
130 * For compilers which don't support __has_attribute, we just define
131 * __has_attribute(x) to 0 so that we can define macros for various
132 * __attribute__s more easily below.
133 *
134 * Note that __has_attribute only tells about GCC-style attributes, not C23 or
135 * C++ attributes.
136 */
137#ifndef __has_attribute
138#define __has_attribute(attribute) 0
139#endif
140
141/*
142 * pg_attribute_unused() suppresses compiler warnings on unused entities.
143 */
144#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201703L)
145#define pg_attribute_unused() [[maybe_unused]]
146#elif defined(__GNUC__)
147#define pg_attribute_unused() __attribute__((unused))
148#else
149#define pg_attribute_unused()
150#endif
151
152/*
153 * pg_fallthrough indicates that the fall through from the previous case is
154 * intentional.
155 */
156#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201703L)
157#define pg_fallthrough [[fallthrough]]
158#elif __has_attribute(fallthrough)
159#define pg_fallthrough __attribute__((fallthrough))
160#else
161#define pg_fallthrough
162#endif
163
164/*
165 * pg_nodiscard means the compiler should warn if the result of a function
166 * call is ignored.
167 */
168#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201703L)
169#define pg_nodiscard [[nodiscard]]
170#elif defined(__GNUC__)
171#define pg_nodiscard __attribute__((warn_unused_result))
172#else
173#define pg_nodiscard
174#endif
175
176/*
177 * pg_noreturn corresponds to the C11 noreturn/_Noreturn function specifier.
178 * We can't use the standard name "noreturn" because some third-party code
179 * uses __attribute__((noreturn)) in headers, which would get confused if
180 * "noreturn" is defined to "_Noreturn", as is done by <stdnoreturn.h>.
181 *
182 * C23 attributes must be placed at the start of a declaration or statement.
183 * C11 function specifiers go before the function name in a declaration, but
184 * it is common style (and required for C23 compatibility) to put them before
185 * the return type.
186 */
187#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || defined(__cplusplus)
188#define pg_noreturn [[noreturn]]
189#else
190#define pg_noreturn _Noreturn
191#endif
192
193/*
194 * This macro will disable address safety instrumentation for a function
195 * when running with "-fsanitize=address". Think twice before using this!
196 */
197#if defined(__clang__) || __GNUC__ >= 8
198#define pg_attribute_no_sanitize_address() __attribute__((no_sanitize("address")))
199#elif __has_attribute(no_sanitize_address)
200/* This would work for clang, but it's deprecated. */
201#define pg_attribute_no_sanitize_address() __attribute__((no_sanitize_address))
202#else
203#define pg_attribute_no_sanitize_address()
204#endif
205
206/*
207 * Place this macro before functions that should be allowed to make misaligned
208 * accesses. Think twice before using it on non-x86-specific code!
209 * Testing can be done with "-fsanitize=alignment -fsanitize-trap=alignment"
210 * on clang, or "-fsanitize=alignment -fno-sanitize-recover=alignment" on gcc.
211 */
212#if __clang_major__ >= 7 || __GNUC__ >= 8
213#define pg_attribute_no_sanitize_alignment() __attribute__((no_sanitize("alignment")))
214#else
215#define pg_attribute_no_sanitize_alignment()
216#endif
217
218/*
219 * pg_attribute_nonnull means the compiler should warn if the function is
220 * called with the listed arguments set to NULL. If no arguments are
221 * listed, the compiler should warn if any pointer arguments are set to NULL.
222 */
223#if __has_attribute (nonnull)
224#define pg_attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
225#else
226#define pg_attribute_nonnull(...)
227#endif
228
229/*
230 * pg_attribute_target allows specifying different target options that the
231 * function should be compiled with (e.g., for using special CPU instructions).
232 * Note that there still needs to be a configure-time check to verify that a
233 * specific target is understood by the compiler.
234 */
235#if __has_attribute (target)
236#define pg_attribute_target(...) __attribute__((target(__VA_ARGS__)))
237#else
238#define pg_attribute_target(...)
239#endif
240
241/*
242 * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
243 * used in assert-enabled builds, to avoid compiler warnings about unused
244 * variables in assert-disabled builds.
245 */
246#ifdef USE_ASSERT_CHECKING
247#define PG_USED_FOR_ASSERTS_ONLY
248#else
249#define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
250#endif
251
252/*
253 * Our C and C++ compilers may have different ideas about which printf
254 * archetype best represents what src/port/snprintf.c can do.
255 */
256#ifndef __cplusplus
257#define PG_PRINTF_ATTRIBUTE PG_C_PRINTF_ATTRIBUTE
258#else
259#define PG_PRINTF_ATTRIBUTE PG_CXX_PRINTF_ATTRIBUTE
260#endif
261
262/* GCC supports format attributes */
263#if defined(__GNUC__)
264#define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
265#define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
266#else
267#define pg_attribute_format_arg(a)
268#define pg_attribute_printf(f,a)
269#endif
270
271/* GCC supports aligned and packed */
272#if defined(__GNUC__)
273#define pg_attribute_aligned(a) __attribute__((aligned(a)))
274#define pg_attribute_packed() __attribute__((packed))
275#elif defined(_MSC_VER)
276/*
277 * MSVC supports aligned.
278 *
279 * Packing is also possible but only by wrapping the entire struct definition
280 * which doesn't fit into our current macro declarations.
281 */
282#define pg_attribute_aligned(a) __declspec(align(a))
283#else
284/*
285 * NB: aligned and packed are not given default definitions because they
286 * affect code functionality; they *must* be implemented by the compiler
287 * if they are to be used.
288 */
289#endif
290
291/*
292 * Use "pg_attribute_always_inline" in place of "inline" for functions that
293 * we wish to force inlining of, even when the compiler's heuristics would
294 * choose not to. But, if possible, don't force inlining in unoptimized
295 * debug builds.
296 */
297#if defined(__GNUC__) && defined(__OPTIMIZE__)
298/* GCC supports always_inline via __attribute__ */
299#define pg_attribute_always_inline __attribute__((always_inline)) inline
300#elif defined(_MSC_VER)
301/* MSVC has a special keyword for this */
302#define pg_attribute_always_inline __forceinline
303#else
304/* Otherwise, the best we can do is to say "inline" */
305#define pg_attribute_always_inline inline
306#endif
307
308/*
309 * Forcing a function not to be inlined can be useful if it's the slow path of
310 * a performance-critical function, or should be visible in profiles to allow
311 * for proper cost attribution. Note that unlike the pg_attribute_XXX macros
312 * above, this should be placed before the function's return type and name.
313 */
314/* GCC supports noinline via __attribute__ */
315#if defined(__GNUC__)
316#define pg_noinline __attribute__((noinline))
317/* msvc via declspec */
318#elif defined(_MSC_VER)
319#define pg_noinline __declspec(noinline)
320#else
321#define pg_noinline
322#endif
323
324/*
325 * For now, just define pg_attribute_cold and pg_attribute_hot to be empty
326 * macros on minGW 8.1. There appears to be a compiler bug that results in
327 * compilation failure. At this time, we still have at least one buildfarm
328 * animal running that compiler, so this should make that green again. It's
329 * likely this compiler is not popular enough to warrant keeping this code
330 * around forever, so let's just remove it once the last buildfarm animal
331 * upgrades.
332 */
333#if defined(__MINGW64__) && __GNUC__ == 8 && __GNUC_MINOR__ == 1
334
335#define pg_attribute_cold
336#define pg_attribute_hot
337
338#else
339/*
340 * Marking certain functions as "hot" or "cold" can be useful to assist the
341 * compiler in arranging the assembly code in a more efficient way.
342 */
343#if __has_attribute (cold)
344#define pg_attribute_cold __attribute__((cold))
345#else
346#define pg_attribute_cold
347#endif
348
349#if __has_attribute (hot)
350#define pg_attribute_hot __attribute__((hot))
351#else
352#define pg_attribute_hot
353#endif
354
355#endif /* defined(__MINGW64__) && __GNUC__ == 8 &&
356 * __GNUC_MINOR__ == 1 */
357/*
358 * Mark a point as unreachable in a portable fashion. This should preferably
359 * be something that the compiler understands, to aid code generation.
360 * In assert-enabled builds, we prefer abort() for debugging reasons.
361 */
362#if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
363#define pg_unreachable() __builtin_unreachable()
364#elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
365#define pg_unreachable() __assume(0)
366#else
367#define pg_unreachable() abort()
368#endif
369
370/*
371 * Define a compiler-independent macro for determining if an expression is a
372 * compile-time integer const. We don't define this macro to return 0 when
373 * unsupported due to the risk of users of the macro misbehaving if we return
374 * 0 when the expression *is* an integer constant. Callers may check if this
375 * macro is defined by checking if HAVE_PG_INTEGER_CONSTANT_P is defined.
376 */
377#if defined(HAVE__BUILTIN_CONSTANT_P)
378
379/* When __builtin_constant_p() is available, use it. */
380#define pg_integer_constant_p(x) __builtin_constant_p(x)
381#define HAVE_PG_INTEGER_CONSTANT_P
382#elif defined(_MSC_VER) && defined(__STDC_VERSION__)
383
384/*
385 * With MSVC we can use a trick with _Generic to make this work. This has
386 * been borrowed from:
387 * https://stackoverflow.com/questions/49480442/detecting-integer-constant-expressions-in-macros
388 * and only works with integer constants. Compilation will fail if given a
389 * constant or variable of any type other than an integer.
390 */
391#define pg_integer_constant_p(x) \
392 _Generic((1 ? ((void *) ((x) * (uintptr_t) 0)) : &(int) {1}), int *: 1, void *: 0)
393#define HAVE_PG_INTEGER_CONSTANT_P
394#endif
395
396/*
397 * pg_assume(expr) states that we assume `expr` to evaluate to true. In assert
398 * enabled builds pg_assume() is turned into an assertion, in optimized builds
399 * we try to clue the compiler into the fact that `expr` is true.
400 *
401 * This is useful for two purposes:
402 *
403 * 1) Avoid compiler warnings by telling the compiler about assumptions the
404 * code makes. This is particularly useful when building with optimizations
405 * and w/o assertions.
406 *
407 * 2) Help the compiler to generate more efficient code
408 *
409 * It is unspecified whether `expr` is evaluated, therefore it better be
410 * side-effect free.
411 */
412#if defined(USE_ASSERT_CHECKING)
413#define pg_assume(expr) Assert(expr)
414#elif defined(HAVE__BUILTIN_UNREACHABLE)
415#define pg_assume(expr) \
416 do { \
417 if (!(expr)) \
418 __builtin_unreachable(); \
419 } while (0)
420#elif defined(_MSC_VER)
421#define pg_assume(expr) __assume(expr)
422#else
423#define pg_assume(expr) ((void) 0)
424#endif
425
426/*
427 * Hints to the compiler about the likelihood of a branch. Both likely() and
428 * unlikely() return the boolean value of the contained expression.
429 *
430 * These should only be used sparingly, in very hot code paths. It's very easy
431 * to mis-estimate likelihoods.
432 */
433#ifdef __GNUC__
434#define likely(x) __builtin_expect((x) != 0, 1)
435#define unlikely(x) __builtin_expect((x) != 0, 0)
436#else
437#define likely(x) ((x) != 0)
438#define unlikely(x) ((x) != 0)
439#endif
440
441/*
442 * When we call clang to generate bitcode, we might be using configure results
443 * from a different compiler, which might not be fully compatible with the
444 * clang we are using. The fully correct solution would be to run a separate
445 * set of configure tests for that clang-for-bitcode, but that could be very
446 * difficult to implement, and in practice clang supports most things other
447 * compilers support. So this section just contains some hardcoded ugliness
448 * to override some configure results where it is necessary.
449 */
450#if defined(__clang__)
451#if __clang_major__ < 19
452#undef HAVE_TYPEOF_UNQUAL
453#else
454#undef typeof_unqual
455#define typeof_unqual __typeof_unqual__
456#endif
457#endif /* __clang__ */
458
459/*
460 * Provide typeof in C++ for C++ compilers that don't support typeof natively.
461 * It might be spelled __typeof__ instead of typeof, in which case
462 * pg_cxx_typeof provides that mapping. If neither is supported, we can use
463 * decltype, but to make it equivalent to C's typeof, we need to remove
464 * references from the result [1]. Also ensure HAVE_TYPEOF is set so that
465 * typeof-dependent code is always enabled in C++ mode.
466 *
467 * [1]: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2927.htm#existing-decltype
468 */
469#if defined(__cplusplus)
470#undef typeof
471#ifdef pg_cxx_typeof
472#define typeof(x) pg_cxx_typeof(x)
473#elif !defined(HAVE_CXX_TYPEOF)
474#define typeof(x) std::remove_reference<decltype(x)>::type
475#endif
476#ifndef HAVE_TYPEOF
477#define HAVE_TYPEOF 1
478#endif
479/*
480 * and analogously for typeof_unqual
481 */
482#undef typeof_unqual
483#ifdef pg_cxx_typeof_unqual
484#define typeof_unqual(x) pg_cxx_typeof_unqual(x)
485#elif !defined(HAVE_CXX_TYPEOF_UNQUAL)
486#define typeof_unqual(x) std::remove_cv<std::remove_reference<decltype(x)>::type>::type
487#endif
488#ifndef HAVE_TYPEOF_UNQUAL
489#define HAVE_TYPEOF_UNQUAL 1
490#endif
491#endif /* __cplusplus */
492
493/*
494 * CppAsString
495 * Convert the argument to a string, using the C preprocessor.
496 * CppAsString2
497 * Convert the argument to a string, after one round of macro expansion.
498 * CppConcat
499 * Concatenate two arguments together, using the C preprocessor.
500 *
501 * Note: There used to be support here for pre-ANSI C compilers that didn't
502 * support # and ##. Nowadays, these macros are just for clarity and/or
503 * backward compatibility with existing PostgreSQL code.
504 */
505#define CppAsString(identifier) #identifier
506#define CppAsString2(x) CppAsString(x)
507#define CppConcat(x, y) x##y
508
509/*
510 * VA_ARGS_NARGS
511 * Returns the number of macro arguments it is passed.
512 *
513 * An empty argument still counts as an argument, so effectively, this is
514 * "one more than the number of commas in the argument list".
515 *
516 * This works for up to 63 arguments. Internally, VA_ARGS_NARGS_() is passed
517 * 64+N arguments, and the C99 standard only requires macros to allow up to
518 * 127 arguments, so we can't portably go higher. The implementation is
519 * pretty trivial: VA_ARGS_NARGS_() returns its 64th argument, and we set up
520 * the call so that that is the appropriate one of the list of constants.
521 * This idea is due to Laurent Deniau.
522 */
523#define VA_ARGS_NARGS(...) \
524 VA_ARGS_NARGS_(__VA_ARGS__, \
525 63,62,61,60, \
526 59,58,57,56,55,54,53,52,51,50, \
527 49,48,47,46,45,44,43,42,41,40, \
528 39,38,37,36,35,34,33,32,31,30, \
529 29,28,27,26,25,24,23,22,21,20, \
530 19,18,17,16,15,14,13,12,11,10, \
531 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
532
533#define VA_ARGS_NARGS_( \
534 _01,_02,_03,_04,_05,_06,_07,_08,_09,_10, \
535 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
536 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
537 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
538 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
539 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
540 _61,_62,_63, N, ...) \
541 (N)
542
543/*
544 * Generic function pointer. This can be used in the rare cases where it's
545 * necessary to cast a function pointer to a seemingly incompatible function
546 * pointer type while avoiding gcc's -Wcast-function-type warnings.
547 */
548typedef void (*pg_funcptr_t) (void);
549
550/*
551 * We require C99, hence the compiler should understand flexible array
552 * members. However, for documentation purposes we still consider it to be
553 * project style to write "field[FLEXIBLE_ARRAY_MEMBER]" not just "field[]".
554 * When computing the size of such an object, use "offsetof(struct s, f)"
555 * for portability. Don't use "offsetof(struct s, f[0])", as this doesn't
556 * work with MSVC and with C++ compilers.
557 */
558#define FLEXIBLE_ARRAY_MEMBER /* empty */
559
560/*
561 * Does the compiler support #pragma GCC system_header? We optionally use it
562 * to avoid warnings that we can't fix (e.g. in the perl headers).
563 * See https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html
564 *
565 * Headers for which we do not want to show compiler warnings can,
566 * conditionally, use #pragma GCC system_header to avoid warnings. Obviously
567 * this should only be used for external headers over which we do not have
568 * control.
569 *
570 * Support for the pragma is tested here, instead of during configure, as gcc
571 * also warns about the pragma being used in a .c file. It's surprisingly hard
572 * to get autoconf to use .h as the file-ending. Looks like gcc has
573 * implemented the pragma since the 2000, so this test should suffice.
574 *
575 *
576 * Alternatively, we could add the include paths for problematic headers with
577 * -isystem, but that is a larger hammer and is harder to search for.
578 *
579 * A more granular alternative would be to use #pragma GCC diagnostic
580 * push/ignored/pop, but gcc warns about unknown warnings being ignored, so
581 * every to-be-ignored-temporarily compiler warning would require its own
582 * pg_config.h symbol and #ifdef.
583 */
584#ifdef __GNUC__
585#define HAVE_PRAGMA_GCC_SYSTEM_HEADER 1
586#endif
587
588
589/* ----------------------------------------------------------------
590 * Section 2: bool, true, false
591 * ----------------------------------------------------------------
592 */
593
594/*
595 * bool
596 * Boolean value, either true or false.
597 *
598 * PostgreSQL currently cannot deal with bool of size other than 1; there are
599 * static assertions around the code to prevent that.
600 */
601
602#include <stdbool.h>
603
604
605/* ----------------------------------------------------------------
606 * Section 3: standard system types
607 * ----------------------------------------------------------------
608 */
609
610/*
611 * Pointer
612 * Variable holding address of any memory resident object.
613 * (obsolescent; use void * or char *)
614 */
615typedef void *Pointer;
616
617/* Historical names for types in <stdint.h>. */
618typedef int8_t int8;
626
627/*
628 * 64-bit integers
629 */
630#define INT64CONST(x) INT64_C(x)
631#define UINT64CONST(x) UINT64_C(x)
632
633/* snprintf format strings to use for 64-bit integers */
634#define INT64_FORMAT "%" PRId64
635#define UINT64_FORMAT "%" PRIu64
636#define OID8_FORMAT "%" PRIu64
637
638/*
639 * 128-bit signed and unsigned integers
640 * There currently is only limited support for such types.
641 * E.g. 128bit literals and snprintf are not supported; but math is.
642 * Also, because we exclude such types when choosing MAXIMUM_ALIGNOF,
643 * it must be possible to coerce the compiler to allocate them on no
644 * more than MAXALIGN boundaries.
645 */
646#if defined(PG_INT128_TYPE)
647#if defined(pg_attribute_aligned) || ALIGNOF_PG_INT128_TYPE <= MAXIMUM_ALIGNOF
648#define HAVE_INT128 1
649
651#if defined(pg_attribute_aligned)
653#endif
654 ;
655
656typedef unsigned PG_INT128_TYPE uint128
657#if defined(pg_attribute_aligned)
659#endif
660 ;
661
662#endif
663#endif
664
665/* Historical names for limits in <stdint.h>. */
666#define PG_INT8_MIN INT8_MIN
667#define PG_INT8_MAX INT8_MAX
668#define PG_UINT8_MAX UINT8_MAX
669#define PG_INT16_MIN INT16_MIN
670#define PG_INT16_MAX INT16_MAX
671#define PG_UINT16_MAX UINT16_MAX
672#define PG_INT32_MIN INT32_MIN
673#define PG_INT32_MAX INT32_MAX
674#define PG_UINT32_MAX UINT32_MAX
675#define PG_INT64_MIN INT64_MIN
676#define PG_INT64_MAX INT64_MAX
677#define PG_UINT64_MAX UINT64_MAX
678
679/*
680 * We now always use int64 timestamps, but keep this symbol defined for the
681 * benefit of external code that might test it.
682 */
683#define HAVE_INT64_TIMESTAMP
684
685/*
686 * Size
687 * Size of any memory resident object, as returned by sizeof.
688 */
689typedef size_t Size;
690
691/*
692 * Index
693 * Index into any memory resident array.
694 *
695 * Note:
696 * Indices are non negative.
697 */
698typedef unsigned int Index;
699
700/*
701 * Offset
702 * Offset into any memory resident array.
703 *
704 * Note:
705 * This differs from an Index in that an Index is always
706 * non negative, whereas Offset may be negative.
707 */
708typedef signed int Offset;
709
710/*
711 * Common Postgres datatype names (as used in the catalogs)
712 */
713typedef float float4;
714typedef double float8;
715
716/*
717 * float8, int8, and related datatypes are now always pass-by-value.
718 * We keep this symbol to avoid breaking extension code that may use it.
719 */
720#define FLOAT8PASSBYVAL true
721
722/*
723 * Oid, Oid8, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
724 * CommandId
725 */
726
727/* typedef Oid is in postgres_ext.h */
728
729/*
730 * regproc is the type name used in the include/catalog headers, but
731 * RegProcedure is the preferred name in C code.
732 */
733typedef Oid regproc;
735
737
739
741
742#define InvalidSubTransactionId ((SubTransactionId) 0)
743#define TopSubTransactionId ((SubTransactionId) 1)
744
745/* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
747
749
751
752#define FirstCommandId ((CommandId) 0)
753#define InvalidCommandId (~(CommandId)0)
754
755/* 8-byte Object ID */
756typedef uint64 Oid8;
757
758#define InvalidOid8 ((Oid8) 0)
759#define OID8_MAX UINT64_MAX
760
761/* ----------------
762 * Variable-length datatypes all share the 'varlena' header.
763 *
764 * NOTE: for TOASTable types, this is an oversimplification, since the value
765 * may be compressed or moved out-of-line. However datatype-specific routines
766 * are mostly content to deal with de-TOASTed values only, and of course
767 * client-side routines should never see a TOASTed value. But even in a
768 * de-TOASTed value, beware of touching vl_len_ directly, as its
769 * representation is no longer convenient. It's recommended that code always
770 * use macros VARDATA_ANY, VARSIZE_ANY, VARSIZE_ANY_EXHDR, VARDATA, VARSIZE,
771 * and SET_VARSIZE instead of relying on direct mentions of the struct fields.
772 * See varatt.h for details of the TOASTed form.
773 * ----------------
774 */
775typedef struct varlena
776{
777 char vl_len_[4]; /* Do not touch this field directly! */
778 char vl_dat[FLEXIBLE_ARRAY_MEMBER]; /* Data content is here */
780
781#define VARHDRSZ ((int32) sizeof(int32))
782
783/*
784 * These widely-used datatypes are just a varlena header and the data bytes.
785 * There is no terminating null or anything like that --- the data length is
786 * always VARSIZE_ANY_EXHDR(ptr).
787 */
789typedef varlena text;
790typedef varlena BpChar; /* blank-padded char, ie SQL char(n) */
791typedef varlena VarChar; /* var-length char, ie SQL varchar(n) */
792
793/*
794 * Specialized array types. These are physically laid out just the same
795 * as regular arrays (so that the regular array subscripting code works
796 * with them). They exist as distinct types mostly for historical reasons:
797 * they have nonstandard I/O behavior which we don't want to change for fear
798 * of breaking applications that look at the system catalogs. There is also
799 * an implementation issue for oidvector: it's part of the primary key for
800 * pg_proc, and we can't use the normal btree array support routines for that
801 * without circularity.
802 */
803typedef struct
804{
805 int32 vl_len_; /* these fields must match ArrayType! */
806 int ndim; /* always 1 for int2vector */
807 int32 dataoffset; /* always 0 for int2vector */
809 int dim1;
812} int2vector;
813
814typedef struct
815{
816 int32 vl_len_; /* these fields must match ArrayType! */
817 int ndim; /* always 1 for oidvector */
818 int32 dataoffset; /* always 0 for oidvector */
820 int dim1;
823} oidvector;
824
825/*
826 * Representation of a Name: effectively just a C string, but null-padded to
827 * exactly NAMEDATALEN bytes. The use of a struct is historical.
828 */
829typedef struct nameData
830{
833typedef NameData *Name;
834
835#define NameStr(name) ((name).data)
836
837
838/* ----------------------------------------------------------------
839 * Section 4: IsValid macros for system types
840 * ----------------------------------------------------------------
841 */
842/*
843 * BoolIsValid
844 * True iff bool is valid.
845 */
846#define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
847
848/*
849 * PointerIsAligned
850 * True iff pointer is properly aligned to point to the given type.
851 */
852#define PointerIsAligned(pointer, type) \
853 (((uintptr_t)(pointer) % (sizeof (type))) == 0)
854
855#define OffsetToPointer(base, offset) \
856 ((void *)((char *) base + offset))
857
858#define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
859
860#define Oid8IsValid(objectId) ((bool) ((objectId) != InvalidOid8))
861
862#define RegProcedureIsValid(p) OidIsValid(p)
863
864
865/* ----------------------------------------------------------------
866 * Section 5: lengthof, alignment
867 * ----------------------------------------------------------------
868 */
869/*
870 * lengthof
871 * Number of elements in an array.
872 */
873#define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
874
875/* ----------------
876 * Alignment macros: align a length or address appropriately for a given type.
877 * The fooALIGN() macros round up to a multiple of the required alignment,
878 * while the fooALIGN_DOWN() macros round down. The latter are more useful
879 * for problems like "how many X-sized structures will fit in a page?".
880 *
881 * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
882 * That case seems extremely unlikely to be needed in practice, however.
883 *
884 * NOTE: MAXIMUM_ALIGNOF, and hence MAXALIGN(), intentionally exclude any
885 * larger-than-8-byte types the compiler might have.
886 * ----------------
887 */
888
889#define TYPEALIGN(ALIGNVAL,LEN) \
890 (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
891
892#define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
893#define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
894#define INT64ALIGN(LEN) TYPEALIGN(ALIGNOF_INT64_T, (LEN))
895#define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
896#define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
897/* MAXALIGN covers only built-in types, not buffers */
898#define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
899#define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
900
901#define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
902 (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
903
904#define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
905#define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
906#define INT64ALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT64_T, (LEN))
907#define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
908#define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
909#define BUFFERALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_BUFFER, (LEN))
910
911/*
912 * The above macros will not work with types wider than uintptr_t, like with
913 * uint64 on 32-bit platforms. That's not problem for the usual use where a
914 * pointer or a length is aligned, but for the odd case that you need to
915 * align something (potentially) wider, use TYPEALIGN64.
916 */
917#define TYPEALIGN64(ALIGNVAL,LEN) \
918 (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
919
920/* we don't currently need wider versions of the other ALIGN macros */
921#define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
922
923
924/* ----------------------------------------------------------------
925 * Section 6: assertions
926 * ----------------------------------------------------------------
927 */
928
929/*
930 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
931 * - plai 9/5/90
932 *
933 * It should _NOT_ be defined in releases or in benchmark copies
934 */
935
936/*
937 * Assert() can be used in both frontend and backend code. In frontend code it
938 * just calls the standard assert, if it's available. If use of assertions is
939 * not configured, it does nothing.
940 */
941#ifndef USE_ASSERT_CHECKING
942
943#define Assert(condition) ((void)true)
944#define AssertMacro(condition) ((void)true)
945
946#elif defined(FRONTEND)
947
948#define Assert(p) assert(p)
949#define AssertMacro(p) ((void) assert(p))
950
951#else /* USE_ASSERT_CHECKING && !FRONTEND */
952
953/*
954 * Assert
955 * Generates a fatal exception if the given condition is false.
956 */
957#define Assert(condition) \
958 do { \
959 if (!(condition)) \
960 ExceptionalCondition(#condition, __FILE__, __LINE__); \
961 } while (0)
962
963/*
964 * AssertMacro is the same as Assert but it's suitable for use in
965 * expression-like macros, for example:
966 *
967 * #define foo(x) (AssertMacro(x != 0), bar(x))
968 */
969#define AssertMacro(condition) \
970 ((void) ((condition) || \
971 (ExceptionalCondition(#condition, __FILE__, __LINE__), 0)))
972
973#endif /* USE_ASSERT_CHECKING && !FRONTEND */
974
975/*
976 * Check that `ptr' is `bndr' aligned.
977 */
978#define AssertPointerAlignment(ptr, bndr) \
979 Assert(TYPEALIGN(bndr, (uintptr_t)(ptr)) == (uintptr_t)(ptr))
980
981/*
982 * ExceptionalCondition is compiled into the backend whether or not
983 * USE_ASSERT_CHECKING is defined, so as to support use of extensions
984 * that are built with that #define with a backend that isn't. Hence,
985 * we should declare it as long as !FRONTEND.
986 */
987#ifndef FRONTEND
988pg_noreturn extern void ExceptionalCondition(const char *conditionName,
989 const char *fileName, int lineNumber);
990#endif
991
992/*
993 * Macros to support compile-time assertion checks.
994 *
995 * If the "condition" (a compile-time-constant expression) evaluates to false,
996 * throw a compile error using the "errmessage" (a string literal).
997 */
998
999/*
1000 * We require C11 and C++11, so static_assert() is expected to be there.
1001 * StaticAssertDecl() was previously used for portability, but it's now just a
1002 * plain wrapper and doesn't need to be used in new code. static_assert() is
1003 * a "declaration", and so it must be placed where for example a variable
1004 * declaration would be valid. As long as we compile with
1005 * -Wno-declaration-after-statement, that also means it cannot be placed after
1006 * statements in a function.
1007 */
1008#define StaticAssertDecl(condition, errmessage) \
1009 static_assert(condition, errmessage)
1010
1011/*
1012 * StaticAssertStmt() was previously used to make static assertions work as a
1013 * statement, but its use is now deprecated.
1014 */
1015#define StaticAssertStmt(condition, errmessage) \
1016 do { static_assert(condition, errmessage); } while(0)
1017
1018/*
1019 * StaticAssertExpr() is for use in an expression.
1020 *
1021 * See <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3715.pdf> for some
1022 * rationale for the precise behavior of this implementation. See
1023 * <https://stackoverflow.com/questions/31311748> about the C++
1024 * implementation.
1025 *
1026 * For compilers that don't support this, we fall back on a kluge that assumes
1027 * the compiler will complain about a negative width for a struct bit-field.
1028 * This will not include a helpful error message, but it beats not getting an
1029 * error at all.
1030 */
1031#ifndef __cplusplus
1032#if !defined(_MSC_VER) || _MSC_VER >= 1933
1033#define StaticAssertExpr(condition, errmessage) \
1034 ((void) sizeof(struct {static_assert(condition, errmessage); char a;}))
1035#else /* _MSC_VER < 1933 */
1036/*
1037 * This compiler is buggy and fails to compile the previous variant; use a
1038 * fallback implementation.
1039 */
1040#define StaticAssertExpr(condition, errmessage) \
1041 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
1042#endif /* _MSC_VER < 1933 */
1043#else /* __cplusplus */
1044#define StaticAssertExpr(condition, errmessage) \
1045 ([]{static_assert(condition, errmessage);})
1046#endif
1047
1048
1049/*
1050 * Compile-time checks that a variable (or expression) has the specified type.
1051 *
1052 * StaticAssertVariableIsOfType() can be used as a declaration.
1053 * StaticAssertVariableIsOfTypeMacro() is intended for use in macros, eg
1054 * #define foo(x) (StaticAssertVariableIsOfTypeMacro(x, int), bar(x))
1055 *
1056 * If we don't have __builtin_types_compatible_p, we can still assert that
1057 * the types have the same size. This is far from ideal (especially on 32-bit
1058 * platforms) but it provides at least some coverage.
1059 */
1060#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
1061#define StaticAssertVariableIsOfType(varname, typename) \
1062 StaticAssertDecl(__builtin_types_compatible_p(typeof(varname), typename), \
1063 CppAsString(varname) " does not have type " CppAsString(typename))
1064#define StaticAssertVariableIsOfTypeMacro(varname, typename) \
1065 (StaticAssertExpr(__builtin_types_compatible_p(typeof(varname), typename), \
1066 CppAsString(varname) " does not have type " CppAsString(typename)))
1067#else /* !HAVE__BUILTIN_TYPES_COMPATIBLE_P */
1068#define StaticAssertVariableIsOfType(varname, typename) \
1069 StaticAssertDecl(sizeof(varname) == sizeof(typename), \
1070 CppAsString(varname) " does not have type " CppAsString(typename))
1071#define StaticAssertVariableIsOfTypeMacro(varname, typename) \
1072 (StaticAssertExpr(sizeof(varname) == sizeof(typename), \
1073 CppAsString(varname) " does not have type " CppAsString(typename)))
1074#endif /* HAVE__BUILTIN_TYPES_COMPATIBLE_P */
1075
1076
1077/* ----------------------------------------------------------------
1078 * Section 7: widely useful macros
1079 * ----------------------------------------------------------------
1080 */
1081/*
1082 * Max
1083 * Return the maximum of two numbers.
1084 */
1085#define Max(x, y) ((x) > (y) ? (x) : (y))
1086
1087/*
1088 * Min
1089 * Return the minimum of two numbers.
1090 */
1091#define Min(x, y) ((x) < (y) ? (x) : (y))
1092
1093
1094/* Get a bit mask of the bits set in non-size_t aligned addresses */
1095#define SIZE_T_ALIGN_MASK (sizeof(size_t) - 1)
1096
1097/*
1098 * MemSet
1099 * Exactly the same as standard library function memset(), but considerably
1100 * faster for zeroing small size_t-aligned structures (such as parsetree
1101 * nodes). This has to be a macro because the main point is to avoid
1102 * function-call overhead. However, we have also found that the loop is
1103 * faster than native libc memset() on some platforms, even those with
1104 * assembler memset() functions. More research needs to be done, perhaps
1105 * with MEMSET_LOOP_LIMIT tests in configure.
1106 */
1107#define MemSet(start, val, len) \
1108 do \
1109 { \
1110 /* must be void* because we don't know if it is size_t aligned yet */ \
1111 void *_vstart = (void *) (start); \
1112 int _val = (val); \
1113 Size _len = (len); \
1114\
1115 if ((((uintptr_t) _vstart) & SIZE_T_ALIGN_MASK) == 0 && \
1116 (_len & SIZE_T_ALIGN_MASK) == 0 && \
1117 _val == 0 && \
1118 _len <= MEMSET_LOOP_LIMIT && \
1119 /* \
1120 * If MEMSET_LOOP_LIMIT == 0, optimizer should find \
1121 * the whole "if" false at compile time. \
1122 */ \
1123 MEMSET_LOOP_LIMIT != 0) \
1124 { \
1125 size_t *_start = (size_t *) _vstart; \
1126 size_t *_stop = (size_t *) ((char *) _start + _len); \
1127 while (_start < _stop) \
1128 *_start++ = 0; \
1129 } \
1130 else \
1131 memset(_vstart, _val, _len); \
1132 } while (0)
1133
1134/*
1135 * MemSetAligned is the same as MemSet except it omits the test to see if
1136 * "start" is size_t-aligned. This is okay to use if the caller knows
1137 * a-priori that the pointer is suitably aligned (typically, because he just
1138 * got it from palloc(), which always delivers a max-aligned pointer).
1139 */
1140#define MemSetAligned(start, val, len) \
1141 do \
1142 { \
1143 size_t *_start = (size_t *) (start); \
1144 int _val = (val); \
1145 Size _len = (len); \
1146\
1147 if ((_len & SIZE_T_ALIGN_MASK) == 0 && \
1148 _val == 0 && \
1149 _len <= MEMSET_LOOP_LIMIT && \
1150 MEMSET_LOOP_LIMIT != 0) \
1151 { \
1152 size_t *_stop = (size_t *) ((char *) _start + _len); \
1153 while (_start < _stop) \
1154 *_start++ = 0; \
1155 } \
1156 else \
1157 memset(_start, _val, _len); \
1158 } while (0)
1159
1160
1161/*
1162 * Macros for range-checking float values before converting to integer.
1163 * We must be careful here that the boundary values are expressed exactly
1164 * in the float domain. PG_INTnn_MIN is an exact power of 2, so it will
1165 * be represented exactly; but PG_INTnn_MAX isn't, and might get rounded
1166 * off, so avoid using that.
1167 * The input must be rounded to an integer beforehand, typically with rint(),
1168 * else we might draw the wrong conclusion about close-to-the-limit values.
1169 * These macros will do the right thing for Inf, but not necessarily for NaN,
1170 * so check isnan(num) first if that's a possibility.
1172#define FLOAT4_FITS_IN_INT16(num) \
1173 ((num) >= (float4) PG_INT16_MIN && (num) < -((float4) PG_INT16_MIN))
1174#define FLOAT4_FITS_IN_INT32(num) \
1175 ((num) >= (float4) PG_INT32_MIN && (num) < -((float4) PG_INT32_MIN))
1176#define FLOAT4_FITS_IN_INT64(num) \
1177 ((num) >= (float4) PG_INT64_MIN && (num) < -((float4) PG_INT64_MIN))
1178#define FLOAT8_FITS_IN_INT16(num) \
1179 ((num) >= (float8) PG_INT16_MIN && (num) < -((float8) PG_INT16_MIN))
1180#define FLOAT8_FITS_IN_INT32(num) \
1181 ((num) >= (float8) PG_INT32_MIN && (num) < -((float8) PG_INT32_MIN))
1182#define FLOAT8_FITS_IN_INT64(num) \
1183 ((num) >= (float8) PG_INT64_MIN && (num) < -((float8) PG_INT64_MIN))
1184
1185
1186/* ----------------------------------------------------------------
1187 * Section 8: random stuff
1188 * ----------------------------------------------------------------
1189 */
1190
1191/*
1192 * Invert the sign of a qsort-style comparison result, ie, exchange negative
1193 * and positive integer values, being careful not to get the wrong answer
1194 * for INT_MIN. The argument should be an integral variable.
1195 */
1196#define INVERT_COMPARE_RESULT(var) \
1197 ((var) = ((var) < 0) ? 1 : -(var))
1198
1199/*
1200 * Use this, not "char buf[BLCKSZ]", to declare a field or local variable
1201 * holding a page buffer, if that page might be accessed as a page. Otherwise
1202 * the variable might be under-aligned, causing problems on alignment-picky
1203 * hardware.
1205typedef struct PGAlignedBlock
1206{
1207 alignas(MAXIMUM_ALIGNOF) char data[BLCKSZ];
1209
1210/*
1211 * alignas with extended alignments is buggy in g++ < 9. As a simple
1212 * workaround, we disable these definitions in that case.
1213 *
1214 * <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89357>
1215 */
1216#if !(defined(__cplusplus) && defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 9)
1217
1218/*
1219 * Use this to declare a field or local variable holding a page buffer, if that
1220 * page might be accessed as a page or passed to an SMgr I/O function. If
1221 * allocating using the MemoryContext API, the aligned allocation functions
1222 * should be used with PG_IO_ALIGN_SIZE. This alignment may be more efficient
1223 * for I/O in general, but may be strictly required on some platforms when
1224 * using direct I/O.
1226typedef struct PGIOAlignedBlock
1227{
1228 alignas(PG_IO_ALIGN_SIZE) char data[BLCKSZ];
1230
1231/* Same, but for an XLOG_BLCKSZ-sized buffer */
1233{
1234 alignas(PG_IO_ALIGN_SIZE) char data[XLOG_BLCKSZ];
1236
1237#else /* (g++ < 9) */
1238
1239/* Allow these types to be used as abstract types when using old g++ */
1240typedef struct PGIOAlignedBlock PGIOAlignedBlock;
1242
1243#endif /* !(g++ < 9) */
1245/* msb for char */
1246#define HIGHBIT (0x80)
1247#define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
1248
1249/*
1250 * Support macros for escaping strings. escape_backslash should be true
1251 * if generating a non-standard-conforming string. Prefixing a string
1252 * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
1253 * Beware of multiple evaluation of the "ch" argument!
1254 */
1255#define SQL_STR_DOUBLE(ch, escape_backslash) \
1256 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
1257
1258#define ESCAPE_STRING_SYNTAX 'E'
1261#define STATUS_OK (0)
1262#define STATUS_ERROR (-1)
1263#define STATUS_EOF (-2)
1264
1265/*
1266 * gettext support
1267 */
1269#ifndef ENABLE_NLS
1270/* stuff we'd otherwise get from <libintl.h> */
1271#define gettext(x) (x)
1272#define dgettext(d,x) (x)
1273#define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
1274#define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
1275#endif
1276
1277#define _(x) gettext(x)
1278
1279/*
1280 * Use this to mark string constants as needing translation at some later
1281 * time, rather than immediately. This is useful for cases where you need
1282 * access to the original string and translated string, and for cases where
1283 * immediate translation is not possible, like when initializing global
1284 * variables.
1286 * https://www.gnu.org/software/gettext/manual/html_node/Special-cases.html
1287 */
1288#define gettext_noop(x) (x)
1289
1290/*
1291 * To better support parallel installations of major PostgreSQL
1292 * versions as well as parallel installations of major library soname
1293 * versions, we mangle the gettext domain name by appending those
1294 * version numbers. The coding rule ought to be that wherever the
1295 * domain name is mentioned as a literal, it must be wrapped into
1296 * PG_TEXTDOMAIN(). The macros below do not work on non-literals; but
1297 * that is somewhat intentional because it avoids having to worry
1298 * about multiple states of premangling and postmangling as the values
1299 * are being passed around.
1300 *
1301 * Make sure this matches the installation rules in nls-global.mk.
1302 */
1303#ifdef SO_MAJOR_VERSION
1304#define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
1305#else
1306#define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1307#endif
1308
1309/*
1310 * Macro that allows to cast constness and volatile away from an expression, but doesn't
1311 * allow changing the underlying type. Enforcement of the latter
1312 * currently only works for gcc like compilers.
1313 *
1314 * Please note IT IS NOT SAFE to cast constness away if the result will ever
1315 * be modified (it would be undefined behaviour). Doing so anyway can cause
1316 * compiler misoptimizations or runtime crashes (modifying readonly memory).
1317 * It is only safe to use when the result will not be modified, but API
1318 * design or language restrictions prevent you from declaring that
1319 * (e.g. because a function returns both const and non-const variables).
1320 *
1321 * Note that this only works in function scope, not for global variables (it'd
1322 * be nice, but not trivial, to improve that).
1323 */
1324#if defined(__cplusplus)
1325#define unconstify(underlying_type, expr) const_cast<underlying_type>(expr)
1326#define unvolatize(underlying_type, expr) const_cast<underlying_type>(expr)
1327#else
1328#define unconstify(underlying_type, expr) \
1329 (StaticAssertVariableIsOfTypeMacro(expr, const underlying_type), \
1330 (underlying_type) (expr))
1331#define unvolatize(underlying_type, expr) \
1332 (StaticAssertVariableIsOfTypeMacro(expr, volatile underlying_type), \
1333 (underlying_type) (expr))
1334#endif
1335
1336/*
1337 * SSE2 instructions are part of the spec for the 64-bit x86 ISA. We assume
1338 * that compilers targeting this architecture understand SSE2 intrinsics.
1339 */
1340#if (defined(__x86_64__) || defined(_M_AMD64))
1341#define USE_SSE2
1342
1343/*
1344 * We use the Neon instructions if the compiler provides access to them (as
1345 * indicated by __ARM_NEON) and we are on aarch64. While Neon support is
1346 * technically optional for aarch64, it appears that all available 64-bit
1347 * hardware does have it. Neon exists in some 32-bit hardware too, but we
1348 * could not realistically use it there without a run-time check, which seems
1349 * not worth the trouble for now.
1350 */
1351#elif defined(__aarch64__) && defined(__ARM_NEON)
1352#define USE_NEON
1353#endif
1354
1355/* ----------------------------------------------------------------
1356 * Section 9: system-specific hacks
1357 *
1358 * This should be limited to things that absolutely have to be
1359 * included in every source file. The port-specific header file
1360 * is usually a better place for this sort of thing.
1361 * ----------------------------------------------------------------
1362 */
1363
1364/*
1365 * NOTE: this is also used for opening text files.
1366 * WIN32 treats Control-Z as EOF in files opened in text mode.
1367 * Therefore, we open files in binary mode on Win32 so we can read
1368 * literal control-Z. The other affect is that we see CRLF, but
1369 * that is OK because we can already handle those cleanly.
1370 */
1371#if defined(WIN32) || defined(__CYGWIN__)
1372#define PG_BINARY O_BINARY
1373#define PG_BINARY_A "ab"
1374#define PG_BINARY_R "rb"
1375#define PG_BINARY_W "wb"
1376#else
1377#define PG_BINARY 0
1378#define PG_BINARY_A "a"
1379#define PG_BINARY_R "r"
1380#define PG_BINARY_W "w"
1381#endif
1382
1383/*
1384 * Provide prototypes for routines not present in a particular machine's
1385 * standard C library.
1386 */
1387
1388#if !HAVE_DECL_FDATASYNC
1389extern int fdatasync(int fd);
1390#endif
1391
1392/*
1393 * Thin wrappers that convert strings to exactly 64-bit integers, matching our
1394 * definition of int64. (For the naming, compare that POSIX has
1395 * strtoimax()/strtoumax() which return intmax_t/uintmax_t.)
1396 */
1397#if SIZEOF_LONG == 8
1398#define strtoi64(str, endptr, base) ((int64) strtol(str, endptr, base))
1399#define strtou64(str, endptr, base) ((uint64) strtoul(str, endptr, base))
1400#elif SIZEOF_LONG_LONG == 8
1401#define strtoi64(str, endptr, base) ((int64) strtoll(str, endptr, base))
1402#define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
1403#else
1404#error "cannot find integer type of the same size as int64_t"
1405#endif
1406
1407/*
1408 * Similarly, wrappers around labs()/llabs() matching our int64.
1409 */
1410#if SIZEOF_LONG == 8
1411#define i64abs(i) ((int64) labs(i))
1412#elif SIZEOF_LONG_LONG == 8
1413#define i64abs(i) ((int64) llabs(i))
1414#else
1415#error "cannot find integer type of the same size as int64_t"
1416#endif
1417
1418/*
1419 * Use "extern PGDLLIMPORT ..." to declare variables that are defined
1420 * in the core backend and need to be accessible by loadable modules.
1421 * No special marking is required on most ports.
1422 */
1423#ifndef PGDLLIMPORT
1424#define PGDLLIMPORT
1425#endif
1426
1427/*
1428 * Use "extern PGDLLEXPORT ..." to declare functions that are defined in
1429 * loadable modules and need to be callable by the core backend or other
1430 * loadable modules.
1431 * If the compiler knows __attribute__((visibility("*"))), we use that,
1432 * unless we already have a platform-specific definition. Otherwise,
1433 * no special marking is required.
1434 */
1435#ifndef PGDLLEXPORT
1436#ifdef HAVE_VISIBILITY_ATTRIBUTE
1437#define PGDLLEXPORT __attribute__((visibility("default")))
1438#else
1439#define PGDLLEXPORT
1440#endif
1441#endif
1442
1443/*
1444 * Platform independent struct representing additional information about the
1445 * received signal. If the system does not support the extended information,
1446 * or a field does not apply to the signal, the value is instead reset to the
1447 * documented default value.
1448 */
1450typedef struct pg_signal_info
1451{
1452 uint32_t pid; /* pid of sending process or 0 if unknown */
1453 uint32_t uid; /* uid of sending process; only meaningful
1454 * when pid is not 0 */
1456
1457/*
1458 * The following is used as the arg list for signal handlers. These days we
1459 * use the same argument to all signal handlers and hide the difference
1460 * between platforms in wrapper functions.
1461 *
1462 * SIGNAL_ARGS just exists separately from the pqsignal() definition for
1463 * historical reasons.
1464 */
1465#define SIGNAL_ARGS int postgres_signal_arg, const pg_signal_info *pg_siginfo
1466
1467/*
1468 * When there is no sigsetjmp, its functionality is provided by plain
1469 * setjmp. We now support the case only on Windows. However, it seems
1470 * that MinGW-64 has some longstanding issues in its setjmp support,
1471 * so on that toolchain we cheat and use gcc's builtins.
1472 */
1473#ifdef WIN32
1474#ifdef __MINGW64__
1475typedef intptr_t sigjmp_buf[5];
1476#define sigsetjmp(x,y) __builtin_setjmp(x)
1477#define siglongjmp __builtin_longjmp
1478#else /* !__MINGW64__ */
1479#define sigjmp_buf jmp_buf
1480#define sigsetjmp(x,y) setjmp(x)
1481#define siglongjmp longjmp
1482#endif /* __MINGW64__ */
1483#endif /* WIN32 */
1484
1485/* /port compatibility functions */
1486#include "port.h"
1487
1488/*
1489 * char16_t and char32_t
1490 * Unicode code points.
1491 *
1492 * uchar.h should always be available in C11, but it's not available on
1493 * Mac. However, these types are keywords in C++11, so when using C++, we
1494 * can't redefine the types.
1495 *
1496 * XXX: when uchar.h is available everywhere, we can remove this check and
1497 * just include uchar.h unconditionally.
1498 *
1499 * XXX: this section is out of place because uchar.h needs to be included
1500 * after port.h, due to an interaction with win32_port.h in some cases.
1501 */
1502#ifdef HAVE_UCHAR_H
1503#include <uchar.h>
1504#else
1505#ifndef __cplusplus
1506typedef uint16_t char16_t;
1507typedef uint32_t char32_t;
1508#endif
1509#endif
1510
1511/* IWYU pragma: end_exports */
1512
1513#endif /* C_H */
static Datum values[MAXATTR]
Definition bootstrap.c:190
uint64 Oid8
Definition c.h:756
NameData * Name
Definition c.h:833
int fdatasync(int fd)
uint8_t uint8
Definition c.h:622
uint32 SubTransactionId
Definition c.h:740
#define pg_noreturn
Definition c.h:190
pg_noreturn void ExceptionalCondition(const char *conditionName, const char *fileName, int lineNumber)
Definition assert.c:30
varlena BpChar
Definition c.h:790
int64_t int64
Definition c.h:621
Oid regproc
Definition c.h:733
double float8
Definition c.h:714
TransactionId MultiXactId
Definition c.h:746
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:558
int16_t int16
Definition c.h:619
regproc RegProcedure
Definition c.h:734
int8_t int8
Definition c.h:618
uint64 MultiXactOffset
Definition c.h:748
int32_t int32
Definition c.h:620
uint64_t uint64
Definition c.h:625
varlena bytea
Definition c.h:788
uint16_t uint16
Definition c.h:623
uint32_t uint32
Definition c.h:624
uint16_t char16_t
Definition c.h:1503
unsigned int Index
Definition c.h:698
float float4
Definition c.h:713
uint32 LocalTransactionId
Definition c.h:738
void * Pointer
Definition c.h:615
uint32 CommandId
Definition c.h:750
uint32 TransactionId
Definition c.h:736
signed int Offset
Definition c.h:708
uint32_t char32_t
Definition c.h:1504
varlena VarChar
Definition c.h:791
varlena text
Definition c.h:789
void(* pg_funcptr_t)(void)
Definition c.h:548
size_t Size
Definition c.h:689
struct nameData NameData
#define NAMEDATALEN
#define PG_IO_ALIGN_SIZE
unsigned int Oid
static int fd(const char *x, int i)
static int fb(int x)
char data[BLCKSZ]
Definition c.h:1204
char data[XLOG_BLCKSZ]
Definition c.h:1231
char data[BLCKSZ]
Definition c.h:1225
int32 vl_len_
Definition c.h:805
int ndim
Definition c.h:806
int dim1
Definition c.h:809
Oid elemtype
Definition c.h:808
int32 dataoffset
Definition c.h:807
int lbound1
Definition c.h:810
Definition c.h:830
char data[NAMEDATALEN]
Definition c.h:831
Definition c.h:815
int dim1
Definition c.h:820
int32 dataoffset
Definition c.h:818
Oid elemtype
Definition c.h:819
int lbound1
Definition c.h:821
int ndim
Definition c.h:817
int32 vl_len_
Definition c.h:816
uint32_t pid
Definition c.h:1449
uint32_t uid
Definition c.h:1450
Definition c.h:776
char vl_len_[4]
Definition c.h:777
char vl_dat[FLEXIBLE_ARRAY_MEMBER]
Definition c.h:778