gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1294 lines
41KB

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