26
26
* section description
27
27
* ------- ------------------------------------------------
28
28
* 0) pg_config.h and standard system headers
29
- * 1) hacks to cope with non-ANSI C compilers
29
+ * 1) compiler characteristics
30
30
* 2) bool, true, false, TRUE, FALSE
31
31
* 3) standard system types
32
32
* 4) IsValid macros for system types
90
90
#include <stdint.h>
91
91
#endif
92
92
#include <sys/types.h>
93
-
94
93
#include <errno.h>
95
94
#if defined(WIN32 ) || defined(__CYGWIN__ )
96
95
#include <fcntl.h> /* ensure O_BINARY is available */
97
96
#endif
97
+ #include <locale.h>
98
+ #ifdef ENABLE_NLS
99
+ #include <libintl.h>
100
+ #endif
98
101
99
102
#if defined(WIN32 ) || defined(__CYGWIN__ )
100
103
/* We have to redefine some system functions after they are included above. */
101
104
#include "pg_config_os.h"
102
105
#endif
103
106
104
- /*
105
- * Force disable inlining if PG_FORCE_DISABLE_INLINE is defined. This is used
106
- * to work around compiler bugs and might also be useful for investigatory
107
- * purposes by defining the symbol in the platform's header..
107
+
108
+ /* ----------------------------------------------------------------
109
+ * Section 1: compiler characteristics
108
110
*
109
- * This is done early (in slightly the wrong section) as functionality later
110
- * in this file might want to rely on inline functions.
111
+ * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
112
+ * ----------------------------------------------------------------
113
+ */
114
+
115
+ /*
116
+ * Disable "inline" if PG_FORCE_DISABLE_INLINE is defined.
117
+ * This is used to work around compiler bugs and might also be useful for
118
+ * investigatory purposes.
111
119
*/
112
120
#ifdef PG_FORCE_DISABLE_INLINE
113
121
#undef inline
114
122
#define inline
115
123
#endif
116
124
117
- /* Must be before gettext() games below */
118
- #include <locale.h>
125
+ /*
126
+ * Attribute macros
127
+ *
128
+ * GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
129
+ * GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
130
+ * Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
131
+ * XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/function_attributes.html
132
+ * XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/type_attrib.html
133
+ */
119
134
120
- #define _ (x ) gettext(x)
135
+ /* only GCC supports the unused attribute */
136
+ #ifdef __GNUC__
137
+ #define pg_attribute_unused () __attribute__((unused))
138
+ #else
139
+ #define pg_attribute_unused ()
140
+ #endif
121
141
122
- #ifdef ENABLE_NLS
123
- #include <libintl.h>
142
+ /*
143
+ * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
144
+ * used in assert-enabled builds, to avoid compiler warnings about unused
145
+ * variables in assert-disabled builds.
146
+ */
147
+ #ifdef USE_ASSERT_CHECKING
148
+ #define PG_USED_FOR_ASSERTS_ONLY
124
149
#else
125
- #define gettext (x ) (x)
126
- #define dgettext (d ,x ) (x)
127
- #define ngettext (s ,p ,n ) ((n) == 1 ? (s) : (p))
128
- #define dngettext (d ,s ,p ,n ) ((n) == 1 ? (s) : (p))
150
+ #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
129
151
#endif
130
152
153
+ /* GCC and XLC support format attributes */
154
+ #if defined(__GNUC__ ) || defined(__IBMC__ )
155
+ #define pg_attribute_format_arg (a ) __attribute__((format_arg(a)))
156
+ #define pg_attribute_printf (f ,a ) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
157
+ #else
158
+ #define pg_attribute_format_arg (a )
159
+ #define pg_attribute_printf (f ,a )
160
+ #endif
161
+
162
+ /* GCC, Sunpro and XLC support aligned, packed and noreturn */
163
+ #if defined(__GNUC__ ) || defined(__SUNPRO_C ) || defined(__IBMC__ )
164
+ #define pg_attribute_aligned (a ) __attribute__((aligned(a)))
165
+ #define pg_attribute_noreturn () __attribute__((noreturn))
166
+ #define pg_attribute_packed () __attribute__((packed))
167
+ #define HAVE_PG_ATTRIBUTE_NORETURN 1
168
+ #else
131
169
/*
132
- * Use this to mark string constants as needing translation at some later
133
- * time, rather than immediately. This is useful for cases where you need
134
- * access to the original string and translated string, and for cases where
135
- * immediate translation is not possible, like when initializing global
136
- * variables.
137
- * http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
170
+ * NB: aligned and packed are not given default definitions because they
171
+ * affect code functionality; they *must* be implemented by the compiler
172
+ * if they are to be used.
138
173
*/
139
- #define gettext_noop (x ) (x)
174
+ #define pg_attribute_noreturn ()
175
+ #endif
140
176
177
+ /*
178
+ * Forcing a function not to be inlined can be useful if it's the slow path of
179
+ * a performance-critical function, or should be visible in profiles to allow
180
+ * for proper cost attribution. Note that unlike the pg_attribute_XXX macros
181
+ * above, this should be placed before the function's return type and name.
182
+ */
183
+ /* GCC, Sunpro and XLC support noinline via __attribute__ */
184
+ #if (defined(__GNUC__ ) && __GNUC__ > 2 ) || defined(__SUNPRO_C ) || defined(__IBMC__ )
185
+ #define pg_noinline __attribute__((noinline))
186
+ /* msvc via declspec */
187
+ #elif defined(_MSC_VER )
188
+ #define pg_noinline __declspec(noinline)
189
+ #else
190
+ #define pg_noinline
191
+ #endif
141
192
142
- /* ----------------------------------------------------------------
143
- * Section 1: hacks to cope with non-ANSI C compilers
193
+ /*
194
+ * Mark a point as unreachable in a portable fashion. This should preferably
195
+ * be something that the compiler understands, to aid code generation.
196
+ * In assert-enabled builds, we prefer abort() for debugging reasons.
197
+ */
198
+ #if defined(HAVE__BUILTIN_UNREACHABLE ) && !defined(USE_ASSERT_CHECKING )
199
+ #define pg_unreachable () __builtin_unreachable()
200
+ #elif defined(_MSC_VER ) && !defined(USE_ASSERT_CHECKING )
201
+ #define pg_unreachable () __assume(0)
202
+ #else
203
+ #define pg_unreachable () abort()
204
+ #endif
205
+
206
+ /*
207
+ * Hints to the compiler about the likelihood of a branch. Both likely() and
208
+ * unlikely() return the boolean value of the contained expression.
144
209
*
145
- * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
146
- * ----------------------------------------------------------------
210
+ * These should only be used sparingly, in very hot code paths. It's very easy
211
+ * to mis-estimate likelihoods.
147
212
*/
213
+ #if __GNUC__ >= 3
214
+ #define likely (x ) __builtin_expect((x) != 0, 1)
215
+ #define unlikely (x ) __builtin_expect((x) != 0, 0)
216
+ #else
217
+ #define likely (x ) ((x) != 0)
218
+ #define unlikely (x ) ((x) != 0)
219
+ #endif
148
220
149
221
/*
150
222
* CppAsString
183
255
#endif
184
256
#endif
185
257
258
+
186
259
/* ----------------------------------------------------------------
187
260
* Section 2: bool, true, false, TRUE, FALSE
188
261
* ----------------------------------------------------------------
@@ -209,6 +282,7 @@ typedef char bool;
209
282
#ifndef false
210
283
#define false ((bool) 0)
211
284
#endif
285
+
212
286
#endif /* not C++ */
213
287
214
288
#ifndef TRUE
@@ -492,16 +566,6 @@ typedef NameData *Name;
492
566
493
567
#define NameStr (name ) ((name).data)
494
568
495
- /*
496
- * Support macros for escaping strings. escape_backslash should be true
497
- * if generating a non-standard-conforming string. Prefixing a string
498
- * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
499
- * Beware of multiple evaluation of the "ch" argument!
500
- */
501
- #define SQL_STR_DOUBLE (ch , escape_backslash ) \
502
- ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
503
-
504
- #define ESCAPE_STRING_SYNTAX 'E'
505
569
506
570
/* ----------------------------------------------------------------
507
571
* Section 4: IsValid macros for system types
@@ -563,6 +627,9 @@ typedef NameData *Name;
563
627
*
564
628
* NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
565
629
* That case seems extremely unlikely to be needed in practice, however.
630
+ *
631
+ * NOTE: MAXIMUM_ALIGNOF, and hence MAXALIGN(), intentionally exclude any
632
+ * larger-than-8-byte types the compiler might have.
566
633
* ----------------
567
634
*/
568
635
@@ -600,64 +667,6 @@ typedef NameData *Name;
600
667
/* we don't currently need wider versions of the other ALIGN macros */
601
668
#define MAXALIGN64 (LEN ) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
602
669
603
- /* ----------------
604
- * Attribute macros
605
- *
606
- * GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
607
- * GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
608
- * Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
609
- * XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/function_attributes.html
610
- * XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/type_attrib.html
611
- * ----------------
612
- */
613
-
614
- /* only GCC supports the unused attribute */
615
- #ifdef __GNUC__
616
- #define pg_attribute_unused () __attribute__((unused))
617
- #else
618
- #define pg_attribute_unused ()
619
- #endif
620
-
621
- /* GCC and XLC support format attributes */
622
- #if defined(__GNUC__ ) || defined(__IBMC__ )
623
- #define pg_attribute_format_arg (a ) __attribute__((format_arg(a)))
624
- #define pg_attribute_printf (f ,a ) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
625
- #else
626
- #define pg_attribute_format_arg (a )
627
- #define pg_attribute_printf (f ,a )
628
- #endif
629
-
630
- /* GCC, Sunpro and XLC support aligned, packed and noreturn */
631
- #if defined(__GNUC__ ) || defined(__SUNPRO_C ) || defined(__IBMC__ )
632
- #define pg_attribute_aligned (a ) __attribute__((aligned(a)))
633
- #define pg_attribute_noreturn () __attribute__((noreturn))
634
- #define pg_attribute_packed () __attribute__((packed))
635
- #define HAVE_PG_ATTRIBUTE_NORETURN 1
636
- #else
637
- /*
638
- * NB: aligned and packed are not given default definitions because they
639
- * affect code functionality; they *must* be implemented by the compiler
640
- * if they are to be used.
641
- */
642
- #define pg_attribute_noreturn ()
643
- #endif
644
-
645
-
646
- /*
647
- * Forcing a function not to be inlined can be useful if it's the slow path of
648
- * a performance-critical function, or should be visible in profiles to allow
649
- * for proper cost attribution. Note that unlike the pg_attribute_XXX macros
650
- * above, this should be placed before the function's return type and name.
651
- */
652
- /* GCC, Sunpro and XLC support noinline via __attribute__ */
653
- #if (defined(__GNUC__ ) && __GNUC__ > 2 ) || defined(__SUNPRO_C ) || defined(__IBMC__ )
654
- #define pg_noinline __attribute__((noinline))
655
- /* msvc via declspec */
656
- #elif defined(_MSC_VER )
657
- #define pg_noinline __declspec(noinline)
658
- #else
659
- #define pg_noinline
660
- #endif
661
670
662
671
/* ----------------------------------------------------------------
663
672
* Section 6: assertions
@@ -694,6 +703,7 @@ typedef NameData *Name;
694
703
#define AssertArg (condition ) assert(condition)
695
704
#define AssertState (condition ) assert(condition)
696
705
#define AssertPointerAlignment (ptr , bndr ) ((void)true)
706
+
697
707
#else /* USE_ASSERT_CHECKING && !FRONTEND */
698
708
699
709
/*
@@ -939,36 +949,6 @@ typedef NameData *Name;
939
949
} while (0)
940
950
941
951
942
- /*
943
- * Mark a point as unreachable in a portable fashion. This should preferably
944
- * be something that the compiler understands, to aid code generation.
945
- * In assert-enabled builds, we prefer abort() for debugging reasons.
946
- */
947
- #if defined(HAVE__BUILTIN_UNREACHABLE ) && !defined(USE_ASSERT_CHECKING )
948
- #define pg_unreachable () __builtin_unreachable()
949
- #elif defined(_MSC_VER ) && !defined(USE_ASSERT_CHECKING )
950
- #define pg_unreachable () __assume(0)
951
- #else
952
- #define pg_unreachable () abort()
953
- #endif
954
-
955
-
956
- /*
957
- * Hints to the compiler about the likelihood of a branch. Both likely() and
958
- * unlikely() return the boolean value of the contained expression.
959
- *
960
- * These should only be used sparingly, in very hot code paths. It's very easy
961
- * to mis-estimate likelihoods.
962
- */
963
- #if __GNUC__ >= 3
964
- #define likely (x ) __builtin_expect((x) != 0, 1)
965
- #define unlikely (x ) __builtin_expect((x) != 0, 0)
966
- #else
967
- #define likely (x ) ((x) != 0)
968
- #define unlikely (x ) ((x) != 0)
969
- #endif
970
-
971
-
972
952
/* ----------------------------------------------------------------
973
953
* Section 8: random stuff
974
954
* ----------------------------------------------------------------
@@ -978,26 +958,47 @@ typedef NameData *Name;
978
958
#define HIGHBIT (0x80)
979
959
#define IS_HIGHBIT_SET (ch ) ((unsigned char)(ch) & HIGHBIT)
980
960
961
+ /*
962
+ * Support macros for escaping strings. escape_backslash should be true
963
+ * if generating a non-standard-conforming string. Prefixing a string
964
+ * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
965
+ * Beware of multiple evaluation of the "ch" argument!
966
+ */
967
+ #define SQL_STR_DOUBLE (ch , escape_backslash ) \
968
+ ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
969
+
970
+ #define ESCAPE_STRING_SYNTAX 'E'
971
+
972
+
981
973
#define STATUS_OK (0)
982
974
#define STATUS_ERROR (-1)
983
975
#define STATUS_EOF (-2)
984
976
#define STATUS_FOUND (1)
985
977
#define STATUS_WAITING (2)
986
978
987
-
988
979
/*
989
- * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
990
- * used in assert-enabled builds, to avoid compiler warnings about unused
991
- * variables in assert-disabled builds.
980
+ * gettext support
992
981
*/
993
- #ifdef USE_ASSERT_CHECKING
994
- #define PG_USED_FOR_ASSERTS_ONLY
995
- #else
996
- #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
982
+
983
+ #ifndef ENABLE_NLS
984
+ /* stuff we'd otherwise get from <libintl.h> */
985
+ #define gettext (x ) (x)
986
+ #define dgettext (d ,x ) (x)
987
+ #define ngettext (s ,p ,n ) ((n) == 1 ? (s) : (p))
988
+ #define dngettext (d ,s ,p ,n ) ((n) == 1 ? (s) : (p))
997
989
#endif
998
990
991
+ #define _ (x ) gettext(x)
999
992
1000
- /* gettext domain name mangling */
993
+ /*
994
+ * Use this to mark string constants as needing translation at some later
995
+ * time, rather than immediately. This is useful for cases where you need
996
+ * access to the original string and translated string, and for cases where
997
+ * immediate translation is not possible, like when initializing global
998
+ * variables.
999
+ * http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
1000
+ */
1001
+ #define gettext_noop (x ) (x)
1001
1002
1002
1003
/*
1003
1004
* To better support parallel installations of major PostgreSQL
0 commit comments