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