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