1074 {
static_assert(condition,
errmessage); }
while(0)
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089#ifndef __cplusplus
1090#if !defined(_MSC_VER) || _MSC_VER >= 1933
1091#define StaticAssertExpr(condition, errmessage) \
1092 ((void) sizeof(struct {static_assert(condition, errmessage); char a;}))
1093#else
1094
1095
1096
1097
1098#define StaticAssertExpr(condition, errmessage) \
1099 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
1100#endif
1101#else
1102#define StaticAssertExpr(condition, errmessage) \
1103 ([]{static_assert(condition, errmessage);})
1104#endif
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
1119#define StaticAssertVariableIsOfType(varname, typename) \
1120 StaticAssertDecl(__builtin_types_compatible_p(typeof(varname), typename), \
1121 CppAsString(varname) " does not have type " CppAsString(typename))
1122#define StaticAssertVariableIsOfTypeMacro(varname, typename) \
1123 (StaticAssertExpr(__builtin_types_compatible_p(typeof(varname), typename), \
1124 CppAsString(varname) " does not have type " CppAsString(typename)))
1125#else
1126#define StaticAssertVariableIsOfType(varname, typename) \
1127 StaticAssertDecl(sizeof(varname) == sizeof(typename), \
1128 CppAsString(varname) " does not have type " CppAsString(typename))
1129#define StaticAssertVariableIsOfTypeMacro(varname, typename) \
1130 (StaticAssertExpr(sizeof(varname) == sizeof(typename), \
1131 CppAsString(varname) " does not have type " CppAsString(typename)))
1132#endif
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143#define Max(x, y) ((x) > (y) ? (x) : (y))
1144
1145
1146
1147
1148
1149#define Min(x, y) ((x) < (y) ? (x) : (y))
1150
1151
1152
1153#define SIZE_T_ALIGN_MASK (sizeof(size_t) - 1)
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165#define MemSet(start, val, len) \
1166 do \
1167 { \
1168 \
1169 void *_vstart = (void *) (start); \
1170 int _val = (val); \
1171 Size _len = (len); \
1172\
1173 if ((((uintptr_t) _vstart) & SIZE_T_ALIGN_MASK) == 0 && \
1174 (_len & SIZE_T_ALIGN_MASK) == 0 && \
1175 _val == 0 && \
1176 _len <= MEMSET_LOOP_LIMIT && \
1177
1178
1179
1180 \
1181 MEMSET_LOOP_LIMIT != 0) \
1182 { \
1183 size_t *_start = (size_t *) _vstart; \
1184 size_t *_stop = (size_t *) ((char *) _start + _len); \
1185 while (_start < _stop) \
1186 *_start++ = 0; \
1187 } \
1188 else \
1189 memset(_vstart, _val, _len); \
1190 } while (0)
1191
1192
1193
1194
1195
1196
1197
1198#define MemSetAligned(start, val, len) \
1199 do \
1200 { \
1201 size_t *_start = (size_t *) (start); \
1202 int _val = (val); \
1203 Size _len = (len); \
1204\
1205 if ((_len & SIZE_T_ALIGN_MASK) == 0 && \
1206 _val == 0 && \
1207 _len <= MEMSET_LOOP_LIMIT && \
1208 MEMSET_LOOP_LIMIT != 0) \
1209 { \
1210 size_t *_stop = (size_t *) ((char *) _start + _len); \
1211 while (_start < _stop) \
1212 *_start++ = 0; \
1213 } \
1214 else \
1215 memset(_start, _val, _len); \
1216 } while (0)
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230#define FLOAT4_FITS_IN_INT16(num) \
1231 ((num) >= (float4) PG_INT16_MIN && (num) < -((float4) PG_INT16_MIN))
1232#define FLOAT4_FITS_IN_INT32(num) \
1233 ((num) >= (float4) PG_INT32_MIN && (num) < -((float4) PG_INT32_MIN))
1234#define FLOAT4_FITS_IN_INT64(num) \
1235 ((num) >= (float4) PG_INT64_MIN && (num) < -((float4) PG_INT64_MIN))
1236#define FLOAT8_FITS_IN_INT16(num) \
1237 ((num) >= (float8) PG_INT16_MIN && (num) < -((float8) PG_INT16_MIN))
1238#define FLOAT8_FITS_IN_INT32(num) \
1239 ((num) >= (float8) PG_INT32_MIN && (num) < -((float8) PG_INT32_MIN))
1240#define FLOAT8_FITS_IN_INT64(num) \
1241 ((num) >= (float8) PG_INT64_MIN && (num) < -((float8) PG_INT64_MIN))
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254#define INVERT_COMPARE_RESULT(var) \
1255 ((var) = ((var) < 0) ? 1 : -(var))
1256
1257
1258
1259
1260
1261
1262
1264{
1267
1268
1269
1270
1271
1272
1273
1274#if !(defined(__cplusplus) && defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 9)
1275
1276
1277
1278
1279
1280
1281
1282
1283
1285{
1288
1289
1291{
1294
1295#else
1296
1297
1300
1301#endif
1302
1303
1304#define HIGHBIT (0x80)
1305#define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
1306
1307
1308
1309
1310
1311
1312
1313#define SQL_STR_DOUBLE(ch, escape_backslash) \
1314 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
1315
1316#define ESCAPE_STRING_SYNTAX 'E'
1317
1318
1319#define STATUS_OK (0)
1320#define STATUS_ERROR (-1)
1321#define STATUS_EOF (-2)
1322
1323
1324
1325
1326
1327#ifndef ENABLE_NLS
1328
1329#define gettext(x) (x)
1330#define dgettext(d,x) (x)
1331#define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
1332#define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
1333#endif
1334
1335#define _(x) gettext(x)
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346#define gettext_noop(x) (x)
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361#ifdef SO_MAJOR_VERSION
1362#define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
1363#else
1364#define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1365#endif
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382#if defined(__cplusplus)
1383#define unconstify(underlying_type, expr) const_cast<underlying_type>(expr)
1384#define unvolatize(underlying_type, expr) const_cast<underlying_type>(expr)
1385#else
1386#define unconstify(underlying_type, expr) \
1387 (StaticAssertVariableIsOfTypeMacro(expr, const underlying_type), \
1388 (underlying_type) (expr))
1389#define unvolatize(underlying_type, expr) \
1390 (StaticAssertVariableIsOfTypeMacro(expr, volatile underlying_type), \
1391 (underlying_type) (expr))
1392#endif
1393
1394
1395
1396
1397
1398#if defined(__x86_64__)
1399#define USE_SSE2
1400
1401#else
1402
1403
1404
1405
1406
1407
1408#undef USE_AVX2_WITH_RUNTIME_CHECK
1409#undef USE_AVX512_CRC32C_WITH_RUNTIME_CHECK
1410#undef USE_AVX512_POPCNT_WITH_RUNTIME_CHECK
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420#if defined(__aarch64__) && defined(__ARM_NEON)
1421#define USE_NEON
1422#endif
1423#endif
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441#if defined(WIN32) || defined(__CYGWIN__)
1442#define PG_BINARY O_BINARY
1443#define PG_BINARY_A "ab"
1444#define PG_BINARY_R "rb"
1445#define PG_BINARY_W "wb"
1446#else
1447#define PG_BINARY 0
1448#define PG_BINARY_A "a"
1449#define PG_BINARY_R "r"
1450#define PG_BINARY_W "w"
1451#endif
1452
1453
1454
1455
1456
1457
1458#if !HAVE_DECL_FDATASYNC
1460#endif
1461
1462
1463
1464
1465
1466
1467#if SIZEOF_LONG == 8
1468#define strtoi64(str, endptr, base) ((int64) strtol(str, endptr, base))
1469#define strtou64(str, endptr, base) ((uint64) strtoul(str, endptr, base))
1470#elif SIZEOF_LONG_LONG == 8
1471#define strtoi64(str, endptr, base) ((int64) strtoll(str, endptr, base))
1472#define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
1473#else
1474#error "cannot find integer type of the same size as int64_t"
1475#endif
1476
1477
1478
1479
1480#if SIZEOF_LONG == 8
1481#define i64abs(i) ((int64) labs(i))
1482#elif SIZEOF_LONG_LONG == 8
1483#define i64abs(i) ((int64) llabs(i))
1484#else
1485#error "cannot find integer type of the same size as int64_t"
1486#endif
1487
1488
1489
1490
1491
1492
1493#ifndef PGDLLIMPORT
1494#define PGDLLIMPORT
1495#endif
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505#ifndef PGDLLEXPORT
1506#ifdef HAVE_VISIBILITY_ATTRIBUTE
1507#define PGDLLEXPORT __attribute__((visibility("default")))
1508#else
1509#define PGDLLEXPORT
1510#endif
1511#endif
1512
1513
1514
1515
1516
1517
1518
1519
1521{
1524
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535#define SIGNAL_ARGS int postgres_signal_arg, const pg_signal_info *pg_siginfo
1536
1537
1538
1539
1540
1541
1542
1543#ifdef WIN32
1544#ifdef __MINGW64__
1546#define sigsetjmp(x,y) __builtin_setjmp(x)
1547#define siglongjmp __builtin_longjmp
1548#else
1549#define sigjmp_buf jmp_buf
1550#define sigsetjmp(x,y) setjmp(x)
1551#define siglongjmp longjmp
1552#endif
1553#endif
1554
1555
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572#ifdef HAVE_UCHAR_H
1573#include <uchar.h>
1574#else
1575#ifndef __cplusplus
1578#endif
1579#endif
1580
1581
1582
1583#endif
static int fd(const char *x, int i)