| 1 |
|
| /* po_compat.h - the portability floor. |
| 2 |
|
| * |
| 3 |
|
| * Every fact this distribution needs about the platform lives here, so that |
| 4 |
|
| * no other header has to ask twice and no call site has to guess. The probes |
| 5 |
|
| * that set the PO_HAVE_* defines are in Makefile.PL, and they are LINK |
| 6 |
|
| * probes: MSVC exits 0 on an undeclared function and old compilers take an |
| 7 |
|
| * implicit declaration as a warning, so a compile-only probe reports a |
| 8 |
|
| * symbol that is not there and the built .so fails to load. Punk::OpenTelemetry |
| 9 |
|
| * learned that from CPAN Testers; this is the same probe, not a fresh guess. |
| 10 |
|
| * |
| 11 |
|
| * Perl headers must be included before this file. |
| 12 |
|
| */ |
| 13 |
|
| #ifndef PO_COMPAT_H |
| 14 |
|
| #define PO_COMPAT_H |
| 15 |
|
| |
| 16 |
|
| #ifdef _MSC_VER |
| 17 |
|
| # if _MSC_VER < 1600 |
| 18 |
|
| typedef signed char int8_t; |
| 19 |
|
| typedef unsigned char uint8_t; |
| 20 |
|
| typedef short int16_t; |
| 21 |
|
| typedef unsigned short uint16_t; |
| 22 |
|
| typedef int int32_t; |
| 23 |
|
| typedef unsigned int uint32_t; |
| 24 |
|
| typedef __int64 int64_t; |
| 25 |
|
| typedef unsigned __int64 uint64_t; |
| 26 |
|
| # else |
| 27 |
|
| # include <stdint.h> |
| 28 |
|
| # endif |
| 29 |
|
| #else |
| 30 |
|
| # include <stdint.h> |
| 31 |
|
| #endif |
| 32 |
|
| |
| 33 |
|
| #include <string.h> |
| 34 |
|
| |
| 35 |
|
| /* The 64-bit type this dist stores timestamps, series ids and durations in. |
| 36 |
|
| * |
| 37 |
|
| * It is NOT an IV. On the i686, quadmath and long-double smokers IVSIZE is 4, |
| 38 |
|
| * and a unix nanosecond timestamp for 2026 is about 1.79e18 - four hundred |
| 39 |
|
| * million times IV_MAX. A cast through IV truncates silently: the developer's |
| 40 |
|
| * box is fine, the smoker stores a wrong instant, and the test suite is green |
| 41 |
|
| * on both. Nothing in this distribution passes one of these through SvIV, and |
| 42 |
|
| * nothing formats one with %d or %f. See po_u64_to_sv / po_sv_to_u64 below. */ |
| 43 |
|
| typedef uint64_t po_u64; |
| 44 |
|
| typedef int64_t po_i64; |
| 45 |
|
| |
| 46 |
|
| #define PO_U64_MAX ((po_u64)~(po_u64)0) |
| 47 |
|
| |
| 48 |
|
| /* G_LIST IS PERL 5.36 AND THIS DISTRIBUTION SAYS 5.10. |
| 49 |
|
| * |
| 50 |
|
| * It is the modern spelling of G_ARRAY - the same value, renamed - and using |
| 51 |
|
| * it unguarded made every perl below 5.36 fail to COMPILE, which a smoker |
| 52 |
|
| * reported before any test could run. The local perl having it is exactly why |
| 53 |
|
| * that went unnoticed: a name that only has to exist at build time leaves no |
| 54 |
|
| * trace on a box that has it. |
| 55 |
|
| * |
| 56 |
|
| * The new name stays in the code because it is the one the perldoc uses now; |
| 57 |
|
| * this is what makes it mean something on an older perl. */ |
| 58 |
|
| /* isWORDCHAR_A is 5.14. Its callers test one ASCII byte for [0-9A-Za-z_]. */ |
| 59 |
|
| #ifndef isWORDCHAR_A |
| 60 |
|
| # define isWORDCHAR_A(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z') \ |
| 61 |
|
| || ((c) >= '0' && (c) <= '9') || (c) == '_') |
| 62 |
|
| #endif |
| 63 |
|
| |
| 64 |
|
| /* SvIV_please_nomg is 5.18: an NV that is integral becomes an IV as well, the |
| 65 |
|
| * way perl's own arithmetic asks of an operand. */ |
| 66 |
|
| #ifndef SvIV_please_nomg |
| 67 |
|
| # define SvIV_please_nomg(sv) \ |
| 68 |
|
| STMT_START { if (SvNOK(sv) && !SvIOK(sv)) (void)SvIV_nomg(sv); } STMT_END |
| 69 |
|
| #endif |
| 70 |
|
| |
| 71 |
|
| #ifndef G_LIST |
| 72 |
|
| # define G_LIST G_ARRAY |
| 73 |
|
| #endif |
| 74 |
|
| |
| 75 |
|
| /* Is an po_u64 representable in this perl's IV/UV without loss? */ |
| 76 |
|
| #define PO_FITS_UV(v) (UVSIZE >= 8 || (po_u64)(v) <= (po_u64)UV_MAX) |
| 77 |
|
| |
| 78 |
|
| /* A 64-bit value crosses to Perl as a UV where that is lossless and as a |
| 79 |
|
| * decimal STRING where it is not. A string is exact on every perl, sorts and |
| 80 |
|
| * compares correctly after a numeric conversion in the caller, and - unlike an |
| 81 |
|
| * NV - does not quietly drop the last two digits of a nanosecond timestamp. |
| 82 |
|
| * The alternative, an (hi, lo) pair, moves the problem to every call site. */ |
| 83 |
8 |
| static SV *po_u64_string_sv(pTHX_ po_u64 v) { |
| 84 |
|
| char buf[21]; /* 20 digits of UINT64_MAX, plus the NUL */ |
| 85 |
8 |
| char *p = buf + sizeof(buf); |
| 86 |
8 |
| *--p = '\0'; |
| 87 |
8 |
▶ | if (v == 0) *--p = '0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v == 0) *--p = '0'; |
1 |
7 |
8 |
|
| 88 |
104 |
▶ | while (v) { *--p = (char)('0' + (int)(v % 10)); v /= 10; } |
Branch: 1 branch
| Branch | True | False | Total |
| while (v) { *--p = (char)('0' + (int)(v % 10)); v /= 10; } |
96 |
8 |
104 |
|
| 89 |
8 |
| return newSVpv(p, (STRLEN)(buf + sizeof(buf) - 1 - p)); |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
|
| /* A FUNCTION, and that is not a style preference. |
| 93 |
|
| * |
| 94 |
|
| * THE OBVIOUS MACRO EVALUATES ITS ARGUMENT A DIFFERENT NUMBER OF TIMES ON |
| 95 |
|
| * DIFFERENT PERLS. |
| 96 |
|
| * |
| 97 |
|
| * #define po_u64_to_sv(v) (PO_FITS_UV(v) ? newSVuv((UV)(v)) : ...) |
| 98 |
|
| * |
| 99 |
|
| * PO_FITS_UV begins `UVSIZE >= 8 ||`. On a 64-bit perl that is a compile-time |
| 100 |
|
| * true and short-circuits the rest away, so the argument is evaluated once. |
| 101 |
|
| * On a 32-bit-IV perl it is false, the second operand runs, and an argument |
| 102 |
|
| * with a side effect happens TWICE. `po_u64_to_sv(po_br_get(&r, width))` |
| 103 |
|
| * therefore advanced the bit reader twice - on exactly the perls nobody |
| 104 |
|
| * develops on, and nowhere else. Passing through a function makes the count |
| 105 |
|
| * one everywhere. */ |
| 106 |
574505 |
| static SV *po_u64_mksv(pTHX_ po_u64 v) { |
| 107 |
574505 |
| if (PO_FITS_UV(v)) return newSVuv((UV)v); |
| 108 |
|
| return po_u64_string_sv(aTHX_ v); |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
|
| #define po_u64_to_sv(v) po_u64_mksv(aTHX_ (v)) |
| 112 |
|
| |
| 113 |
|
| /* The SIGNED crossing, for values that are two's complement on the wire. |
| 114 |
|
| * |
| 115 |
|
| * OTLP declares an integer attribute `int64`, so the bit pattern stored is |
| 116 |
|
| * signed. Rendering it unsigned turns -1 into 18446744073709551615 - a number |
| 117 |
|
| * that is not wrong so much as unrecognisable, and one that compares above |
| 118 |
|
| * every positive value in a query. */ |
| 119 |
552 |
| static SV *po_i64_mksv(pTHX_ po_u64 bits) { |
| 120 |
|
| po_i64 v; |
| 121 |
552 |
| memcpy(&v, &bits, sizeof(v)); |
| 122 |
552 |
▶ | if (v >= 0) return po_u64_mksv(aTHX_ (po_u64)v); |
Branch: 1 branch
| Branch | True | False | Total |
| if (v >= 0) return po_u64_mksv(aTHX_ (po_u64)v); |
552 |
0 |
552 |
|
| 123 |
|
| /* The magnitude, then a sign. Negating in the signed type is undefined |
| 124 |
|
| * at the bottom of the range, so it is done in the unsigned one. */ |
| 125 |
|
| { |
| 126 |
0 |
| po_u64 mag = (po_u64)0 - bits; |
| 127 |
0 |
| if (IVSIZE >= 8 || mag <= (po_u64)IV_MAX) return newSViv(-(IV)mag); |
| 128 |
|
| { |
| 129 |
|
| SV *sv = po_u64_string_sv(aTHX_ mag); |
| 130 |
|
| SV *out = newSVpvs("-"); |
| 131 |
|
| sv_catsv(out, sv); |
| 132 |
|
| SvREFCNT_dec(sv); |
| 133 |
|
| return out; |
| 134 |
|
| } |
| 135 |
|
| } |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| #define po_i64_to_sv(v) po_i64_mksv(aTHX_ (v)) |
| 139 |
|
| |
| 140 |
|
| /* The inverse, accepting either form. Returns 0 and leaves *out alone if the |
| 141 |
|
| * string is not a bare non-negative integer or would overflow - a caller that |
| 142 |
|
| * ignores the return value gets zero, not garbage. */ |
| 143 |
589750 |
| static int po_sv_to_u64(pTHX_ SV *sv, po_u64 *out) { |
| 144 |
|
| STRLEN len; |
| 145 |
|
| const char *p, *end; |
| 146 |
589750 |
| po_u64 v = 0; |
| 147 |
|
| |
| 148 |
589750 |
▶ | if (!sv || !SvOK(sv)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv || !SvOK(sv)) return 0; |
589750 |
0 |
589750 |
| if (!sv || !SvOK(sv)) return 0; |
45 |
589705 |
589750 |
|
| 149 |
|
| |
| 150 |
|
| /* Only trust the numeric slot where it cannot have lost anything. Taking |
| 151 |
|
| * SvUV of an NV-backed SV is exactly the silent truncation this type |
| 152 |
|
| * exists to avoid, so a string is parsed as a string even when perl has |
| 153 |
|
| * helpfully given it a numeric value too. */ |
| 154 |
589705 |
▶ | if (!SvPOK(sv) && SvIOK(sv) && !SvIsUV(sv) && SvIVX(sv) >= 0) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!SvPOK(sv) && SvIOK(sv) && !SvIsUV(sv) && SvIVX(sv) >= 0) { |
440744 |
148961 |
589705 |
| if (!SvPOK(sv) && SvIOK(sv) && !SvIsUV(sv) && SvIVX(sv) >= 0) { |
440743 |
1 |
440744 |
| if (!SvPOK(sv) && SvIOK(sv) && !SvIsUV(sv) && SvIVX(sv) >= 0) { |
426548 |
14195 |
440743 |
| if (!SvPOK(sv) && SvIOK(sv) && !SvIsUV(sv) && SvIVX(sv) >= 0) { |
426548 |
0 |
426548 |
|
| 155 |
426548 |
| *out = (po_u64)SvIVX(sv); |
| 156 |
426548 |
| return 1; |
| 157 |
|
| } |
| 158 |
163157 |
▶ | if (!SvPOK(sv) && SvIOK(sv) && SvIsUV(sv)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvPOK(sv) && SvIOK(sv) && SvIsUV(sv)) { |
14196 |
148961 |
163157 |
| if (!SvPOK(sv) && SvIOK(sv) && SvIsUV(sv)) { |
14195 |
1 |
14196 |
| if (!SvPOK(sv) && SvIOK(sv) && SvIsUV(sv)) { |
14195 |
0 |
14195 |
|
| 159 |
14195 |
| *out = (po_u64)SvUVX(sv); |
| 160 |
14195 |
| return 1; |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
148962 |
| p = SvPV(sv, len); /* on its own line: see the sibling-arg note */ |
| 164 |
148962 |
| end = p + len; |
| 165 |
148962 |
▶ | if (p == end) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == end) return 0; |
1 |
148961 |
148962 |
|
| 166 |
148961 |
▶ | while (p < end && *p == ' ') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p == ' ') p++; |
148961 |
0 |
148961 |
| while (p < end && *p == ' ') p++; |
0 |
148961 |
148961 |
|
| 167 |
148961 |
▶ | if (p == end) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == end) return 0; |
0 |
148961 |
148961 |
|
| 168 |
1583812 |
▶ | for (; p < end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; p < end; p++) { |
1434857 |
148955 |
1583812 |
|
| 169 |
|
| po_u64 d; |
| 170 |
1434857 |
▶ | if (*p < '0' || *p > '9') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p < '0' || *p > '9') return 0; |
1434855 |
2 |
1434857 |
| if (*p < '0' || *p > '9') return 0; |
3 |
1434852 |
1434855 |
|
| 171 |
1434852 |
| d = (po_u64)(*p - '0'); |
| 172 |
1434852 |
▶ | if (v > (PO_U64_MAX - d) / 10) return 0; /* overflow, not wrap */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (v > (PO_U64_MAX - d) / 10) return 0; /* overflow, not wrap */ |
1 |
1434851 |
1434852 |
|
| 173 |
1434851 |
| v = v * 10 + d; |
| 174 |
|
| } |
| 175 |
148955 |
| *out = v; |
| 176 |
148955 |
| return 1; |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
|
| /* Formats. A Perl-flavoured formatter (croak, warn, sv_catpvf, newSVpvf) |
| 180 |
|
| * reads an NV for %f and an IV for %d, so a double or a 64-bit integer passed |
| 181 |
|
| * to one is undefined: the quadmath smokers panic and x86_64 reads silent |
| 182 |
|
| * garbage. These are the only spellings used in this distribution. */ |
| 183 |
|
| #define PO_UVf "%" UVuf |
| 184 |
|
| #define PO_IVf "%" IVdf |
| 185 |
|
| #define PO_NVf "%" NVff |
| 186 |
|
| |
| 187 |
|
| /* Byte order. The WAL and the segment are little-endian on disk, and the |
| 188 |
|
| * READER swaps rather than the writer, because the writer is the hot path and |
| 189 |
|
| * every box that matters is already little-endian. */ |
| 190 |
|
| #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \ |
| 191 |
|
| __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 192 |
|
| # define PO_BIG_ENDIAN 1 |
| 193 |
|
| #endif |
| 194 |
|
| |
| 195 |
86476 |
| static po_u64 po_le64(po_u64 v) { |
| 196 |
|
| #ifdef PO_BIG_ENDIAN |
| 197 |
|
| return ((v & 0x00000000000000FFULL) << 56) |
| 198 |
|
| | ((v & 0x000000000000FF00ULL) << 40) |
| 199 |
|
| | ((v & 0x0000000000FF0000ULL) << 24) |
| 200 |
|
| | ((v & 0x00000000FF000000ULL) << 8) |
| 201 |
|
| | ((v & 0x000000FF00000000ULL) >> 8) |
| 202 |
|
| | ((v & 0x0000FF0000000000ULL) >> 24) |
| 203 |
|
| | ((v & 0x00FF000000000000ULL) >> 40) |
| 204 |
|
| | ((v & 0xFF00000000000000ULL) >> 56); |
| 205 |
|
| #else |
| 206 |
86476 |
| return v; |
| 207 |
|
| #endif |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
265373 |
| static uint32_t po_le32(uint32_t v) { |
| 211 |
|
| #ifdef PO_BIG_ENDIAN |
| 212 |
|
| return ((v & 0x000000FFU) << 24) | ((v & 0x0000FF00U) << 8) |
| 213 |
|
| | ((v & 0x00FF0000U) >> 8) | ((v & 0xFF000000U) >> 24); |
| 214 |
|
| #else |
| 215 |
265373 |
| return v; |
| 216 |
|
| #endif |
| 217 |
|
| } |
| 218 |
|
| |
| 219 |
|
| /* Atomics. The FreeBSD 9 smoker is gcc 4.2.1 and has no __atomic builtins at |
| 220 |
|
| * all, so the feature is probed rather than inferred from __GNUC__. Hyperman |
| 221 |
|
| * solved this once in hm_atomic.h; where this dist is built beside it that |
| 222 |
|
| * header is used instead of this fallback. */ |
| 223 |
|
| #ifdef PO_HAVE_ATOMICS |
| 224 |
|
| # define po_atomic_add(p, n) __atomic_add_fetch((p), (n), __ATOMIC_SEQ_CST) |
| 225 |
|
| # define po_atomic_load(p) __atomic_load_n((p), __ATOMIC_SEQ_CST) |
| 226 |
|
| # define po_atomic_store(p,v) __atomic_store_n((p), (v), __ATOMIC_SEQ_CST) |
| 227 |
|
| #else |
| 228 |
|
| /* Single-threaded fallback. Correct for one process; the cross-worker |
| 229 |
|
| * counters in phase 15 REQUIRE the real thing and check for it at boot |
| 230 |
|
| * rather than silently miscounting. */ |
| 231 |
|
| # define po_atomic_add(p, n) (*(p) += (n)) |
| 232 |
|
| # define po_atomic_load(p) (*(p)) |
| 233 |
|
| # define po_atomic_store(p,v) (*(p) = (v)) |
| 234 |
|
| #endif |
| 235 |
|
| |
| 236 |
|
| #endif /* PO_COMPAT_H */ |