c.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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-2016, 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) hacks to cope with non-ANSI C compilers
  30. * 2) bool, true, false, TRUE, FALSE, NULL
  31. * 3) standard system types
  32. * 4) IsValid macros for system types
  33. * 5) offsetof, lengthof, endof, 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, it's
  40. * almost certainly wrong to put an "extern" declaration here. typedefs and
  41. * macros are the kind of thing that might go here.
  42. *
  43. *----------------------------------------------------------------
  44. */
  45. #ifndef C_H
  46. #define C_H
  47. #include "postgres_ext.h"
  48. /* Must undef pg_config_ext.h symbols before including pg_config.h */
  49. #undef PG_INT64_TYPE
  50. #include "pg_config.h"
  51. #include "pg_config_manual.h" /* must be after pg_config.h */
  52. /*
  53. * We always rely on the WIN32 macro being set by our build system,
  54. * but _WIN32 is the compiler pre-defined macro. So make sure we define
  55. * WIN32 whenever _WIN32 is set, to facilitate standalone building.
  56. */
  57. #if defined(_WIN32) && !defined(WIN32)
  58. #define WIN32
  59. #endif
  60. #if !defined(WIN32) && !defined(__CYGWIN__) /* win32 includes further down */
  61. #include "pg_config_os.h" /* must be before any system header files */
  62. #endif
  63. #if _MSC_VER >= 1400 || defined(HAVE_CRTDEFS_H)
  64. #define errcode __msvc_errcode
  65. #include <crtdefs.h>
  66. #undef errcode
  67. #endif
  68. /*
  69. * We have to include stdlib.h here because it defines many of these macros
  70. * on some platforms, and we only want our definitions used if stdlib.h doesn't
  71. * have its own. The same goes for stddef and stdarg if present.
  72. */
  73. #include <stdio.h>
  74. #include <stdlib.h>
  75. #include <string.h>
  76. #include <stddef.h>
  77. #include <stdarg.h>
  78. #ifdef HAVE_STRINGS_H
  79. #include <strings.h>
  80. #endif
  81. #ifdef HAVE_STDINT_H
  82. #include <stdint.h>
  83. #endif
  84. #include <sys/types.h>
  85. #include <errno.h>
  86. #if defined(WIN32) || defined(__CYGWIN__)
  87. #include <fcntl.h> /* ensure O_BINARY is available */
  88. #endif
  89. #if defined(WIN32) || defined(__CYGWIN__)
  90. /* We have to redefine some system functions after they are included above. */
  91. #include "pg_config_os.h"
  92. #endif
  93. /*
  94. * Force disable inlining if PG_FORCE_DISABLE_INLINE is defined. This is used
  95. * to work around compiler bugs and might also be useful for investigatory
  96. * purposes by defining the symbol in the platform's header..
  97. *
  98. * This is done early (in slightly the wrong section) as functionality later
  99. * in this file might want to rely on inline functions.
  100. */
  101. #ifdef PG_FORCE_DISABLE_INLINE
  102. #undef inline
  103. #define inline
  104. #endif
  105. /* Must be before gettext() games below */
  106. #include <locale.h>
  107. #define _(x) gettext(x)
  108. #ifdef ENABLE_NLS
  109. #include <libintl.h>
  110. #else
  111. #define gettext(x) (x)
  112. #define dgettext(d,x) (x)
  113. #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
  114. #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
  115. #endif
  116. /*
  117. * Use this to mark string constants as needing translation at some later
  118. * time, rather than immediately. This is useful for cases where you need
  119. * access to the original string and translated string, and for cases where
  120. * immediate translation is not possible, like when initializing global
  121. * variables.
  122. * http://www.gnu.org/software/autoconf/manual/gettext/Special-cases.html
  123. */
  124. #define gettext_noop(x) (x)
  125. /* ----------------------------------------------------------------
  126. * Section 1: hacks to cope with non-ANSI C compilers
  127. *
  128. * type prefixes (const, signed, volatile, inline) are handled in pg_config.h.
  129. * ----------------------------------------------------------------
  130. */
  131. /*
  132. * CppAsString
  133. * Convert the argument to a string, using the C preprocessor.
  134. * CppConcat
  135. * Concatenate two arguments together, using the C preprocessor.
  136. *
  137. * Note: There used to be support here for pre-ANSI C compilers that didn't
  138. * support # and ##. Nowadays, these macros are just for clarity and/or
  139. * backward compatibility with existing PostgreSQL code.
  140. */
  141. #define CppAsString(identifier) #identifier
  142. #define CppConcat(x, y) x##y
  143. /*
  144. * dummyret is used to set return values in macros that use ?: to make
  145. * assignments. gcc wants these to be void, other compilers like char
  146. */
  147. #ifdef __GNUC__ /* GNU cc */
  148. #define dummyret void
  149. #else
  150. #define dummyret char
  151. #endif
  152. /* Which __func__ symbol do we have, if any? */
  153. #ifdef HAVE_FUNCNAME__FUNC
  154. #define PG_FUNCNAME_MACRO __func__
  155. #else
  156. #ifdef HAVE_FUNCNAME__FUNCTION
  157. #define PG_FUNCNAME_MACRO __FUNCTION__
  158. #else
  159. #define PG_FUNCNAME_MACRO NULL
  160. #endif
  161. #endif
  162. /* ----------------------------------------------------------------
  163. * Section 2: bool, true, false, TRUE, FALSE, NULL
  164. * ----------------------------------------------------------------
  165. */
  166. /*
  167. * bool
  168. * Boolean value, either true or false.
  169. *
  170. * XXX for C++ compilers, we assume the compiler has a compatible
  171. * built-in definition of bool.
  172. */
  173. #ifndef __cplusplus
  174. #ifndef bool
  175. typedef char bool;
  176. #endif
  177. #ifndef true
  178. #define true ((bool) 1)
  179. #endif
  180. #ifndef false
  181. #define false ((bool) 0)
  182. #endif
  183. #endif /* not C++ */
  184. typedef bool *BoolPtr;
  185. #ifndef TRUE
  186. #define TRUE 1
  187. #endif
  188. #ifndef FALSE
  189. #define FALSE 0
  190. #endif
  191. /*
  192. * NULL
  193. * Null pointer.
  194. */
  195. #ifndef NULL
  196. #define NULL ((void *) 0)
  197. #endif
  198. /* ----------------------------------------------------------------
  199. * Section 3: standard system types
  200. * ----------------------------------------------------------------
  201. */
  202. /*
  203. * Pointer
  204. * Variable holding address of any memory resident object.
  205. *
  206. * XXX Pointer arithmetic is done with this, so it can't be void *
  207. * under "true" ANSI compilers.
  208. */
  209. typedef char *Pointer;
  210. /*
  211. * intN
  212. * Signed integer, EXACTLY N BITS IN SIZE,
  213. * used for numerical computations and the
  214. * frontend/backend protocol.
  215. */
  216. #ifndef HAVE_INT8
  217. typedef signed char int8; /* == 8 bits */
  218. typedef signed short int16; /* == 16 bits */
  219. typedef signed int int32; /* == 32 bits */
  220. #endif /* not HAVE_INT8 */
  221. /*
  222. * uintN
  223. * Unsigned integer, EXACTLY N BITS IN SIZE,
  224. * used for numerical computations and the
  225. * frontend/backend protocol.
  226. */
  227. #ifndef HAVE_UINT8
  228. typedef unsigned char uint8; /* == 8 bits */
  229. typedef unsigned short uint16; /* == 16 bits */
  230. typedef unsigned int uint32; /* == 32 bits */
  231. #endif /* not HAVE_UINT8 */
  232. /*
  233. * bitsN
  234. * Unit of bitwise operation, AT LEAST N BITS IN SIZE.
  235. */
  236. typedef uint8 bits8; /* >= 8 bits */
  237. typedef uint16 bits16; /* >= 16 bits */
  238. typedef uint32 bits32; /* >= 32 bits */
  239. /*
  240. * 64-bit integers
  241. */
  242. #ifdef HAVE_LONG_INT_64
  243. /* Plain "long int" fits, use it */
  244. #ifndef HAVE_INT64
  245. typedef long int int64;
  246. #endif
  247. #ifndef HAVE_UINT64
  248. typedef unsigned long int uint64;
  249. #endif
  250. #elif defined(HAVE_LONG_LONG_INT_64)
  251. /* We have working support for "long long int", use that */
  252. #ifndef HAVE_INT64
  253. typedef long long int int64;
  254. #endif
  255. #ifndef HAVE_UINT64
  256. typedef unsigned long long int uint64;
  257. #endif
  258. #else
  259. /* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */
  260. #error must have a working 64-bit integer datatype
  261. #endif
  262. /* Decide if we need to decorate 64-bit constants */
  263. #ifdef HAVE_LL_CONSTANTS
  264. #define INT64CONST(x) ((int64) x##LL)
  265. #define UINT64CONST(x) ((uint64) x##ULL)
  266. #else
  267. #define INT64CONST(x) ((int64) x)
  268. #define UINT64CONST(x) ((uint64) x)
  269. #endif
  270. /* snprintf format strings to use for 64-bit integers */
  271. #define INT64_FORMAT "%" INT64_MODIFIER "d"
  272. #define UINT64_FORMAT "%" INT64_MODIFIER "u"
  273. /*
  274. * 128-bit signed and unsigned integers
  275. * There currently is only a limited support for the type. E.g. 128bit
  276. * literals and snprintf are not supported; but math is.
  277. */
  278. #if defined(PG_INT128_TYPE)
  279. #define HAVE_INT128
  280. typedef PG_INT128_TYPE int128;
  281. typedef unsigned PG_INT128_TYPE uint128;
  282. #endif
  283. /*
  284. * stdint.h limits aren't guaranteed to be present and aren't guaranteed to
  285. * have compatible types with our fixed width types. So just define our own.
  286. */
  287. #define PG_INT8_MIN (-0x7F-1)
  288. #define PG_INT8_MAX (0x7F)
  289. #define PG_UINT8_MAX (0xFF)
  290. #define PG_INT16_MIN (-0x7FFF-1)
  291. #define PG_INT16_MAX (0x7FFF)
  292. #define PG_UINT16_MAX (0xFFFF)
  293. #define PG_INT32_MIN (-0x7FFFFFFF-1)
  294. #define PG_INT32_MAX (0x7FFFFFFF)
  295. #define PG_UINT32_MAX (0xFFFFFFFF)
  296. #define PG_INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
  297. #define PG_INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
  298. #define PG_UINT64_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
  299. /* Select timestamp representation (float8 or int64) */
  300. #ifdef USE_INTEGER_DATETIMES
  301. #define HAVE_INT64_TIMESTAMP
  302. #endif
  303. /*
  304. * Size
  305. * Size of any memory resident object, as returned by sizeof.
  306. */
  307. typedef size_t Size;
  308. /*
  309. * Index
  310. * Index into any memory resident array.
  311. *
  312. * Note:
  313. * Indices are non negative.
  314. */
  315. typedef unsigned int Index;
  316. /*
  317. * Offset
  318. * Offset into any memory resident array.
  319. *
  320. * Note:
  321. * This differs from an Index in that an Index is always
  322. * non negative, whereas Offset may be negative.
  323. */
  324. typedef signed int Offset;
  325. /*
  326. * Common Postgres datatype names (as used in the catalogs)
  327. */
  328. typedef float float4;
  329. typedef double float8;
  330. /*
  331. * Oid, RegProcedure, TransactionId, SubTransactionId, MultiXactId,
  332. * CommandId
  333. */
  334. /* typedef Oid is in postgres_ext.h */
  335. /*
  336. * regproc is the type name used in the include/catalog headers, but
  337. * RegProcedure is the preferred name in C code.
  338. */
  339. typedef Oid regproc;
  340. typedef regproc RegProcedure;
  341. typedef uint32 TransactionId;
  342. typedef uint32 LocalTransactionId;
  343. typedef uint32 SubTransactionId;
  344. #define InvalidSubTransactionId ((SubTransactionId) 0)
  345. #define TopSubTransactionId ((SubTransactionId) 1)
  346. /* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
  347. typedef TransactionId MultiXactId;
  348. typedef uint32 MultiXactOffset;
  349. typedef uint32 CommandId;
  350. #define FirstCommandId ((CommandId) 0)
  351. #define InvalidCommandId (~(CommandId)0)
  352. /*
  353. * Array indexing support
  354. */
  355. #define MAXDIM 6
  356. typedef struct
  357. {
  358. int indx[MAXDIM];
  359. } IntArray;
  360. /* ----------------
  361. * Variable-length datatypes all share the 'struct varlena' header.
  362. *
  363. * NOTE: for TOASTable types, this is an oversimplification, since the value
  364. * may be compressed or moved out-of-line. However datatype-specific routines
  365. * are mostly content to deal with de-TOASTed values only, and of course
  366. * client-side routines should never see a TOASTed value. But even in a
  367. * de-TOASTed value, beware of touching vl_len_ directly, as its representation
  368. * is no longer convenient. It's recommended that code always use the VARDATA,
  369. * VARSIZE, and SET_VARSIZE macros instead of relying on direct mentions of
  370. * the struct fields. See postgres.h for details of the TOASTed form.
  371. * ----------------
  372. */
  373. struct varlena
  374. {
  375. char vl_len_[4]; /* Do not touch this field directly! */
  376. char vl_dat[FLEXIBLE_ARRAY_MEMBER]; /* Data content is here */
  377. };
  378. #define VARHDRSZ ((int32) sizeof(int32))
  379. /*
  380. * These widely-used datatypes are just a varlena header and the data bytes.
  381. * There is no terminating null or anything like that --- the data length is
  382. * always VARSIZE(ptr) - VARHDRSZ.
  383. */
  384. typedef struct varlena bytea;
  385. typedef struct varlena text;
  386. typedef struct varlena BpChar; /* blank-padded char, ie SQL char(n) */
  387. typedef struct varlena VarChar; /* var-length char, ie SQL varchar(n) */
  388. /*
  389. * Specialized array types. These are physically laid out just the same
  390. * as regular arrays (so that the regular array subscripting code works
  391. * with them). They exist as distinct types mostly for historical reasons:
  392. * they have nonstandard I/O behavior which we don't want to change for fear
  393. * of breaking applications that look at the system catalogs. There is also
  394. * an implementation issue for oidvector: it's part of the primary key for
  395. * pg_proc, and we can't use the normal btree array support routines for that
  396. * without circularity.
  397. */
  398. typedef struct
  399. {
  400. int32 vl_len_; /* these fields must match ArrayType! */
  401. int ndim; /* always 1 for int2vector */
  402. int32 dataoffset; /* always 0 for int2vector */
  403. Oid elemtype;
  404. int dim1;
  405. int lbound1;
  406. int16 values[FLEXIBLE_ARRAY_MEMBER];
  407. } int2vector;
  408. typedef struct
  409. {
  410. int32 vl_len_; /* these fields must match ArrayType! */
  411. int ndim; /* always 1 for oidvector */
  412. int32 dataoffset; /* always 0 for oidvector */
  413. Oid elemtype;
  414. int dim1;
  415. int lbound1;
  416. Oid values[FLEXIBLE_ARRAY_MEMBER];
  417. } oidvector;
  418. /*
  419. * Representation of a Name: effectively just a C string, but null-padded to
  420. * exactly NAMEDATALEN bytes. The use of a struct is historical.
  421. */
  422. typedef struct nameData
  423. {
  424. char data[NAMEDATALEN];
  425. } NameData;
  426. typedef NameData *Name;
  427. #define NameStr(name) ((name).data)
  428. /*
  429. * Support macros for escaping strings. escape_backslash should be TRUE
  430. * if generating a non-standard-conforming string. Prefixing a string
  431. * with ESCAPE_STRING_SYNTAX guarantees it is non-standard-conforming.
  432. * Beware of multiple evaluation of the "ch" argument!
  433. */
  434. #define SQL_STR_DOUBLE(ch, escape_backslash) \
  435. ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
  436. #define ESCAPE_STRING_SYNTAX 'E'
  437. /* ----------------------------------------------------------------
  438. * Section 4: IsValid macros for system types
  439. * ----------------------------------------------------------------
  440. */
  441. /*
  442. * BoolIsValid
  443. * True iff bool is valid.
  444. */
  445. #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
  446. /*
  447. * PointerIsValid
  448. * True iff pointer is valid.
  449. */
  450. #define PointerIsValid(pointer) ((const void*)(pointer) != NULL)
  451. /*
  452. * PointerIsAligned
  453. * True iff pointer is properly aligned to point to the given type.
  454. */
  455. #define PointerIsAligned(pointer, type) \
  456. (((uintptr_t)(pointer) % (sizeof (type))) == 0)
  457. #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
  458. #define RegProcedureIsValid(p) OidIsValid(p)
  459. /* ----------------------------------------------------------------
  460. * Section 5: offsetof, lengthof, endof, alignment
  461. * ----------------------------------------------------------------
  462. */
  463. /*
  464. * offsetof
  465. * Offset of a structure/union field within that structure/union.
  466. *
  467. * XXX This is supposed to be part of stddef.h, but isn't on
  468. * some systems (like SunOS 4).
  469. */
  470. #ifndef offsetof
  471. #define offsetof(type, field) ((long) &((type *)0)->field)
  472. #endif /* offsetof */
  473. /*
  474. * lengthof
  475. * Number of elements in an array.
  476. */
  477. #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
  478. /*
  479. * endof
  480. * Address of the element one past the last in an array.
  481. */
  482. #define endof(array) (&(array)[lengthof(array)])
  483. /* ----------------
  484. * Alignment macros: align a length or address appropriately for a given type.
  485. * The fooALIGN() macros round up to a multiple of the required alignment,
  486. * while the fooALIGN_DOWN() macros round down. The latter are more useful
  487. * for problems like "how many X-sized structures will fit in a page?".
  488. *
  489. * NOTE: TYPEALIGN[_DOWN] will not work if ALIGNVAL is not a power of 2.
  490. * That case seems extremely unlikely to be needed in practice, however.
  491. * ----------------
  492. */
  493. #define TYPEALIGN(ALIGNVAL,LEN) \
  494. (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
  495. #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
  496. #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
  497. #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
  498. #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
  499. #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
  500. /* MAXALIGN covers only built-in types, not buffers */
  501. #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
  502. #define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
  503. #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
  504. (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
  505. #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
  506. #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
  507. #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
  508. #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
  509. #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
  510. /*
  511. * The above macros will not work with types wider than uintptr_t, like with
  512. * uint64 on 32-bit platforms. That's not problem for the usual use where a
  513. * pointer or a length is aligned, but for the odd case that you need to
  514. * align something (potentially) wider, use TYPEALIGN64.
  515. */
  516. #define TYPEALIGN64(ALIGNVAL,LEN) \
  517. (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
  518. /* we don't currently need wider versions of the other ALIGN macros */
  519. #define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
  520. /* ----------------
  521. * Attribute macros
  522. *
  523. * GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
  524. * GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
  525. * Sunpro: https://docs.oracle.com/cd/E18659_01/html/821-1384/gjzke.html
  526. * XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/function_attributes.html
  527. * XLC: http://www-01.ibm.com/support/knowledgecenter/SSGH2K_11.1.0/com.ibm.xlc111.aix.doc/language_ref/type_attrib.html
  528. * ----------------
  529. */
  530. /* only GCC supports the unused attribute */
  531. #ifdef __GNUC__
  532. #define pg_attribute_unused() __attribute__((unused))
  533. #else
  534. #define pg_attribute_unused()
  535. #endif
  536. /* GCC and XLC support format attributes */
  537. #if defined(__GNUC__) || defined(__IBMC__)
  538. #define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
  539. #define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
  540. #else
  541. #define pg_attribute_format_arg(a)
  542. #define pg_attribute_printf(f,a)
  543. #endif
  544. /* GCC, Sunpro and XLC support aligned, packed and noreturn */
  545. #if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
  546. #define pg_attribute_aligned(a) __attribute__((aligned(a)))
  547. #define pg_attribute_noreturn() __attribute__((noreturn))
  548. #define pg_attribute_packed() __attribute__((packed))
  549. #define HAVE_PG_ATTRIBUTE_NORETURN 1
  550. #else
  551. /*
  552. * NB: aligned and packed are not given default definitions because they
  553. * affect code functionality; they *must* be implemented by the compiler
  554. * if they are to be used.
  555. */
  556. #define pg_attribute_noreturn()
  557. #endif
  558. /* ----------------------------------------------------------------
  559. * Section 6: assertions
  560. * ----------------------------------------------------------------
  561. */
  562. /*
  563. * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
  564. * - plai 9/5/90
  565. *
  566. * It should _NOT_ be defined in releases or in benchmark copies
  567. */
  568. /*
  569. * Assert() can be used in both frontend and backend code. In frontend code it
  570. * just calls the standard assert, if it's available. If use of assertions is
  571. * not configured, it does nothing.
  572. */
  573. #ifndef USE_ASSERT_CHECKING
  574. #define Assert(condition) ((void)true)
  575. #define AssertMacro(condition) ((void)true)
  576. #define AssertArg(condition) ((void)true)
  577. #define AssertState(condition) ((void)true)
  578. #define AssertPointerAlignment(ptr, bndr) ((void)true)
  579. #define Trap(condition, errorType) ((void)true)
  580. #define TrapMacro(condition, errorType) (true)
  581. #elif defined(FRONTEND)
  582. #include <assert.h>
  583. #define Assert(p) assert(p)
  584. #define AssertMacro(p) ((void) assert(p))
  585. #define AssertArg(condition) assert(condition)
  586. #define AssertState(condition) assert(condition)
  587. #define AssertPointerAlignment(ptr, bndr) ((void)true)
  588. #else /* USE_ASSERT_CHECKING && !FRONTEND */
  589. /*
  590. * Trap
  591. * Generates an exception if the given condition is true.
  592. */
  593. #define Trap(condition, errorType) \
  594. do { \
  595. if (condition) \
  596. ExceptionalCondition(CppAsString(condition), (errorType), \
  597. __FILE__, __LINE__); \
  598. } while (0)
  599. /*
  600. * TrapMacro is the same as Trap but it's intended for use in macros:
  601. *
  602. * #define foo(x) (AssertMacro(x != 0), bar(x))
  603. *
  604. * Isn't CPP fun?
  605. */
  606. #define TrapMacro(condition, errorType) \
  607. ((bool) (! (condition) || \
  608. (ExceptionalCondition(CppAsString(condition), (errorType), \
  609. __FILE__, __LINE__), 0)))
  610. #define Assert(condition) \
  611. Trap(!(condition), "FailedAssertion")
  612. #define AssertMacro(condition) \
  613. ((void) TrapMacro(!(condition), "FailedAssertion"))
  614. #define AssertArg(condition) \
  615. Trap(!(condition), "BadArgument")
  616. #define AssertState(condition) \
  617. Trap(!(condition), "BadState")
  618. /*
  619. * Check that `ptr' is `bndr' aligned.
  620. */
  621. #define AssertPointerAlignment(ptr, bndr) \
  622. Trap(TYPEALIGN(bndr, (uintptr_t)(ptr)) != (uintptr_t)(ptr), \
  623. "UnalignedPointer")
  624. #endif /* USE_ASSERT_CHECKING && !FRONTEND */
  625. /*
  626. * Macros to support compile-time assertion checks.
  627. *
  628. * If the "condition" (a compile-time-constant expression) evaluates to false,
  629. * throw a compile error using the "errmessage" (a string literal).
  630. *
  631. * gcc 4.6 and up supports _Static_assert(), but there are bizarre syntactic
  632. * placement restrictions. These macros make it safe to use as a statement
  633. * or in an expression, respectively.
  634. *
  635. * Otherwise we fall back on a kluge that assumes the compiler will complain
  636. * about a negative width for a struct bit-field. This will not include a
  637. * helpful error message, but it beats not getting an error at all.
  638. */
  639. #ifdef HAVE__STATIC_ASSERT
  640. #define StaticAssertStmt(condition, errmessage) \
  641. do { _Static_assert(condition, errmessage); } while(0)
  642. #define StaticAssertExpr(condition, errmessage) \
  643. ({ StaticAssertStmt(condition, errmessage); true; })
  644. #else /* !HAVE__STATIC_ASSERT */
  645. #define StaticAssertStmt(condition, errmessage) \
  646. ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
  647. #define StaticAssertExpr(condition, errmessage) \
  648. StaticAssertStmt(condition, errmessage)
  649. #endif /* HAVE__STATIC_ASSERT */
  650. /*
  651. * Compile-time checks that a variable (or expression) has the specified type.
  652. *
  653. * AssertVariableIsOfType() can be used as a statement.
  654. * AssertVariableIsOfTypeMacro() is intended for use in macros, eg
  655. * #define foo(x) (AssertVariableIsOfTypeMacro(x, int), bar(x))
  656. *
  657. * If we don't have __builtin_types_compatible_p, we can still assert that
  658. * the types have the same size. This is far from ideal (especially on 32-bit
  659. * platforms) but it provides at least some coverage.
  660. */
  661. #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
  662. #define AssertVariableIsOfType(varname, typename) \
  663. StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \
  664. CppAsString(varname) " does not have type " CppAsString(typename))
  665. #define AssertVariableIsOfTypeMacro(varname, typename) \
  666. ((void) StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
  667. CppAsString(varname) " does not have type " CppAsString(typename)))
  668. #else /* !HAVE__BUILTIN_TYPES_COMPATIBLE_P */
  669. #define AssertVariableIsOfType(varname, typename) \
  670. StaticAssertStmt(sizeof(varname) == sizeof(typename), \
  671. CppAsString(varname) " does not have type " CppAsString(typename))
  672. #define AssertVariableIsOfTypeMacro(varname, typename) \
  673. ((void) StaticAssertExpr(sizeof(varname) == sizeof(typename), \
  674. CppAsString(varname) " does not have type " CppAsString(typename)))
  675. #endif /* HAVE__BUILTIN_TYPES_COMPATIBLE_P */
  676. /* ----------------------------------------------------------------
  677. * Section 7: widely useful macros
  678. * ----------------------------------------------------------------
  679. */
  680. /*
  681. * Max
  682. * Return the maximum of two numbers.
  683. */
  684. #define Max(x, y) ((x) > (y) ? (x) : (y))
  685. /*
  686. * Min
  687. * Return the minimum of two numbers.
  688. */
  689. #define Min(x, y) ((x) < (y) ? (x) : (y))
  690. /*
  691. * Abs
  692. * Return the absolute value of the argument.
  693. */
  694. #define Abs(x) ((x) >= 0 ? (x) : -(x))
  695. /*
  696. * StrNCpy
  697. * Like standard library function strncpy(), except that result string
  698. * is guaranteed to be null-terminated --- that is, at most N-1 bytes
  699. * of the source string will be kept.
  700. * Also, the macro returns no result (too hard to do that without
  701. * evaluating the arguments multiple times, which seems worse).
  702. *
  703. * BTW: when you need to copy a non-null-terminated string (like a text
  704. * datum) and add a null, do not do it with StrNCpy(..., len+1). That
  705. * might seem to work, but it fetches one byte more than there is in the
  706. * text object. One fine day you'll have a SIGSEGV because there isn't
  707. * another byte before the end of memory. Don't laugh, we've had real
  708. * live bug reports from real live users over exactly this mistake.
  709. * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead.
  710. */
  711. #define StrNCpy(dst,src,len) \
  712. do \
  713. { \
  714. char * _dst = (dst); \
  715. Size _len = (len); \
  716. \
  717. if (_len > 0) \
  718. { \
  719. strncpy(_dst, (src), _len); \
  720. _dst[_len-1] = '\0'; \
  721. } \
  722. } while (0)
  723. /* Get a bit mask of the bits set in non-long aligned addresses */
  724. #define LONG_ALIGN_MASK (sizeof(long) - 1)
  725. /*
  726. * MemSet
  727. * Exactly the same as standard library function memset(), but considerably
  728. * faster for zeroing small word-aligned structures (such as parsetree nodes).
  729. * This has to be a macro because the main point is to avoid function-call
  730. * overhead. However, we have also found that the loop is faster than
  731. * native libc memset() on some platforms, even those with assembler
  732. * memset() functions. More research needs to be done, perhaps with
  733. * MEMSET_LOOP_LIMIT tests in configure.
  734. */
  735. #define MemSet(start, val, len) \
  736. do \
  737. { \
  738. /* must be void* because we don't know if it is integer aligned yet */ \
  739. void *_vstart = (void *) (start); \
  740. int _val = (val); \
  741. Size _len = (len); \
  742. \
  743. if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
  744. (_len & LONG_ALIGN_MASK) == 0 && \
  745. _val == 0 && \
  746. _len <= MEMSET_LOOP_LIMIT && \
  747. /* \
  748. * If MEMSET_LOOP_LIMIT == 0, optimizer should find \
  749. * the whole "if" false at compile time. \
  750. */ \
  751. MEMSET_LOOP_LIMIT != 0) \
  752. { \
  753. long *_start = (long *) _vstart; \
  754. long *_stop = (long *) ((char *) _start + _len); \
  755. while (_start < _stop) \
  756. *_start++ = 0; \
  757. } \
  758. else \
  759. memset(_vstart, _val, _len); \
  760. } while (0)
  761. /*
  762. * MemSetAligned is the same as MemSet except it omits the test to see if
  763. * "start" is word-aligned. This is okay to use if the caller knows a-priori
  764. * that the pointer is suitably aligned (typically, because he just got it
  765. * from palloc(), which always delivers a max-aligned pointer).
  766. */
  767. #define MemSetAligned(start, val, len) \
  768. do \
  769. { \
  770. long *_start = (long *) (start); \
  771. int _val = (val); \
  772. Size _len = (len); \
  773. \
  774. if ((_len & LONG_ALIGN_MASK) == 0 && \
  775. _val == 0 && \
  776. _len <= MEMSET_LOOP_LIMIT && \
  777. MEMSET_LOOP_LIMIT != 0) \
  778. { \
  779. long *_stop = (long *) ((char *) _start + _len); \
  780. while (_start < _stop) \
  781. *_start++ = 0; \
  782. } \
  783. else \
  784. memset(_start, _val, _len); \
  785. } while (0)
  786. /*
  787. * MemSetTest/MemSetLoop are a variant version that allow all the tests in
  788. * MemSet to be done at compile time in cases where "val" and "len" are
  789. * constants *and* we know the "start" pointer must be word-aligned.
  790. * If MemSetTest succeeds, then it is okay to use MemSetLoop, otherwise use
  791. * MemSetAligned. Beware of multiple evaluations of the arguments when using
  792. * this approach.
  793. */
  794. #define MemSetTest(val, len) \
  795. ( ((len) & LONG_ALIGN_MASK) == 0 && \
  796. (len) <= MEMSET_LOOP_LIMIT && \
  797. MEMSET_LOOP_LIMIT != 0 && \
  798. (val) == 0 )
  799. #define MemSetLoop(start, val, len) \
  800. do \
  801. { \
  802. long * _start = (long *) (start); \
  803. long * _stop = (long *) ((char *) _start + (Size) (len)); \
  804. \
  805. while (_start < _stop) \
  806. *_start++ = 0; \
  807. } while (0)
  808. /*
  809. * Mark a point as unreachable in a portable fashion. This should preferably
  810. * be something that the compiler understands, to aid code generation.
  811. * In assert-enabled builds, we prefer abort() for debugging reasons.
  812. */
  813. #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
  814. #define pg_unreachable() __builtin_unreachable()
  815. #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
  816. #define pg_unreachable() __assume(0)
  817. #else
  818. #define pg_unreachable() abort()
  819. #endif
  820. /* ----------------------------------------------------------------
  821. * Section 8: random stuff
  822. * ----------------------------------------------------------------
  823. */
  824. /* msb for char */
  825. #define HIGHBIT (0x80)
  826. #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
  827. #define STATUS_OK (0)
  828. #define STATUS_ERROR (-1)
  829. #define STATUS_EOF (-2)
  830. #define STATUS_FOUND (1)
  831. #define STATUS_WAITING (2)
  832. /*
  833. * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
  834. * used in assert-enabled builds, to avoid compiler warnings about unused
  835. * variables in assert-disabled builds.
  836. */
  837. #ifdef USE_ASSERT_CHECKING
  838. #define PG_USED_FOR_ASSERTS_ONLY
  839. #else
  840. #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
  841. #endif
  842. /* gettext domain name mangling */
  843. /*
  844. * To better support parallel installations of major PostgreSQL
  845. * versions as well as parallel installations of major library soname
  846. * versions, we mangle the gettext domain name by appending those
  847. * version numbers. The coding rule ought to be that wherever the
  848. * domain name is mentioned as a literal, it must be wrapped into
  849. * PG_TEXTDOMAIN(). The macros below do not work on non-literals; but
  850. * that is somewhat intentional because it avoids having to worry
  851. * about multiple states of premangling and postmangling as the values
  852. * are being passed around.
  853. *
  854. * Make sure this matches the installation rules in nls-global.mk.
  855. */
  856. /* need a second indirection because we want to stringize the macro value, not the name */
  857. #define CppAsString2(x) CppAsString(x)
  858. #ifdef SO_MAJOR_VERSION
  859. #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
  860. #else
  861. #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
  862. #endif
  863. /* ----------------------------------------------------------------
  864. * Section 9: system-specific hacks
  865. *
  866. * This should be limited to things that absolutely have to be
  867. * included in every source file. The port-specific header file
  868. * is usually a better place for this sort of thing.
  869. * ----------------------------------------------------------------
  870. */
  871. /*
  872. * NOTE: this is also used for opening text files.
  873. * WIN32 treats Control-Z as EOF in files opened in text mode.
  874. * Therefore, we open files in binary mode on Win32 so we can read
  875. * literal control-Z. The other affect is that we see CRLF, but
  876. * that is OK because we can already handle those cleanly.
  877. */
  878. #if defined(WIN32) || defined(__CYGWIN__)
  879. #define PG_BINARY O_BINARY
  880. #define PG_BINARY_A "ab"
  881. #define PG_BINARY_R "rb"
  882. #define PG_BINARY_W "wb"
  883. #else
  884. #define PG_BINARY 0
  885. #define PG_BINARY_A "a"
  886. #define PG_BINARY_R "r"
  887. #define PG_BINARY_W "w"
  888. #endif
  889. /*
  890. * Provide prototypes for routines not present in a particular machine's
  891. * standard C library.
  892. */
  893. #if !HAVE_DECL_SNPRINTF
  894. extern int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3, 4);
  895. #endif
  896. #if !HAVE_DECL_VSNPRINTF
  897. extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
  898. #endif
  899. #if !defined(HAVE_MEMMOVE) && !defined(memmove)
  900. #define memmove(d, s, c) bcopy(s, d, c)
  901. #endif
  902. /* no special DLL markers on most ports */
  903. #ifndef PGDLLIMPORT
  904. #define PGDLLIMPORT
  905. #endif
  906. #ifndef PGDLLEXPORT
  907. #define PGDLLEXPORT
  908. #endif
  909. /*
  910. * The following is used as the arg list for signal handlers. Any ports
  911. * that take something other than an int argument should override this in
  912. * their pg_config_os.h file. Note that variable names are required
  913. * because it is used in both the prototypes as well as the definitions.
  914. * Note also the long name. We expect that this won't collide with
  915. * other names causing compiler warnings.
  916. */
  917. #ifndef SIGNAL_ARGS
  918. #define SIGNAL_ARGS int postgres_signal_arg
  919. #endif
  920. /*
  921. * When there is no sigsetjmp, its functionality is provided by plain
  922. * setjmp. Incidentally, nothing provides setjmp's functionality in
  923. * that case. We now support the case only on Windows.
  924. */
  925. #ifdef WIN32
  926. #define sigjmp_buf jmp_buf
  927. #define sigsetjmp(x,y) setjmp(x)
  928. #define siglongjmp longjmp
  929. #endif
  930. #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
  931. extern int fdatasync(int fildes);
  932. #endif
  933. /* If strtoq() exists, rename it to the more standard strtoll() */
  934. #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
  935. #define strtoll strtoq
  936. #define HAVE_STRTOLL 1
  937. #endif
  938. /* If strtouq() exists, rename it to the more standard strtoull() */
  939. #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
  940. #define strtoull strtouq
  941. #define HAVE_STRTOULL 1
  942. #endif
  943. /*
  944. * We assume if we have these two functions, we have their friends too, and
  945. * can use the wide-character functions.
  946. */
  947. #if defined(HAVE_WCSTOMBS) && defined(HAVE_TOWLOWER)
  948. #define USE_WIDE_UPPER_LOWER
  949. #endif
  950. /* EXEC_BACKEND defines */
  951. #ifdef EXEC_BACKEND
  952. #define NON_EXEC_STATIC
  953. #else
  954. #define NON_EXEC_STATIC static
  955. #endif
  956. /* /port compatibility functions */
  957. #include "port.h"
  958. #endif /* C_H */