c.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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-2022, PostgreSQL Global Development Group
  13. * Portions Copyright (c) 1994, Regents of the University of California
  14. *
  15. * src/include/c.h
  16. *
  17. *-------------------------------------------------------------------------
  18. */
  19. /*
  20. *----------------------------------------------------------------
  21. * TABLE OF CONTENTS
  22. *
  23. * When adding stuff to this file, please try to put stuff
  24. * into the relevant section, or add new sections as appropriate.
  25. *
  26. * section description
  27. * ------- ------------------------------------------------
  28. * 0) pg_config.h and standard system headers
  29. * 1) compiler characteristics
  30. * 2) bool, true, false
  31. * 3) standard system types
  32. * 4) IsValid macros for system types
  33. * 5) offsetof, lengthof, alignment
  34. * 6) assertions
  35. * 7) widely useful macros
  36. * 8) random stuff
  37. * 9) system-specific hacks
  38. *
  39. * NOTE: since this file is included by both frontend and backend modules,
  40. * it's usually wrong to put an "extern" declaration here, unless it's
  41. * ifdef'd so that it's seen in only one case or the other.
  42. * typedefs and macros are the kind of thing that might go here.
  43. *
  44. *----------------------------------------------------------------
  45. */
  46. #ifndef C_H
  47. #define C_H
  48. #include "postgres_ext.h"
  49. /* Must undef pg_config_ext.h symbols before including pg_config.h */
  50. #undef PG_INT64_TYPE
  51. #include "pg_config.h"
  52. #include "pg_config_manual.h" /* must be after pg_config.h */
  53. #include "pg_config_os.h" /* must be before any system header files */
  54. /* System header files that should be available everywhere in Postgres */
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <stddef.h>
  59. #include <stdarg.h>
  60. #ifdef HAVE_STRINGS_H
  61. #include <strings.h>
  62. #endif
  63. #include <stdint.h>
  64. #include <sys/types.h>
  65. #include <errno.h>
  66. #if defined(WIN32) || defined(__CYGWIN__)
  67. #include <fcntl.h> /* ensure O_BINARY is available */
  68. #endif
  69. #include <locale.h>
  70. #ifdef ENABLE_NLS
  71. #include <libintl.h>
  72. #endif
  73. /* ----------------------------------------------------------------
  74. * Section 1: compiler characteristics
  75. *
  76. * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
  77. * ----------------------------------------------------------------
  78. */
  79. /*
  80. * Disable "inline" if PG_FORCE_DISABLE_INLINE is defined.
  81. * This is used to work around compiler bugs and might also be useful for
  82. * investigatory purposes.
  83. */
  84. #ifdef PG_FORCE_DISABLE_INLINE
  85. #undef inline
  86. #define inline
  87. #endif
  88. /*
  89. * Attribute macros
  90. *
  91. * GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
  92. * GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
  93. * Clang: https://clang.llvm.org/docs/AttributeReference.html
  94. * Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
  95. * XLC: https://www.ibm.com/support/knowledgecenter/SSGH2K_13.1.2/com.ibm.xlc131.aix.doc/language_ref/function_attributes.html
  96. * XLC: https://www.ibm.com/support/knowledgecenter/SSGH2K_13.1.2/com.ibm.xlc131.aix.doc/language_ref/type_attrib.html
  97. */
  98. /*
  99. * For compilers which don't support __has_attribute, we just define
  100. * __has_attribute(x) to 0 so that we can define macros for various
  101. * __attribute__s more easily below.
  102. */
  103. #ifndef __has_attribute
  104. #define __has_attribute(attribute) 0
  105. #endif
  106. /* only GCC supports the unused attribute */
  107. #ifdef __GNUC__
  108. #define pg_attribute_unused() __attribute__((unused))
  109. #else
  110. #define pg_attribute_unused()
  111. #endif
  112. /*
  113. * pg_nodiscard means the compiler should warn if the result of a function
  114. * call is ignored. The name "nodiscard" is chosen in alignment with
  115. * (possibly future) C and C++ standards. For maximum compatibility, use it
  116. * as a function declaration specifier, so it goes before the return type.
  117. */
  118. #ifdef __GNUC__
  119. #define pg_nodiscard __attribute__((warn_unused_result))
  120. #else
  121. #define pg_nodiscard
  122. #endif
  123. /*
  124. * Place this macro before functions that should be allowed to make misaligned
  125. * accesses. Think twice before using it on non-x86-specific code!
  126. * Testing can be done with "-fsanitize=alignment -fsanitize-trap=alignment"
  127. * on clang, or "-fsanitize=alignment -fno-sanitize-recover=alignment" on gcc.
  128. */
  129. #if __clang_major__ >= 7 || __GNUC__ >= 8
  130. #define pg_attribute_no_sanitize_alignment() __attribute__((no_sanitize("alignment")))
  131. #else
  132. #define pg_attribute_no_sanitize_alignment()
  133. #endif
  134. /*
  135. * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
  136. * used in assert-enabled builds, to avoid compiler warnings about unused
  137. * variables in assert-disabled builds.
  138. */
  139. #ifdef USE_ASSERT_CHECKING
  140. #define PG_USED_FOR_ASSERTS_ONLY
  141. #else
  142. #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
  143. #endif
  144. /* GCC and XLC support format attributes */
  145. #if defined(__GNUC__) || defined(__IBMC__)
  146. #define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
  147. #define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
  148. #else
  149. #define pg_attribute_format_arg(a)
  150. #define pg_attribute_printf(f,a)
  151. #endif
  152. /* GCC, Sunpro and XLC support aligned, packed and noreturn */
  153. #if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
  154. #define pg_attribute_aligned(a) __attribute__((aligned(a)))
  155. #define pg_attribute_noreturn() __attribute__((noreturn))
  156. #define pg_attribute_packed() __attribute__((packed))
  157. #define HAVE_PG_ATTRIBUTE_NORETURN 1
  158. #else
  159. /*
  160. * NB: aligned and packed are not given default definitions because they
  161. * affect code functionality; they *must* be implemented by the compiler
  162. * if they are to be used.
  163. */
  164. #define pg_attribute_noreturn()
  165. #endif
  166. /*
  167. * Use "pg_attribute_always_inline" in place of "inline" for functions that
  168. * we wish to force inlining of, even when the compiler's heuristics would
  169. * choose not to. But, if possible, don't force inlining in unoptimized
  170. * debug builds.
  171. */
  172. #if (defined(__GNUC__) && __GNUC__ > 3 && defined(__OPTIMIZE__)) || defined(__SUNPRO_C) || defined(__IBMC__)
  173. /* GCC > 3, Sunpro and XLC support always_inline via __attribute__ */
  174. #define pg_attribute_always_inline __attribute__((always_inline)) inline
  175. #elif defined(_MSC_VER)
  176. /* MSVC has a special keyword for this */
  177. #define pg_attribute_always_inline __forceinline
  178. #else
  179. /* Otherwise, the best we can do is to say "inline" */
  180. #define pg_attribute_always_inline inline
  181. #endif
  182. /*
  183. * Forcing a function not to be inlined can be useful if it's the slow path of
  184. * a performance-critical function, or should be visible in profiles to allow
  185. * for proper cost attribution. Note that unlike the pg_attribute_XXX macros
  186. * above, this should be placed before the function's return type and name.
  187. */
  188. /* GCC, Sunpro and XLC support noinline via __attribute__ */
  189. #if (defined(__GNUC__) && __GNUC__ > 2) || defined(__SUNPRO_C) || defined(__IBMC__)
  190. #define pg_noinline __attribute__((noinline))
  191. /* msvc via declspec */
  192. #elif defined(_MSC_VER)
  193. #define pg_noinline __declspec(noinline)
  194. #else
  195. #define pg_noinline
  196. #endif
  197. /*
  198. * For now, just define pg_attribute_cold and pg_attribute_hot to be empty
  199. * macros on minGW 8.1. There appears to be a compiler bug that results in
  200. * compilation failure. At this time, we still have at least one buildfarm
  201. * animal running that compiler, so this should make that green again. It's
  202. * likely this compiler is not popular enough to warrant keeping this code
  203. * around forever, so let's just remove it once the last buildfarm animal
  204. * upgrades.
  205. */
  206. #if defined(__MINGW64__) && __GNUC__ == 8 && __GNUC_MINOR__ == 1
  207. #define pg_attribute_cold
  208. #define pg_attribute_hot
  209. #else
  210. /*
  211. * Marking certain functions as "hot" or "cold" can be useful to assist the
  212. * compiler in arranging the assembly code in a more efficient way.
  213. */
  214. #if __has_attribute (cold)
  215. #define pg_attribute_cold __attribute__((cold))
  216. #else
  217. #define pg_attribute_cold
  218. #endif
  219. #if __has_attribute (hot)
  220. #define pg_attribute_hot __attribute__((hot))
  221. #else
  222. #define pg_attribute_hot
  223. #endif
  224. #endif /* defined(__MINGW64__) && __GNUC__ == 8 &&
  225. * __GNUC_MINOR__ == 1 */
  226. /*
  227. * Mark a point as unreachable in a portable fashion. This should preferably
  228. * be something that the compiler understands, to aid code generation.
  229. * In assert-enabled builds, we prefer abort() for debugging reasons.
  230. */
  231. #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
  232. #define pg_unreachable() __builtin_unreachable()
  233. #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
  234. #define pg_unreachable() __assume(0)
  235. #else
  236. #define pg_unreachable() abort()
  237. #endif
  238. /*
  239. * Hints to the compiler about the likelihood of a branch. Both likely() and
  240. * unlikely() return the boolean value of the contained expression.
  241. *
  242. * These should only be used sparingly, in very hot code paths. It's very easy
  243. * to mis-estimate likelihoods.
  244. */
  245. #if __GNUC__ >= 3
  246. #define likely(x) __builtin_expect((x) != 0, 1)
  247. #define unlikely(x) __builtin_expect((x) != 0, 0)
  248. #else
  249. #define likely(x) ((x) != 0)
  250. #define unlikely(x) ((x) != 0)
  251. #endif
  252. /*
  253. * CppAsString
  254. * Convert the argument to a string, using the C preprocessor.
  255. * CppAsString2
  256. * Convert the argument to a string, after one round of macro expansion.
  257. * CppConcat
  258. * Concatenate two arguments together, using the C preprocessor.
  259. *
  260. * Note: There used to be support here for pre-ANSI C compilers that didn't
  261. * support # and ##. Nowadays, these macros are just for clarity and/or
  262. * backward compatibility with existing PostgreSQL code.
  263. */
  264. #define CppAsString(identifier) #identifier
  265. #define CppAsString2(x) CppAsString(x)
  266. #define CppConcat(x, y) x##y
  267. /*
  268. * VA_ARGS_NARGS
  269. * Returns the number of macro arguments it is passed.
  270. *
  271. * An empty argument still counts as an argument, so effectively, this is
  272. * "one more than the number of commas in the argument list".
  273. *
  274. * This works for up to 63 arguments. Internally, VA_ARGS_NARGS_() is passed
  275. * 64+N arguments, and the C99 standard only requires macros to allow up to
  276. * 127 arguments, so we can't portably go higher. The implementation is
  277. * pretty trivial: VA_ARGS_NARGS_() returns its 64th argument, and we set up
  278. * the call so that that is the appropriate one of the list of constants.
  279. * This idea is due to Laurent Deniau.
  280. */
  281. #define VA_ARGS_NARGS(...) \
  282. VA_ARGS_NARGS_(__VA_ARGS__, \
  283. 63,62,61,60, \
  284. 59,58,57,56,55,54,53,52,51,50, \
  285. 49,48,47,46,45,44,43,42,41,40, \
  286. 39,38,37,36,35,34,33,32,31,30, \
  287. 29,28,27,26,25,24,23,22,21,20, \
  288. 19,18,17,16,15,14,13,12,11,10, \
  289. 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  290. #define VA_ARGS_NARGS_( \
  291. _01,_02,_03,_04,_05,_06,_07,_08,_09,_10, \
  292. _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
  293. _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
  294. _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
  295. _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
  296. _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
  297. _61,_62,_63, N, ...) \
  298. (N)
  299. /*
  300. * dummyret is used to set return values in macros that use ?: to make
  301. * assignments. gcc wants these to be void, other compilers like char
  302. */
  303. #ifdef __GNUC__ /* GNU cc */
  304. #define dummyret void
  305. #else
  306. #define dummyret char
  307. #endif
  308. /*
  309. * Generic function pointer. This can be used in the rare cases where it's
  310. * necessary to cast a function pointer to a seemingly incompatible function
  311. * pointer type while avoiding gcc's -Wcast-function-type warnings.
  312. */
  313. typedef void (*pg_funcptr_t) (void);
  314. /*
  315. * We require C99, hence the compiler should understand flexible array
  316. * members. However, for documentation purposes we still consider it to be
  317. * project style to write "field[FLEXIBLE_ARRAY_MEMBER]" not just "field[]".
  318. * When computing the size of such an object, use "offsetof(struct s, f)"
  319. * for portability. Don't use "offsetof(struct s, f[0])", as this doesn't
  320. * work with MSVC and with C++ compilers.
  321. */
  322. #define FLEXIBLE_ARRAY_MEMBER /* empty */
  323. /* Which __func__ symbol do we have, if any? */
  324. #ifdef HAVE_FUNCNAME__FUNC
  325. #define PG_FUNCNAME_MACRO __func__
  326. #else
  327. #ifdef HAVE_FUNCNAME__FUNCTION
  328. #define PG_FUNCNAME_MACRO __FUNCTION__
  329. #else
  330. #define PG_FUNCNAME_MACRO NULL
  331. #endif
  332. #endif
  333. /*
  334. * Does the compiler support #pragma GCC system_header? We optionally use it
  335. * to avoid warnings that we can't fix (e.g. in the perl headers).
  336. * See https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html
  337. *
  338. * Headers for which we do not want to show compiler warnings can,
  339. * conditionally, use #pragma GCC system_header to avoid warnings. Obviously
  340. * this should only be used for external headers over which we do not have
  341. * control.
  342. *
  343. * Support for the pragma is tested here, instead of during configure, as gcc
  344. * also warns about the pragma being used in a .c file. It's surprisingly hard
  345. * to get autoconf to use .h as the file-ending. Looks like gcc has
  346. * implemented the pragma since the 2000, so this test should suffice.
  347. *
  348. *
  349. * Alternatively, we could add the include paths for problematic headers with
  350. * -isystem, but that is a larger hammer and is harder to search for.
  351. *
  352. * A more granular alternative would be to use #pragma GCC diagnostic
  353. * push/ignored/pop, but gcc warns about unknown warnings being ignored, so
  354. * every to-be-ignored-temporarily compiler warning would require its own
  355. * pg_config.h symbol and #ifdef.
  356. */
  357. #ifdef __GNUC__
  358. #define HAVE_PRAGMA_GCC_SYSTEM_HEADER 1
  359. #endif
  360. /* ----------------------------------------------------------------
  361. * Section 2: bool, true, false
  362. * ----------------------------------------------------------------
  363. */
  364. /*
  365. * bool
  366. * Boolean value, either true or false.
  367. *
  368. * We use stdbool.h if available and its bool has size 1. That's useful for
  369. * better compiler and debugger output and for compatibility with third-party
  370. * libraries. But PostgreSQL currently cannot deal with bool of other sizes;
  371. * there are static assertions around the code to prevent that.
  372. *
  373. * For C++ compilers, we assume the compiler has a compatible built-in
  374. * definition of bool.
  375. *
  376. * See also the version of this code in src/interfaces/ecpg/include/ecpglib.h.
  377. */
  378. #ifndef __cplusplus
  379. #ifdef PG_USE_STDBOOL
  380. #include <stdbool.h>
  381. #else
  382. #ifndef bool
  383. typedef unsigned char bool;
  384. #endif
  385. #ifndef true
  386. #define true ((bool) 1)
  387. #endif
  388. #ifndef false
  389. #define false ((bool) 0)
  390. #endif
  391. #endif /* not PG_USE_STDBOOL */
  392. #endif /* not C++ */
  393. /* ----------------------------------------------------------------
  394. * Section 3: standard system types
  395. * ----------------------------------------------------------------
  396. */
  397. /*
  398. * Pointer
  399. * Variable holding address of any memory resident object.
  400. *
  401. * XXX Pointer arithmetic is done with this, so it can't be void *
  402. * under "true" ANSI compilers.
  403. */
  404. typedef char *Pointer;
  405. /*
  406. * intN
  407. * Signed integer, EXACTLY N BITS IN SIZE,
  408. * used for numerical computations and the
  409. * frontend/backend protocol.
  410. */
  411. #ifndef HAVE_INT8
  412. typedef signed char int8; /* == 8 bits */
  413. typedef signed short int16; /* == 16 bits */
  414. typedef signed int int32; /* == 32 bits */
  415. #endif /* not HAVE_INT8 */
  416. /*
  417. * uintN
  418. * Unsigned integer, EXACTLY N BITS IN SIZE,
  419. * used for numerical computations and the
  420. * frontend/backend protocol.
  421. */
  422. #ifndef HAVE_UINT8
  423. typedef unsigned char uint8; /* == 8 bits */
  424. typedef unsigned short uint16; /* == 16 bits */
  425. typedef unsigned int uint32; /* == 32 bits */
  426. #endif /* not HAVE_UINT8 */
  427. /*
  428. * bitsN
  429. * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
  430. */
  431. typedef uint8 bits8; /* >= 8 bits */
  432. typedef uint16 bits16; /* >= 16 bits */
  433. typedef uint32 bits32; /* >= 32 bits */
  434. /*
  435. * 64-bit integers
  436. */
  437. #ifdef HAVE_LONG_INT_64
  438. /* Plain "long int" fits, use it */
  439. #ifndef HAVE_INT64
  440. typedef long int int64;
  441. #endif
  442. #ifndef HAVE_UINT64
  443. typedef unsigned long int uint64;
  444. #endif
  445. #define INT64CONST(x) (x##L)
  446. #define UINT64CONST(x) (x##UL)
  447. #elif defined(HAVE_LONG_LONG_INT_64)
  448. /* We have working support for "long long int", use that */
  449. #ifndef HAVE_INT64
  450. typedef long long int int64;
  451. #endif
  452. #ifndef HAVE_UINT64
  453. typedef unsigned long long int uint64;
  454. #endif
  455. #define INT64CONST(x) (x##LL)
  456. #define UINT64CONST(x) (x##ULL)
  457. #else
  458. /* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */
  459. #error must have a working 64-bit integer datatype
  460. #endif
  461. /* snprintf format strings to use for 64-bit integers */
  462. #define INT64_FORMAT "%" INT64_MODIFIER "d"
  463. #define UINT64_FORMAT "%" INT64_MODIFIER "u"
  464. /*
  465. * 128-bit signed and unsigned integers
  466. * There currently is only limited support for such types.
  467. * E.g. 128bit literals and snprintf are not supported; but math is.
  468. * Also, because we exclude such types when choosing MAXIMUM_ALIGNOF,
  469. * it must be possible to coerce the compiler to allocate them on no
  470. * more than MAXALIGN boundaries.
  471. */
  472. #if defined(PG_INT128_TYPE)
  473. #if defined(pg_attribute_aligned) || ALIGNOF_PG_INT128_TYPE <= MAXIMUM_ALIGNOF
  474. #define HAVE_INT128 1
  475. typedef PG_INT128_TYPE int128
  476. #if defined(pg_attribute_aligned)
  477. pg_attribute_aligned(MAXIMUM_ALIGNOF)
  478. #endif
  479. ;
  480. typedef unsigned PG_INT128_TYPE uint128
  481. #if defined(pg_attribute_aligned)
  482. pg_attribute_aligned(MAXIMUM_ALIGNOF)
  483. #endif
  484. ;
  485. #endif
  486. #endif
  487. /*
  488. * stdint.h limits aren't guaranteed to have compatible types with our fixed
  489. * width types. So just define our own.
  490. */
  491. #define PG_INT8_MIN (-0x7F-1)
  492. #define PG_INT8_MAX (0x7F)
  493. #define PG_UINT8_MAX (0xFF)
  494. #define PG_INT16_MIN (-0x7FFF-1)
  495. #define PG_INT16_MAX (0x7FFF)
  496. #define PG_UINT16_MAX (0xFFFF)
  497. #define PG_INT32_MIN (-0x7FFFFFFF-1)
  498. #define PG_INT32_MAX (0x7FFFFFFF)
  499. #define PG_UINT32_MAX (0xFFFFFFFFU)
  500. #define PG_INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
  501. #define PG_INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
  502. #define PG_UINT64_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
  503. /*
  504. * We now always use int64 timestamps, but keep this symbol defined for the
  505. * benefit of external code that might test it.
  506. */
  507. #define HAVE_INT64_TIMESTAMP
  508. /*
  509. * Size
  510. * Size of any memory resident object, as returned by sizeof.
  511. */
  512. typedef size_t Size;
  513. /*
  514. * Index
  515. * Index into any memory resident array.
  516. *
  517. * Note:
  518. * Indices are non negative.
  519. */
  520. typedef unsigned int Index;
  521. /*
  522. * Offset
  523. * Offset into any memory resident array.
  524. *
  525. * Note:
  526. * This differs from an Index in that an Index is always
  527. * non negative, whereas Offset may be negative.
  528. */
  529. typedef signed int Offset;
  530. /*
  531. * Common Postgres datatype names (as used in the catalogs)
  532. */
  533. typedef float float4;
  534. typedef double float8;
  535. #ifdef USE_FLOAT8_BYVAL
  536. #define FLOAT8PASSBYVAL true
  537. #else
  538. #define FLOAT8PASSBYVAL false
  539. #endif
  540. /*
  541. * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
  542. * CommandId
  543. */
  544. /* typedef Oid is in postgres_ext.h */
  545. /*
  546. * regproc is the type name used in the include/catalog headers, but
  547. * RegProcedure is the preferred name in C code.
  548. */
  549. typedef Oid regproc;
  550. typedef regproc RegProcedure;
  551. typedef uint32 TransactionId;
  552. typedef uint32 LocalTransactionId;
  553. typedef uint32 SubTransactionId;
  554. #define InvalidSubTransactionId ((SubTransactionId) 0)
  555. #define TopSubTransactionId ((SubTransactionId) 1)
  556. /* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
  557. typedef TransactionId MultiXactId;
  558. typedef uint32 MultiXactOffset;
  559. typedef uint32 CommandId;
  560. #define FirstCommandId ((CommandId) 0)
  561. #define InvalidCommandId (~(CommandId)0)
  562. /* ----------------
  563. * Variable-length datatypes all share the 'struct varlena' header.
  564. *
  565. * NOTE: for TOASTable types, this is an oversimplification, since the value
  566. * may be compressed or moved out-of-line. However datatype-specific routines
  567. * are mostly content to deal with de-TOASTed values only, and of course
  568. * client-side routines should never see a TOASTed value. But even in a
  569. * de-TOASTed value, beware of touching vl_len_ directly, as its
  570. * representation is no longer convenient. It's recommended that code always
  571. * use macros VARDATA_ANY, VARSIZE_ANY, VARSIZE_ANY_EXHDR, VARDATA, VARSIZE,
  572. * and SET_VARSIZE instead of relying on direct mentions of the struct fields.
  573. * See postgres.h for details of the TOASTed form.
  574. * ----------------
  575. */
  576. struct varlena
  577. {
  578. char vl_len_[4]; /* Do not touch this field directly! */
  579. char vl_dat[FLEXIBLE_ARRAY_MEMBER]; /* Data content is here */
  580. };
  581. #define VARHDRSZ ((int32) sizeof(int32))
  582. /*
  583. * These widely-used datatypes are just a varlena header and the data bytes.
  584. * There is no terminating null or anything like that --- the data length is
  585. * always VARSIZE_ANY_EXHDR(ptr).
  586. */
  587. typedef struct varlena bytea;
  588. typedef struct varlena text;
  589. typedef struct varlena BpChar; /* blank-padded char, ie SQL char(n) */
  590. typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
  591. /*
  592. * Specialized array types. These are physically laid out just the same
  593. * as regular arrays (so that the regular array subscripting code works
  594. * with them). They exist as distinct types mostly for historical reasons:
  595. * they have nonstandard I/O behavior which we don't want to change for fear
  596. * of breaking applications that look at the system catalogs. There is also
  597. * an implementation issue for oidvector: it's part of the primary key for
  598. * pg_proc, and we can't use the normal btree array support routines for that
  599. * without circularity.
  600. */
  601. typedef struct
  602. {
  603. int32 vl_len_; /* these fields must match ArrayType! */
  604. int ndim; /* always 1 for int2vector */
  605. int32 dataoffset; /* always 0 for int2vector */
  606. Oid elemtype;
  607. int dim1;
  608. int lbound1;
  609. int16 values[FLEXIBLE_ARRAY_MEMBER];
  610. } int2vector;
  611. typedef struct
  612. {
  613. int32 vl_len_; /* these fields must match ArrayType! */
  614. int ndim; /* always 1 for oidvector */
  615. int32 dataoffset; /* always 0 for oidvector */
  616. Oid elemtype;
  617. int dim1;
  618. int lbound1;
  619. Oid values[FLEXIBLE_ARRAY_MEMBER];
  620. } oidvector;
  621. /*
  622. * Representation of a Name: effectively just a C string, but null-padded to
  623. * exactly NAMEDATALEN bytes. The use of a struct is historical.
  624. */
  625. typedef struct nameData
  626. {
  627. char data[NAMEDATALEN];
  628. } NameData;
  629. typedef NameData *Name;
  630. #define NameStr(name) ((name).data)
  631. /* ----------------------------------------------------------------
  632. * Section 4: IsValid macros for system types
  633. * ----------------------------------------------------------------
  634. */
  635. /*
  636. * BoolIsValid
  637. * True iff bool is valid.
  638. */
  639. #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
  640. /*
  641. * PointerIsValid
  642. * True iff pointer is valid.
  643. */
  644. #define PointerIsValid(pointer) ((const void*)(pointer) != NULL)
  645. /*
  646. * PointerIsAligned
  647. * True iff pointer is properly aligned to point to the given type.
  648. */
  649. #define PointerIsAligned(pointer, type) \
  650. (((uintptr_t)(pointer) % (sizeof (type))) == 0)
  651. #define OffsetToPointer(base, offset) \
  652. ((void *)((char *) base + offset))
  653. #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
  654. #define RegProcedureIsValid(p) OidIsValid(p)
  655. /* ----------------------------------------------------------------
  656. * Section 5: offsetof, lengthof, alignment
  657. * ----------------------------------------------------------------
  658. */
  659. /*
  660. * offsetof
  661. * Offset of a structure/union field within that structure/union.
  662. *
  663. * XXX This is supposed to be part of stddef.h, but isn't on
  664. * some systems (like SunOS 4).
  665. */
  666. #ifndef offsetof
  667. #define offsetof(type, field) ((long) &((type *)0)->field)
  668. #endif /* offsetof */
  669. /*
  670. * lengthof
  671. * Number of elements in an array.
  672. */
  673. #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
  674. /* ----------------
  675. * Alignment macros: align a length or address appropriately for a given type.
  676. * The fooALIGN() macros round up to a multiple of the required alignment,
  677. * while the fooALIGN_DOWN() macros round down. The latter are more useful
  678. * for problems like "how many X-sized structures will fit in a page?".
  679. *
  680. * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
  681. * That case seems extremely unlikely to be needed in practice, however.
  682. *
  683. * NOTE: MAXIMUM_ALIGNOF, and hence MAXALIGN(), intentionally exclude any
  684. * larger-than-8-byte types the compiler might have.
  685. * ----------------
  686. */
  687. #define TYPEALIGN(ALIGNVAL,LEN) \
  688. (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
  689. #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
  690. #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
  691. #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
  692. #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
  693. #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
  694. /* MAXALIGN covers only built-in types, not buffers */
  695. #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
  696. #define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
  697. #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
  698. (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
  699. #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
  700. #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
  701. #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
  702. #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
  703. #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
  704. #define BUFFERALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_BUFFER, (LEN))
  705. /*
  706. * The above macros will not work with types wider than uintptr_t, like with
  707. * uint64 on 32-bit platforms. That's not problem for the usual use where a
  708. * pointer or a length is aligned, but for the odd case that you need to
  709. * align something (potentially) wider, use TYPEALIGN64.
  710. */
  711. #define TYPEALIGN64(ALIGNVAL,LEN) \
  712. (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
  713. /* we don't currently need wider versions of the other ALIGN macros */
  714. #define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
  715. /* ----------------------------------------------------------------
  716. * Section 6: assertions
  717. * ----------------------------------------------------------------
  718. */
  719. /*
  720. * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
  721. * - plai 9/5/90
  722. *
  723. * It should _NOT_ be defined in releases or in benchmark copies
  724. */
  725. /*
  726. * Assert() can be used in both frontend and backend code. In frontend code it
  727. * just calls the standard assert, if it's available. If use of assertions is
  728. * not configured, it does nothing.
  729. */
  730. #ifndef USE_ASSERT_CHECKING
  731. #define Assert(condition) ((void)true)
  732. #define AssertMacro(condition) ((void)true)
  733. #define AssertArg(condition) ((void)true)
  734. #define AssertState(condition) ((void)true)
  735. #define AssertPointerAlignment(ptr, bndr) ((void)true)
  736. #define Trap(condition, errorType) ((void)true)
  737. #define TrapMacro(condition, errorType) (true)
  738. #elif defined(FRONTEND)
  739. #include <assert.h>
  740. #define Assert(p) assert(p)
  741. #define AssertMacro(p) ((void) assert(p))
  742. #define AssertArg(condition) assert(condition)
  743. #define AssertState(condition) assert(condition)
  744. #define AssertPointerAlignment(ptr, bndr) ((void)true)
  745. #else /* USE_ASSERT_CHECKING && !FRONTEND */
  746. /*
  747. * Trap
  748. * Generates an exception if the given condition is true.
  749. */
  750. #define Trap(condition, errorType) \
  751. do { \
  752. if (condition) \
  753. ExceptionalCondition(#condition, (errorType), \
  754. __FILE__, __LINE__); \
  755. } while (0)
  756. /*
  757. * TrapMacro is the same as Trap but it's intended for use in macros:
  758. *
  759. * #define foo(x) (AssertMacro(x != 0), bar(x))
  760. *
  761. * Isn't CPP fun?
  762. */
  763. #define TrapMacro(condition, errorType) \
  764. ((bool) (! (condition) || \
  765. (ExceptionalCondition(#condition, (errorType), \
  766. __FILE__, __LINE__), 0)))
  767. #define Assert(condition) \
  768. do { \
  769. if (!(condition)) \
  770. ExceptionalCondition(#condition, "FailedAssertion", \
  771. __FILE__, __LINE__); \
  772. } while (0)
  773. #define AssertMacro(condition) \
  774. ((void) ((condition) || \
  775. (ExceptionalCondition(#condition, "FailedAssertion", \
  776. __FILE__, __LINE__), 0)))
  777. #define AssertArg(condition) \
  778. do { \
  779. if (!(condition)) \
  780. ExceptionalCondition(#condition, "BadArgument", \
  781. __FILE__, __LINE__); \
  782. } while (0)
  783. #define AssertState(condition) \
  784. do { \
  785. if (!(condition)) \
  786. ExceptionalCondition(#condition, "BadState", \
  787. __FILE__, __LINE__); \
  788. } while (0)
  789. /*
  790. * Check that `ptr' is `bndr' aligned.
  791. */
  792. #define AssertPointerAlignment(ptr, bndr) \
  793. Trap(TYPEALIGN(bndr, (uintptr_t)(ptr)) != (uintptr_t)(ptr), \
  794. "UnalignedPointer")
  795. #endif /* USE_ASSERT_CHECKING && !FRONTEND */
  796. /*
  797. * ExceptionalCondition is compiled into the backend whether or not
  798. * USE_ASSERT_CHECKING is defined, so as to support use of extensions
  799. * that are built with that #define with a backend that isn't. Hence,
  800. * we should declare it as long as !FRONTEND.
  801. */
  802. #ifndef FRONTEND
  803. extern void ExceptionalCondition(const char *conditionName,
  804. const char *errorType,
  805. const char *fileName, int lineNumber) pg_attribute_noreturn();
  806. #endif
  807. /*
  808. * Macros to support compile-time assertion checks.
  809. *
  810. * If the "condition" (a compile-time-constant expression) evaluates to false,
  811. * throw a compile error using the "errmessage" (a string literal).
  812. *
  813. * gcc 4.6 and up supports _Static_assert(), but there are bizarre syntactic
  814. * placement restrictions. Macros StaticAssertStmt() and StaticAssertExpr()
  815. * make it safe to use as a statement or in an expression, respectively.
  816. * The macro StaticAssertDecl() is suitable for use at file scope (outside of
  817. * any function).
  818. *
  819. * Otherwise we fall back on a kluge that assumes the compiler will complain
  820. * about a negative width for a struct bit-field. This will not include a
  821. * helpful error message, but it beats not getting an error at all.
  822. */
  823. #ifndef __cplusplus
  824. #ifdef HAVE__STATIC_ASSERT
  825. #define StaticAssertStmt(condition, errmessage) \
  826. do { _Static_assert(condition, errmessage); } while(0)
  827. #define StaticAssertExpr(condition, errmessage) \
  828. ((void) ({ StaticAssertStmt(condition, errmessage); true; }))
  829. #define StaticAssertDecl(condition, errmessage) \
  830. _Static_assert(condition, errmessage)
  831. #else /* !HAVE__STATIC_ASSERT */
  832. #define StaticAssertStmt(condition, errmessage) \
  833. ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
  834. #define StaticAssertExpr(condition, errmessage) \
  835. StaticAssertStmt(condition, errmessage)
  836. #define StaticAssertDecl(condition, errmessage) \
  837. extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
  838. #endif /* HAVE__STATIC_ASSERT */
  839. #else /* C++ */
  840. #if defined(__cpp_static_assert) && __cpp_static_assert >= 200410
  841. #define StaticAssertStmt(condition, errmessage) \
  842. static_assert(condition, errmessage)
  843. #define StaticAssertExpr(condition, errmessage) \
  844. ({ static_assert(condition, errmessage); })
  845. #define StaticAssertDecl(condition, errmessage) \
  846. static_assert(condition, errmessage)
  847. #else /* !__cpp_static_assert */
  848. #define StaticAssertStmt(condition, errmessage) \
  849. do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
  850. #define StaticAssertExpr(condition, errmessage) \
  851. ((void) ({ StaticAssertStmt(condition, errmessage); }))
  852. #define StaticAssertDecl(condition, errmessage) \
  853. extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
  854. #endif /* __cpp_static_assert */
  855. #endif /* C++ */
  856. /*
  857. * Compile-time checks that a variable (or expression) has the specified type.
  858. *
  859. * AssertVariableIsOfType() can be used as a statement.
  860. * AssertVariableIsOfTypeMacro() is intended for use in macros, eg
  861. * #define foo(x) (AssertVariableIsOfTypeMacro(x, int), bar(x))
  862. *
  863. * If we don't have __builtin_types_compatible_p, we can still assert that
  864. * the types have the same size. This is far from ideal (especially on 32-bit
  865. * platforms) but it provides at least some coverage.
  866. */
  867. #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
  868. #define AssertVariableIsOfType(varname, typename) \
  869. StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \
  870. CppAsString(varname) " does not have type " CppAsString(typename))
  871. #define AssertVariableIsOfTypeMacro(varname, typename) \
  872. (StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
  873. CppAsString(varname) " does not have type " CppAsString(typename)))
  874. #else /* !HAVE__BUILTIN_TYPES_COMPATIBLE_P */
  875. #define AssertVariableIsOfType(varname, typename) \
  876. StaticAssertStmt(sizeof(varname) == sizeof(typename), \
  877. CppAsString(varname) " does not have type " CppAsString(typename))
  878. #define AssertVariableIsOfTypeMacro(varname, typename) \
  879. (StaticAssertExpr(sizeof(varname) == sizeof(typename), \
  880. CppAsString(varname) " does not have type " CppAsString(typename)))
  881. #endif /* HAVE__BUILTIN_TYPES_COMPATIBLE_P */
  882. /* ----------------------------------------------------------------
  883. * Section 7: widely useful macros
  884. * ----------------------------------------------------------------
  885. */
  886. /*
  887. * Max
  888. * Return the maximum of two numbers.
  889. */
  890. #define Max(x, y) ((x) > (y) ? (x) : (y))
  891. /*
  892. * Min
  893. * Return the minimum of two numbers.
  894. */
  895. #define Min(x, y) ((x) < (y) ? (x) : (y))
  896. /*
  897. * Abs
  898. * Return the absolute value of the argument.
  899. */
  900. #define Abs(x) ((x) >= 0 ? (x) : -(x))
  901. /* Get a bit mask of the bits set in non-long aligned addresses */
  902. #define LONG_ALIGN_MASK (sizeof(long) - 1)
  903. /*
  904. * MemSet
  905. * Exactly the same as standard library function memset(), but considerably
  906. * faster for zeroing small word-aligned structures (such as parsetree nodes).
  907. * This has to be a macro because the main point is to avoid function-call
  908. * overhead. However, we have also found that the loop is faster than
  909. * native libc memset() on some platforms, even those with assembler
  910. * memset() functions. More research needs to be done, perhaps with
  911. * MEMSET_LOOP_LIMIT tests in configure.
  912. */
  913. #define MemSet(start, val, len) \
  914. do \
  915. { \
  916. /* must be void* because we don't know if it is integer aligned yet */ \
  917. void *_vstart = (void *) (start); \
  918. int _val = (val); \
  919. Size _len = (len); \
  920. \
  921. if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
  922. (_len & LONG_ALIGN_MASK) == 0 && \
  923. _val == 0 && \
  924. _len <= MEMSET_LOOP_LIMIT && \
  925. /* \
  926. * If MEMSET_LOOP_LIMIT == 0, optimizer should find \
  927. * the whole "if" false at compile time. \
  928. */ \
  929. MEMSET_LOOP_LIMIT != 0) \
  930. { \
  931. long *_start = (long *) _vstart; \
  932. long *_stop = (long *) ((char *) _start + _len); \
  933. while (_start < _stop) \
  934. *_start++ = 0; \
  935. } \
  936. else \
  937. memset(_vstart, _val, _len); \
  938. } while (0)
  939. /*
  940. * MemSetAligned is the same as MemSet except it omits the test to see if
  941. * "start" is word-aligned. This is okay to use if the caller knows a-priori
  942. * that the pointer is suitably aligned (typically, because he just got it
  943. * from palloc(), which always delivers a max-aligned pointer).
  944. */
  945. #define MemSetAligned(start, val, len) \
  946. do \
  947. { \
  948. long *_start = (long *) (start); \
  949. int _val = (val); \
  950. Size _len = (len); \
  951. \
  952. if ((_len & LONG_ALIGN_MASK) == 0 && \
  953. _val == 0 && \
  954. _len <= MEMSET_LOOP_LIMIT && \
  955. MEMSET_LOOP_LIMIT != 0) \
  956. { \
  957. long *_stop = (long *) ((char *) _start + _len); \
  958. while (_start < _stop) \
  959. *_start++ = 0; \
  960. } \
  961. else \
  962. memset(_start, _val, _len); \
  963. } while (0)
  964. /*
  965. * MemSetTest/MemSetLoop are a variant version that allow all the tests in
  966. * MemSet to be done at compile time in cases where "val" and "len" are
  967. * constants *and* we know the "start" pointer must be word-aligned.
  968. * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use
  969. * MemSetAligned. Beware of multiple evaluations of the arguments when using
  970. * this approach.
  971. */
  972. #define MemSetTest(val, len) \
  973. ( ((len) & LONG_ALIGN_MASK) == 0 && \
  974. (len) <= MEMSET_LOOP_LIMIT && \
  975. MEMSET_LOOP_LIMIT != 0 && \
  976. (val) == 0 )
  977. #define MemSetLoop(start, val, len) \
  978. do \
  979. { \
  980. long * _start = (long *) (start); \
  981. long * _stop = (long *) ((char *) _start + (Size) (len)); \
  982. \
  983. while (_start < _stop) \
  984. *_start++ = 0; \
  985. } while (0)
  986. /*
  987. * Macros for range-checking float values before converting to integer.
  988. * We must be careful here that the boundary values are expressed exactly
  989. * in the float domain. PG_INTnn_MIN is an exact power of 2, so it will
  990. * be represented exactly; but PG_INTnn_MAX isn't, and might get rounded
  991. * off, so avoid using that.
  992. * The input must be rounded to an integer beforehand, typically with rint(),
  993. * else we might draw the wrong conclusion about close-to-the-limit values.
  994. * These macros will do the right thing for Inf, but not necessarily for NaN,
  995. * so check isnan(num) first if that's a possibility.
  996. */
  997. #define FLOAT4_FITS_IN_INT16(num) \
  998. ((num) >= (float4) PG_INT16_MIN && (num) < -((float4) PG_INT16_MIN))
  999. #define FLOAT4_FITS_IN_INT32(num) \
  1000. ((num) >= (float4) PG_INT32_MIN && (num) < -((float4) PG_INT32_MIN))
  1001. #define FLOAT4_FITS_IN_INT64(num) \
  1002. ((num) >= (float4) PG_INT64_MIN && (num) < -((float4) PG_INT64_MIN))
  1003. #define FLOAT8_FITS_IN_INT16(num) \
  1004. ((num) >= (float8) PG_INT16_MIN && (num) < -((float8) PG_INT16_MIN))
  1005. #define FLOAT8_FITS_IN_INT32(num) \
  1006. ((num) >= (float8) PG_INT32_MIN && (num) < -((float8) PG_INT32_MIN))
  1007. #define FLOAT8_FITS_IN_INT64(num) \
  1008. ((num) >= (float8) PG_INT64_MIN && (num) < -((float8) PG_INT64_MIN))
  1009. /* ----------------------------------------------------------------
  1010. * Section 8: random stuff
  1011. * ----------------------------------------------------------------
  1012. */
  1013. #ifdef HAVE_STRUCT_SOCKADDR_UN
  1014. #define HAVE_UNIX_SOCKETS 1
  1015. #endif
  1016. /*
  1017. * Invert the sign of a qsort-style comparison result, ie, exchange negative
  1018. * and positive integer values, being careful not to get the wrong answer
  1019. * for INT_MIN. The argument should be an integral variable.
  1020. */
  1021. #define INVERT_COMPARE_RESULT(var) \
  1022. ((var) = ((var) < 0) ? 1 : -(var))
  1023. /*
  1024. * Use this, not "char buf[BLCKSZ]", to declare a field or local variable
  1025. * holding a page buffer, if that page might be accessed as a page and not
  1026. * just a string of bytes. Otherwise the variable might be under-aligned,
  1027. * causing problems on alignment-picky hardware. (In some places, we use
  1028. * this to declare buffers even though we only pass them to read() and
  1029. * write(), because copying to/from aligned buffers is usually faster than
  1030. * using unaligned buffers.) We include both "double" and "int64" in the
  1031. * union to ensure that the compiler knows the value must be MAXALIGN'ed
  1032. * (cf. configure's computation of MAXIMUM_ALIGNOF).
  1033. */
  1034. typedef union PGAlignedBlock
  1035. {
  1036. char data[BLCKSZ];
  1037. double force_align_d;
  1038. int64 force_align_i64;
  1039. } PGAlignedBlock;
  1040. /* Same, but for an XLOG_BLCKSZ-sized buffer */
  1041. typedef union PGAlignedXLogBlock
  1042. {
  1043. char data[XLOG_BLCKSZ];
  1044. double force_align_d;
  1045. int64 force_align_i64;
  1046. } PGAlignedXLogBlock;
  1047. /* msb for char */
  1048. #define HIGHBIT (0x80)
  1049. #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
  1050. /*
  1051. * Support macros for escaping strings. escape_backslash should be true
  1052. * if generating a non-standard-conforming string. Prefixing a string
  1053. * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
  1054. * Beware of multiple evaluation of the "ch" argument!
  1055. */
  1056. #define SQL_STR_DOUBLE(ch, escape_backslash) \
  1057. ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
  1058. #define ESCAPE_STRING_SYNTAX 'E'
  1059. #define STATUS_OK (0)
  1060. #define STATUS_ERROR (-1)
  1061. #define STATUS_EOF (-2)
  1062. /*
  1063. * gettext support
  1064. */
  1065. #ifndef ENABLE_NLS
  1066. /* stuff we'd otherwise get from <libintl.h> */
  1067. #define gettext(x) (x)
  1068. #define dgettext(d,x) (x)
  1069. #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
  1070. #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
  1071. #endif
  1072. #define _(x) gettext(x)
  1073. /*
  1074. * Use this to mark string constants as needing translation at some later
  1075. * time, rather than immediately. This is useful for cases where you need
  1076. * access to the original string and translated string, and for cases where
  1077. * immediate translation is not possible, like when initializing global
  1078. * variables.
  1079. *
  1080. * https://www.gnu.org/software/gettext/manual/html_node/Special-cases.html
  1081. */
  1082. #define gettext_noop(x) (x)
  1083. /*
  1084. * To better support parallel installations of major PostgreSQL
  1085. * versions as well as parallel installations of major library soname
  1086. * versions, we mangle the gettext domain name by appending those
  1087. * version numbers. The coding rule ought to be that wherever the
  1088. * domain name is mentioned as a literal, it must be wrapped into
  1089. * PG_TEXTDOMAIN(). The macros below do not work on non-literals; but
  1090. * that is somewhat intentional because it avoids having to worry
  1091. * about multiple states of premangling and postmangling as the values
  1092. * are being passed around.
  1093. *
  1094. * Make sure this matches the installation rules in nls-global.mk.
  1095. */
  1096. #ifdef SO_MAJOR_VERSION
  1097. #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
  1098. #else
  1099. #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
  1100. #endif
  1101. /*
  1102. * Macro that allows to cast constness and volatile away from an expression, but doesn't
  1103. * allow changing the underlying type. Enforcement of the latter
  1104. * currently only works for gcc like compilers.
  1105. *
  1106. * Please note IT IS NOT SAFE to cast constness away if the result will ever
  1107. * be modified (it would be undefined behaviour). Doing so anyway can cause
  1108. * compiler misoptimizations or runtime crashes (modifying readonly memory).
  1109. * It is only safe to use when the result will not be modified, but API
  1110. * design or language restrictions prevent you from declaring that
  1111. * (e.g. because a function returns both const and non-const variables).
  1112. *
  1113. * Note that this only works in function scope, not for global variables (it'd
  1114. * be nice, but not trivial, to improve that).
  1115. */
  1116. #if defined(HAVE__BUILTIN_TYPES_COMPATIBLE_P)
  1117. #define unconstify(underlying_type, expr) \
  1118. (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), const underlying_type), \
  1119. "wrong cast"), \
  1120. (underlying_type) (expr))
  1121. #define unvolatize(underlying_type, expr) \
  1122. (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), volatile underlying_type), \
  1123. "wrong cast"), \
  1124. (underlying_type) (expr))
  1125. #else
  1126. #define unconstify(underlying_type, expr) \
  1127. ((underlying_type) (expr))
  1128. #define unvolatize(underlying_type, expr) \
  1129. ((underlying_type) (expr))
  1130. #endif
  1131. /* ----------------------------------------------------------------
  1132. * Section 9: system-specific hacks
  1133. *
  1134. * This should be limited to things that absolutely have to be
  1135. * included in every source file. The port-specific header file
  1136. * is usually a better place for this sort of thing.
  1137. * ----------------------------------------------------------------
  1138. */
  1139. /*
  1140. * NOTE: this is also used for opening text files.
  1141. * WIN32 treats Control-Z as EOF in files opened in text mode.
  1142. * Therefore, we open files in binary mode on Win32 so we can read
  1143. * literal control-Z. The other affect is that we see CRLF, but
  1144. * that is OK because we can already handle those cleanly.
  1145. */
  1146. #if defined(WIN32) || defined(__CYGWIN__)
  1147. #define PG_BINARY O_BINARY
  1148. #define PG_BINARY_A "ab"
  1149. #define PG_BINARY_R "rb"
  1150. #define PG_BINARY_W "wb"
  1151. #else
  1152. #define PG_BINARY 0
  1153. #define PG_BINARY_A "a"
  1154. #define PG_BINARY_R "r"
  1155. #define PG_BINARY_W "w"
  1156. #endif
  1157. /*
  1158. * Provide prototypes for routines not present in a particular machine's
  1159. * standard C library.
  1160. */
  1161. #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
  1162. extern int fdatasync(int fildes);
  1163. #endif
  1164. /* Older platforms may provide strto[u]ll functionality under other names */
  1165. #if !defined(HAVE_STRTOLL) && defined(HAVE___STRTOLL)
  1166. #define strtoll __strtoll
  1167. #define HAVE_STRTOLL 1
  1168. #endif
  1169. #if !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
  1170. #define strtoll strtoq
  1171. #define HAVE_STRTOLL 1
  1172. #endif
  1173. #if !defined(HAVE_STRTOULL) && defined(HAVE___STRTOULL)
  1174. #define strtoull __strtoull
  1175. #define HAVE_STRTOULL 1
  1176. #endif
  1177. #if !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
  1178. #define strtoull strtouq
  1179. #define HAVE_STRTOULL 1
  1180. #endif
  1181. #if defined(HAVE_STRTOLL) && !HAVE_DECL_STRTOLL
  1182. extern long long strtoll(const char *str, char **endptr, int base);
  1183. #endif
  1184. #if defined(HAVE_STRTOULL) && !HAVE_DECL_STRTOULL
  1185. extern unsigned long long strtoull(const char *str, char **endptr, int base);
  1186. #endif
  1187. /*
  1188. * Thin wrappers that convert strings to exactly 64-bit integers, matching our
  1189. * definition of int64. (For the naming, compare that POSIX has
  1190. * strtoimax()/strtoumax() which return intmax_t/uintmax_t.)
  1191. */
  1192. #ifdef HAVE_LONG_INT_64
  1193. #define strtoi64(str, endptr, base) ((int64) strtol(str, endptr, base))
  1194. #define strtou64(str, endptr, base) ((uint64) strtoul(str, endptr, base))
  1195. #else
  1196. #define strtoi64(str, endptr, base) ((int64) strtoll(str, endptr, base))
  1197. #define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
  1198. #endif
  1199. /*
  1200. * Use "extern PGDLLIMPORT ..." to declare variables that are defined
  1201. * in the core backend and need to be accessible by loadable modules.
  1202. * No special marking is required on most ports.
  1203. */
  1204. #ifndef PGDLLIMPORT
  1205. #define PGDLLIMPORT
  1206. #endif
  1207. /*
  1208. * Use "extern PGDLLEXPORT ..." to declare functions that are defined in
  1209. * loadable modules and need to be callable by the core backend. (Usually,
  1210. * this is not necessary because our build process automatically exports
  1211. * such symbols, but sometimes manual marking is required.)
  1212. * No special marking is required on most ports.
  1213. */
  1214. #ifndef PGDLLEXPORT
  1215. #define PGDLLEXPORT
  1216. #endif
  1217. /*
  1218. * The following is used as the arg list for signal handlers. Any ports
  1219. * that take something other than an int argument should override this in
  1220. * their pg_config_os.h file. Note that variable names are required
  1221. * because it is used in both the prototypes as well as the definitions.
  1222. * Note also the long name. We expect that this won't collide with
  1223. * other names causing compiler warnings.
  1224. */
  1225. #ifndef SIGNAL_ARGS
  1226. #define SIGNAL_ARGS int postgres_signal_arg
  1227. #endif
  1228. /*
  1229. * When there is no sigsetjmp, its functionality is provided by plain
  1230. * setjmp. We now support the case only on Windows. However, it seems
  1231. * that MinGW-64 has some longstanding issues in its setjmp support,
  1232. * so on that toolchain we cheat and use gcc's builtins.
  1233. */
  1234. #ifdef WIN32
  1235. #ifdef __MINGW64__
  1236. typedef intptr_t sigjmp_buf[5];
  1237. #define sigsetjmp(x,y) __builtin_setjmp(x)
  1238. #define siglongjmp __builtin_longjmp
  1239. #else /* !__MINGW64__ */
  1240. #define sigjmp_buf jmp_buf
  1241. #define sigsetjmp(x,y) setjmp(x)
  1242. #define siglongjmp longjmp
  1243. #endif /* __MINGW64__ */
  1244. #endif /* WIN32 */
  1245. /* EXEC_BACKEND defines */
  1246. #ifdef EXEC_BACKEND
  1247. #define NON_EXEC_STATIC
  1248. #else
  1249. #define NON_EXEC_STATIC static
  1250. #endif
  1251. /* /port compatibility functions */
  1252. #include "port.h"
  1253. #endif /* C_H */