978 {
static_assert(condition,
errmessage); }
while(0)
979
980
981
982
983
984
985
986
987
988#ifdef HAVE_STATEMENT_EXPRESSIONS
989#define StaticAssertExpr(condition, errmessage) \
990 ((void) ({ static_assert(condition, errmessage); true; }))
991#else
992#define StaticAssertExpr(condition, errmessage) \
993 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
994#endif
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
1009#define StaticAssertVariableIsOfType(varname, typename) \
1010 StaticAssertDecl(__builtin_types_compatible_p(__typeof__(varname), typename), \
1011 CppAsString(varname) " does not have type " CppAsString(typename))
1012#define StaticAssertVariableIsOfTypeMacro(varname, typename) \
1013 (StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
1014 CppAsString(varname) " does not have type " CppAsString(typename)))
1015#else
1016#define StaticAssertVariableIsOfType(varname, typename) \
1017 StaticAssertDecl(sizeof(varname) == sizeof(typename), \
1018 CppAsString(varname) " does not have type " CppAsString(typename))
1019#define StaticAssertVariableIsOfTypeMacro(varname, typename) \
1020 (StaticAssertExpr(sizeof(varname) == sizeof(typename), \
1021 CppAsString(varname) " does not have type " CppAsString(typename)))
1022#endif
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033#define Max(x, y) ((x) > (y) ? (x) : (y))
1034
1035
1036
1037
1038
1039#define Min(x, y) ((x) < (y) ? (x) : (y))
1040
1041
1042
1043#define SIZE_T_ALIGN_MASK (sizeof(size_t) - 1)
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055#define MemSet(start, val, len) \
1056 do \
1057 { \
1058 \
1059 void *_vstart = (void *) (start); \
1060 int _val = (val); \
1061 Size _len = (len); \
1062\
1063 if ((((uintptr_t) _vstart) & SIZE_T_ALIGN_MASK) == 0 && \
1064 (_len & SIZE_T_ALIGN_MASK) == 0 && \
1065 _val == 0 && \
1066 _len <= MEMSET_LOOP_LIMIT && \
1067
1068
1069
1070 \
1071 MEMSET_LOOP_LIMIT != 0) \
1072 { \
1073 size_t *_start = (size_t *) _vstart; \
1074 size_t *_stop = (size_t *) ((char *) _start + _len); \
1075 while (_start < _stop) \
1076 *_start++ = 0; \
1077 } \
1078 else \
1079 memset(_vstart, _val, _len); \
1080 } while (0)
1081
1082
1083
1084
1085
1086
1087
1088#define MemSetAligned(start, val, len) \
1089 do \
1090 { \
1091 size_t *_start = (size_t *) (start); \
1092 int _val = (val); \
1093 Size _len = (len); \
1094\
1095 if ((_len & SIZE_T_ALIGN_MASK) == 0 && \
1096 _val == 0 && \
1097 _len <= MEMSET_LOOP_LIMIT && \
1098 MEMSET_LOOP_LIMIT != 0) \
1099 { \
1100 size_t *_stop = (size_t *) ((char *) _start + _len); \
1101 while (_start < _stop) \
1102 *_start++ = 0; \
1103 } \
1104 else \
1105 memset(_start, _val, _len); \
1106 } while (0)
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120#define FLOAT4_FITS_IN_INT16(num) \
1121 ((num) >= (float4) PG_INT16_MIN && (num) < -((float4) PG_INT16_MIN))
1122#define FLOAT4_FITS_IN_INT32(num) \
1123 ((num) >= (float4) PG_INT32_MIN && (num) < -((float4) PG_INT32_MIN))
1124#define FLOAT4_FITS_IN_INT64(num) \
1125 ((num) >= (float4) PG_INT64_MIN && (num) < -((float4) PG_INT64_MIN))
1126#define FLOAT8_FITS_IN_INT16(num) \
1127 ((num) >= (float8) PG_INT16_MIN && (num) < -((float8) PG_INT16_MIN))
1128#define FLOAT8_FITS_IN_INT32(num) \
1129 ((num) >= (float8) PG_INT32_MIN && (num) < -((float8) PG_INT32_MIN))
1130#define FLOAT8_FITS_IN_INT64(num) \
1131 ((num) >= (float8) PG_INT64_MIN && (num) < -((float8) PG_INT64_MIN))
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144#define INVERT_COMPARE_RESULT(var) \
1145 ((var) = ((var) < 0) ? 1 : -(var))
1146
1147
1148
1149
1150
1151
1152
1154{
1157
1158
1159
1160
1161
1162
1163
1164#if !(defined(__cplusplus) && defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 9)
1165
1166
1167
1168
1169
1170
1171
1172
1173
1175{
1178
1179
1181{
1184
1185#else
1186
1187
1190
1191#endif
1192
1193
1194#define HIGHBIT (0x80)
1195#define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
1196
1197
1198
1199
1200
1201
1202
1203#define SQL_STR_DOUBLE(ch, escape_backslash) \
1204 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
1205
1206#define ESCAPE_STRING_SYNTAX 'E'
1207
1208
1209#define STATUS_OK (0)
1210#define STATUS_ERROR (-1)
1211#define STATUS_EOF (-2)
1212
1213
1214
1215
1216
1217#ifndef ENABLE_NLS
1218
1219#define gettext(x) (x)
1220#define dgettext(d,x) (x)
1221#define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
1222#define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
1223#endif
1224
1225#define _(x) gettext(x)
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236#define gettext_noop(x) (x)
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251#ifdef SO_MAJOR_VERSION
1252#define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
1253#else
1254#define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1255#endif
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272#if defined(__cplusplus)
1273#define unconstify(underlying_type, expr) const_cast<underlying_type>(expr)
1274#define unvolatize(underlying_type, expr) const_cast<underlying_type>(expr)
1275#elif defined(HAVE__BUILTIN_TYPES_COMPATIBLE_P)
1276#define unconstify(underlying_type, expr) \
1277 (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), const underlying_type), \
1278 "wrong cast"), \
1279 (underlying_type) (expr))
1280#define unvolatize(underlying_type, expr) \
1281 (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), volatile underlying_type), \
1282 "wrong cast"), \
1283 (underlying_type) (expr))
1284#else
1285#define unconstify(underlying_type, expr) \
1286 ((underlying_type) (expr))
1287#define unvolatize(underlying_type, expr) \
1288 ((underlying_type) (expr))
1289#endif
1290
1291
1292
1293
1294
1295#if (defined(__x86_64__) || defined(_M_AMD64))
1296#define USE_SSE2
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306#elif defined(__aarch64__) && defined(__ARM_NEON)
1307#define USE_NEON
1308#endif
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326#if defined(WIN32) || defined(__CYGWIN__)
1327#define PG_BINARY O_BINARY
1328#define PG_BINARY_A "ab"
1329#define PG_BINARY_R "rb"
1330#define PG_BINARY_W "wb"
1331#else
1332#define PG_BINARY 0
1333#define PG_BINARY_A "a"
1334#define PG_BINARY_R "r"
1335#define PG_BINARY_W "w"
1336#endif
1337
1338
1339
1340
1341
1342
1343#if !HAVE_DECL_FDATASYNC
1345#endif
1346
1347
1348
1349
1350
1351
1352#if SIZEOF_LONG == 8
1353#define strtoi64(str, endptr, base) ((int64) strtol(str, endptr, base))
1354#define strtou64(str, endptr, base) ((uint64) strtoul(str, endptr, base))
1355#elif SIZEOF_LONG_LONG == 8
1356#define strtoi64(str, endptr, base) ((int64) strtoll(str, endptr, base))
1357#define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
1358#else
1359#error "cannot find integer type of the same size as int64_t"
1360#endif
1361
1362
1363
1364
1365#if SIZEOF_LONG == 8
1366#define i64abs(i) ((int64) labs(i))
1367#elif SIZEOF_LONG_LONG == 8
1368#define i64abs(i) ((int64) llabs(i))
1369#else
1370#error "cannot find integer type of the same size as int64_t"
1371#endif
1372
1373
1374
1375
1376
1377
1378#ifndef PGDLLIMPORT
1379#define PGDLLIMPORT
1380#endif
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390#ifndef PGDLLEXPORT
1391#ifdef HAVE_VISIBILITY_ATTRIBUTE
1392#define PGDLLEXPORT __attribute__((visibility("default")))
1393#else
1394#define PGDLLEXPORT
1395#endif
1396#endif
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407#ifndef SIGNAL_ARGS
1408#define SIGNAL_ARGS int postgres_signal_arg
1409#endif
1410
1411
1412
1413
1414
1415
1416
1417#ifdef WIN32
1418#ifdef __MINGW64__
1420#define sigsetjmp(x,y) __builtin_setjmp(x)
1421#define siglongjmp __builtin_longjmp
1422#else
1423#define sigjmp_buf jmp_buf
1424#define sigsetjmp(x,y) setjmp(x)
1425#define siglongjmp longjmp
1426#endif
1427#endif
1428
1429
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446#ifdef HAVE_UCHAR_H
1447#include <uchar.h>
1448#else
1449#ifndef __cplusplus
1452#endif
1453#endif
1454
1455
1456
1457#endif
static int fd(const char *x, int i)