| 1 |
|
| #ifndef SRL_COMMON_H_ |
| 2 |
|
| #define SRL_COMMON_H_ |
| 3 |
|
| |
| 4 |
|
| #include "srl_inline.h" |
| 5 |
|
| #include <string.h> |
| 6 |
|
| #include <float.h> |
| 7 |
|
| |
| 8 |
|
| /* Legacy quadmath packets use the host byte order for their complete |
| 9 |
|
| * binary128 payload. Reversing its bytes on a big-endian host normalizes |
| 10 |
|
| * that payload for the portable converter. This operation is symmetric, |
| 11 |
|
| * and callers provide separate source and destination storage. FLOAT and |
| 12 |
|
| * DOUBLE use the layout-aware helpers in srl_float.h because their word |
| 13 |
|
| * order need not follow integer byte order. */ |
| 14 |
|
| SRL_STATIC_INLINE void |
| 15 |
|
| srl_copy_little_endian(U8 *dst, const U8 *src, STRLEN len) |
| 16 |
|
| { |
| 17 |
|
| #if BYTEORDER == 0x4321 || BYTEORDER == 0x87654321 |
| 18 |
|
| STRLEN i; |
| 19 |
|
| for (i = 0; i < len; i++) |
| 20 |
|
| dst[i] = src[len - 1 - i]; |
| 21 |
|
| #else |
| 22 |
|
| memcpy(dst, src, len); |
| 23 |
|
| #endif |
| 24 |
|
| } |
| 25 |
|
| |
| 26 |
|
| #define SRL_UNKNOWN_OPTION_POLICY_WARN 0 |
| 27 |
|
| #define SRL_UNKNOWN_OPTION_POLICY_DIE 1 |
| 28 |
|
| #define SRL_UNKNOWN_OPTION_POLICY_IGNORE 2 |
| 29 |
|
| |
| 30 |
|
| SRL_STATIC_INLINE U8 |
| 31 |
0 |
| srl_parse_unknown_option_policy(pTHX_ const char *policy, STRLEN len, |
| 32 |
|
| const char *source) |
| 33 |
|
| { |
| 34 |
0 |
▶ | if (len == sizeof("warn") - 1 && memEQ(policy, "warn", len)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == sizeof("warn") - 1 && memEQ(policy, "warn", len)) |
0 |
0 |
0 |
| if (len == sizeof("warn") - 1 && memEQ(policy, "warn", len)) |
0 |
0 |
0 |
|
| 35 |
0 |
| return SRL_UNKNOWN_OPTION_POLICY_WARN; |
| 36 |
0 |
▶ | if (len == sizeof("die") - 1 && memEQ(policy, "die", len)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == sizeof("die") - 1 && memEQ(policy, "die", len)) |
0 |
0 |
0 |
| if (len == sizeof("die") - 1 && memEQ(policy, "die", len)) |
0 |
0 |
0 |
|
| 37 |
0 |
| return SRL_UNKNOWN_OPTION_POLICY_DIE; |
| 38 |
0 |
▶ | if (len == sizeof("ignore") - 1 && memEQ(policy, "ignore", len)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == sizeof("ignore") - 1 && memEQ(policy, "ignore", len)) |
0 |
0 |
0 |
| if (len == sizeof("ignore") - 1 && memEQ(policy, "ignore", len)) |
0 |
0 |
0 |
|
| 39 |
0 |
| return SRL_UNKNOWN_OPTION_POLICY_IGNORE; |
| 40 |
0 |
| croak("Invalid %s; expected 'warn', 'die', or 'ignore'", source); |
| 41 |
|
| return SRL_UNKNOWN_OPTION_POLICY_WARN; /* not reached */ |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
|
| SRL_STATIC_INLINE U8 |
| 45 |
7 |
| srl_get_unknown_option_policy(pTHX_ SV *option_value, const char *module_policy_var) |
| 46 |
|
| { |
| 47 |
7 |
| SV *value = option_value; |
| 48 |
|
| STRLEN len; |
| 49 |
|
| const char *policy; |
| 50 |
|
| |
| 51 |
7 |
▶ | if (value && SvGMAGICAL(value)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (value && SvGMAGICAL(value)) |
0 |
7 |
7 |
| if (value && SvGMAGICAL(value)) |
0 |
0 |
0 |
|
| 52 |
0 |
| SvGETMAGIC(value); |
| 53 |
7 |
▶ | if (value && SvOK(value)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (value && SvOK(value)) { |
0 |
7 |
7 |
| if (value && SvOK(value)) { |
0 |
0 |
0 |
|
| 54 |
0 |
| policy = SvPVbyte(value, len); |
| 55 |
0 |
| return srl_parse_unknown_option_policy(aTHX_ policy, len, "unknown_option_policy"); |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
7 |
| value = get_sv(module_policy_var, 0); |
| 59 |
7 |
▶ | if (value && SvGMAGICAL(value)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (value && SvGMAGICAL(value)) |
7 |
0 |
7 |
| if (value && SvGMAGICAL(value)) |
0 |
7 |
7 |
|
| 60 |
0 |
| SvGETMAGIC(value); |
| 61 |
7 |
▶ | if (value && SvOK(value)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (value && SvOK(value)) { |
7 |
0 |
7 |
| if (value && SvOK(value)) { |
0 |
7 |
7 |
|
| 62 |
0 |
| policy = SvPVbyte(value, len); |
| 63 |
0 |
| return srl_parse_unknown_option_policy(aTHX_ policy, len, module_policy_var); |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
7 |
| value = get_sv("Sereal::UnknownOptionPolicy", 0); |
| 67 |
7 |
▶ | if (value && SvGMAGICAL(value)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (value && SvGMAGICAL(value)) |
7 |
0 |
7 |
| if (value && SvGMAGICAL(value)) |
0 |
7 |
7 |
|
| 68 |
0 |
| SvGETMAGIC(value); |
| 69 |
7 |
▶ | if (value && SvOK(value)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (value && SvOK(value)) { |
7 |
0 |
7 |
| if (value && SvOK(value)) { |
0 |
7 |
7 |
|
| 70 |
0 |
| policy = SvPVbyte(value, len); |
| 71 |
0 |
| return srl_parse_unknown_option_policy(aTHX_ policy, len, "$Sereal::UnknownOptionPolicy"); |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
7 |
| return SRL_UNKNOWN_OPTION_POLICY_WARN; |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
|
| /* Constructors collect unknown names during their option loop, then resolve |
| 78 |
|
| * the policy once all supplied options (including the policy itself) are read. */ |
| 79 |
|
| SRL_STATIC_INLINE void |
| 80 |
7 |
| srl_handle_unknown_option_keys(pTHX_ AV *keys, SV *policy_value, |
| 81 |
|
| const char *module, const char *module_policy_var) |
| 82 |
|
| { |
| 83 |
7 |
| U8 policy = srl_get_unknown_option_policy(aTHX_ policy_value, module_policy_var); |
| 84 |
7 |
▶ | if (keys && av_len(keys) >= 0 && policy != SRL_UNKNOWN_OPTION_POLICY_IGNORE) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (keys && av_len(keys) >= 0 && policy != SRL_UNKNOWN_OPTION_POLICY_IGNORE) { |
0 |
7 |
7 |
| if (keys && av_len(keys) >= 0 && policy != SRL_UNKNOWN_OPTION_POLICY_IGNORE) { |
0 |
0 |
0 |
| if (keys && av_len(keys) >= 0 && policy != SRL_UNKNOWN_OPTION_POLICY_IGNORE) { |
0 |
0 |
0 |
|
| 85 |
0 |
| SV *key_list = sv_2mortal(newSVpvs("")); |
| 86 |
|
| SSize_t i; |
| 87 |
0 |
▶ | for (i = 0; i <= av_len(keys); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= av_len(keys); i++) { |
0 |
0 |
0 |
|
| 88 |
0 |
| SV **key = av_fetch(keys, i, 0); |
| 89 |
0 |
▶ | if (i) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i) |
0 |
0 |
0 |
|
| 90 |
0 |
| sv_catpvs(key_list, ", "); |
| 91 |
0 |
| sv_catpvs(key_list, "'"); |
| 92 |
0 |
| sv_catsv(key_list, *key); |
| 93 |
0 |
| sv_catpvs(key_list, "'"); |
| 94 |
|
| } |
| 95 |
0 |
▶ | if (policy == SRL_UNKNOWN_OPTION_POLICY_DIE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (policy == SRL_UNKNOWN_OPTION_POLICY_DIE) |
0 |
0 |
0 |
|
| 96 |
0 |
| croak("Unknown option keys passed to %s: %s", module, SvPV_nolen(key_list)); |
| 97 |
0 |
| warn("Unknown option keys passed to %s: %s", module, SvPV_nolen(key_list)); |
| 98 |
|
| } |
| 99 |
7 |
| } |
| 100 |
|
| |
| 101 |
|
| /* inspired by JSON::XS code */ |
| 102 |
|
| #if __GNUC__ >= 3 |
| 103 |
|
| # define expect(expr,value) __builtin_expect((expr), (value)) |
| 104 |
|
| #else |
| 105 |
|
| # define expect(expr,value) (expr) |
| 106 |
|
| #endif |
| 107 |
|
| |
| 108 |
|
| #define expect_false(expr) expect((expr) != 0, 0) |
| 109 |
|
| #define expect_true(expr) expect((expr) != 0, 1) |
| 110 |
|
| |
| 111 |
|
| #define HAS_LONG_FLOAT (sizeof(NV) > sizeof(double)) |
| 112 |
|
| |
| 113 |
|
| #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__) || defined(__x86_64) |
| 114 |
|
| /* SRL_X86_OR_X64_CPU is only used to help set the below macros. */ |
| 115 |
|
| #define SRL_X86_OR_X64_CPU |
| 116 |
|
| #endif |
| 117 |
|
| |
| 118 |
|
| #ifndef SRL_USE_ALIGNED_LOADS_AND_STORES |
| 119 |
|
| |
| 120 |
|
| #ifdef __hpux |
| 121 |
|
| /* HP-UX runs on Itanium but has strict alignment so we check it first. */ |
| 122 |
|
| #define SRL_USE_ALIGNED_LOADS_AND_STORES 1 |
| 123 |
|
| #elif defined(SRL_X86_OR_X64_CPU) |
| 124 |
|
| /* Set SRL_USE_ALIGNED_LOADS_AND_STORES to 0 on CPU's that permit efficient integer loads and stores from unaligned addresses. */ |
| 125 |
|
| #define SRL_USE_ALIGNED_LOADS_AND_STORES 0 |
| 126 |
|
| #else |
| 127 |
|
| /* When in doubt use aligned loads and stores */ |
| 128 |
|
| #define SRL_USE_ALIGNED_LOADS_AND_STORES 1 |
| 129 |
|
| #endif |
| 130 |
|
| |
| 131 |
|
| #endif |
| 132 |
|
| |
| 133 |
|
| |
| 134 |
|
| /* In x86 one can try to enforce strict alignment in runtime. |
| 135 |
|
| * |
| 136 |
|
| * Setting the CPU flag bit 18 (called "AC", aligment check) in |
| 137 |
|
| * the "EFLAGS" (user-settable) causes unaligned access traps but |
| 138 |
|
| * only iff the system register CR0 (only system-settable, usually done |
| 139 |
|
| * (or not) during kernel boot) has the same bit set (there called "AM", |
| 140 |
|
| * alignment mask). If both flags are not set, the strict alignment |
| 141 |
|
| * traps (silently) do not happen. |
| 142 |
|
| * |
| 143 |
|
| * The Linux kernel and the Solarix x86 set the "AM". The Windows and |
| 144 |
|
| * OS X do not. The *BSD behavior is unknown, though suspecting they do. |
| 145 |
|
| * |
| 146 |
|
| * http://en.wikipedia.org/wiki/Control_register |
| 147 |
|
| * http://en.wikipedia.org/wiki/FLAGS_register_(computing) |
| 148 |
|
| */ |
| 149 |
|
| #ifdef SRL_X86_OR_X64_CPU |
| 150 |
|
| # if defined(__x86_64__) || defined(__x86_64) |
| 151 |
|
| # define SRL_TRY_ENABLE_STRICT_ALIGN() asm("pushf\norl $0x40000, (%rsp)\npopf") |
| 152 |
|
| # elif defined(__i386__) || defined(__i386) |
| 153 |
|
| # define SRL_TRY_ENABLE_STRICT_ALIGN() asm("pushf\norl $0x40000, (%esp)\npopf") |
| 154 |
|
| # endif |
| 155 |
|
| #else |
| 156 |
|
| # define SRL_TRY_ENABLE_STRICT_ALIGN() (void)0 |
| 157 |
|
| #endif |
| 158 |
|
| |
| 159 |
|
| /* define constant for other code to use in preallocations or buffer space |
| 160 |
|
| * assertions */ |
| 161 |
|
| #define SRL_MAX_VARINT_LENGTH 11 |
| 162 |
|
| |
| 163 |
|
| |
| 164 |
|
| /* perl 5.25 op_sibling renaming related compat macros. Should probably |
| 165 |
|
| * live in ppport or so. */ |
| 166 |
|
| |
| 167 |
|
| #ifndef OpSIBLING |
| 168 |
|
| # define OpSIBLING(op) ((op)->op_sibling) |
| 169 |
|
| #endif |
| 170 |
|
| |
| 171 |
|
| #ifndef OpHAS_SIBLING |
| 172 |
|
| # define OpHAS_SIBLING(op) ((op)->op_sibling != NULL) |
| 173 |
|
| #endif |
| 174 |
|
| |
| 175 |
|
| /* This is completely opting out, sigh */ |
| 176 |
|
| #ifndef op_parent |
| 177 |
|
| # undef OpLASTSIB_set |
| 178 |
|
| # undef OpMORESIB_set |
| 179 |
|
| # define op_parent(op) NULL |
| 180 |
|
| # define OpMORESIB_set(op, sib) ((op)->op_sibling = (sib)) |
| 181 |
|
| # define OpLASTSIB_set(op, parent) ((op)->op_sibling = NULL) |
| 182 |
|
| #endif |
| 183 |
|
| |
| 184 |
|
| #endif |