| 1 |
|
| #include <assert.h> |
| 2 |
|
| |
| 3 |
|
| #include "EXTERN.h" |
| 4 |
|
| #include "perl.h" |
| 5 |
|
| #include "XSUB.h" |
| 6 |
|
| #include "ppport.h" |
| 7 |
|
| |
| 8 |
|
| #define CLONE_KEY(x) ((char *) &x) |
| 9 |
|
| |
| 10 |
|
| /* Maximum safe recursion depth before switching to iterative mode. |
| 11 |
|
| * Each nesting level of [[[...]]] consumes ~3 C stack frames in the |
| 12 |
|
| * recursive clone path (sv_clone for RV + sv_clone for AV + av_clone). |
| 13 |
|
| * The rdepth counter increments once per sv_clone() call, so the |
| 14 |
|
| * nesting level is roughly rdepth/2, using ~450 bytes of stack each. |
| 15 |
|
| * |
| 16 |
|
| * rdepth is passed BY VALUE, so it measures the depth of the current |
| 17 |
|
| * sv_clone() call chain — not the number of calls made. Sibling |
| 18 |
|
| * elements in the av_clone/hv_clone element loops each receive the |
| 19 |
|
| * same parent rdepth, so width does not accumulate: a flat array of |
| 20 |
|
| * N elements peaks at rdepth 3 regardless of N. Only nesting raises |
| 21 |
|
| * it (~2 units per [[[...]]] level, 1 per scalar-ref chain link). |
| 22 |
|
| * Do not "fix" width-driven rdepth inflation; it does not occur. |
| 23 |
|
| * |
| 24 |
|
| * Windows has a 1 MB default thread stack; Cygwin typically 2 MB. |
| 25 |
|
| * Linux/macOS default to 8 MB but some CPAN smokers and containers |
| 26 |
|
| * may have 4 MB or less available after Perl/harness overhead. |
| 27 |
|
| * |
| 28 |
|
| * MAX_DEPTH=2000 on Windows/Cygwin -> ~1000 nesting levels -> ~450 KB. |
| 29 |
|
| * MAX_DEPTH=4000 elsewhere -> ~2000 nesting levels -> ~900 KB. |
| 30 |
|
| * (GH #77: 32000 was too aggressive — caused SEGV on CPAN smokers.) */ |
| 31 |
|
| #if defined(_WIN32) || defined(__CYGWIN__) |
| 32 |
|
| #define MAX_DEPTH 2000 |
| 33 |
|
| #else |
| 34 |
|
| #define MAX_DEPTH 4000 |
| 35 |
|
| #endif |
| 36 |
|
| |
| 37 |
|
| #define CLONE_STORE(x,y) \ |
| 38 |
|
| do { \ |
| 39 |
|
| if (!hv_store(hseen, CLONE_KEY(x), PTRSIZE, SvREFCNT_inc(y), 0)) { \ |
| 40 |
|
| SvREFCNT_dec(y); /* Restore the refcount */ \ |
| 41 |
|
| croak("Can't store clone in seen hash (hseen)"); \ |
| 42 |
|
| } \ |
| 43 |
|
| else { \ |
| 44 |
|
| TRACEME(("storing ref = 0x%" UVxf " clone = 0x%" UVxf "\n", \ |
| 45 |
|
| PTR2UV(x), PTR2UV(y))); \ |
| 46 |
|
| TRACEME(("clone = 0x%" UVxf "(%d)\n", \ |
| 47 |
|
| PTR2UV(y), SvREFCNT(y))); \ |
| 48 |
|
| TRACEME(("ref = 0x%" UVxf "(%d)\n", \ |
| 49 |
|
| PTR2UV(x), SvREFCNT(x))); \ |
| 50 |
|
| } \ |
| 51 |
|
| } while (0) |
| 52 |
|
| |
| 53 |
|
| #define CLONE_FETCH(x) (hv_fetch(hseen, CLONE_KEY(x), PTRSIZE, 0)) |
| 54 |
|
| |
| 55 |
|
| /* Work item for the iterative (past-MAX_DEPTH) cloner: a source |
| 56 |
|
| * container paired with its already-allocated clone shell. */ |
| 57 |
|
| typedef struct { |
| 58 |
|
| SV *src; /* source AV or HV */ |
| 59 |
|
| SV *dst; /* its clone, already registered in hseen, empty */ |
| 60 |
|
| } clone_task; |
| 61 |
|
| |
| 62 |
|
| typedef struct { |
| 63 |
|
| clone_task *items; |
| 64 |
|
| I32 len; |
| 65 |
|
| I32 max; |
| 66 |
|
| /* Scratch buffer rv_clone_chain walks RV chains into. It lives here |
| 67 |
|
| * so it is allocated once per clone rather than once per element, and |
| 68 |
|
| * so it is covered by the queue's croak-safe cleanup. rv_clone_chain |
| 69 |
|
| * never re-enters itself, so a single shared buffer is safe. */ |
| 70 |
|
| SV **chain; |
| 71 |
|
| I32 chain_max; |
| 72 |
|
| } clone_queue; |
| 73 |
|
| |
| 74 |
|
| static SV *hv_clone (SV *, SV *, HV *, int, int, AV *); |
| 75 |
|
| static SV *av_clone (SV *, SV *, HV *, int, int, AV *); |
| 76 |
|
| static SV *sv_clone (SV *, HV *, int, int, AV *); |
| 77 |
|
| static SV *clone_container_iterative(SV *, HV *, int, AV *); |
| 78 |
|
| static SV *rv_clone_chain(SV *, HV *, int, AV *, clone_queue *); |
| 79 |
|
| static SV *rv_clone_iterative(SV *, HV *, int, AV *); |
| 80 |
|
| static int clone_magic(SV *, SV *, HV *, int, AV *); |
| 81 |
|
| |
| 82 |
|
| #ifdef DEBUG_CLONE |
| 83 |
|
| /* __FUNCTION__ is supported by GCC, Clang and MSVC (all versions); |
| 84 |
|
| * __func__ needs C99 (MSVC only since VS2015) and is rejected by a |
| 85 |
|
| * strict -std=c89 -pedantic build. */ |
| 86 |
|
| #define TRACEME(a) do { printf("%s:%d: ",__FUNCTION__, __LINE__); printf a; } while (0) |
| 87 |
|
| #else |
| 88 |
|
| #define TRACEME(a) |
| 89 |
|
| #endif |
| 90 |
|
| |
| 91 |
|
| /* Check whether an mg_obj is a threads::shared::tie instance. |
| 92 |
|
| * The mg_obj is an RV pointing to a blessed PVMG. (GH #18) */ |
| 93 |
|
| static int |
| 94 |
137 |
| is_threads_shared_tie(SV *obj) |
| 95 |
|
| { |
| 96 |
|
| HV *stash; |
| 97 |
137 |
▶ | if (!obj || !SvROK(obj) || !SvOBJECT(SvRV(obj))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!obj || !SvROK(obj) || !SvOBJECT(SvRV(obj))) |
137 |
0 |
137 |
| if (!obj || !SvROK(obj) || !SvOBJECT(SvRV(obj))) |
137 |
0 |
137 |
| if (!obj || !SvROK(obj) || !SvOBJECT(SvRV(obj))) |
0 |
137 |
137 |
|
| 98 |
0 |
| return 0; |
| 99 |
137 |
| stash = SvSTASH(SvRV(obj)); |
| 100 |
274 |
▶ | return stash && HvNAME(stash) |
Branch: 6 branches
| Branch | True | False | Total |
| return stash && HvNAME(stash) |
137 |
0 |
137 |
| return stash && HvNAME(stash) |
137 |
0 |
137 |
| return stash && HvNAME(stash) |
0 |
0 |
0 |
| return stash && HvNAME(stash) |
137 |
0 |
137 |
| return stash && HvNAME(stash) |
0 |
137 |
137 |
| return stash && HvNAME(stash) |
137 |
0 |
137 |
|
| 101 |
411 |
▶ | && strEQ(HvNAME(stash), "threads::shared::tie"); |
Branch: 9 branches
| Branch | True | False | Total |
| && strEQ(HvNAME(stash), "threads::shared::tie"); |
137 |
0 |
137 |
| && strEQ(HvNAME(stash), "threads::shared::tie"); |
0 |
137 |
137 |
| && strEQ(HvNAME(stash), "threads::shared::tie"); |
137 |
0 |
137 |
| && strEQ(HvNAME(stash), "threads::shared::tie"); |
137 |
0 |
137 |
| && strEQ(HvNAME(stash), "threads::shared::tie"); |
0 |
137 |
137 |
| && strEQ(HvNAME(stash), "threads::shared::tie"); |
0 |
0 |
0 |
| && strEQ(HvNAME(stash), "threads::shared::tie"); |
137 |
0 |
137 |
| && strEQ(HvNAME(stash), "threads::shared::tie"); |
0 |
137 |
137 |
| && strEQ(HvNAME(stash), "threads::shared::tie"); |
0 |
137 |
137 |
|
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
|
| static SV * |
| 105 |
21226 |
| hv_clone (SV * ref, SV * target, HV* hseen, int depth, int rdepth, AV * weakrefs) |
| 106 |
|
| { |
| 107 |
21226 |
| HV *clone = (HV *) target; |
| 108 |
21226 |
| HV *self = (HV *) ref; |
| 109 |
21226 |
| HE *next = NULL; |
| 110 |
21226 |
▶ | int recur = depth ? depth - 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int recur = depth ? depth - 1 : 0; |
21226 |
0 |
21226 |
|
| 111 |
|
| |
| 112 |
|
| assert(SvTYPE(ref) == SVt_PVHV); |
| 113 |
|
| |
| 114 |
|
| TRACEME(("ref = 0x%" UVxf "(%d)\n", PTR2UV(ref), SvREFCNT(ref))); |
| 115 |
|
| |
| 116 |
|
| /* Pre-size the target hash to avoid incremental resizing */ |
| 117 |
21226 |
▶ | if (HvKEYS(self) > 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (HvKEYS(self) > 0) |
0 |
21226 |
21226 |
| if (HvKEYS(self) > 0) |
21221 |
5 |
21226 |
|
| 118 |
21221 |
▶ | hv_ksplit(clone, HvKEYS(self)); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_ksplit(clone, HvKEYS(self)); |
0 |
21221 |
21221 |
|
| 119 |
|
| |
| 120 |
21226 |
| hv_iterinit (self); |
| 121 |
55612 |
▶ | while ((next = hv_iternext (self))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((next = hv_iternext (self))) |
34386 |
21226 |
55612 |
|
| 122 |
|
| { |
| 123 |
|
| I32 klen; |
| 124 |
34386 |
| char *kpv = hv_iterkey(next, &klen); |
| 125 |
34386 |
| SV *val = sv_clone(hv_iterval(self, next), hseen, recur, rdepth, weakrefs); |
| 126 |
|
| /* Use hv_iterkey + HeHASH to avoid allocating a mortal SV per key. |
| 127 |
|
| * Negate klen for UTF-8 keys per Perl API convention. */ |
| 128 |
34386 |
▶ | if (HeKUTF8(next)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (HeKUTF8(next)) |
503 |
33883 |
34386 |
|
| 129 |
503 |
| klen = -klen; |
| 130 |
|
| TRACEME(("clone item %.*s\n", (int)(klen > 0 ? klen : -klen), kpv)); |
| 131 |
34386 |
| hv_store(clone, kpv, klen, val, HeHASH(next)); |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
|
| TRACEME(("clone = 0x%" UVxf "(%d)\n", PTR2UV(clone), SvREFCNT(clone))); |
| 135 |
21226 |
| return (SV *) clone; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| /* ------------------------------------------------------------------- * |
| 139 |
|
| * Iterative container cloning (used once rdepth exceeds MAX_DEPTH) |
| 140 |
|
| * |
| 141 |
|
| * Past MAX_DEPTH the recursive path would overflow the C stack, so |
| 142 |
|
| * nested containers are cloned through an explicit, heap-allocated work |
| 143 |
|
| * queue instead. Each task pairs a source container with an empty clone |
| 144 |
|
| * shell that is already registered in hseen; draining a task fills the |
| 145 |
|
| * shell and pushes a new task for every nested container it finds. |
| 146 |
|
| * |
| 147 |
|
| * The previous implementation only unrolled single-element array chains |
| 148 |
|
| * and otherwise recursed back through sv_clone, costing one C stack |
| 149 |
|
| * frame per nesting level for hashes and mixed array/hash structures. |
| 150 |
|
| * That overflowed Windows' 1 MB default thread stack (GH #121, GH #146). |
| 151 |
|
| * The queue makes C stack usage O(1) in the nesting depth for every shape. |
| 152 |
|
| * ------------------------------------------------------------------- */ |
| 153 |
|
| |
| 154 |
|
| /* Registered with SAVEDESTRUCTOR_X so the queue is released both on the |
| 155 |
|
| * normal path (at LEAVE) and when cloning croaks — a dying __WARN__ |
| 156 |
|
| * handler on the depth-limit warning, or a tied FETCH, longjmps straight |
| 157 |
|
| * past any explicit Safefree. The queue itself lives on the heap because |
| 158 |
|
| * the savestack is unwound after our C frame is already gone. */ |
| 159 |
|
| static void |
| 160 |
32 |
| clone_queue_free(pTHX_ void *p) |
| 161 |
|
| { |
| 162 |
32 |
| clone_queue *q = (clone_queue *) p; |
| 163 |
|
| I32 i; |
| 164 |
|
| |
| 165 |
47488 |
▶ | for (i = 0; i < q->len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < q->len; i++) |
47456 |
32 |
47488 |
|
| 166 |
47456 |
| SvREFCNT_dec(q->items[i].src); |
| 167 |
|
| |
| 168 |
32 |
| Safefree(q->items); |
| 169 |
32 |
| Safefree(q->chain); |
| 170 |
32 |
| Safefree(q); |
| 171 |
32 |
| } |
| 172 |
|
| |
| 173 |
|
| static void |
| 174 |
47456 |
| clone_queue_push(clone_queue *q, SV *src, SV *dst) |
| 175 |
|
| { |
| 176 |
47456 |
▶ | if (q->len >= q->max) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (q->len >= q->max) { |
131 |
47325 |
47456 |
|
| 177 |
131 |
▶ | q->max = q->max ? q->max * 2 : 64; |
Branch: 1 branch
| Branch | True | False | Total |
| q->max = q->max ? q->max * 2 : 64; |
107 |
24 |
131 |
|
| 178 |
131 |
▶ | if (q->items) |
Branch: 1 branch
| Branch | True | False | Total |
| if (q->items) |
107 |
24 |
131 |
|
| 179 |
107 |
| Renew(q->items, q->max, clone_task); |
| 180 |
|
| else |
| 181 |
24 |
| Newx(q->items, q->max, clone_task); |
| 182 |
|
| } |
| 183 |
|
| /* Hold a reference to the source until the task is drained. Filling |
| 184 |
|
| * is deferred, and anything running in between (a tied FETCH, a |
| 185 |
|
| * DESTROY, a __WARN__ handler) can drop the caller's last reference to |
| 186 |
|
| * a container we have already queued. dst needs no such reference: |
| 187 |
|
| * hseen holds one. clone_queue_free releases these. */ |
| 188 |
47456 |
| q->items[q->len].src = SvREFCNT_inc_simple_NN(src); |
| 189 |
47456 |
| q->items[q->len].dst = dst; |
| 190 |
47456 |
| q->len++; |
| 191 |
47456 |
| } |
| 192 |
|
| |
| 193 |
|
| /* Return the clone of a container (AV or HV), creating an empty shell and |
| 194 |
|
| * queueing it for filling the first time we see it. Registering the shell |
| 195 |
|
| * in hseen before it is filled is what makes circular references safe. |
| 196 |
|
| * The returned SV carries one reference for the caller. */ |
| 197 |
|
| static SV * |
| 198 |
47461 |
| clone_shell(SV *ref, HV *hseen, clone_queue *q) |
| 199 |
|
| { |
| 200 |
|
| SV **seen; |
| 201 |
|
| SV *clone; /* named to match CLONE_STORE's TRACEME under DEBUG_CLONE */ |
| 202 |
|
| |
| 203 |
47461 |
▶ | if ((seen = CLONE_FETCH(ref))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((seen = CLONE_FETCH(ref))) |
5 |
47456 |
47461 |
|
| 204 |
5 |
| return SvREFCNT_inc(*seen); |
| 205 |
|
| |
| 206 |
47456 |
▶ | clone = (SvTYPE(ref) == SVt_PVHV) ? (SV *) newHV() : (SV *) newAV(); |
Branch: 1 branch
| Branch | True | False | Total |
| clone = (SvTYPE(ref) == SVt_PVHV) ? (SV *) newHV() : (SV *) newAV(); |
14010 |
33446 |
47456 |
|
| 207 |
47456 |
▶ | CLONE_STORE(ref, clone); |
Branch: 1 branch
| Branch | True | False | Total |
| CLONE_STORE(ref, clone); |
0 |
47456 |
47456 |
|
| 208 |
47456 |
| clone_queue_push(q, ref, clone); |
| 209 |
|
| |
| 210 |
47456 |
| return clone; |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
|
| /* Clone one element of a container without recursing into nested |
| 214 |
|
| * containers: those become queued tasks instead. */ |
| 215 |
|
| static SV * |
| 216 |
47457 |
| clone_elem(SV *e, HV *hseen, int rdepth, AV *weakrefs, clone_queue *q) |
| 217 |
|
| { |
| 218 |
47457 |
▶ | if (!e) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) |
0 |
47457 |
47457 |
|
| 219 |
0 |
| return NULL; |
| 220 |
|
| |
| 221 |
47457 |
▶ | if (SvROK(e)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(e)) { |
47440 |
17 |
47457 |
|
| 222 |
47440 |
| SV *referent = SvRV(e); |
| 223 |
|
| |
| 224 |
|
| /* Handled inline rather than through rv_clone_chain: a direct |
| 225 |
|
| * reference to a container is by far the common case, and this |
| 226 |
|
| * skips the chain walk for a chain of length one. */ |
| 227 |
47440 |
▶ | if (referent |
Branch: 1 branch
| Branch | True | False | Total |
| if (referent |
47440 |
0 |
47440 |
|
| 228 |
47440 |
▶ | && (SvTYPE(referent) == SVt_PVAV || SvTYPE(referent) == SVt_PVHV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| && (SvTYPE(referent) == SVt_PVAV || SvTYPE(referent) == SVt_PVHV)) { |
14004 |
33436 |
47440 |
| && (SvTYPE(referent) == SVt_PVAV || SvTYPE(referent) == SVt_PVHV)) { |
14001 |
3 |
14004 |
|
| 229 |
47437 |
| SV *new_rv = newRV_noinc(clone_shell(referent, hseen, q)); |
| 230 |
47437 |
▶ | if (SvOBJECT(referent)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOBJECT(referent)) |
10499 |
36938 |
47437 |
|
| 231 |
10499 |
| sv_bless(new_rv, SvSTASH(referent)); |
| 232 |
47437 |
▶ | if (SvWEAKREF(e)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvWEAKREF(e)) |
0 |
47437 |
47437 |
|
| 233 |
0 |
| av_push(weakrefs, SvREFCNT_inc_simple_NN(new_rv)); |
| 234 |
47437 |
| return new_rv; |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
|
| /* Scalar-ref chain: walked iteratively, container leaves rejoin |
| 238 |
|
| * this queue. */ |
| 239 |
3 |
| return rv_clone_chain(e, hseen, rdepth, weakrefs, q); |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
|
| /* Plain scalar leaf. rdepth is above MAX_DEPTH here, so sv_clone |
| 243 |
|
| * takes its non-recursive branch (newSVsv, or share-with-warning for |
| 244 |
|
| * types that cannot be copied at all). */ |
| 245 |
17 |
| return sv_clone(e, hseen, 1, rdepth, weakrefs); |
| 246 |
|
| } |
| 247 |
|
| |
| 248 |
|
| static void |
| 249 |
33446 |
| clone_fill_av(AV *src, AV *dst, HV *hseen, int rdepth, AV *weakrefs, |
| 250 |
|
| clone_queue *q) |
| 251 |
|
| { |
| 252 |
|
| SV **svp; |
| 253 |
|
| SV **slot; |
| 254 |
|
| I32 arrlen; |
| 255 |
|
| I32 i; |
| 256 |
|
| |
| 257 |
33446 |
| arrlen = av_len(src); |
| 258 |
33446 |
▶ | if (arrlen < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (arrlen < 0) |
3 |
33443 |
33446 |
|
| 259 |
3 |
| return; |
| 260 |
|
| |
| 261 |
33443 |
| av_extend(dst, arrlen); |
| 262 |
|
| |
| 263 |
|
| /* Fetch from the source (which may be magical) but write straight |
| 264 |
|
| * into the target's AvARRAY: we just created it, so it has no magic. */ |
| 265 |
33443 |
| slot = AvARRAY(dst); |
| 266 |
66890 |
▶ | for (i = 0; i <= arrlen; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= arrlen; i++) { |
33447 |
33443 |
66890 |
|
| 267 |
33447 |
| svp = av_fetch(src, i, 0); |
| 268 |
33447 |
▶ | if (svp) |
Branch: 1 branch
| Branch | True | False | Total |
| if (svp) |
33446 |
1 |
33447 |
|
| 269 |
33446 |
| slot[i] = clone_elem(*svp, hseen, rdepth, weakrefs, q); |
| 270 |
|
| } |
| 271 |
33443 |
| AvFILLp(dst) = arrlen; |
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
|
| static void |
| 275 |
14010 |
| clone_fill_hv(HV *src, HV *dst, HV *hseen, int rdepth, AV *weakrefs, |
| 276 |
|
| clone_queue *q) |
| 277 |
|
| { |
| 278 |
|
| HE *next; |
| 279 |
|
| |
| 280 |
|
| /* Pre-size to avoid incremental resizing */ |
| 281 |
14010 |
▶ | if (HvKEYS(src) > 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (HvKEYS(src) > 0) |
0 |
14010 |
14010 |
| if (HvKEYS(src) > 0) |
14009 |
1 |
14010 |
|
| 282 |
14009 |
▶ | hv_ksplit(dst, HvKEYS(src)); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_ksplit(dst, HvKEYS(src)); |
0 |
14009 |
14009 |
|
| 283 |
|
| |
| 284 |
14010 |
| hv_iterinit(src); |
| 285 |
28021 |
▶ | while ((next = hv_iternext(src))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((next = hv_iternext(src))) { |
14011 |
14010 |
28021 |
|
| 286 |
|
| I32 klen; |
| 287 |
14011 |
| char *kpv = hv_iterkey(next, &klen); |
| 288 |
14011 |
| SV *val = clone_elem(hv_iterval(src, next), hseen, rdepth, |
| 289 |
|
| weakrefs, q); |
| 290 |
|
| /* Negate klen for UTF-8 keys per Perl API convention. */ |
| 291 |
14011 |
▶ | if (HeKUTF8(next)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (HeKUTF8(next)) |
0 |
14011 |
14011 |
|
| 292 |
0 |
| klen = -klen; |
| 293 |
14011 |
| hv_store(dst, kpv, klen, val, HeHASH(next)); |
| 294 |
|
| } |
| 295 |
14010 |
| } |
| 296 |
|
| |
| 297 |
|
| /* Fill every queued shell. Tasks appended while draining are picked up |
| 298 |
|
| * by the same loop, so the whole sub-graph is cloned without recursion. |
| 299 |
|
| * q->items may be reallocated by clone_queue_push, hence the re-indexing |
| 300 |
|
| * on each iteration rather than a cached pointer. */ |
| 301 |
|
| static void |
| 302 |
32 |
| clone_drain(clone_queue *q, HV *hseen, int rdepth, AV *weakrefs) |
| 303 |
|
| { |
| 304 |
|
| I32 i; |
| 305 |
|
| |
| 306 |
47488 |
▶ | for (i = 0; i < q->len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < q->len; i++) { |
47456 |
32 |
47488 |
|
| 307 |
47456 |
| SV *src = q->items[i].src; |
| 308 |
47456 |
| SV *dst = q->items[i].dst; |
| 309 |
|
| |
| 310 |
47456 |
▶ | if (SvTYPE(src) == SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(src) == SVt_PVHV) |
14010 |
33446 |
47456 |
|
| 311 |
14010 |
| clone_fill_hv((HV *)src, (HV *)dst, hseen, rdepth, weakrefs, q); |
| 312 |
|
| else |
| 313 |
33446 |
| clone_fill_av((AV *)src, (AV *)dst, hseen, rdepth, weakrefs, q); |
| 314 |
|
| } |
| 315 |
32 |
| } |
| 316 |
|
| |
| 317 |
|
| /* Entry point for cloning an AV or HV past MAX_DEPTH. */ |
| 318 |
|
| static SV * |
| 319 |
0 |
| clone_container_iterative(SV * ref, HV* hseen, int rdepth, AV * weakrefs) |
| 320 |
|
| { |
| 321 |
|
| clone_queue *q; |
| 322 |
|
| SV *root_clone; |
| 323 |
|
| |
| 324 |
0 |
▶ | if (!ref) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ref) return NULL; |
0 |
0 |
0 |
|
| 325 |
|
| |
| 326 |
0 |
| Newxz(q, 1, clone_queue); |
| 327 |
0 |
| ENTER; |
| 328 |
0 |
| SAVEDESTRUCTOR_X(clone_queue_free, q); |
| 329 |
|
| |
| 330 |
0 |
| root_clone = clone_shell(ref, hseen, q); |
| 331 |
0 |
| clone_drain(q, hseen, rdepth, weakrefs); |
| 332 |
|
| |
| 333 |
0 |
| LEAVE; |
| 334 |
0 |
| return root_clone; |
| 335 |
|
| } |
| 336 |
|
| |
| 337 |
|
| /* Iterative clone for deeply nested scalar-ref chains past MAX_DEPTH. |
| 338 |
|
| * Avoids stack overflow by unrolling the RV->RV->...->leaf chain without |
| 339 |
|
| * recursion, then rebuilding from the bottom up. |
| 340 |
|
| * |
| 341 |
|
| * This mirrors clone_container_iterative: instead of returning |
| 342 |
|
| * SvREFCNT_inc(ref) (a shared alias), it produces a true deep copy of the |
| 343 |
|
| * entire scalar-ref chain, preserving isolation. (GH #107) |
| 344 |
|
| * |
| 345 |
|
| * A container at the end of the chain is handed to the caller's work |
| 346 |
|
| * queue rather than cloned inline, so an alternating ref/container |
| 347 |
|
| * structure costs no C stack either. */ |
| 348 |
|
| static SV * |
| 349 |
35 |
| rv_clone_chain(SV * ref, HV* hseen, int rdepth, AV * weakrefs, clone_queue *q) |
| 350 |
|
| { |
| 351 |
|
| SV **chain; |
| 352 |
|
| I32 chain_len; |
| 353 |
|
| SV *current; |
| 354 |
|
| SV *leaf_clone; |
| 355 |
|
| SV *result; |
| 356 |
|
| SV **seen; |
| 357 |
|
| I32 i; |
| 358 |
|
| |
| 359 |
35 |
▶ | if (!ref || !SvROK(ref)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ref || !SvROK(ref)) return NULL; |
35 |
0 |
35 |
| if (!ref || !SvROK(ref)) return NULL; |
0 |
35 |
35 |
|
| 360 |
|
| |
| 361 |
35 |
▶ | if (!q->chain) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!q->chain) { |
32 |
3 |
35 |
|
| 362 |
32 |
| q->chain_max = 64; |
| 363 |
32 |
| Newx(q->chain, q->chain_max, SV *); |
| 364 |
|
| } |
| 365 |
35 |
| chain = q->chain; |
| 366 |
35 |
| chain_len = 0; |
| 367 |
|
| |
| 368 |
|
| /* Walk the RV chain, collecting each node until we reach a non-RV leaf |
| 369 |
|
| * or an RV we have already cloned. |
| 370 |
|
| * |
| 371 |
|
| * Cycle guard: a chain reachable only past MAX_DEPTH never passed |
| 372 |
|
| * through the recursive sv_clone path, so nothing registered its nodes |
| 373 |
|
| * in hseen and it can fold back on itself ("my $x; $x = \$x" hung below |
| 374 |
|
| * a deep spine, growing chain[] via Renew() until allocation aborted). |
| 375 |
|
| * Registering a placeholder RV for every link as we descend makes the |
| 376 |
|
| * revisit visible: CLONE_FETCH below then terminates the walk and the |
| 377 |
|
| * rebuild closes the cycle onto the placeholder. The placeholder is a |
| 378 |
|
| * live RV (to undef) rather than an empty SV so the rebuild can simply |
| 379 |
|
| * retarget it, exactly as the recursive path does in sv_clone. |
| 380 |
|
| * |
| 381 |
|
| * The ordering matters for more than cycles: each node joins chain[] |
| 382 |
|
| * before we descend into its referent, so a referent found cached on |
| 383 |
|
| * the next iteration still has its wrapping RV in the chain and the |
| 384 |
|
| * rebuild produces a proper RV->referent_clone. Checking the referent |
| 385 |
|
| * before adding the wrapper would hand a bare HV/AV back to a caller |
| 386 |
|
| * expecting an RV ("Bizarre copy of HASH"). */ |
| 387 |
35 |
| leaf_clone = NULL; |
| 388 |
35 |
| current = ref; |
| 389 |
7081 |
▶ | while (current && SvROK(current)) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (current && SvROK(current)) { |
7081 |
0 |
7081 |
| while (current && SvROK(current)) { |
7052 |
29 |
7081 |
|
| 390 |
|
| SV **already; |
| 391 |
|
| SV *placeholder; |
| 392 |
|
| |
| 393 |
7052 |
▶ | if ((already = CLONE_FETCH(current))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((already = CLONE_FETCH(current))) { |
6 |
7046 |
7052 |
|
| 394 |
|
| /* Cycle, or an RV shared with somewhere already cloned. */ |
| 395 |
6 |
| leaf_clone = SvREFCNT_inc(*already); |
| 396 |
6 |
| break; |
| 397 |
|
| } |
| 398 |
|
| |
| 399 |
7046 |
▶ | if (chain_len >= q->chain_max) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (chain_len >= q->chain_max) { |
30 |
7016 |
7046 |
|
| 400 |
30 |
| q->chain_max *= 2; |
| 401 |
30 |
| Renew(q->chain, q->chain_max, SV *); |
| 402 |
30 |
| chain = q->chain; |
| 403 |
|
| } |
| 404 |
7046 |
| chain[chain_len++] = current; |
| 405 |
|
| |
| 406 |
|
| /* hseen takes the placeholder's only reference (CLONE_STORE incs, |
| 407 |
|
| * so drop ours). Holding a second one here would leak the whole |
| 408 |
|
| * chain if cloning croaked before the rebuild below could hand it |
| 409 |
|
| * on -- hseen is released during unwinding, nothing else is. */ |
| 410 |
7046 |
| placeholder = newRV_noinc(newSV(0)); |
| 411 |
7046 |
▶ | CLONE_STORE(current, placeholder); |
Branch: 1 branch
| Branch | True | False | Total |
| CLONE_STORE(current, placeholder); |
0 |
7046 |
7046 |
|
| 412 |
7046 |
| SvREFCNT_dec(placeholder); |
| 413 |
|
| |
| 414 |
7046 |
| current = SvRV(current); |
| 415 |
|
| } |
| 416 |
|
| |
| 417 |
|
| /* If we did not hit a cached referent above, current is now the non-RV |
| 418 |
|
| * leaf; clone it based on its type. */ |
| 419 |
35 |
▶ | if (!leaf_clone && current) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!leaf_clone && current) { |
29 |
6 |
35 |
| if (!leaf_clone && current) { |
29 |
0 |
29 |
|
| 420 |
29 |
▶ | if (SvTYPE(current) == SVt_PVAV || SvTYPE(current) == SVt_PVHV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTYPE(current) == SVt_PVAV || SvTYPE(current) == SVt_PVHV) { |
15 |
14 |
29 |
| if (SvTYPE(current) == SVt_PVAV || SvTYPE(current) == SVt_PVHV) { |
10 |
5 |
15 |
|
| 421 |
24 |
| leaf_clone = clone_shell(current, hseen, q); |
| 422 |
|
| } else { |
| 423 |
5 |
| seen = CLONE_FETCH(current); |
| 424 |
5 |
▶ | if (seen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (seen) { |
0 |
5 |
5 |
|
| 425 |
0 |
| leaf_clone = SvREFCNT_inc(*seen); |
| 426 |
|
| } else { |
| 427 |
|
| /* Mirror the non-cloneable cases from the regular sv_clone |
| 428 |
|
| * switch (PVCV/PVGV/PVFM/PVIO/PVLV/REGEXP/BM): newSVsv() |
| 429 |
|
| * croaks on these ("Bizarre copy of CODE in subroutine |
| 430 |
|
| * entry") or stringifies them. Share via SvREFCNT_inc |
| 431 |
|
| * instead. */ |
| 432 |
5 |
▶ | switch (SvTYPE(current)) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (SvTYPE(current)) { |
3 |
2 |
5 |
|
| 433 |
|
| #if PERL_VERSION <= 8 |
| 434 |
|
| case SVt_PVBM: /* 8 */ |
| 435 |
|
| #elif PERL_VERSION >= 11 |
| 436 |
3 |
| case SVt_REGEXP: /* 8 */ |
| 437 |
|
| #endif |
| 438 |
|
| case SVt_PVLV: /* 9 */ |
| 439 |
|
| case SVt_PVCV: /* 12 */ |
| 440 |
|
| case SVt_PVGV: /* 13 */ |
| 441 |
|
| case SVt_PVFM: /* 14 */ |
| 442 |
|
| case SVt_PVIO: /* 15 */ |
| 443 |
3 |
| leaf_clone = SvREFCNT_inc(current); |
| 444 |
3 |
| break; |
| 445 |
2 |
| default: |
| 446 |
2 |
| leaf_clone = newSVsv(current); |
| 447 |
2 |
▶ | if ((SvREFCNT(current) > 1) || SvMAGICAL(current)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((SvREFCNT(current) > 1) || SvMAGICAL(current)) |
0 |
2 |
2 |
| if ((SvREFCNT(current) > 1) || SvMAGICAL(current)) |
0 |
0 |
0 |
|
| 448 |
2 |
▶ | CLONE_STORE(current, leaf_clone); |
Branch: 1 branch
| Branch | True | False | Total |
| CLONE_STORE(current, leaf_clone); |
0 |
2 |
2 |
|
| 449 |
2 |
| break; |
| 450 |
|
| } |
| 451 |
|
| } |
| 452 |
|
| } |
| 453 |
|
| } |
| 454 |
|
| |
| 455 |
|
| /* Degenerate case (a ref with no referent at all): the placeholders are |
| 456 |
|
| * already registered in hseen, so hand the chain an undef leaf rather |
| 457 |
|
| * than returning the original and leaving hseen inconsistent. */ |
| 458 |
35 |
▶ | if (!leaf_clone) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!leaf_clone) |
0 |
35 |
35 |
|
| 459 |
0 |
| leaf_clone = newSV(0); |
| 460 |
|
| |
| 461 |
|
| /* Rebuild the RV chain from the bottom up by retargeting each link's |
| 462 |
|
| * placeholder at the clone below it. Retargeting rather than building |
| 463 |
|
| * fresh RVs is what lets a cycle close: the link that folded back |
| 464 |
|
| * already holds the placeholder it has to point at. */ |
| 465 |
35 |
| result = leaf_clone; |
| 466 |
7081 |
▶ | for (i = chain_len - 1; i >= 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = chain_len - 1; i >= 0; i--) { |
7046 |
35 |
7081 |
|
| 467 |
7046 |
| SV *rv = chain[i]; |
| 468 |
7046 |
| SV **php = CLONE_FETCH(rv); |
| 469 |
|
| SV *new_rv; |
| 470 |
|
| |
| 471 |
7046 |
▶ | if (!php) /* cannot happen: stored during the walk */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!php) /* cannot happen: stored during the walk */ |
0 |
7046 |
7046 |
|
| 472 |
0 |
| continue; |
| 473 |
7046 |
| new_rv = *php; |
| 474 |
|
| |
| 475 |
7046 |
| SvREFCNT_dec(SvRV(new_rv)); /* drop the placeholder's undef */ |
| 476 |
7046 |
| SvRV_set(new_rv, result); /* hands our reference to new_rv */ |
| 477 |
|
| |
| 478 |
7046 |
▶ | if (SvOBJECT(SvRV(rv))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOBJECT(SvRV(rv))) |
5 |
7041 |
7046 |
|
| 479 |
5 |
| sv_bless(new_rv, SvSTASH(SvRV(rv))); |
| 480 |
7046 |
▶ | if (SvWEAKREF(rv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvWEAKREF(rv)) |
0 |
7046 |
7046 |
|
| 481 |
0 |
| av_push(weakrefs, SvREFCNT_inc_simple_NN(new_rv)); |
| 482 |
|
| |
| 483 |
|
| /* new_rv is owned by hseen; take a reference for whoever ends up |
| 484 |
|
| * holding this link (the next link up, or our caller). */ |
| 485 |
7046 |
| result = SvREFCNT_inc_simple_NN(new_rv); |
| 486 |
|
| } |
| 487 |
|
| |
| 488 |
35 |
| return result; |
| 489 |
|
| } |
| 490 |
|
| |
| 491 |
|
| /* Entry point for cloning a reference past MAX_DEPTH: owns the work |
| 492 |
|
| * queue that rv_clone_chain and clone_elem feed. */ |
| 493 |
|
| static SV * |
| 494 |
32 |
| rv_clone_iterative(SV * ref, HV* hseen, int rdepth, AV * weakrefs) |
| 495 |
|
| { |
| 496 |
|
| clone_queue *q; |
| 497 |
|
| SV *result; |
| 498 |
|
| |
| 499 |
32 |
| Newxz(q, 1, clone_queue); |
| 500 |
32 |
| ENTER; |
| 501 |
32 |
| SAVEDESTRUCTOR_X(clone_queue_free, q); |
| 502 |
|
| |
| 503 |
32 |
| result = rv_clone_chain(ref, hseen, rdepth, weakrefs, q); |
| 504 |
32 |
| clone_drain(q, hseen, rdepth, weakrefs); |
| 505 |
|
| |
| 506 |
32 |
| LEAVE; |
| 507 |
32 |
| return result; |
| 508 |
|
| } |
| 509 |
|
| |
| 510 |
|
| /* Clone all magic entries from ref onto clone. |
| 511 |
|
| * Returns the number of tie-magic entries cloned; the caller uses this |
| 512 |
|
| * to decide whether to skip direct HV/AV element iteration (tied |
| 513 |
|
| * containers are managed entirely by their tie magic). */ |
| 514 |
|
| static int |
| 515 |
200047 |
| clone_magic(SV * ref, SV * clone, HV* hseen, int rdepth, AV * weakrefs) |
| 516 |
|
| { |
| 517 |
|
| MAGIC* mg; |
| 518 |
200047 |
| int has_qr = 0; |
| 519 |
200047 |
| int magic_ref = 0; |
| 520 |
|
| |
| 521 |
400094 |
▶ | for (mg = SvMAGIC(ref); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(ref); mg; mg = mg->mg_moremagic) |
200047 |
200047 |
400094 |
|
| 522 |
|
| { |
| 523 |
200047 |
| SV *obj = (SV *) NULL; |
| 524 |
200047 |
| int obj_cloned = 0; |
| 525 |
|
| TRACEME(("magic type: %c\n", mg->mg_type)); |
| 526 |
|
| |
| 527 |
|
| /* PERL_MAGIC_ext: opaque XS data, handle before the mg_obj check |
| 528 |
|
| * since ext magic often has mg_obj == NULL (GH #27, GH #16) */ |
| 529 |
200047 |
▶ | if (mg->mg_type == '~') |
Branch: 1 branch
| Branch | True | False | Total |
| if (mg->mg_type == '~') |
1 |
200046 |
200047 |
|
| 530 |
|
| { |
| 531 |
|
| #if defined(MGf_DUP) && defined(sv_magicext) |
| 532 |
|
| /* If the ext magic has a dup callback (e.g. Math::BigInt::GMP), |
| 533 |
|
| * clone it properly via sv_magicext + svt_dup. |
| 534 |
|
| * Otherwise skip it (e.g. DBI handles have no dup). |
| 535 |
|
| * Note: we check only for svt_dup presence, not MGf_DUP flag, |
| 536 |
|
| * because some older XS modules (e.g. Math::BigInt::GMP on |
| 537 |
|
| * Perl 5.22) provide svt_dup without setting MGf_DUP. (GH #76) */ |
| 538 |
1 |
▶ | if (mg->mg_virtual && mg->mg_virtual->svt_dup) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_virtual && mg->mg_virtual->svt_dup) |
0 |
1 |
1 |
| if (mg->mg_virtual && mg->mg_virtual->svt_dup) |
0 |
0 |
0 |
|
| 539 |
|
| { |
| 540 |
|
| MAGIC *new_mg; |
| 541 |
0 |
| new_mg = sv_magicext(clone, mg->mg_obj, |
| 542 |
|
| mg->mg_type, mg->mg_virtual, |
| 543 |
|
| mg->mg_ptr, mg->mg_len); |
| 544 |
0 |
| new_mg->mg_flags |= MGf_DUP; |
| 545 |
|
| /* CLONE_PARAMS is NULL since we are not in a thread clone. |
| 546 |
|
| * Known callers (e.g. Math::BigInt::GMP) ignore it. */ |
| 547 |
0 |
| mg->mg_virtual->svt_dup(aTHX_ new_mg, NULL); |
| 548 |
|
| } |
| 549 |
|
| #endif |
| 550 |
1 |
| continue; |
| 551 |
|
| } |
| 552 |
|
| |
| 553 |
|
| /* threads::shared uses tie magic ('P') with a threads::shared::tie |
| 554 |
|
| * object, and shared_scalar magic ('n'/'N') for scalars. |
| 555 |
|
| * Cloning these produces invalid tie objects that crash on access. |
| 556 |
|
| * Strip the sharing magic so hv_clone/av_clone can iterate through |
| 557 |
|
| * the tie to read the actual data. (GH #18) */ |
| 558 |
200046 |
▶ | if (mg->mg_type == PERL_MAGIC_shared_scalar |
Branch: 1 branch
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_shared_scalar |
200046 |
0 |
200046 |
|
| 559 |
200046 |
▶ | || mg->mg_type == PERL_MAGIC_shared) |
Branch: 1 branch
| Branch | True | False | Total |
| || mg->mg_type == PERL_MAGIC_shared) |
0 |
200046 |
200046 |
|
| 560 |
0 |
| continue; |
| 561 |
|
| |
| 562 |
|
| /* Some mg_obj's can be null, don't bother cloning */ |
| 563 |
200046 |
▶ | if ( mg->mg_obj != NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( mg->mg_obj != NULL ) |
40 |
200006 |
200046 |
|
| 564 |
|
| { |
| 565 |
40 |
| switch (mg->mg_type) |
| 566 |
|
| { |
| 567 |
0 |
| case 'r': /* PERL_MAGIC_qr */ |
| 568 |
0 |
| obj = mg->mg_obj; |
| 569 |
0 |
| has_qr = 1; |
| 570 |
0 |
| break; |
| 571 |
4 |
| case 't': /* PERL_MAGIC_taint */ |
| 572 |
|
| case '<': /* PERL_MAGIC_backref */ |
| 573 |
|
| case '@': /* PERL_MAGIC_arylen_p */ |
| 574 |
4 |
| continue; /* resumes the outer magic iteration loop */ |
| 575 |
35 |
| case 'P': /* PERL_MAGIC_tied */ |
| 576 |
|
| case 'p': /* PERL_MAGIC_tiedelem */ |
| 577 |
|
| case 'q': /* PERL_MAGIC_tiedscalar */ |
| 578 |
|
| /* threads::shared::tie objects are not real tie objects -- |
| 579 |
|
| * skip them so the clone becomes a plain unshared copy. |
| 580 |
|
| * The data will be read through the tie during hv_clone/av_clone. */ |
| 581 |
35 |
▶ | if (is_threads_shared_tie(mg->mg_obj)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_threads_shared_tie(mg->mg_obj)) |
0 |
35 |
35 |
|
| 582 |
0 |
| continue; |
| 583 |
35 |
| magic_ref++; |
| 584 |
|
| /* fall through */ |
| 585 |
36 |
| default: |
| 586 |
36 |
| obj = sv_clone(mg->mg_obj, hseen, -1, rdepth, weakrefs); |
| 587 |
36 |
| obj_cloned = 1; |
| 588 |
|
| } |
| 589 |
|
| } else { |
| 590 |
|
| TRACEME(("magic object for type %c in NULL\n", mg->mg_type)); |
| 591 |
|
| } |
| 592 |
|
| |
| 593 |
|
| { /* clone the mg_ptr pv */ |
| 594 |
200042 |
| char *mg_ptr = mg->mg_ptr; /* default */ |
| 595 |
200042 |
▶ | U32 obj_rc = obj_cloned ? SvREFCNT(obj) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| U32 obj_rc = obj_cloned ? SvREFCNT(obj) : 0; |
36 |
200006 |
200042 |
|
| 596 |
|
| |
| 597 |
200042 |
▶ | if (mg->mg_len >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mg->mg_len >= 0) { |
2 |
200040 |
200042 |
|
| 598 |
|
| /* sv_magic() with non-negative namlen calls savepvn() |
| 599 |
|
| * internally to make its own copy — no need to allocate |
| 600 |
|
| * an intermediate buffer here; just pass the original |
| 601 |
|
| * mg_ptr through. (fixes 20-year-old memory leak) */ |
| 602 |
2 |
▶ | } else if (mg->mg_len == HEf_SVKEY) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (mg->mg_len == HEf_SVKEY) { |
2 |
0 |
2 |
|
| 603 |
|
| /* mg_ptr is an SV*; sv_magic() below will SvREFCNT_inc it */ |
| 604 |
2 |
▶ | } else if (mg->mg_len == -1 && mg->mg_type == PERL_MAGIC_utf8) { /* copy the cache */ |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (mg->mg_len == -1 && mg->mg_type == PERL_MAGIC_utf8) { /* copy the cache */ |
2 |
0 |
2 |
| } else if (mg->mg_len == -1 && mg->mg_type == PERL_MAGIC_utf8) { /* copy the cache */ |
2 |
0 |
2 |
|
| 605 |
2 |
▶ | if (mg->mg_ptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mg->mg_ptr) { |
1 |
1 |
2 |
|
| 606 |
|
| STRLEN *cache; |
| 607 |
1 |
| Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN); |
| 608 |
1 |
| mg_ptr = (char *) cache; |
| 609 |
1 |
| Copy(mg->mg_ptr, mg_ptr, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN); |
| 610 |
|
| } |
| 611 |
0 |
▶ | } else if ( mg->mg_ptr != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( mg->mg_ptr != NULL) { |
0 |
0 |
0 |
|
| 612 |
0 |
| croak("Unsupported magic_ptr clone"); |
| 613 |
|
| } |
| 614 |
|
| |
| 615 |
200042 |
| sv_magic(clone, |
| 616 |
|
| obj, |
| 617 |
|
| mg->mg_type, |
| 618 |
|
| mg_ptr, |
| 619 |
|
| mg->mg_len); |
| 620 |
|
| |
| 621 |
|
| /* sv_magic() usually takes its own SvREFCNT_inc on obj (setting |
| 622 |
|
| * MGf_REFCOUNTED), in which case the caller reference returned |
| 623 |
|
| * by sv_clone() must be released -- otherwise the cloned mg_obj |
| 624 |
|
| * leaks one refcount per clone (DESTROY never fires on the |
| 625 |
|
| * cloned tie object). |
| 626 |
|
| * But it stores mg_obj *unreferenced* in several cases: a |
| 627 |
|
| * self-referential obj, arylen ('#') / regdata ('D') / |
| 628 |
|
| * regdatum ('d') / symtab (':') magic, and glob-slot |
| 629 |
|
| * back-pointers. That list has changed across perl releases, |
| 630 |
|
| * so instead of mirroring it here, observe whether the refcount |
| 631 |
|
| * actually went up. Decrementing when perl took no reference |
| 632 |
|
| * would free an SV the magic still points at. */ |
| 633 |
200042 |
▶ | if (obj_cloned && SvREFCNT(obj) > obj_rc) |
Branch: 2 branches
| Branch | True | False | Total |
| if (obj_cloned && SvREFCNT(obj) > obj_rc) |
36 |
200006 |
200042 |
| if (obj_cloned && SvREFCNT(obj) > obj_rc) |
35 |
1 |
36 |
|
| 634 |
35 |
| SvREFCNT_dec(obj); |
| 635 |
|
| } |
| 636 |
|
| } |
| 637 |
|
| /* Null the qr vtable -- avoid mg_find traversal if we already know */ |
| 638 |
200047 |
▶ | if (has_qr && (mg = mg_find(clone, 'r'))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (has_qr && (mg = mg_find(clone, 'r'))) |
0 |
200047 |
200047 |
| if (has_qr && (mg = mg_find(clone, 'r'))) |
0 |
0 |
0 |
|
| 639 |
0 |
| mg->mg_virtual = (MGVTBL *) NULL; |
| 640 |
|
| |
| 641 |
200047 |
| return magic_ref; |
| 642 |
|
| } |
| 643 |
|
| |
| 644 |
|
| static SV * |
| 645 |
39053 |
| av_clone (SV * ref, SV * target, HV* hseen, int depth, int rdepth, AV * weakrefs) |
| 646 |
|
| { |
| 647 |
|
| AV *clone; |
| 648 |
|
| AV *self; |
| 649 |
|
| SV **svp; |
| 650 |
|
| SV **dst; |
| 651 |
39053 |
| I32 arrlen = 0; |
| 652 |
|
| I32 i; |
| 653 |
|
| int recur; |
| 654 |
|
| |
| 655 |
|
| /* Note: depth is never 0 here. sv_clone() returns SvREFCNT_inc(ref) on |
| 656 |
|
| * depth == 0 before dispatching to av_clone. depth < 0 means unlimited |
| 657 |
|
| * (the XS default is -1), which is why recur pins at -1 below instead of |
| 658 |
|
| * decrementing. Deep structures do not reach here either -- sv_clone's |
| 659 |
|
| * rdepth > MAX_DEPTH guard hands them to clone_container_iterative. */ |
| 660 |
|
| |
| 661 |
39053 |
| clone = (AV *) target; |
| 662 |
39053 |
| self = (AV *) ref; |
| 663 |
39053 |
| recur = depth > 0 ? depth - 1 : -1; |
| 664 |
|
| |
| 665 |
|
| assert(SvTYPE(ref) == SVt_PVAV); |
| 666 |
|
| |
| 667 |
|
| TRACEME(("ref = 0x%" UVxf "(%d)\n", PTR2UV(ref), SvREFCNT(ref))); |
| 668 |
|
| |
| 669 |
39053 |
| arrlen = av_len(self); |
| 670 |
39053 |
| av_extend(clone, arrlen); |
| 671 |
|
| |
| 672 |
|
| /* Use av_fetch on the source (may be magical/tied) but write |
| 673 |
|
| * directly to the target's AvARRAY (we just created it, no magic). */ |
| 674 |
39053 |
| dst = AvARRAY(clone); |
| 675 |
90176 |
▶ | for (i = 0; i <= arrlen; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= arrlen; i++) { |
51123 |
39053 |
90176 |
|
| 676 |
51123 |
| svp = av_fetch(self, i, 0); |
| 677 |
51123 |
▶ | if (svp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (svp) { |
51123 |
0 |
51123 |
|
| 678 |
51123 |
| dst[i] = sv_clone(*svp, hseen, recur, rdepth, weakrefs); |
| 679 |
|
| } |
| 680 |
|
| } |
| 681 |
39053 |
| AvFILLp(clone) = arrlen; |
| 682 |
|
| |
| 683 |
|
| TRACEME(("clone = 0x%" UVxf "(%d)\n", PTR2UV(clone), SvREFCNT(clone))); |
| 684 |
39053 |
| return (SV *) clone; |
| 685 |
|
| } |
| 686 |
|
| |
| 687 |
|
| static SV * |
| 688 |
680724 |
| sv_clone (SV * ref, HV* hseen, int depth, int rdepth, AV * weakrefs) |
| 689 |
|
| { |
| 690 |
|
| SV *clone; |
| 691 |
680724 |
| SV **seen = NULL; |
| 692 |
|
| UV visible; |
| 693 |
680724 |
| int magic_ref = 0; |
| 694 |
|
| |
| 695 |
680724 |
▶ | if (!ref) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ref) |
0 |
680724 |
680724 |
|
| 696 |
0 |
| return NULL; |
| 697 |
|
| |
| 698 |
680724 |
| rdepth++; |
| 699 |
|
| |
| 700 |
|
| /* depth == 0 means "share, don't clone" — honour it before the |
| 701 |
|
| * MAX_DEPTH iterative fallback below. Both can trigger on the same |
| 702 |
|
| * call: rdepth advances two per nesting level while depth drops one, |
| 703 |
|
| * so a chain nested MAX_DEPTH/2 levels deep and cloned with that same |
| 704 |
|
| * explicit depth reaches depth 0 exactly as rdepth crosses MAX_DEPTH. |
| 705 |
|
| * Checking depth later let the fallback intercept that call and |
| 706 |
|
| * deep-copy a leaf the caller asked to share. */ |
| 707 |
680724 |
▶ | if (depth == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth == 0) |
26 |
680698 |
680724 |
|
| 708 |
26 |
| return SvREFCNT_inc(ref); |
| 709 |
|
| |
| 710 |
|
| /* Check for deep recursion and switch to iterative mode. |
| 711 |
|
| * A deeply nested arrayref like [[[...]]] alternates between RV and AV |
| 712 |
|
| * at each level, consuming ~3 C stack frames per nesting level. |
| 713 |
|
| * On Windows (1MB default stack), this overflows around depth 2000. |
| 714 |
|
| * When we exceed MAX_DEPTH, handle both AV and RV-to-AV cases. */ |
| 715 |
680698 |
▶ | if (rdepth > MAX_DEPTH) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rdepth > MAX_DEPTH) { |
49 |
680649 |
680698 |
|
| 716 |
49 |
▶ | if (SvTYPE(ref) == SVt_PVAV || SvTYPE(ref) == SVt_PVHV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTYPE(ref) == SVt_PVAV || SvTYPE(ref) == SVt_PVHV) { |
49 |
0 |
49 |
| if (SvTYPE(ref) == SVt_PVAV || SvTYPE(ref) == SVt_PVHV) { |
0 |
49 |
49 |
|
| 717 |
0 |
| return clone_container_iterative(ref, hseen, rdepth, weakrefs); |
| 718 |
|
| } |
| 719 |
|
| /* All RV types (AV, HV, scalar-ref chains) are handled uniformly |
| 720 |
|
| * by rv_clone_iterative, which walks the reference chain, hands |
| 721 |
|
| * container referents to the iterative work queue, |
| 722 |
|
| * and properly preserves blessings and SvWEAKREF flags. |
| 723 |
|
| * (The AV/HV cases were previously inlined here but lacked weakref |
| 724 |
|
| * handling — see GH #107, #116, #119 for the iterative gap pattern.) */ |
| 725 |
49 |
▶ | if (SvROK(ref)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(ref)) |
32 |
17 |
49 |
|
| 726 |
32 |
| return rv_clone_iterative(ref, hseen, rdepth, weakrefs); |
| 727 |
|
| /* Simple scalars (non-reference, non-container) can always be |
| 728 |
|
| * safely copied without recursion. newSVsv creates an independent |
| 729 |
|
| * copy, preventing aliasing of leaf values inside iteratively-cloned |
| 730 |
|
| * containers. Without this, the iterative container cloner |
| 731 |
|
| * would share leaf SVs between original and clone — mutations |
| 732 |
|
| * through a reference to the clone's value would corrupt the |
| 733 |
|
| * original. (GH #113) */ |
| 734 |
17 |
▶ | switch (SvTYPE(ref)) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (SvTYPE(ref)) { |
16 |
1 |
17 |
|
| 735 |
16 |
| case SVt_NULL: |
| 736 |
|
| case SVt_IV: |
| 737 |
|
| case SVt_NV: |
| 738 |
|
| #if PERL_VERSION <= 10 |
| 739 |
|
| case SVt_RV: |
| 740 |
|
| #endif |
| 741 |
|
| case SVt_PV: |
| 742 |
|
| case SVt_PVIV: |
| 743 |
|
| case SVt_PVNV: |
| 744 |
|
| case SVt_PVMG: |
| 745 |
16 |
| return newSVsv(ref); |
| 746 |
1 |
| default: |
| 747 |
1 |
| break; |
| 748 |
|
| } |
| 749 |
|
| /* Non-clonable types past MAX_DEPTH (e.g. PVGV, PVCV, PVFM, PVIO): |
| 750 |
|
| * these cannot be deep-copied regardless of depth; share with a |
| 751 |
|
| * warning. */ |
| 752 |
|
| { |
| 753 |
1 |
| SV *warn_sv = get_sv("Clone::WARN", 0); |
| 754 |
1 |
▶ | if (!warn_sv || SvTRUE(warn_sv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!warn_sv || SvTRUE(warn_sv)) |
1 |
0 |
1 |
| if (!warn_sv || SvTRUE(warn_sv)) |
1 |
0 |
1 |
|
| 755 |
1 |
| Perl_warn(aTHX_ "Clone: depth limit (%d) exceeded; " |
| 756 |
|
| "reference will be shared, not deep-copied", MAX_DEPTH); |
| 757 |
|
| } |
| 758 |
1 |
| return SvREFCNT_inc(ref); |
| 759 |
|
| } |
| 760 |
|
| |
| 761 |
680649 |
| clone = ref; |
| 762 |
|
| |
| 763 |
|
| /* Track this SV in hseen only if it could be reached from multiple |
| 764 |
|
| * paths in the data structure. Single-refcount, non-magical SVs |
| 765 |
|
| * are unique leaves — skipping the hash lookup/store for them is a |
| 766 |
|
| * significant win on structures with many distinct scalar values. |
| 767 |
|
| * |
| 768 |
|
| * Cases that require tracking: |
| 769 |
|
| * - SvREFCNT > 1 : SV is shared (appears in multiple slots) |
| 770 |
|
| * - SvMAGICAL : may be a weakref target (backref '<' magic |
| 771 |
|
| * for non-HV types), tied, or carry other magic |
| 772 |
|
| * - HV with SvOOK : since Perl 5.10, weakref back-references for |
| 773 |
|
| * HVs are stored in the HV's AUX struct (via |
| 774 |
|
| * SvOOK) rather than as PERL_MAGIC_backref. An |
| 775 |
|
| * HV that is the target of a weakened reference |
| 776 |
|
| * has SvOOK set but is NOT SvMAGICAL, so we must |
| 777 |
|
| * check SvOOK explicitly for HVs. |
| 778 |
|
| * |
| 779 |
|
| * Historical note: Perl 5.9.x moved HV backrefs from magic to |
| 780 |
|
| * HvAUX; a blanket "visible = 1" was used as a workaround. The |
| 781 |
|
| * check below replaces that with a targeted condition. */ |
| 782 |
648282 |
▶ | visible = (SvREFCNT(ref) > 1) || SvMAGICAL(ref) |
Branch: 1 branch
| Branch | True | False | Total |
| visible = (SvREFCNT(ref) > 1) || SvMAGICAL(ref) |
248169 |
400113 |
648282 |
|
| 783 |
1328931 |
▶ | || (SvTYPE(ref) == SVt_PVHV && SvOOK(ref)); |
Branch: 6 branches
| Branch | True | False | Total |
| || (SvTYPE(ref) == SVt_PVHV && SvOOK(ref)); |
648282 |
32367 |
680649 |
| || (SvTYPE(ref) == SVt_PVHV && SvOOK(ref)); |
21197 |
226972 |
248169 |
| || (SvTYPE(ref) == SVt_PVHV && SvOOK(ref)); |
21197 |
0 |
21197 |
| || (SvTYPE(ref) == SVt_PVHV && SvOOK(ref)); |
0 |
21197 |
21197 |
| || (SvTYPE(ref) == SVt_PVHV && SvOOK(ref)); |
0 |
0 |
0 |
| || (SvTYPE(ref) == SVt_PVHV && SvOOK(ref)); |
25 |
21172 |
21197 |
|
| 784 |
|
| |
| 785 |
|
| TRACEME(("ref = 0x%" UVxf "(%d)\n", PTR2UV(ref), SvREFCNT(ref))); |
| 786 |
|
| |
| 787 |
680649 |
▶ | if (visible && (seen = CLONE_FETCH(ref))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (visible && (seen = CLONE_FETCH(ref))) |
432505 |
248144 |
680649 |
| if (visible && (seen = CLONE_FETCH(ref))) |
145 |
432360 |
432505 |
|
| 788 |
|
| { |
| 789 |
|
| TRACEME(("fetch ref (0x%" UVxf ")\n", PTR2UV(ref))); |
| 790 |
145 |
| return SvREFCNT_inc(*seen); |
| 791 |
|
| } |
| 792 |
|
| |
| 793 |
|
| /* threads::shared tiedelem PVLVs are proxies to shared data. |
| 794 |
|
| * They would normally be returned by SvREFCNT_inc (like other PVLVs), |
| 795 |
|
| * but that shares the proxy — mutations go back to the shared var. |
| 796 |
|
| * Copy through magic to get a plain unshared value. (GH #18) */ |
| 797 |
680504 |
▶ | if (SvTYPE(ref) == SVt_PVLV && SvMAGICAL(ref)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTYPE(ref) == SVt_PVLV && SvMAGICAL(ref)) |
200106 |
480398 |
680504 |
| if (SvTYPE(ref) == SVt_PVLV && SvMAGICAL(ref)) |
200106 |
0 |
200106 |
|
| 798 |
|
| { |
| 799 |
|
| MAGIC *mg; |
| 800 |
400212 |
▶ | for (mg = SvMAGIC(ref); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(ref); mg; mg = mg->mg_moremagic) |
200106 |
200106 |
400212 |
|
| 801 |
|
| { |
| 802 |
200106 |
▶ | if ((mg->mg_type == PERL_MAGIC_tiedelem |
Branch: 1 branch
| Branch | True | False | Total |
| if ((mg->mg_type == PERL_MAGIC_tiedelem |
200004 |
102 |
200106 |
|
| 803 |
200004 |
▶ | || mg->mg_type == PERL_MAGIC_tiedscalar) |
Branch: 1 branch
| Branch | True | False | Total |
| || mg->mg_type == PERL_MAGIC_tiedscalar) |
0 |
200004 |
200004 |
|
| 804 |
102 |
▶ | && is_threads_shared_tie(mg->mg_obj)) |
Branch: 1 branch
| Branch | True | False | Total |
| && is_threads_shared_tie(mg->mg_obj)) |
0 |
102 |
102 |
|
| 805 |
|
| { |
| 806 |
|
| TRACEME(("threads::shared tiedelem PVLV — copy value\n")); |
| 807 |
0 |
| clone = newSVsv(ref); |
| 808 |
0 |
▶ | if (visible && ref != clone) |
Branch: 2 branches
| Branch | True | False | Total |
| if (visible && ref != clone) |
0 |
0 |
0 |
| if (visible && ref != clone) |
0 |
0 |
0 |
|
| 809 |
0 |
▶ | CLONE_STORE(ref, clone); |
Branch: 1 branch
| Branch | True | False | Total |
| CLONE_STORE(ref, clone); |
0 |
0 |
0 |
|
| 810 |
0 |
| return clone; |
| 811 |
|
| } |
| 812 |
|
| } |
| 813 |
|
| } |
| 814 |
|
| |
| 815 |
|
| TRACEME(("switch: (0x%" UVxf ")\n", PTR2UV(ref))); |
| 816 |
680504 |
| switch (SvTYPE (ref)) |
| 817 |
|
| { |
| 818 |
100004 |
| case SVt_NULL: /* 0 */ |
| 819 |
|
| TRACEME(("sv_null\n")); |
| 820 |
100004 |
| clone = newSVsv (ref); |
| 821 |
100004 |
| break; |
| 822 |
94519 |
| case SVt_IV: /* 1 */ |
| 823 |
|
| TRACEME(("int scalar\n")); |
| 824 |
|
| case SVt_NV: /* 2 */ |
| 825 |
|
| TRACEME(("double scalar\n")); |
| 826 |
94519 |
| clone = newSVsv (ref); |
| 827 |
94519 |
| break; |
| 828 |
|
| #if PERL_VERSION <= 10 |
| 829 |
|
| case SVt_RV: /* 3 */ |
| 830 |
|
| TRACEME(("ref scalar\n")); |
| 831 |
|
| clone = newSVsv (ref); |
| 832 |
|
| break; |
| 833 |
|
| #endif |
| 834 |
181 |
| case SVt_PV: /* 4 */ |
| 835 |
|
| TRACEME(("string scalar\n")); |
| 836 |
|
| /* |
| 837 |
|
| * Note: when using a Debug Perl with READONLY_COW |
| 838 |
|
| * we cannot do 'sv_buf_to_rw + sv_buf_to_ro' as these APIs calls are not exported |
| 839 |
|
| */ |
| 840 |
|
| #if defined(SV_COW_REFCNT_MAX) && !defined(PERL_DEBUG_READONLY_COW) |
| 841 |
|
| /* only for simple PVs unblessed */ |
| 842 |
181 |
▶ | if ( SvIsCOW(ref) && !SvOOK(ref) && SvLEN(ref) > 0 ) { |
Branch: 6 branches
| Branch | True | False | Total |
| if ( SvIsCOW(ref) && !SvOOK(ref) && SvLEN(ref) > 0 ) { |
69 |
112 |
181 |
| if ( SvIsCOW(ref) && !SvOOK(ref) && SvLEN(ref) > 0 ) { |
0 |
69 |
69 |
| if ( SvIsCOW(ref) && !SvOOK(ref) && SvLEN(ref) > 0 ) { |
0 |
0 |
0 |
| if ( SvIsCOW(ref) && !SvOOK(ref) && SvLEN(ref) > 0 ) { |
0 |
0 |
0 |
| if ( SvIsCOW(ref) && !SvOOK(ref) && SvLEN(ref) > 0 ) { |
69 |
0 |
69 |
| if ( SvIsCOW(ref) && !SvOOK(ref) && SvLEN(ref) > 0 ) { |
59 |
10 |
69 |
|
| 843 |
|
| |
| 844 |
59 |
▶ | if ( CowREFCNT(ref) < (SV_COW_REFCNT_MAX - 1) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( CowREFCNT(ref) < (SV_COW_REFCNT_MAX - 1) ) { |
57 |
2 |
59 |
|
| 845 |
|
| /* cannot use newSVpv_share as this going to use a new PV we do not want to clone it */ |
| 846 |
|
| /* create a fresh new PV */ |
| 847 |
57 |
| clone = newSV(0); |
| 848 |
57 |
| sv_upgrade(clone, SVt_PV); |
| 849 |
57 |
| SvPOK_on(clone); |
| 850 |
57 |
| SvIsCOW_on(clone); |
| 851 |
|
| |
| 852 |
|
| /* points the str slot to the COWed one */ |
| 853 |
57 |
| SvPV_set(clone, SvPVX(ref) ); |
| 854 |
57 |
| CowREFCNT(ref)++; |
| 855 |
|
| |
| 856 |
|
| /* preserve cur, len, and value-relevant flags */ |
| 857 |
57 |
| SvCUR_set(clone, SvCUR(ref)); |
| 858 |
57 |
| SvLEN_set(clone, SvLEN(ref)); |
| 859 |
57 |
▶ | if (SvUTF8(ref)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8(ref)) |
1 |
56 |
57 |
|
| 860 |
1 |
| SvUTF8_on(clone); |
| 861 |
|
| } else { |
| 862 |
|
| /* we are above SV_COW_REFCNT_MAX, create a new SvPV but preserve the COW */ |
| 863 |
2 |
| clone = newSVsv (ref); |
| 864 |
2 |
| SvIsCOW_on(clone); |
| 865 |
2 |
| CowREFCNT(clone) = 0; /* set the CowREFCNT to 0 */ |
| 866 |
|
| } |
| 867 |
|
| |
| 868 |
|
| } else { |
| 869 |
122 |
| clone = newSVsv (ref); |
| 870 |
|
| } |
| 871 |
|
| #else |
| 872 |
|
| clone = newSVsv (ref); |
| 873 |
|
| #endif |
| 874 |
181 |
| break; |
| 875 |
523 |
| case SVt_PVIV: /* 5 */ |
| 876 |
|
| TRACEME (("PVIV double-type\n")); |
| 877 |
|
| case SVt_PVNV: /* 6 */ |
| 878 |
|
| TRACEME (("PVNV double-type\n")); |
| 879 |
523 |
| clone = newSVsv (ref); |
| 880 |
523 |
| break; |
| 881 |
200035 |
| case SVt_PVMG: /* 7 */ |
| 882 |
|
| TRACEME(("magic scalar\n")); |
| 883 |
200035 |
| clone = newSVsv (ref); |
| 884 |
200035 |
| break; |
| 885 |
39064 |
| case SVt_PVAV: /* 10 */ |
| 886 |
39064 |
| clone = (SV *) newAV(); |
| 887 |
39064 |
| break; |
| 888 |
21239 |
| case SVt_PVHV: /* 11 */ |
| 889 |
21239 |
| clone = (SV *) newHV(); |
| 890 |
21239 |
| break; |
| 891 |
|
| #if PERL_VERSION >= 38 |
| 892 |
814 |
| case SVt_PVOBJ: /* 16 — class instances (Perl 5.38+) */ |
| 893 |
814 |
| clone = newSV(0); |
| 894 |
814 |
| sv_upgrade(clone, SVt_PVOBJ); |
| 895 |
|
| /* Establish the empty-field state explicitly rather than trusting |
| 896 |
|
| * sv_upgrade to have done it: the field-cloning block below only |
| 897 |
|
| * runs when the source has at least one field, so a field-less |
| 898 |
|
| * class would otherwise ship whatever the upgrade left behind and |
| 899 |
|
| * sv_clear would walk it at free time. */ |
| 900 |
814 |
| ObjectFIELDS(clone) = NULL; |
| 901 |
814 |
| ObjectMAXFIELD(clone) = -1; |
| 902 |
814 |
| break; |
| 903 |
|
| #endif |
| 904 |
|
| #if PERL_VERSION <= 8 |
| 905 |
|
| case SVt_PVBM: /* 8 */ |
| 906 |
|
| #elif PERL_VERSION >= 11 |
| 907 |
224125 |
| case SVt_REGEXP: /* 8 */ |
| 908 |
|
| #endif |
| 909 |
|
| case SVt_PVLV: /* 9 */ |
| 910 |
|
| case SVt_PVCV: /* 12 */ |
| 911 |
|
| case SVt_PVGV: /* 13 */ |
| 912 |
|
| case SVt_PVFM: /* 14 */ |
| 913 |
|
| case SVt_PVIO: /* 15 */ |
| 914 |
|
| TRACEME(("default: type = 0x%x\n", (int)SvTYPE (ref))); |
| 915 |
224125 |
| clone = SvREFCNT_inc(ref); /* just return the ref */ |
| 916 |
224125 |
| break; |
| 917 |
0 |
| default: |
| 918 |
0 |
| croak("unknown type: 0x%x", SvTYPE(ref)); |
| 919 |
|
| } |
| 920 |
|
| |
| 921 |
|
| /** |
| 922 |
|
| * It is *vital* that this is performed *before* recursion, |
| 923 |
|
| * to properly handle circular references. cb 2001-02-06 |
| 924 |
|
| */ |
| 925 |
|
| |
| 926 |
680504 |
▶ | if ( visible && ref != clone ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( visible && ref != clone ) |
432360 |
248144 |
680504 |
| if ( visible && ref != clone ) |
232243 |
200117 |
432360 |
|
| 927 |
232243 |
▶ | CLONE_STORE(ref,clone); |
Branch: 1 branch
| Branch | True | False | Total |
| CLONE_STORE(ref,clone); |
0 |
232243 |
232243 |
|
| 928 |
|
| |
| 929 |
|
| /* If clone == ref (e.g. for PVLV, PVGV, PVCV types), we just |
| 930 |
|
| * incremented the refcount — skip all internal cloning to avoid |
| 931 |
|
| * adding duplicate magic entries or corrupting the original SV. |
| 932 |
|
| * (fixes GH #42: memory leak when cloning non-existent hash values) */ |
| 933 |
680504 |
▶ | if (ref == clone) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ref == clone) |
224125 |
456379 |
680504 |
|
| 934 |
224125 |
| return clone; |
| 935 |
|
| |
| 936 |
|
| /* |
| 937 |
|
| * We'll assume (in the absence of evidence to the contrary) that A) a |
| 938 |
|
| * tied hash/array doesn't store its elements in the usual way (i.e. |
| 939 |
|
| * the mg->mg_object(s) take full responsibility for them) and B) that |
| 940 |
|
| * references aren't tied. |
| 941 |
|
| * |
| 942 |
|
| * If theses assumptions hold, the three options below are mutually |
| 943 |
|
| * exclusive. |
| 944 |
|
| * |
| 945 |
|
| * More precisely: 1 & 2 are probably mutually exclusive; 2 & 3 are |
| 946 |
|
| * definitely mutually exclusive; we have to test 1 before giving 2 |
| 947 |
|
| * a chance; and we'll assume that 1 & 3 are mutually exclusive unless |
| 948 |
|
| * and until we can be test-cased out of our delusion. |
| 949 |
|
| * |
| 950 |
|
| * chocolateboy: 2001-05-29 |
| 951 |
|
| */ |
| 952 |
|
| |
| 953 |
|
| /* 1: TIED / MAGIC */ |
| 954 |
456379 |
▶ | if (SvMAGICAL(ref)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvMAGICAL(ref)) |
200047 |
256332 |
456379 |
|
| 955 |
200047 |
| magic_ref = clone_magic(ref, clone, hseen, rdepth, weakrefs); |
| 956 |
|
| |
| 957 |
|
| /* 2: HASH/ARRAY - (with 'internal' elements) */ |
| 958 |
|
| /* For tied HV/AV (magic_ref > 0): skip direct element iteration; |
| 959 |
|
| * the tie magic cloned above handles the data. */ |
| 960 |
456379 |
▶ | if ( !magic_ref ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !magic_ref ) |
456344 |
35 |
456379 |
|
| 961 |
|
| { |
| 962 |
456344 |
▶ | if ( SvTYPE(ref) == SVt_PVHV ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( SvTYPE(ref) == SVt_PVHV ) |
21226 |
435118 |
456344 |
|
| 963 |
21226 |
| clone = hv_clone (ref, clone, hseen, depth, rdepth, weakrefs); |
| 964 |
435118 |
▶ | else if ( SvTYPE(ref) == SVt_PVAV ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( SvTYPE(ref) == SVt_PVAV ) |
39053 |
396065 |
435118 |
|
| 965 |
39053 |
| clone = av_clone (ref, clone, hseen, depth, rdepth, weakrefs); |
| 966 |
|
| #if PERL_VERSION >= 38 |
| 967 |
|
| /* class instances (Perl 5.38+): clone each object field */ |
| 968 |
396065 |
▶ | else if ( SvTYPE(ref) == SVt_PVOBJ ) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ( SvTYPE(ref) == SVt_PVOBJ ) |
814 |
395251 |
396065 |
|
| 969 |
|
| { |
| 970 |
814 |
| SSize_t maxfield = ObjectMAXFIELD(ref); |
| 971 |
814 |
▶ | if (maxfield >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (maxfield >= 0) |
613 |
201 |
814 |
|
| 972 |
|
| { |
| 973 |
613 |
| SV **src_fields = ObjectFIELDS(ref); |
| 974 |
|
| SV **dst_fields; |
| 975 |
613 |
| int recur = depth > 0 ? depth - 1 : -1; |
| 976 |
|
| SSize_t fi; |
| 977 |
|
| |
| 978 |
613 |
▶ | Newx(dst_fields, maxfield + 1, SV *); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(dst_fields, maxfield + 1, SV *); |
0 |
613 |
613 |
|
| 979 |
1227 |
▶ | for (fi = 0; fi <= maxfield; fi++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (fi = 0; fi <= maxfield; fi++) |
614 |
613 |
1227 |
|
| 980 |
|
| { |
| 981 |
614 |
| dst_fields[fi] = src_fields[fi] |
| 982 |
614 |
| ? sv_clone(src_fields[fi], hseen, recur, rdepth, weakrefs) |
| 983 |
614 |
▶ | : newSV(0); |
Branch: 1 branch
| Branch | True | False | Total |
| : newSV(0); |
614 |
0 |
614 |
|
| 984 |
|
| } |
| 985 |
613 |
| ObjectFIELDS(clone) = dst_fields; |
| 986 |
613 |
| ObjectMAXFIELD(clone) = maxfield; |
| 987 |
|
| } |
| 988 |
|
| } |
| 989 |
|
| #endif /* PERL_VERSION >= 38 */ |
| 990 |
|
| /* 3: REFERENCE (inlined for speed) */ |
| 991 |
395251 |
▶ | else if (SvROK (ref)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (SvROK (ref)) |
93425 |
301826 |
395251 |
|
| 992 |
|
| { |
| 993 |
|
| TRACEME(("clone = 0x%" UVxf "(%d)\n", PTR2UV(clone), SvREFCNT(clone))); |
| 994 |
93425 |
| SvREFCNT_dec(SvRV(clone)); |
| 995 |
93425 |
| SvRV(clone) = sv_clone (SvRV(ref), hseen, depth, rdepth, weakrefs); /* Clone the referent */ |
| 996 |
93425 |
▶ | if (SvOBJECT(SvRV(ref))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOBJECT(SvRV(ref))) |
8030 |
85395 |
93425 |
|
| 997 |
|
| { |
| 998 |
|
| #if PERL_VERSION >= 38 |
| 999 |
|
| /* sv_bless rejects class stashes (Perl 5.38+): set directly */ |
| 1000 |
8030 |
▶ | if (SvTYPE(SvRV(ref)) == SVt_PVOBJ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(SvRV(ref)) == SVt_PVOBJ) { |
920 |
7110 |
8030 |
|
| 1001 |
920 |
| SV * const robj = SvRV(clone); |
| 1002 |
920 |
| HV * const stash = (HV *)SvREFCNT_inc(SvSTASH(SvRV(ref))); |
| 1003 |
|
| /* The referent may come from the hseen cache already |
| 1004 |
|
| * blessed (same instance in several slots). Release the |
| 1005 |
|
| * stash it holds first, exactly as sv_bless does, or the |
| 1006 |
|
| * stash leaks one reference per extra alias. */ |
| 1007 |
920 |
▶ | if (SvOBJECT(robj)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOBJECT(robj)) |
106 |
814 |
920 |
|
| 1008 |
106 |
| SvREFCNT_dec(SvSTASH(robj)); |
| 1009 |
920 |
| SvOBJECT_on(robj); |
| 1010 |
920 |
| SvSTASH_set(robj, stash); |
| 1011 |
|
| } else |
| 1012 |
|
| #endif |
| 1013 |
7110 |
| sv_bless (clone, SvSTASH (SvRV (ref))); |
| 1014 |
|
| } |
| 1015 |
93425 |
▶ | if (SvWEAKREF(ref)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvWEAKREF(ref)) { |
16 |
93409 |
93425 |
|
| 1016 |
|
| /* Defer weakening until after the entire clone graph is built. |
| 1017 |
|
| * sv_rvweaken decrements the referent's refcount, which can |
| 1018 |
|
| * destroy it if no other strong references exist yet. |
| 1019 |
|
| * By deferring, we ensure all strong references are in place |
| 1020 |
|
| * before any weakening occurs. (fixes GH #15) */ |
| 1021 |
16 |
| av_push(weakrefs, SvREFCNT_inc_simple_NN(clone)); |
| 1022 |
|
| } |
| 1023 |
|
| } |
| 1024 |
|
| } |
| 1025 |
|
| |
| 1026 |
|
| TRACEME(("clone = 0x%" UVxf "(%d)\n", PTR2UV(clone), SvREFCNT(clone))); |
| 1027 |
456379 |
| return clone; |
| 1028 |
|
| } |
| 1029 |
|
| |
| 1030 |
|
| MODULE = Clone PACKAGE = Clone |
| 1031 |
|
| |
| 1032 |
|
| PROTOTYPES: ENABLE |
| 1033 |
|
| |
| 1034 |
|
| void |
| 1035 |
|
| clone(self, depth=-1) |
| 1036 |
|
| SV *self |
| 1037 |
|
| int depth |
| 1038 |
|
| PREINIT: |
| 1039 |
501123 |
▶ | SV *clone = &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *clone = &PL_sv_undef; |
501095 |
28 |
501123 |
|
| 1040 |
|
| HV *hseen; |
| 1041 |
|
| AV *weakrefs; |
| 1042 |
|
| PPCODE: |
| 1043 |
501123 |
| hseen = newHV(); |
| 1044 |
501123 |
| weakrefs = newAV(); |
| 1045 |
|
| /* Register for automatic cleanup on scope exit. If sv_clone() |
| 1046 |
|
| * or the weakening loop croaks, the longjmp would skip the |
| 1047 |
|
| * explicit SvREFCNT_dec below — SAVEFREESV ensures both are |
| 1048 |
|
| * freed during stack unwinding. */ |
| 1049 |
501123 |
| SAVEFREESV((SV *)hseen); |
| 1050 |
501123 |
| SAVEFREESV((SV *)weakrefs); |
| 1051 |
|
| TRACEME(("ref = 0x%" UVxf "\n", PTR2UV(self))); |
| 1052 |
501123 |
| clone = sv_clone(self, hseen, depth, 0, weakrefs); |
| 1053 |
|
| /* Now apply deferred weakening (GH #15). |
| 1054 |
|
| * All strong references in the clone graph are established, |
| 1055 |
|
| * so it is safe to weaken references without destroying referents. */ |
| 1056 |
|
| { |
| 1057 |
|
| I32 i; |
| 1058 |
501123 |
| I32 len = av_len(weakrefs); |
| 1059 |
501139 |
▶ | for (i = 0; i <= len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= len; i++) { |
16 |
501123 |
501139 |
|
| 1060 |
16 |
| SV **svp = av_fetch(weakrefs, i, 0); |
| 1061 |
16 |
▶ | if (svp && *svp && SvROK(*svp)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (svp && *svp && SvROK(*svp)) { |
16 |
0 |
16 |
| if (svp && *svp && SvROK(*svp)) { |
16 |
0 |
16 |
| if (svp && *svp && SvROK(*svp)) { |
16 |
0 |
16 |
|
| 1062 |
16 |
| sv_rvweaken(*svp); |
| 1063 |
|
| } |
| 1064 |
|
| } |
| 1065 |
|
| } |
| 1066 |
|
| /* hseen and weakrefs are freed automatically via SAVEFREESV */ |
| 1067 |
501123 |
▶ | EXTEND(SP,1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP,1); |
0 |
501123 |
501123 |
|
| 1068 |
501123 |
| PUSHs(sv_2mortal(clone)); |