Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
4bb101f2 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
b94e2f88 | 4 | * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others |
79072805 LW |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
a0d0e21e | 9 | * "I wonder what the Entish is for 'yes' and 'no'," he thought. |
645c22ef DM |
10 | * |
11 | * | |
5e045b90 AMS |
12 | * This file contains the code that creates, manipulates and destroys |
13 | * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the | |
14 | * structure of an SV, so their creation and destruction is handled | |
15 | * here; higher-level functions are in av.c, hv.c, and so on. Opcode | |
16 | * level functions (eg. substr, split, join) for each of the types are | |
17 | * in the pp*.c files. | |
79072805 LW |
18 | */ |
19 | ||
20 | #include "EXTERN.h" | |
864dbfa3 | 21 | #define PERL_IN_SV_C |
79072805 | 22 | #include "perl.h" |
d2f185dc | 23 | #include "regcomp.h" |
79072805 | 24 | |
51371543 | 25 | #define FCALL *f |
2c5424a7 | 26 | |
2f8ed50e OS |
27 | #ifdef __Lynx__ |
28 | /* Missing proto on LynxOS */ | |
29 | char *gconvert(double, int, int, char *); | |
30 | #endif | |
31 | ||
e23c8137 | 32 | #ifdef PERL_UTF8_CACHE_ASSERT |
ab455f60 | 33 | /* if adding more checks watch out for the following tests: |
e23c8137 JH |
34 | * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t |
35 | * lib/utf8.t lib/Unicode/Collate/t/index.t | |
36 | * --jhi | |
37 | */ | |
38 | #define ASSERT_UTF8_CACHE(cache) \ | |
ab455f60 NC |
39 | STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); \ |
40 | assert((cache)[2] <= (cache)[3]); \ | |
41 | assert((cache)[3] <= (cache)[1]);} \ | |
42 | } STMT_END | |
e23c8137 JH |
43 | #else |
44 | #define ASSERT_UTF8_CACHE(cache) NOOP | |
45 | #endif | |
46 | ||
f8c7b90f | 47 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 48 | #define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv)) |
607fa7f2 | 49 | #define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next)) |
b5ccf5f2 | 50 | /* This is a pessimistic view. Scalar must be purely a read-write PV to copy- |
765f542d | 51 | on-write. */ |
765f542d | 52 | #endif |
645c22ef DM |
53 | |
54 | /* ============================================================================ | |
55 | ||
56 | =head1 Allocation and deallocation of SVs. | |
57 | ||
d2a0f284 JC |
58 | An SV (or AV, HV, etc.) is allocated in two parts: the head (struct |
59 | sv, av, hv...) contains type and reference count information, and for | |
60 | many types, a pointer to the body (struct xrv, xpv, xpviv...), which | |
61 | contains fields specific to each type. Some types store all they need | |
62 | in the head, so don't have a body. | |
63 | ||
64 | In all but the most memory-paranoid configuations (ex: PURIFY), heads | |
65 | and bodies are allocated out of arenas, which by default are | |
66 | approximately 4K chunks of memory parcelled up into N heads or bodies. | |
93e68bfb JC |
67 | Sv-bodies are allocated by their sv-type, guaranteeing size |
68 | consistency needed to allocate safely from arrays. | |
69 | ||
d2a0f284 JC |
70 | For SV-heads, the first slot in each arena is reserved, and holds a |
71 | link to the next arena, some flags, and a note of the number of slots. | |
72 | Snaked through each arena chain is a linked list of free items; when | |
73 | this becomes empty, an extra arena is allocated and divided up into N | |
74 | items which are threaded into the free list. | |
75 | ||
76 | SV-bodies are similar, but they use arena-sets by default, which | |
77 | separate the link and info from the arena itself, and reclaim the 1st | |
78 | slot in the arena. SV-bodies are further described later. | |
645c22ef DM |
79 | |
80 | The following global variables are associated with arenas: | |
81 | ||
82 | PL_sv_arenaroot pointer to list of SV arenas | |
83 | PL_sv_root pointer to list of free SV structures | |
84 | ||
d2a0f284 JC |
85 | PL_body_arenas head of linked-list of body arenas |
86 | PL_body_roots[] array of pointers to list of free bodies of svtype | |
87 | arrays are indexed by the svtype needed | |
93e68bfb | 88 | |
d2a0f284 JC |
89 | A few special SV heads are not allocated from an arena, but are |
90 | instead directly created in the interpreter structure, eg PL_sv_undef. | |
93e68bfb JC |
91 | The size of arenas can be changed from the default by setting |
92 | PERL_ARENA_SIZE appropriately at compile time. | |
645c22ef DM |
93 | |
94 | The SV arena serves the secondary purpose of allowing still-live SVs | |
95 | to be located and destroyed during final cleanup. | |
96 | ||
97 | At the lowest level, the macros new_SV() and del_SV() grab and free | |
98 | an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv() | |
99 | to return the SV to the free list with error checking.) new_SV() calls | |
100 | more_sv() / sv_add_arena() to add an extra arena if the free list is empty. | |
101 | SVs in the free list have their SvTYPE field set to all ones. | |
102 | ||
ff276b08 | 103 | At the time of very final cleanup, sv_free_arenas() is called from |
645c22ef | 104 | perl_destruct() to physically free all the arenas allocated since the |
6a93a7e5 | 105 | start of the interpreter. |
645c22ef DM |
106 | |
107 | Manipulation of any of the PL_*root pointers is protected by enclosing | |
108 | LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing | |
109 | if threads are enabled. | |
110 | ||
111 | The function visit() scans the SV arenas list, and calls a specified | |
112 | function for each SV it finds which is still live - ie which has an SvTYPE | |
113 | other than all 1's, and a non-zero SvREFCNT. visit() is used by the | |
114 | following functions (specified as [function that calls visit()] / [function | |
115 | called by visit() for each SV]): | |
116 | ||
117 | sv_report_used() / do_report_used() | |
f2524eef | 118 | dump all remaining SVs (debugging aid) |
645c22ef DM |
119 | |
120 | sv_clean_objs() / do_clean_objs(),do_clean_named_objs() | |
121 | Attempt to free all objects pointed to by RVs, | |
122 | and, unless DISABLE_DESTRUCTOR_KLUDGE is defined, | |
123 | try to do the same for all objects indirectly | |
124 | referenced by typeglobs too. Called once from | |
125 | perl_destruct(), prior to calling sv_clean_all() | |
126 | below. | |
127 | ||
128 | sv_clean_all() / do_clean_all() | |
129 | SvREFCNT_dec(sv) each remaining SV, possibly | |
130 | triggering an sv_free(). It also sets the | |
131 | SVf_BREAK flag on the SV to indicate that the | |
132 | refcnt has been artificially lowered, and thus | |
133 | stopping sv_free() from giving spurious warnings | |
134 | about SVs which unexpectedly have a refcnt | |
135 | of zero. called repeatedly from perl_destruct() | |
136 | until there are no SVs left. | |
137 | ||
93e68bfb | 138 | =head2 Arena allocator API Summary |
645c22ef DM |
139 | |
140 | Private API to rest of sv.c | |
141 | ||
142 | new_SV(), del_SV(), | |
143 | ||
144 | new_XIV(), del_XIV(), | |
145 | new_XNV(), del_XNV(), | |
146 | etc | |
147 | ||
148 | Public API: | |
149 | ||
8cf8f3d1 | 150 | sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas() |
645c22ef | 151 | |
645c22ef DM |
152 | =cut |
153 | ||
154 | ============================================================================ */ | |
155 | ||
4561caa4 CS |
156 | /* |
157 | * "A time to plant, and a time to uproot what was planted..." | |
158 | */ | |
159 | ||
77354fb4 NC |
160 | /* |
161 | * nice_chunk and nice_chunk size need to be set | |
162 | * and queried under the protection of sv_mutex | |
163 | */ | |
164 | void | |
165 | Perl_offer_nice_chunk(pTHX_ void *chunk, U32 chunk_size) | |
166 | { | |
97aff369 | 167 | dVAR; |
77354fb4 NC |
168 | void *new_chunk; |
169 | U32 new_chunk_size; | |
170 | LOCK_SV_MUTEX; | |
171 | new_chunk = (void *)(chunk); | |
172 | new_chunk_size = (chunk_size); | |
173 | if (new_chunk_size > PL_nice_chunk_size) { | |
174 | Safefree(PL_nice_chunk); | |
175 | PL_nice_chunk = (char *) new_chunk; | |
176 | PL_nice_chunk_size = new_chunk_size; | |
177 | } else { | |
178 | Safefree(chunk); | |
179 | } | |
180 | UNLOCK_SV_MUTEX; | |
181 | } | |
cac9b346 | 182 | |
fd0854ff | 183 | #ifdef DEBUG_LEAKING_SCALARS |
22162ca8 | 184 | # define FREE_SV_DEBUG_FILE(sv) Safefree((sv)->sv_debug_file) |
fd0854ff DM |
185 | #else |
186 | # define FREE_SV_DEBUG_FILE(sv) | |
187 | #endif | |
188 | ||
48614a46 NC |
189 | #ifdef PERL_POISON |
190 | # define SvARENA_CHAIN(sv) ((sv)->sv_u.svu_rv) | |
191 | /* Whilst I'd love to do this, it seems that things like to check on | |
192 | unreferenced scalars | |
7e337ee0 | 193 | # define POSION_SV_HEAD(sv) PoisonNew(sv, 1, struct STRUCT_SV) |
48614a46 | 194 | */ |
7e337ee0 JH |
195 | # define POSION_SV_HEAD(sv) PoisonNew(&SvANY(sv), 1, void *), \ |
196 | PoisonNew(&SvREFCNT(sv), 1, U32) | |
48614a46 NC |
197 | #else |
198 | # define SvARENA_CHAIN(sv) SvANY(sv) | |
199 | # define POSION_SV_HEAD(sv) | |
200 | #endif | |
201 | ||
053fc874 GS |
202 | #define plant_SV(p) \ |
203 | STMT_START { \ | |
fd0854ff | 204 | FREE_SV_DEBUG_FILE(p); \ |
48614a46 NC |
205 | POSION_SV_HEAD(p); \ |
206 | SvARENA_CHAIN(p) = (void *)PL_sv_root; \ | |
053fc874 GS |
207 | SvFLAGS(p) = SVTYPEMASK; \ |
208 | PL_sv_root = (p); \ | |
209 | --PL_sv_count; \ | |
210 | } STMT_END | |
a0d0e21e | 211 | |
fba3b22e | 212 | /* sv_mutex must be held while calling uproot_SV() */ |
053fc874 GS |
213 | #define uproot_SV(p) \ |
214 | STMT_START { \ | |
215 | (p) = PL_sv_root; \ | |
bb7bbd9c | 216 | PL_sv_root = (SV*)SvARENA_CHAIN(p); \ |
053fc874 GS |
217 | ++PL_sv_count; \ |
218 | } STMT_END | |
219 | ||
645c22ef | 220 | |
cac9b346 NC |
221 | /* make some more SVs by adding another arena */ |
222 | ||
223 | /* sv_mutex must be held while calling more_sv() */ | |
224 | STATIC SV* | |
225 | S_more_sv(pTHX) | |
226 | { | |
97aff369 | 227 | dVAR; |
cac9b346 NC |
228 | SV* sv; |
229 | ||
230 | if (PL_nice_chunk) { | |
231 | sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0); | |
bd61b366 | 232 | PL_nice_chunk = NULL; |
cac9b346 NC |
233 | PL_nice_chunk_size = 0; |
234 | } | |
235 | else { | |
236 | char *chunk; /* must use New here to match call to */ | |
d2a0f284 | 237 | Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */ |
2e7ed132 | 238 | sv_add_arena(chunk, PERL_ARENA_SIZE, 0); |
cac9b346 NC |
239 | } |
240 | uproot_SV(sv); | |
241 | return sv; | |
242 | } | |
243 | ||
645c22ef DM |
244 | /* new_SV(): return a new, empty SV head */ |
245 | ||
eba0f806 DM |
246 | #ifdef DEBUG_LEAKING_SCALARS |
247 | /* provide a real function for a debugger to play with */ | |
248 | STATIC SV* | |
249 | S_new_SV(pTHX) | |
250 | { | |
251 | SV* sv; | |
252 | ||
253 | LOCK_SV_MUTEX; | |
254 | if (PL_sv_root) | |
255 | uproot_SV(sv); | |
256 | else | |
cac9b346 | 257 | sv = S_more_sv(aTHX); |
eba0f806 DM |
258 | UNLOCK_SV_MUTEX; |
259 | SvANY(sv) = 0; | |
260 | SvREFCNT(sv) = 1; | |
261 | SvFLAGS(sv) = 0; | |
fd0854ff DM |
262 | sv->sv_debug_optype = PL_op ? PL_op->op_type : 0; |
263 | sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ? | |
264 | (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline); | |
265 | sv->sv_debug_inpad = 0; | |
266 | sv->sv_debug_cloned = 0; | |
fd0854ff | 267 | sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL; |
fd0854ff | 268 | |
eba0f806 DM |
269 | return sv; |
270 | } | |
271 | # define new_SV(p) (p)=S_new_SV(aTHX) | |
272 | ||
273 | #else | |
274 | # define new_SV(p) \ | |
053fc874 GS |
275 | STMT_START { \ |
276 | LOCK_SV_MUTEX; \ | |
277 | if (PL_sv_root) \ | |
278 | uproot_SV(p); \ | |
279 | else \ | |
cac9b346 | 280 | (p) = S_more_sv(aTHX); \ |
053fc874 GS |
281 | UNLOCK_SV_MUTEX; \ |
282 | SvANY(p) = 0; \ | |
283 | SvREFCNT(p) = 1; \ | |
284 | SvFLAGS(p) = 0; \ | |
285 | } STMT_END | |
eba0f806 | 286 | #endif |
463ee0b2 | 287 | |
645c22ef DM |
288 | |
289 | /* del_SV(): return an empty SV head to the free list */ | |
290 | ||
a0d0e21e | 291 | #ifdef DEBUGGING |
4561caa4 | 292 | |
053fc874 GS |
293 | #define del_SV(p) \ |
294 | STMT_START { \ | |
295 | LOCK_SV_MUTEX; \ | |
aea4f609 | 296 | if (DEBUG_D_TEST) \ |
053fc874 GS |
297 | del_sv(p); \ |
298 | else \ | |
299 | plant_SV(p); \ | |
300 | UNLOCK_SV_MUTEX; \ | |
301 | } STMT_END | |
a0d0e21e | 302 | |
76e3520e | 303 | STATIC void |
cea2e8a9 | 304 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 305 | { |
97aff369 | 306 | dVAR; |
aea4f609 | 307 | if (DEBUG_D_TEST) { |
4633a7c4 | 308 | SV* sva; |
a3b680e6 | 309 | bool ok = 0; |
3280af22 | 310 | for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) { |
53c1dcc0 AL |
311 | const SV * const sv = sva + 1; |
312 | const SV * const svend = &sva[SvREFCNT(sva)]; | |
c0ff570e | 313 | if (p >= sv && p < svend) { |
a0d0e21e | 314 | ok = 1; |
c0ff570e NC |
315 | break; |
316 | } | |
a0d0e21e LW |
317 | } |
318 | if (!ok) { | |
0453d815 | 319 | if (ckWARN_d(WARN_INTERNAL)) |
9014280d | 320 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), |
472d47bc SB |
321 | "Attempt to free non-arena SV: 0x%"UVxf |
322 | pTHX__FORMAT, PTR2UV(p) pTHX__VALUE); | |
a0d0e21e LW |
323 | return; |
324 | } | |
325 | } | |
4561caa4 | 326 | plant_SV(p); |
463ee0b2 | 327 | } |
a0d0e21e | 328 | |
4561caa4 CS |
329 | #else /* ! DEBUGGING */ |
330 | ||
331 | #define del_SV(p) plant_SV(p) | |
332 | ||
333 | #endif /* DEBUGGING */ | |
463ee0b2 | 334 | |
645c22ef DM |
335 | |
336 | /* | |
ccfc67b7 JH |
337 | =head1 SV Manipulation Functions |
338 | ||
645c22ef DM |
339 | =for apidoc sv_add_arena |
340 | ||
341 | Given a chunk of memory, link it to the head of the list of arenas, | |
342 | and split it into a list of free SVs. | |
343 | ||
344 | =cut | |
345 | */ | |
346 | ||
4633a7c4 | 347 | void |
864dbfa3 | 348 | Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags) |
463ee0b2 | 349 | { |
97aff369 | 350 | dVAR; |
0bd48802 | 351 | SV* const sva = (SV*)ptr; |
463ee0b2 LW |
352 | register SV* sv; |
353 | register SV* svend; | |
4633a7c4 LW |
354 | |
355 | /* The first SV in an arena isn't an SV. */ | |
3280af22 | 356 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
357 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
358 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
359 | ||
3280af22 NIS |
360 | PL_sv_arenaroot = sva; |
361 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
362 | |
363 | svend = &sva[SvREFCNT(sva) - 1]; | |
364 | sv = sva + 1; | |
463ee0b2 | 365 | while (sv < svend) { |
48614a46 | 366 | SvARENA_CHAIN(sv) = (void *)(SV*)(sv + 1); |
03e36789 | 367 | #ifdef DEBUGGING |
978b032e | 368 | SvREFCNT(sv) = 0; |
03e36789 NC |
369 | #endif |
370 | /* Must always set typemask because it's awlays checked in on cleanup | |
371 | when the arenas are walked looking for objects. */ | |
8990e307 | 372 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
373 | sv++; |
374 | } | |
48614a46 | 375 | SvARENA_CHAIN(sv) = 0; |
03e36789 NC |
376 | #ifdef DEBUGGING |
377 | SvREFCNT(sv) = 0; | |
378 | #endif | |
4633a7c4 LW |
379 | SvFLAGS(sv) = SVTYPEMASK; |
380 | } | |
381 | ||
055972dc DM |
382 | /* visit(): call the named function for each non-free SV in the arenas |
383 | * whose flags field matches the flags/mask args. */ | |
645c22ef | 384 | |
5226ed68 | 385 | STATIC I32 |
055972dc | 386 | S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask) |
8990e307 | 387 | { |
97aff369 | 388 | dVAR; |
4633a7c4 | 389 | SV* sva; |
5226ed68 | 390 | I32 visited = 0; |
8990e307 | 391 | |
3280af22 | 392 | for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) { |
53c1dcc0 | 393 | register const SV * const svend = &sva[SvREFCNT(sva)]; |
a3b680e6 | 394 | register SV* sv; |
4561caa4 | 395 | for (sv = sva + 1; sv < svend; ++sv) { |
055972dc DM |
396 | if (SvTYPE(sv) != SVTYPEMASK |
397 | && (sv->sv_flags & mask) == flags | |
398 | && SvREFCNT(sv)) | |
399 | { | |
acfe0abc | 400 | (FCALL)(aTHX_ sv); |
5226ed68 JH |
401 | ++visited; |
402 | } | |
8990e307 LW |
403 | } |
404 | } | |
5226ed68 | 405 | return visited; |
8990e307 LW |
406 | } |
407 | ||
758a08c3 JH |
408 | #ifdef DEBUGGING |
409 | ||
645c22ef DM |
410 | /* called by sv_report_used() for each live SV */ |
411 | ||
412 | static void | |
acfe0abc | 413 | do_report_used(pTHX_ SV *sv) |
645c22ef DM |
414 | { |
415 | if (SvTYPE(sv) != SVTYPEMASK) { | |
416 | PerlIO_printf(Perl_debug_log, "****\n"); | |
417 | sv_dump(sv); | |
418 | } | |
419 | } | |
758a08c3 | 420 | #endif |
645c22ef DM |
421 | |
422 | /* | |
423 | =for apidoc sv_report_used | |
424 | ||
425 | Dump the contents of all SVs not yet freed. (Debugging aid). | |
426 | ||
427 | =cut | |
428 | */ | |
429 | ||
8990e307 | 430 | void |
864dbfa3 | 431 | Perl_sv_report_used(pTHX) |
4561caa4 | 432 | { |
ff270d3a | 433 | #ifdef DEBUGGING |
055972dc | 434 | visit(do_report_used, 0, 0); |
96a5add6 AL |
435 | #else |
436 | PERL_UNUSED_CONTEXT; | |
ff270d3a | 437 | #endif |
4561caa4 CS |
438 | } |
439 | ||
645c22ef DM |
440 | /* called by sv_clean_objs() for each live SV */ |
441 | ||
442 | static void | |
e15faf7d | 443 | do_clean_objs(pTHX_ SV *ref) |
645c22ef | 444 | { |
97aff369 | 445 | dVAR; |
823a54a3 AL |
446 | if (SvROK(ref)) { |
447 | SV * const target = SvRV(ref); | |
448 | if (SvOBJECT(target)) { | |
449 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref))); | |
450 | if (SvWEAKREF(ref)) { | |
451 | sv_del_backref(target, ref); | |
452 | SvWEAKREF_off(ref); | |
453 | SvRV_set(ref, NULL); | |
454 | } else { | |
455 | SvROK_off(ref); | |
456 | SvRV_set(ref, NULL); | |
457 | SvREFCNT_dec(target); | |
458 | } | |
645c22ef DM |
459 | } |
460 | } | |
461 | ||
462 | /* XXX Might want to check arrays, etc. */ | |
463 | } | |
464 | ||
465 | /* called by sv_clean_objs() for each live SV */ | |
466 | ||
467 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
468 | static void | |
acfe0abc | 469 | do_clean_named_objs(pTHX_ SV *sv) |
645c22ef | 470 | { |
97aff369 | 471 | dVAR; |
f7877b28 | 472 | if (SvTYPE(sv) == SVt_PVGV && isGV_with_GP(sv) && GvGP(sv)) { |
c69033f2 NC |
473 | if (( |
474 | #ifdef PERL_DONT_CREATE_GVSV | |
475 | GvSV(sv) && | |
476 | #endif | |
477 | SvOBJECT(GvSV(sv))) || | |
645c22ef DM |
478 | (GvAV(sv) && SvOBJECT(GvAV(sv))) || |
479 | (GvHV(sv) && SvOBJECT(GvHV(sv))) || | |
480 | (GvIO(sv) && SvOBJECT(GvIO(sv))) || | |
481 | (GvCV(sv) && SvOBJECT(GvCV(sv))) ) | |
482 | { | |
483 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv))); | |
ec5f3c78 | 484 | SvFLAGS(sv) |= SVf_BREAK; |
645c22ef DM |
485 | SvREFCNT_dec(sv); |
486 | } | |
487 | } | |
488 | } | |
489 | #endif | |
490 | ||
491 | /* | |
492 | =for apidoc sv_clean_objs | |
493 | ||
494 | Attempt to destroy all objects not yet freed | |
495 | ||
496 | =cut | |
497 | */ | |
498 | ||
4561caa4 | 499 | void |
864dbfa3 | 500 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 501 | { |
97aff369 | 502 | dVAR; |
3280af22 | 503 | PL_in_clean_objs = TRUE; |
055972dc | 504 | visit(do_clean_objs, SVf_ROK, SVf_ROK); |
4561caa4 | 505 | #ifndef DISABLE_DESTRUCTOR_KLUDGE |
2d0f3c12 | 506 | /* some barnacles may yet remain, clinging to typeglobs */ |
055972dc | 507 | visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK); |
4561caa4 | 508 | #endif |
3280af22 | 509 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
510 | } |
511 | ||
645c22ef DM |
512 | /* called by sv_clean_all() for each live SV */ |
513 | ||
514 | static void | |
acfe0abc | 515 | do_clean_all(pTHX_ SV *sv) |
645c22ef | 516 | { |
97aff369 | 517 | dVAR; |
645c22ef DM |
518 | DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) )); |
519 | SvFLAGS(sv) |= SVf_BREAK; | |
0e705b3b | 520 | if (PL_comppad == (AV*)sv) { |
7d49f689 | 521 | PL_comppad = NULL; |
4608196e | 522 | PL_curpad = NULL; |
0e705b3b | 523 | } |
645c22ef DM |
524 | SvREFCNT_dec(sv); |
525 | } | |
526 | ||
527 | /* | |
528 | =for apidoc sv_clean_all | |
529 | ||
530 | Decrement the refcnt of each remaining SV, possibly triggering a | |
531 | cleanup. This function may have to be called multiple times to free | |
ff276b08 | 532 | SVs which are in complex self-referential hierarchies. |
645c22ef DM |
533 | |
534 | =cut | |
535 | */ | |
536 | ||
5226ed68 | 537 | I32 |
864dbfa3 | 538 | Perl_sv_clean_all(pTHX) |
8990e307 | 539 | { |
97aff369 | 540 | dVAR; |
5226ed68 | 541 | I32 cleaned; |
3280af22 | 542 | PL_in_clean_all = TRUE; |
055972dc | 543 | cleaned = visit(do_clean_all, 0,0); |
3280af22 | 544 | PL_in_clean_all = FALSE; |
5226ed68 | 545 | return cleaned; |
8990e307 | 546 | } |
463ee0b2 | 547 | |
5e258f8c JC |
548 | /* |
549 | ARENASETS: a meta-arena implementation which separates arena-info | |
550 | into struct arena_set, which contains an array of struct | |
551 | arena_descs, each holding info for a single arena. By separating | |
552 | the meta-info from the arena, we recover the 1st slot, formerly | |
553 | borrowed for list management. The arena_set is about the size of an | |
554 | arena, avoiding the needless malloc overhead of a naive linked-list | |
555 | ||
556 | The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused | |
557 | memory in the last arena-set (1/2 on average). In trade, we get | |
558 | back the 1st slot in each arena (ie 1.7% of a CV-arena, less for | |
d2a0f284 JC |
559 | smaller types). The recovery of the wasted space allows use of |
560 | small arenas for large, rare body types, | |
5e258f8c | 561 | */ |
5e258f8c | 562 | struct arena_desc { |
398c677b NC |
563 | char *arena; /* the raw storage, allocated aligned */ |
564 | size_t size; /* its size ~4k typ */ | |
565 | int unit_type; /* useful for arena audits */ | |
5e258f8c JC |
566 | /* info for sv-heads (eventually) |
567 | int count, flags; | |
568 | */ | |
569 | }; | |
570 | ||
e6148039 NC |
571 | struct arena_set; |
572 | ||
573 | /* Get the maximum number of elements in set[] such that struct arena_set | |
574 | will fit within PERL_ARENA_SIZE, which is probabably just under 4K, and | |
575 | therefore likely to be 1 aligned memory page. */ | |
576 | ||
577 | #define ARENAS_PER_SET ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \ | |
578 | - 2 * sizeof(int)) / sizeof (struct arena_desc)) | |
5e258f8c JC |
579 | |
580 | struct arena_set { | |
581 | struct arena_set* next; | |
582 | int set_size; /* ie ARENAS_PER_SET */ | |
583 | int curr; /* index of next available arena-desc */ | |
584 | struct arena_desc set[ARENAS_PER_SET]; | |
585 | }; | |
586 | ||
645c22ef DM |
587 | /* |
588 | =for apidoc sv_free_arenas | |
589 | ||
590 | Deallocate the memory used by all arenas. Note that all the individual SV | |
591 | heads and bodies within the arenas must already have been freed. | |
592 | ||
593 | =cut | |
594 | */ | |
4633a7c4 | 595 | void |
864dbfa3 | 596 | Perl_sv_free_arenas(pTHX) |
4633a7c4 | 597 | { |
97aff369 | 598 | dVAR; |
4633a7c4 LW |
599 | SV* sva; |
600 | SV* svanext; | |
93e68bfb | 601 | int i; |
4633a7c4 LW |
602 | |
603 | /* Free arenas here, but be careful about fake ones. (We assume | |
604 | contiguity of the fake ones with the corresponding real ones.) */ | |
605 | ||
3280af22 | 606 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
4633a7c4 LW |
607 | svanext = (SV*) SvANY(sva); |
608 | while (svanext && SvFAKE(svanext)) | |
609 | svanext = (SV*) SvANY(svanext); | |
610 | ||
611 | if (!SvFAKE(sva)) | |
1df70142 | 612 | Safefree(sva); |
4633a7c4 | 613 | } |
93e68bfb | 614 | |
5e258f8c JC |
615 | { |
616 | struct arena_set *next, *aroot = (struct arena_set*) PL_body_arenas; | |
617 | ||
618 | for (; aroot; aroot = next) { | |
96a5add6 | 619 | const int max = aroot->curr; |
5e258f8c JC |
620 | for (i=0; i<max; i++) { |
621 | assert(aroot->set[i].arena); | |
622 | Safefree(aroot->set[i].arena); | |
623 | } | |
624 | next = aroot->next; | |
625 | Safefree(aroot); | |
626 | } | |
627 | } | |
dc8220bf | 628 | PL_body_arenas = 0; |
fdda85ca | 629 | |
232d1c15 | 630 | for (i=0; i<PERL_ARENA_ROOTS_SIZE; i++) |
93e68bfb | 631 | PL_body_roots[i] = 0; |
93e68bfb | 632 | |
43c5f42d | 633 | Safefree(PL_nice_chunk); |
bd61b366 | 634 | PL_nice_chunk = NULL; |
3280af22 NIS |
635 | PL_nice_chunk_size = 0; |
636 | PL_sv_arenaroot = 0; | |
637 | PL_sv_root = 0; | |
4633a7c4 LW |
638 | } |
639 | ||
bd81e77b NC |
640 | /* |
641 | Here are mid-level routines that manage the allocation of bodies out | |
642 | of the various arenas. There are 5 kinds of arenas: | |
29489e7c | 643 | |
bd81e77b NC |
644 | 1. SV-head arenas, which are discussed and handled above |
645 | 2. regular body arenas | |
646 | 3. arenas for reduced-size bodies | |
647 | 4. Hash-Entry arenas | |
648 | 5. pte arenas (thread related) | |
29489e7c | 649 | |
bd81e77b NC |
650 | Arena types 2 & 3 are chained by body-type off an array of |
651 | arena-root pointers, which is indexed by svtype. Some of the | |
652 | larger/less used body types are malloced singly, since a large | |
653 | unused block of them is wasteful. Also, several svtypes dont have | |
654 | bodies; the data fits into the sv-head itself. The arena-root | |
655 | pointer thus has a few unused root-pointers (which may be hijacked | |
656 | later for arena types 4,5) | |
29489e7c | 657 | |
bd81e77b NC |
658 | 3 differs from 2 as an optimization; some body types have several |
659 | unused fields in the front of the structure (which are kept in-place | |
660 | for consistency). These bodies can be allocated in smaller chunks, | |
661 | because the leading fields arent accessed. Pointers to such bodies | |
662 | are decremented to point at the unused 'ghost' memory, knowing that | |
663 | the pointers are used with offsets to the real memory. | |
29489e7c | 664 | |
bd81e77b NC |
665 | HE, HEK arenas are managed separately, with separate code, but may |
666 | be merge-able later.. | |
667 | ||
668 | PTE arenas are not sv-bodies, but they share these mid-level | |
669 | mechanics, so are considered here. The new mid-level mechanics rely | |
670 | on the sv_type of the body being allocated, so we just reserve one | |
671 | of the unused body-slots for PTEs, then use it in those (2) PTE | |
672 | contexts below (line ~10k) | |
673 | */ | |
674 | ||
bd26d9a3 | 675 | /* get_arena(size): this creates custom-sized arenas |
5e258f8c JC |
676 | TBD: export properly for hv.c: S_more_he(). |
677 | */ | |
678 | void* | |
679 | Perl_get_arena(pTHX_ int arena_size) | |
680 | { | |
5e258f8c | 681 | struct arena_desc* adesc; |
476a1e16 | 682 | struct arena_set *newroot, **aroot = (struct arena_set**) &PL_body_arenas; |
5e258f8c JC |
683 | int curr; |
684 | ||
476a1e16 JC |
685 | /* shouldnt need this |
686 | if (!arena_size) arena_size = PERL_ARENA_SIZE; | |
687 | */ | |
5e258f8c JC |
688 | |
689 | /* may need new arena-set to hold new arena */ | |
476a1e16 | 690 | if (!*aroot || (*aroot)->curr >= (*aroot)->set_size) { |
5e258f8c JC |
691 | Newxz(newroot, 1, struct arena_set); |
692 | newroot->set_size = ARENAS_PER_SET; | |
476a1e16 JC |
693 | newroot->next = *aroot; |
694 | *aroot = newroot; | |
695 | DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", *aroot)); | |
5e258f8c JC |
696 | } |
697 | ||
698 | /* ok, now have arena-set with at least 1 empty/available arena-desc */ | |
476a1e16 JC |
699 | curr = (*aroot)->curr++; |
700 | adesc = &((*aroot)->set[curr]); | |
5e258f8c JC |
701 | assert(!adesc->arena); |
702 | ||
5e258f8c JC |
703 | Newxz(adesc->arena, arena_size, char); |
704 | adesc->size = arena_size; | |
d2a0f284 JC |
705 | DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %d\n", |
706 | curr, adesc->arena, arena_size)); | |
5e258f8c JC |
707 | |
708 | return adesc->arena; | |
5e258f8c JC |
709 | } |
710 | ||
53c1dcc0 | 711 | |
bd81e77b | 712 | /* return a thing to the free list */ |
29489e7c | 713 | |
bd81e77b NC |
714 | #define del_body(thing, root) \ |
715 | STMT_START { \ | |
00b6aa41 | 716 | void ** const thing_copy = (void **)thing;\ |
bd81e77b NC |
717 | LOCK_SV_MUTEX; \ |
718 | *thing_copy = *root; \ | |
719 | *root = (void*)thing_copy; \ | |
720 | UNLOCK_SV_MUTEX; \ | |
721 | } STMT_END | |
29489e7c | 722 | |
bd81e77b | 723 | /* |
d2a0f284 JC |
724 | |
725 | =head1 SV-Body Allocation | |
726 | ||
727 | Allocation of SV-bodies is similar to SV-heads, differing as follows; | |
728 | the allocation mechanism is used for many body types, so is somewhat | |
729 | more complicated, it uses arena-sets, and has no need for still-live | |
730 | SV detection. | |
731 | ||
732 | At the outermost level, (new|del)_X*V macros return bodies of the | |
733 | appropriate type. These macros call either (new|del)_body_type or | |
734 | (new|del)_body_allocated macro pairs, depending on specifics of the | |
735 | type. Most body types use the former pair, the latter pair is used to | |
736 | allocate body types with "ghost fields". | |
737 | ||
738 | "ghost fields" are fields that are unused in certain types, and | |
739 | consequently dont need to actually exist. They are declared because | |
740 | they're part of a "base type", which allows use of functions as | |
741 | methods. The simplest examples are AVs and HVs, 2 aggregate types | |
742 | which don't use the fields which support SCALAR semantics. | |
743 | ||
744 | For these types, the arenas are carved up into *_allocated size | |
745 | chunks, we thus avoid wasted memory for those unaccessed members. | |
746 | When bodies are allocated, we adjust the pointer back in memory by the | |
747 | size of the bit not allocated, so it's as if we allocated the full | |
748 | structure. (But things will all go boom if you write to the part that | |
749 | is "not there", because you'll be overwriting the last members of the | |
750 | preceding structure in memory.) | |
751 | ||
752 | We calculate the correction using the STRUCT_OFFSET macro. For | |
753 | example, if xpv_allocated is the same structure as XPV then the two | |
754 | OFFSETs sum to zero, and the pointer is unchanged. If the allocated | |
755 | structure is smaller (no initial NV actually allocated) then the net | |
756 | effect is to subtract the size of the NV from the pointer, to return a | |
757 | new pointer as if an initial NV were actually allocated. | |
758 | ||
759 | This is the same trick as was used for NV and IV bodies. Ironically it | |
760 | doesn't need to be used for NV bodies any more, because NV is now at | |
761 | the start of the structure. IV bodies don't need it either, because | |
762 | they are no longer allocated. | |
763 | ||
764 | In turn, the new_body_* allocators call S_new_body(), which invokes | |
765 | new_body_inline macro, which takes a lock, and takes a body off the | |
766 | linked list at PL_body_roots[sv_type], calling S_more_bodies() if | |
767 | necessary to refresh an empty list. Then the lock is released, and | |
768 | the body is returned. | |
769 | ||
770 | S_more_bodies calls get_arena(), and carves it up into an array of N | |
771 | bodies, which it strings into a linked list. It looks up arena-size | |
772 | and body-size from the body_details table described below, thus | |
773 | supporting the multiple body-types. | |
774 | ||
775 | If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and | |
776 | the (new|del)_X*V macros are mapped directly to malloc/free. | |
777 | ||
778 | */ | |
779 | ||
780 | /* | |
781 | ||
782 | For each sv-type, struct body_details bodies_by_type[] carries | |
783 | parameters which control these aspects of SV handling: | |
784 | ||
785 | Arena_size determines whether arenas are used for this body type, and if | |
786 | so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to | |
787 | zero, forcing individual mallocs and frees. | |
788 | ||
789 | Body_size determines how big a body is, and therefore how many fit into | |
790 | each arena. Offset carries the body-pointer adjustment needed for | |
791 | *_allocated body types, and is used in *_allocated macros. | |
792 | ||
793 | But its main purpose is to parameterize info needed in | |
794 | Perl_sv_upgrade(). The info here dramatically simplifies the function | |
795 | vs the implementation in 5.8.7, making it table-driven. All fields | |
796 | are used for this, except for arena_size. | |
797 | ||
798 | For the sv-types that have no bodies, arenas are not used, so those | |
799 | PL_body_roots[sv_type] are unused, and can be overloaded. In | |
800 | something of a special case, SVt_NULL is borrowed for HE arenas; | |
801 | PL_body_roots[SVt_NULL] is filled by S_more_he, but the | |
802 | bodies_by_type[SVt_NULL] slot is not used, as the table is not | |
803 | available in hv.c, | |
804 | ||
805 | PTEs also use arenas, but are never seen in Perl_sv_upgrade. | |
806 | Nonetheless, they get their own slot in bodies_by_type[SVt_NULL], so | |
807 | they can just use the same allocation semantics. At first, PTEs were | |
808 | also overloaded to a non-body sv-type, but this yielded hard-to-find | |
809 | malloc bugs, so was simplified by claiming a new slot. This choice | |
810 | has no consequence at this time. | |
811 | ||
29489e7c DM |
812 | */ |
813 | ||
bd81e77b | 814 | struct body_details { |
0fb58b32 | 815 | U8 body_size; /* Size to allocate */ |
10666ae3 | 816 | U8 copy; /* Size of structure to copy (may be shorter) */ |
0fb58b32 | 817 | U8 offset; |
10666ae3 NC |
818 | unsigned int type : 4; /* We have space for a sanity check. */ |
819 | unsigned int cant_upgrade : 1; /* Cannot upgrade this type */ | |
820 | unsigned int zero_nv : 1; /* zero the NV when upgrading from this */ | |
821 | unsigned int arena : 1; /* Allocated from an arena */ | |
822 | size_t arena_size; /* Size of arena to allocate */ | |
bd81e77b | 823 | }; |
29489e7c | 824 | |
bd81e77b NC |
825 | #define HADNV FALSE |
826 | #define NONV TRUE | |
29489e7c | 827 | |
d2a0f284 | 828 | |
bd81e77b NC |
829 | #ifdef PURIFY |
830 | /* With -DPURFIY we allocate everything directly, and don't use arenas. | |
831 | This seems a rather elegant way to simplify some of the code below. */ | |
832 | #define HASARENA FALSE | |
833 | #else | |
834 | #define HASARENA TRUE | |
835 | #endif | |
836 | #define NOARENA FALSE | |
29489e7c | 837 | |
d2a0f284 JC |
838 | /* Size the arenas to exactly fit a given number of bodies. A count |
839 | of 0 fits the max number bodies into a PERL_ARENA_SIZE.block, | |
840 | simplifying the default. If count > 0, the arena is sized to fit | |
841 | only that many bodies, allowing arenas to be used for large, rare | |
842 | bodies (XPVFM, XPVIO) without undue waste. The arena size is | |
843 | limited by PERL_ARENA_SIZE, so we can safely oversize the | |
844 | declarations. | |
845 | */ | |
95db5f15 MB |
846 | #define FIT_ARENA0(body_size) \ |
847 | ((size_t)(PERL_ARENA_SIZE / body_size) * body_size) | |
848 | #define FIT_ARENAn(count,body_size) \ | |
849 | ( count * body_size <= PERL_ARENA_SIZE) \ | |
850 | ? count * body_size \ | |
851 | : FIT_ARENA0 (body_size) | |
852 | #define FIT_ARENA(count,body_size) \ | |
853 | count \ | |
854 | ? FIT_ARENAn (count, body_size) \ | |
855 | : FIT_ARENA0 (body_size) | |
d2a0f284 | 856 | |
bd81e77b | 857 | /* A macro to work out the offset needed to subtract from a pointer to (say) |
29489e7c | 858 | |
bd81e77b NC |
859 | typedef struct { |
860 | STRLEN xpv_cur; | |
861 | STRLEN xpv_len; | |
862 | } xpv_allocated; | |
29489e7c | 863 | |
bd81e77b | 864 | to make its members accessible via a pointer to (say) |
29489e7c | 865 | |
bd81e77b NC |
866 | struct xpv { |
867 | NV xnv_nv; | |
868 | STRLEN xpv_cur; | |
869 | STRLEN xpv_len; | |
870 | }; | |
29489e7c | 871 | |
bd81e77b | 872 | */ |
29489e7c | 873 | |
bd81e77b NC |
874 | #define relative_STRUCT_OFFSET(longer, shorter, member) \ |
875 | (STRUCT_OFFSET(shorter, member) - STRUCT_OFFSET(longer, member)) | |
29489e7c | 876 | |
bd81e77b NC |
877 | /* Calculate the length to copy. Specifically work out the length less any |
878 | final padding the compiler needed to add. See the comment in sv_upgrade | |
879 | for why copying the padding proved to be a bug. */ | |
29489e7c | 880 | |
bd81e77b NC |
881 | #define copy_length(type, last_member) \ |
882 | STRUCT_OFFSET(type, last_member) \ | |
883 | + sizeof (((type*)SvANY((SV*)0))->last_member) | |
29489e7c | 884 | |
bd81e77b | 885 | static const struct body_details bodies_by_type[] = { |
10666ae3 NC |
886 | { sizeof(HE), 0, 0, SVt_NULL, |
887 | FALSE, NONV, NOARENA, FIT_ARENA(0, sizeof(HE)) }, | |
d2a0f284 JC |
888 | |
889 | /* IVs are in the head, so the allocation size is 0. | |
890 | However, the slot is overloaded for PTEs. */ | |
891 | { sizeof(struct ptr_tbl_ent), /* This is used for PTEs. */ | |
892 | sizeof(IV), /* This is used to copy out the IV body. */ | |
10666ae3 | 893 | STRUCT_OFFSET(XPVIV, xiv_iv), SVt_IV, FALSE, NONV, |
d2a0f284 JC |
894 | NOARENA /* IVS don't need an arena */, |
895 | /* But PTEs need to know the size of their arena */ | |
896 | FIT_ARENA(0, sizeof(struct ptr_tbl_ent)) | |
897 | }, | |
898 | ||
bd81e77b | 899 | /* 8 bytes on most ILP32 with IEEE doubles */ |
10666ae3 | 900 | { sizeof(NV), sizeof(NV), 0, SVt_NV, FALSE, HADNV, HASARENA, |
d2a0f284 JC |
901 | FIT_ARENA(0, sizeof(NV)) }, |
902 | ||
903 | /* RVs are in the head now. */ | |
10666ae3 | 904 | { 0, 0, 0, SVt_RV, FALSE, NONV, NOARENA, 0 }, |
d2a0f284 | 905 | |
bd81e77b | 906 | /* 8 bytes on most ILP32 with IEEE doubles */ |
d2a0f284 JC |
907 | { sizeof(xpv_allocated), |
908 | copy_length(XPV, xpv_len) | |
909 | - relative_STRUCT_OFFSET(xpv_allocated, XPV, xpv_cur), | |
910 | + relative_STRUCT_OFFSET(xpv_allocated, XPV, xpv_cur), | |
10666ae3 | 911 | SVt_PV, FALSE, NONV, HASARENA, FIT_ARENA(0, sizeof(xpv_allocated)) }, |
d2a0f284 | 912 | |
bd81e77b | 913 | /* 12 */ |
d2a0f284 JC |
914 | { sizeof(xpviv_allocated), |
915 | copy_length(XPVIV, xiv_u) | |
916 | - relative_STRUCT_OFFSET(xpviv_allocated, XPVIV, xpv_cur), | |
917 | + relative_STRUCT_OFFSET(xpviv_allocated, XPVIV, xpv_cur), | |
10666ae3 | 918 | SVt_PVIV, FALSE, NONV, HASARENA, FIT_ARENA(0, sizeof(xpviv_allocated)) }, |
d2a0f284 | 919 | |
bd81e77b | 920 | /* 20 */ |
10666ae3 | 921 | { sizeof(XPVNV), copy_length(XPVNV, xiv_u), 0, SVt_PVNV, FALSE, HADNV, |
d2a0f284 JC |
922 | HASARENA, FIT_ARENA(0, sizeof(XPVNV)) }, |
923 | ||
bd81e77b | 924 | /* 28 */ |
10666ae3 | 925 | { sizeof(XPVMG), copy_length(XPVMG, xmg_stash), 0, SVt_PVMG, FALSE, HADNV, |
d2a0f284 JC |
926 | HASARENA, FIT_ARENA(0, sizeof(XPVMG)) }, |
927 | ||
bd81e77b | 928 | /* 36 */ |
10666ae3 | 929 | { sizeof(XPVBM), sizeof(XPVBM), 0, SVt_PVBM, TRUE, HADNV, |
d2a0f284 JC |
930 | HASARENA, FIT_ARENA(0, sizeof(XPVBM)) }, |
931 | ||
bd81e77b | 932 | /* 48 */ |
10666ae3 | 933 | { sizeof(XPVGV), sizeof(XPVGV), 0, SVt_PVGV, TRUE, HADNV, |
d2a0f284 JC |
934 | HASARENA, FIT_ARENA(0, sizeof(XPVGV)) }, |
935 | ||
bd81e77b | 936 | /* 64 */ |
10666ae3 | 937 | { sizeof(XPVLV), sizeof(XPVLV), 0, SVt_PVLV, TRUE, HADNV, |
d2a0f284 JC |
938 | HASARENA, FIT_ARENA(0, sizeof(XPVLV)) }, |
939 | ||
940 | { sizeof(xpvav_allocated), | |
941 | copy_length(XPVAV, xmg_stash) | |
942 | - relative_STRUCT_OFFSET(xpvav_allocated, XPVAV, xav_fill), | |
943 | + relative_STRUCT_OFFSET(xpvav_allocated, XPVAV, xav_fill), | |
10666ae3 | 944 | SVt_PVAV, TRUE, HADNV, HASARENA, FIT_ARENA(0, sizeof(xpvav_allocated)) }, |
d2a0f284 JC |
945 | |
946 | { sizeof(xpvhv_allocated), | |
947 | copy_length(XPVHV, xmg_stash) | |
948 | - relative_STRUCT_OFFSET(xpvhv_allocated, XPVHV, xhv_fill), | |
949 | + relative_STRUCT_OFFSET(xpvhv_allocated, XPVHV, xhv_fill), | |
10666ae3 | 950 | SVt_PVHV, TRUE, HADNV, HASARENA, FIT_ARENA(0, sizeof(xpvhv_allocated)) }, |
d2a0f284 | 951 | |
c84c4652 | 952 | /* 56 */ |
4115f141 | 953 | { sizeof(xpvcv_allocated), sizeof(xpvcv_allocated), |
c84c4652 | 954 | + relative_STRUCT_OFFSET(xpvcv_allocated, XPVCV, xpv_cur), |
10666ae3 | 955 | SVt_PVCV, TRUE, NONV, HASARENA, FIT_ARENA(0, sizeof(xpvcv_allocated)) }, |
d2a0f284 | 956 | |
4115f141 | 957 | { sizeof(xpvfm_allocated), sizeof(xpvfm_allocated), |
3038937b | 958 | + relative_STRUCT_OFFSET(xpvfm_allocated, XPVFM, xpv_cur), |
10666ae3 | 959 | SVt_PVFM, TRUE, NONV, NOARENA, FIT_ARENA(20, sizeof(xpvfm_allocated)) }, |
d2a0f284 JC |
960 | |
961 | /* XPVIO is 84 bytes, fits 48x */ | |
10666ae3 | 962 | { sizeof(XPVIO), sizeof(XPVIO), 0, SVt_PVIO, TRUE, HADNV, |
d2a0f284 | 963 | HASARENA, FIT_ARENA(24, sizeof(XPVIO)) }, |
bd81e77b | 964 | }; |
29489e7c | 965 | |
d2a0f284 JC |
966 | #define new_body_type(sv_type) \ |
967 | (void *)((char *)S_new_body(aTHX_ sv_type)) | |
29489e7c | 968 | |
bd81e77b NC |
969 | #define del_body_type(p, sv_type) \ |
970 | del_body(p, &PL_body_roots[sv_type]) | |
29489e7c | 971 | |
29489e7c | 972 | |
bd81e77b | 973 | #define new_body_allocated(sv_type) \ |
d2a0f284 | 974 | (void *)((char *)S_new_body(aTHX_ sv_type) \ |
bd81e77b | 975 | - bodies_by_type[sv_type].offset) |
29489e7c | 976 | |
bd81e77b NC |
977 | #define del_body_allocated(p, sv_type) \ |
978 | del_body(p + bodies_by_type[sv_type].offset, &PL_body_roots[sv_type]) | |
29489e7c | 979 | |
29489e7c | 980 | |
bd81e77b NC |
981 | #define my_safemalloc(s) (void*)safemalloc(s) |
982 | #define my_safecalloc(s) (void*)safecalloc(s, 1) | |
983 | #define my_safefree(p) safefree((char*)p) | |
29489e7c | 984 | |
bd81e77b | 985 | #ifdef PURIFY |
29489e7c | 986 | |
bd81e77b NC |
987 | #define new_XNV() my_safemalloc(sizeof(XPVNV)) |
988 | #define del_XNV(p) my_safefree(p) | |
29489e7c | 989 | |
bd81e77b NC |
990 | #define new_XPVNV() my_safemalloc(sizeof(XPVNV)) |
991 | #define del_XPVNV(p) my_safefree(p) | |
29489e7c | 992 | |
bd81e77b NC |
993 | #define new_XPVAV() my_safemalloc(sizeof(XPVAV)) |
994 | #define del_XPVAV(p) my_safefree(p) | |
29489e7c | 995 | |
bd81e77b NC |
996 | #define new_XPVHV() my_safemalloc(sizeof(XPVHV)) |
997 | #define del_XPVHV(p) my_safefree(p) | |
29489e7c | 998 | |
bd81e77b NC |
999 | #define new_XPVMG() my_safemalloc(sizeof(XPVMG)) |
1000 | #define del_XPVMG(p) my_safefree(p) | |
29489e7c | 1001 | |
bd81e77b NC |
1002 | #define new_XPVGV() my_safemalloc(sizeof(XPVGV)) |
1003 | #define del_XPVGV(p) my_safefree(p) | |
29489e7c | 1004 | |
bd81e77b | 1005 | #else /* !PURIFY */ |
29489e7c | 1006 | |
bd81e77b NC |
1007 | #define new_XNV() new_body_type(SVt_NV) |
1008 | #define del_XNV(p) del_body_type(p, SVt_NV) | |
29489e7c | 1009 | |
bd81e77b NC |
1010 | #define new_XPVNV() new_body_type(SVt_PVNV) |
1011 | #define del_XPVNV(p) del_body_type(p, SVt_PVNV) | |
29489e7c | 1012 | |
bd81e77b NC |
1013 | #define new_XPVAV() new_body_allocated(SVt_PVAV) |
1014 | #define del_XPVAV(p) del_body_allocated(p, SVt_PVAV) | |
645c22ef | 1015 | |
bd81e77b NC |
1016 | #define new_XPVHV() new_body_allocated(SVt_PVHV) |
1017 | #define del_XPVHV(p) del_body_allocated(p, SVt_PVHV) | |
645c22ef | 1018 | |
bd81e77b NC |
1019 | #define new_XPVMG() new_body_type(SVt_PVMG) |
1020 | #define del_XPVMG(p) del_body_type(p, SVt_PVMG) | |
645c22ef | 1021 | |
bd81e77b NC |
1022 | #define new_XPVGV() new_body_type(SVt_PVGV) |
1023 | #define del_XPVGV(p) del_body_type(p, SVt_PVGV) | |
1d7c1841 | 1024 | |
bd81e77b | 1025 | #endif /* PURIFY */ |
93e68bfb | 1026 | |
bd81e77b | 1027 | /* no arena for you! */ |
93e68bfb | 1028 | |
bd81e77b | 1029 | #define new_NOARENA(details) \ |
d2a0f284 | 1030 | my_safemalloc((details)->body_size + (details)->offset) |
bd81e77b | 1031 | #define new_NOARENAZ(details) \ |
d2a0f284 JC |
1032 | my_safecalloc((details)->body_size + (details)->offset) |
1033 | ||
10666ae3 NC |
1034 | #ifdef DEBUGGING |
1035 | static bool done_sanity_check; | |
1036 | #endif | |
1037 | ||
d2a0f284 JC |
1038 | STATIC void * |
1039 | S_more_bodies (pTHX_ svtype sv_type) | |
1040 | { | |
1041 | dVAR; | |
1042 | void ** const root = &PL_body_roots[sv_type]; | |
96a5add6 | 1043 | const struct body_details * const bdp = &bodies_by_type[sv_type]; |
d2a0f284 JC |
1044 | const size_t body_size = bdp->body_size; |
1045 | char *start; | |
1046 | const char *end; | |
1047 | ||
1048 | assert(bdp->arena_size); | |
10666ae3 NC |
1049 | |
1050 | #ifdef DEBUGGING | |
1051 | if (!done_sanity_check) { | |
ea471437 | 1052 | unsigned int i = SVt_LAST; |
10666ae3 NC |
1053 | |
1054 | done_sanity_check = TRUE; | |
1055 | ||
1056 | while (i--) | |
1057 | assert (bodies_by_type[i].type == i); | |
1058 | } | |
1059 | #endif | |
1060 | ||
d2a0f284 JC |
1061 | start = (char*) Perl_get_arena(aTHX_ bdp->arena_size); |
1062 | ||
1063 | end = start + bdp->arena_size - body_size; | |
1064 | ||
d2a0f284 JC |
1065 | /* computed count doesnt reflect the 1st slot reservation */ |
1066 | DEBUG_m(PerlIO_printf(Perl_debug_log, | |
1067 | "arena %p end %p arena-size %d type %d size %d ct %d\n", | |
1068 | start, end, bdp->arena_size, sv_type, body_size, | |
1069 | bdp->arena_size / body_size)); | |
d2a0f284 JC |
1070 | |
1071 | *root = (void *)start; | |
1072 | ||
1073 | while (start < end) { | |
1074 | char * const next = start + body_size; | |
1075 | *(void**) start = (void *)next; | |
1076 | start = next; | |
1077 | } | |
1078 | *(void **)start = 0; | |
1079 | ||
1080 | return *root; | |
1081 | } | |
1082 | ||
1083 | /* grab a new thing from the free list, allocating more if necessary. | |
1084 | The inline version is used for speed in hot routines, and the | |
1085 | function using it serves the rest (unless PURIFY). | |
1086 | */ | |
1087 | #define new_body_inline(xpv, sv_type) \ | |
1088 | STMT_START { \ | |
1089 | void ** const r3wt = &PL_body_roots[sv_type]; \ | |
1090 | LOCK_SV_MUTEX; \ | |
1091 | xpv = *((void **)(r3wt)) \ | |
d4c19fe8 | 1092 | ? *((void **)(r3wt)) : more_bodies(sv_type); \ |
d2a0f284 JC |
1093 | *(r3wt) = *(void**)(xpv); \ |
1094 | UNLOCK_SV_MUTEX; \ | |
1095 | } STMT_END | |
1096 | ||
1097 | #ifndef PURIFY | |
1098 | ||
1099 | STATIC void * | |
1100 | S_new_body(pTHX_ svtype sv_type) | |
1101 | { | |
1102 | dVAR; | |
1103 | void *xpv; | |
1104 | new_body_inline(xpv, sv_type); | |
1105 | return xpv; | |
1106 | } | |
1107 | ||
1108 | #endif | |
93e68bfb | 1109 | |
bd81e77b NC |
1110 | /* |
1111 | =for apidoc sv_upgrade | |
93e68bfb | 1112 | |
bd81e77b NC |
1113 | Upgrade an SV to a more complex form. Generally adds a new body type to the |
1114 | SV, then copies across as much information as possible from the old body. | |
1115 | You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>. | |
93e68bfb | 1116 | |
bd81e77b | 1117 | =cut |
93e68bfb | 1118 | */ |
93e68bfb | 1119 | |
bd81e77b NC |
1120 | void |
1121 | Perl_sv_upgrade(pTHX_ register SV *sv, U32 new_type) | |
cac9b346 | 1122 | { |
97aff369 | 1123 | dVAR; |
bd81e77b NC |
1124 | void* old_body; |
1125 | void* new_body; | |
1126 | const U32 old_type = SvTYPE(sv); | |
d2a0f284 | 1127 | const struct body_details *new_type_details; |
bd81e77b NC |
1128 | const struct body_details *const old_type_details |
1129 | = bodies_by_type + old_type; | |
cac9b346 | 1130 | |
bd81e77b NC |
1131 | if (new_type != SVt_PV && SvIsCOW(sv)) { |
1132 | sv_force_normal_flags(sv, 0); | |
1133 | } | |
cac9b346 | 1134 | |
bd81e77b NC |
1135 | if (old_type == new_type) |
1136 | return; | |
cac9b346 | 1137 | |
bd81e77b NC |
1138 | if (old_type > new_type) |
1139 | Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d", | |
1140 | (int)old_type, (int)new_type); | |
cac9b346 | 1141 | |
cac9b346 | 1142 | |
bd81e77b | 1143 | old_body = SvANY(sv); |
de042e1d | 1144 | |
bd81e77b NC |
1145 | /* Copying structures onto other structures that have been neatly zeroed |
1146 | has a subtle gotcha. Consider XPVMG | |
cac9b346 | 1147 | |
bd81e77b NC |
1148 | +------+------+------+------+------+-------+-------+ |
1149 | | NV | CUR | LEN | IV | MAGIC | STASH | | |
1150 | +------+------+------+------+------+-------+-------+ | |
1151 | 0 4 8 12 16 20 24 28 | |
645c22ef | 1152 | |
bd81e77b NC |
1153 | where NVs are aligned to 8 bytes, so that sizeof that structure is |
1154 | actually 32 bytes long, with 4 bytes of padding at the end: | |
08742458 | 1155 | |
bd81e77b NC |
1156 | +------+------+------+------+------+-------+-------+------+ |
1157 | | NV | CUR | LEN | IV | MAGIC | STASH | ??? | | |
1158 | +------+------+------+------+------+-------+-------+------+ | |
1159 | 0 4 8 12 16 20 24 28 32 | |
08742458 | 1160 | |
bd81e77b | 1161 | so what happens if you allocate memory for this structure: |
30f9da9e | 1162 | |
bd81e77b NC |
1163 | +------+------+------+------+------+-------+-------+------+------+... |
1164 | | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME | | |
1165 | +------+------+------+------+------+-------+-------+------+------+... | |
1166 | 0 4 8 12 16 20 24 28 32 36 | |
bfc44f79 | 1167 | |
bd81e77b NC |
1168 | zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you |
1169 | expect, because you copy the area marked ??? onto GP. Now, ??? may have | |
1170 | started out as zero once, but it's quite possible that it isn't. So now, | |
1171 | rather than a nicely zeroed GP, you have it pointing somewhere random. | |
1172 | Bugs ensue. | |
bfc44f79 | 1173 | |
bd81e77b NC |
1174 | (In fact, GP ends up pointing at a previous GP structure, because the |
1175 | principle cause of the padding in XPVMG getting garbage is a copy of | |
1176 | sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob) | |
30f9da9e | 1177 | |
bd81e77b NC |
1178 | So we are careful and work out the size of used parts of all the |
1179 | structures. */ | |
bfc44f79 | 1180 | |
bd81e77b NC |
1181 | switch (old_type) { |
1182 | case SVt_NULL: | |
1183 | break; | |
1184 | case SVt_IV: | |
1185 | if (new_type < SVt_PVIV) { | |
1186 | new_type = (new_type == SVt_NV) | |
1187 | ? SVt_PVNV : SVt_PVIV; | |
bd81e77b NC |
1188 | } |
1189 | break; | |
1190 | case SVt_NV: | |
1191 | if (new_type < SVt_PVNV) { | |
1192 | new_type = SVt_PVNV; | |
bd81e77b NC |
1193 | } |
1194 | break; | |
1195 | case SVt_RV: | |
1196 | break; | |
1197 | case SVt_PV: | |
1198 | assert(new_type > SVt_PV); | |
1199 | assert(SVt_IV < SVt_PV); | |
1200 | assert(SVt_NV < SVt_PV); | |
1201 | break; | |
1202 | case SVt_PVIV: | |
1203 | break; | |
1204 | case SVt_PVNV: | |
1205 | break; | |
1206 | case SVt_PVMG: | |
1207 | /* Because the XPVMG of PL_mess_sv isn't allocated from the arena, | |
1208 | there's no way that it can be safely upgraded, because perl.c | |
1209 | expects to Safefree(SvANY(PL_mess_sv)) */ | |
1210 | assert(sv != PL_mess_sv); | |
1211 | /* This flag bit is used to mean other things in other scalar types. | |
1212 | Given that it only has meaning inside the pad, it shouldn't be set | |
1213 | on anything that can get upgraded. */ | |
00b1698f | 1214 | assert(!SvPAD_TYPED(sv)); |
bd81e77b NC |
1215 | break; |
1216 | default: | |
1217 | if (old_type_details->cant_upgrade) | |
c81225bc NC |
1218 | Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf, |
1219 | sv_reftype(sv, 0), (UV) old_type, (UV) new_type); | |
bd81e77b | 1220 | } |
2fa1109b | 1221 | new_type_details = bodies_by_type + new_type; |
645c22ef | 1222 | |
bd81e77b NC |
1223 | SvFLAGS(sv) &= ~SVTYPEMASK; |
1224 | SvFLAGS(sv) |= new_type; | |
932e9ff9 | 1225 | |
ab4416c0 NC |
1226 | /* This can't happen, as SVt_NULL is <= all values of new_type, so one of |
1227 | the return statements above will have triggered. */ | |
1228 | assert (new_type != SVt_NULL); | |
bd81e77b | 1229 | switch (new_type) { |
bd81e77b NC |
1230 | case SVt_IV: |
1231 | assert(old_type == SVt_NULL); | |
1232 | SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv)); | |
1233 | SvIV_set(sv, 0); | |
1234 | return; | |
1235 | case SVt_NV: | |
1236 | assert(old_type == SVt_NULL); | |
1237 | SvANY(sv) = new_XNV(); | |
1238 | SvNV_set(sv, 0); | |
1239 | return; | |
1240 | case SVt_RV: | |
1241 | assert(old_type == SVt_NULL); | |
1242 | SvANY(sv) = &sv->sv_u.svu_rv; | |
1243 | SvRV_set(sv, 0); | |
1244 | return; | |
1245 | case SVt_PVHV: | |
bd81e77b | 1246 | case SVt_PVAV: |
d2a0f284 | 1247 | assert(new_type_details->body_size); |
c1ae03ae NC |
1248 | |
1249 | #ifndef PURIFY | |
1250 | assert(new_type_details->arena); | |
d2a0f284 | 1251 | assert(new_type_details->arena_size); |
c1ae03ae | 1252 | /* This points to the start of the allocated area. */ |
d2a0f284 JC |
1253 | new_body_inline(new_body, new_type); |
1254 | Zero(new_body, new_type_details->body_size, char); | |
c1ae03ae NC |
1255 | new_body = ((char *)new_body) - new_type_details->offset; |
1256 | #else | |
1257 | /* We always allocated the full length item with PURIFY. To do this | |
1258 | we fake things so that arena is false for all 16 types.. */ | |
1259 | new_body = new_NOARENAZ(new_type_details); | |
1260 | #endif | |
1261 | SvANY(sv) = new_body; | |
1262 | if (new_type == SVt_PVAV) { | |
1263 | AvMAX(sv) = -1; | |
1264 | AvFILLp(sv) = -1; | |
1265 | AvREAL_only(sv); | |
1266 | } | |
aeb18a1e | 1267 | |
bd81e77b NC |
1268 | /* SVt_NULL isn't the only thing upgraded to AV or HV. |
1269 | The target created by newSVrv also is, and it can have magic. | |
1270 | However, it never has SvPVX set. | |
1271 | */ | |
1272 | if (old_type >= SVt_RV) { | |
1273 | assert(SvPVX_const(sv) == 0); | |
1274 | } | |
aeb18a1e | 1275 | |
bd81e77b NC |
1276 | /* Could put this in the else clause below, as PVMG must have SvPVX |
1277 | 0 already (the assertion above) */ | |
6136c704 | 1278 | SvPV_set(sv, NULL); |
93e68bfb | 1279 | |
bd81e77b | 1280 | if (old_type >= SVt_PVMG) { |
e736a858 | 1281 | SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic); |
bd81e77b | 1282 | SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash); |
bd81e77b NC |
1283 | } |
1284 | break; | |
93e68bfb | 1285 | |
93e68bfb | 1286 | |
bd81e77b NC |
1287 | case SVt_PVIV: |
1288 | /* XXX Is this still needed? Was it ever needed? Surely as there is | |
1289 | no route from NV to PVIV, NOK can never be true */ | |
1290 | assert(!SvNOKp(sv)); | |
1291 | assert(!SvNOK(sv)); | |
1292 | case SVt_PVIO: | |
1293 | case SVt_PVFM: | |
1294 | case SVt_PVBM: | |
1295 | case SVt_PVGV: | |
1296 | case SVt_PVCV: | |
1297 | case SVt_PVLV: | |
1298 | case SVt_PVMG: | |
1299 | case SVt_PVNV: | |
1300 | case SVt_PV: | |
93e68bfb | 1301 | |
d2a0f284 | 1302 | assert(new_type_details->body_size); |
bd81e77b NC |
1303 | /* We always allocated the full length item with PURIFY. To do this |
1304 | we fake things so that arena is false for all 16 types.. */ | |
1305 | if(new_type_details->arena) { | |
1306 | /* This points to the start of the allocated area. */ | |
d2a0f284 JC |
1307 | new_body_inline(new_body, new_type); |
1308 | Zero(new_body, new_type_details->body_size, char); | |
bd81e77b NC |
1309 | new_body = ((char *)new_body) - new_type_details->offset; |
1310 | } else { | |
1311 | new_body = new_NOARENAZ(new_type_details); | |
1312 | } | |
1313 | SvANY(sv) = new_body; | |
5e2fc214 | 1314 | |
bd81e77b | 1315 | if (old_type_details->copy) { |
f9ba3d20 NC |
1316 | /* There is now the potential for an upgrade from something without |
1317 | an offset (PVNV or PVMG) to something with one (PVCV, PVFM) */ | |
1318 | int offset = old_type_details->offset; | |
1319 | int length = old_type_details->copy; | |
1320 | ||
1321 | if (new_type_details->offset > old_type_details->offset) { | |
d4c19fe8 | 1322 | const int difference |
f9ba3d20 NC |
1323 | = new_type_details->offset - old_type_details->offset; |
1324 | offset += difference; | |
1325 | length -= difference; | |
1326 | } | |
1327 | assert (length >= 0); | |
1328 | ||
1329 | Copy((char *)old_body + offset, (char *)new_body + offset, length, | |
1330 | char); | |
bd81e77b NC |
1331 | } |
1332 | ||
1333 | #ifndef NV_ZERO_IS_ALLBITS_ZERO | |
f2524eef | 1334 | /* If NV 0.0 is stores as all bits 0 then Zero() already creates a |
e5ce394c NC |
1335 | * correct 0.0 for us. Otherwise, if the old body didn't have an |
1336 | * NV slot, but the new one does, then we need to initialise the | |
1337 | * freshly created NV slot with whatever the correct bit pattern is | |
1338 | * for 0.0 */ | |
1339 | if (old_type_details->zero_nv && !new_type_details->zero_nv) | |
bd81e77b | 1340 | SvNV_set(sv, 0); |
82048762 | 1341 | #endif |
5e2fc214 | 1342 | |
bd81e77b | 1343 | if (new_type == SVt_PVIO) |
f2524eef | 1344 | IoPAGE_LEN(sv) = 60; |
bd81e77b | 1345 | if (old_type < SVt_RV) |
6136c704 | 1346 | SvPV_set(sv, NULL); |
bd81e77b NC |
1347 | break; |
1348 | default: | |
afd78fd5 JH |
1349 | Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu", |
1350 | (unsigned long)new_type); | |
bd81e77b | 1351 | } |
73171d91 | 1352 | |
d2a0f284 JC |
1353 | if (old_type_details->arena) { |
1354 | /* If there was an old body, then we need to free it. | |
1355 | Note that there is an assumption that all bodies of types that | |
1356 | can be upgraded came from arenas. Only the more complex non- | |
1357 | upgradable types are allowed to be directly malloc()ed. */ | |
bd81e77b NC |
1358 | #ifdef PURIFY |
1359 | my_safefree(old_body); | |
1360 | #else | |
1361 | del_body((void*)((char*)old_body + old_type_details->offset), | |
1362 | &PL_body_roots[old_type]); | |
1363 | #endif | |
1364 | } | |
1365 | } | |
73171d91 | 1366 | |
bd81e77b NC |
1367 | /* |
1368 | =for apidoc sv_backoff | |
73171d91 | 1369 | |
bd81e77b NC |
1370 | Remove any string offset. You should normally use the C<SvOOK_off> macro |
1371 | wrapper instead. | |
73171d91 | 1372 | |
bd81e77b | 1373 | =cut |
73171d91 NC |
1374 | */ |
1375 | ||
bd81e77b NC |
1376 | int |
1377 | Perl_sv_backoff(pTHX_ register SV *sv) | |
1378 | { | |
96a5add6 | 1379 | PERL_UNUSED_CONTEXT; |
bd81e77b NC |
1380 | assert(SvOOK(sv)); |
1381 | assert(SvTYPE(sv) != SVt_PVHV); | |
1382 | assert(SvTYPE(sv) != SVt_PVAV); | |
1383 | if (SvIVX(sv)) { | |
1384 | const char * const s = SvPVX_const(sv); | |
1385 | SvLEN_set(sv, SvLEN(sv) + SvIVX(sv)); | |
1386 | SvPV_set(sv, SvPVX(sv) - SvIVX(sv)); | |
1387 | SvIV_set(sv, 0); | |
1388 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); | |
1389 | } | |
1390 | SvFLAGS(sv) &= ~SVf_OOK; | |
1391 | return 0; | |
1392 | } | |
73171d91 | 1393 | |
bd81e77b NC |
1394 | /* |
1395 | =for apidoc sv_grow | |
73171d91 | 1396 | |
bd81e77b NC |
1397 | Expands the character buffer in the SV. If necessary, uses C<sv_unref> and |
1398 | upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1399 | Use the C<SvGROW> wrapper instead. | |
93e68bfb | 1400 | |
bd81e77b NC |
1401 | =cut |
1402 | */ | |
93e68bfb | 1403 | |
bd81e77b NC |
1404 | char * |
1405 | Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen) | |
1406 | { | |
1407 | register char *s; | |
93e68bfb | 1408 | |
5db06880 NC |
1409 | if (PL_madskills && newlen >= 0x100000) { |
1410 | PerlIO_printf(Perl_debug_log, | |
1411 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
1412 | } | |
bd81e77b NC |
1413 | #ifdef HAS_64K_LIMIT |
1414 | if (newlen >= 0x10000) { | |
1415 | PerlIO_printf(Perl_debug_log, | |
1416 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
1417 | my_exit(1); | |
1418 | } | |
1419 | #endif /* HAS_64K_LIMIT */ | |
1420 | if (SvROK(sv)) | |
1421 | sv_unref(sv); | |
1422 | if (SvTYPE(sv) < SVt_PV) { | |
1423 | sv_upgrade(sv, SVt_PV); | |
1424 | s = SvPVX_mutable(sv); | |
1425 | } | |
1426 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1427 | sv_backoff(sv); | |
1428 | s = SvPVX_mutable(sv); | |
1429 | if (newlen > SvLEN(sv)) | |
1430 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
1431 | #ifdef HAS_64K_LIMIT | |
1432 | if (newlen >= 0x10000) | |
1433 | newlen = 0xFFFF; | |
1434 | #endif | |
1435 | } | |
1436 | else | |
1437 | s = SvPVX_mutable(sv); | |
aeb18a1e | 1438 | |
bd81e77b NC |
1439 | if (newlen > SvLEN(sv)) { /* need more room? */ |
1440 | newlen = PERL_STRLEN_ROUNDUP(newlen); | |
1441 | if (SvLEN(sv) && s) { | |
1442 | #ifdef MYMALLOC | |
1443 | const STRLEN l = malloced_size((void*)SvPVX_const(sv)); | |
1444 | if (newlen <= l) { | |
1445 | SvLEN_set(sv, l); | |
1446 | return s; | |
1447 | } else | |
1448 | #endif | |
1449 | s = saferealloc(s, newlen); | |
1450 | } | |
1451 | else { | |
1452 | s = safemalloc(newlen); | |
1453 | if (SvPVX_const(sv) && SvCUR(sv)) { | |
1454 | Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char); | |
1455 | } | |
1456 | } | |
1457 | SvPV_set(sv, s); | |
1458 | SvLEN_set(sv, newlen); | |
1459 | } | |
1460 | return s; | |
1461 | } | |
aeb18a1e | 1462 | |
bd81e77b NC |
1463 | /* |
1464 | =for apidoc sv_setiv | |
932e9ff9 | 1465 | |
bd81e77b NC |
1466 | Copies an integer into the given SV, upgrading first if necessary. |
1467 | Does not handle 'set' magic. See also C<sv_setiv_mg>. | |
463ee0b2 | 1468 | |
bd81e77b NC |
1469 | =cut |
1470 | */ | |
463ee0b2 | 1471 | |
bd81e77b NC |
1472 | void |
1473 | Perl_sv_setiv(pTHX_ register SV *sv, IV i) | |
1474 | { | |
97aff369 | 1475 | dVAR; |
bd81e77b NC |
1476 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
1477 | switch (SvTYPE(sv)) { | |
1478 | case SVt_NULL: | |
1479 | sv_upgrade(sv, SVt_IV); | |
1480 | break; | |
1481 | case SVt_NV: | |
1482 | sv_upgrade(sv, SVt_PVNV); | |
1483 | break; | |
1484 | case SVt_RV: | |
1485 | case SVt_PV: | |
1486 | sv_upgrade(sv, SVt_PVIV); | |
1487 | break; | |
463ee0b2 | 1488 | |
bd81e77b NC |
1489 | case SVt_PVGV: |
1490 | case SVt_PVAV: | |
1491 | case SVt_PVHV: | |
1492 | case SVt_PVCV: | |
1493 | case SVt_PVFM: | |
1494 | case SVt_PVIO: | |
1495 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), | |
1496 | OP_DESC(PL_op)); | |
1497 | } | |
1498 | (void)SvIOK_only(sv); /* validate number */ | |
1499 | SvIV_set(sv, i); | |
1500 | SvTAINT(sv); | |
1501 | } | |
932e9ff9 | 1502 | |
bd81e77b NC |
1503 | /* |
1504 | =for apidoc sv_setiv_mg | |
d33b2eba | 1505 | |
bd81e77b | 1506 | Like C<sv_setiv>, but also handles 'set' magic. |
1c846c1f | 1507 | |
bd81e77b NC |
1508 | =cut |
1509 | */ | |
d33b2eba | 1510 | |
bd81e77b NC |
1511 | void |
1512 | Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i) | |
1513 | { | |
1514 | sv_setiv(sv,i); | |
1515 | SvSETMAGIC(sv); | |
1516 | } | |
727879eb | 1517 | |
bd81e77b NC |
1518 | /* |
1519 | =for apidoc sv_setuv | |
d33b2eba | 1520 | |
bd81e77b NC |
1521 | Copies an unsigned integer into the given SV, upgrading first if necessary. |
1522 | Does not handle 'set' magic. See also C<sv_setuv_mg>. | |
9b94d1dd | 1523 | |
bd81e77b NC |
1524 | =cut |
1525 | */ | |
d33b2eba | 1526 | |
bd81e77b NC |
1527 | void |
1528 | Perl_sv_setuv(pTHX_ register SV *sv, UV u) | |
1529 | { | |
1530 | /* With these two if statements: | |
1531 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d33b2eba | 1532 | |
bd81e77b NC |
1533 | without |
1534 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
1c846c1f | 1535 | |
bd81e77b NC |
1536 | If you wish to remove them, please benchmark to see what the effect is |
1537 | */ | |
1538 | if (u <= (UV)IV_MAX) { | |
1539 | sv_setiv(sv, (IV)u); | |
1540 | return; | |
1541 | } | |
1542 | sv_setiv(sv, 0); | |
1543 | SvIsUV_on(sv); | |
1544 | SvUV_set(sv, u); | |
1545 | } | |
d33b2eba | 1546 | |
bd81e77b NC |
1547 | /* |
1548 | =for apidoc sv_setuv_mg | |
727879eb | 1549 | |
bd81e77b | 1550 | Like C<sv_setuv>, but also handles 'set' magic. |
9b94d1dd | 1551 | |
bd81e77b NC |
1552 | =cut |
1553 | */ | |
5e2fc214 | 1554 | |
bd81e77b NC |
1555 | void |
1556 | Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u) | |
1557 | { | |
1558 | sv_setiv(sv, 0); | |
1559 | SvIsUV_on(sv); | |
1560 | sv_setuv(sv,u); | |
1561 | SvSETMAGIC(sv); | |
1562 | } | |
5e2fc214 | 1563 | |
954c1994 | 1564 | /* |
bd81e77b | 1565 | =for apidoc sv_setnv |
954c1994 | 1566 | |
bd81e77b NC |
1567 | Copies a double into the given SV, upgrading first if necessary. |
1568 | Does not handle 'set' magic. See also C<sv_setnv_mg>. | |
954c1994 GS |
1569 | |
1570 | =cut | |
1571 | */ | |
1572 | ||
63f97190 | 1573 | void |
bd81e77b | 1574 | Perl_sv_setnv(pTHX_ register SV *sv, NV num) |
79072805 | 1575 | { |
97aff369 | 1576 | dVAR; |
bd81e77b NC |
1577 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
1578 | switch (SvTYPE(sv)) { | |
79072805 | 1579 | case SVt_NULL: |
79072805 | 1580 | case SVt_IV: |
bd81e77b | 1581 | sv_upgrade(sv, SVt_NV); |
79072805 | 1582 | break; |
ed6116ce | 1583 | case SVt_RV: |
79072805 | 1584 | case SVt_PV: |
79072805 | 1585 | case SVt_PVIV: |
bd81e77b | 1586 | sv_upgrade(sv, SVt_PVNV); |
79072805 | 1587 | break; |
bd4b1eb5 | 1588 | |
bd4b1eb5 | 1589 | case SVt_PVGV: |
bd81e77b NC |
1590 | case SVt_PVAV: |
1591 | case SVt_PVHV: | |
79072805 | 1592 | case SVt_PVCV: |
bd81e77b NC |
1593 | case SVt_PVFM: |
1594 | case SVt_PVIO: | |
1595 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), | |
1596 | OP_NAME(PL_op)); | |
2068cd4d | 1597 | } |
bd81e77b NC |
1598 | SvNV_set(sv, num); |
1599 | (void)SvNOK_only(sv); /* validate number */ | |
1600 | SvTAINT(sv); | |
79072805 LW |
1601 | } |
1602 | ||
645c22ef | 1603 | /* |
bd81e77b | 1604 | =for apidoc sv_setnv_mg |
645c22ef | 1605 | |
bd81e77b | 1606 | Like C<sv_setnv>, but also handles 'set' magic. |
645c22ef DM |
1607 | |
1608 | =cut | |
1609 | */ | |
1610 | ||
bd81e77b NC |
1611 | void |
1612 | Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num) | |
79072805 | 1613 | { |
bd81e77b NC |
1614 | sv_setnv(sv,num); |
1615 | SvSETMAGIC(sv); | |
79072805 LW |
1616 | } |
1617 | ||
bd81e77b NC |
1618 | /* Print an "isn't numeric" warning, using a cleaned-up, |
1619 | * printable version of the offending string | |
1620 | */ | |
954c1994 | 1621 | |
bd81e77b NC |
1622 | STATIC void |
1623 | S_not_a_number(pTHX_ SV *sv) | |
79072805 | 1624 | { |
97aff369 | 1625 | dVAR; |
bd81e77b NC |
1626 | SV *dsv; |
1627 | char tmpbuf[64]; | |
1628 | const char *pv; | |
94463019 JH |
1629 | |
1630 | if (DO_UTF8(sv)) { | |
396482e1 | 1631 | dsv = sv_2mortal(newSVpvs("")); |
94463019 JH |
1632 | pv = sv_uni_display(dsv, sv, 10, 0); |
1633 | } else { | |
1634 | char *d = tmpbuf; | |
551405c4 | 1635 | const char * const limit = tmpbuf + sizeof(tmpbuf) - 8; |
94463019 JH |
1636 | /* each *s can expand to 4 chars + "...\0", |
1637 | i.e. need room for 8 chars */ | |
ecdeb87c | 1638 | |
00b6aa41 AL |
1639 | const char *s = SvPVX_const(sv); |
1640 | const char * const end = s + SvCUR(sv); | |
1641 | for ( ; s < end && d < limit; s++ ) { | |
94463019 JH |
1642 | int ch = *s & 0xFF; |
1643 | if (ch & 128 && !isPRINT_LC(ch)) { | |
1644 | *d++ = 'M'; | |
1645 | *d++ = '-'; | |
1646 | ch &= 127; | |
1647 | } | |
1648 | if (ch == '\n') { | |
1649 | *d++ = '\\'; | |
1650 | *d++ = 'n'; | |
1651 | } | |
1652 | else if (ch == '\r') { | |
1653 | *d++ = '\\'; | |
1654 | *d++ = 'r'; | |
1655 | } | |
1656 | else if (ch == '\f') { | |
1657 | *d++ = '\\'; | |
1658 | *d++ = 'f'; | |
1659 | } | |
1660 | else if (ch == '\\') { | |
1661 | *d++ = '\\'; | |
1662 | *d++ = '\\'; | |
1663 | } | |
1664 | else if (ch == '\0') { | |
1665 | *d++ = '\\'; | |
1666 | *d++ = '0'; | |
1667 | } | |
1668 | else if (isPRINT_LC(ch)) | |
1669 | *d++ = ch; | |
1670 | else { | |
1671 | *d++ = '^'; | |
1672 | *d++ = toCTRL(ch); | |
1673 | } | |
1674 | } | |
1675 | if (s < end) { | |
1676 | *d++ = '.'; | |
1677 | *d++ = '.'; | |
1678 | *d++ = '.'; | |
1679 | } | |
1680 | *d = '\0'; | |
1681 | pv = tmpbuf; | |
a0d0e21e | 1682 | } |
a0d0e21e | 1683 | |
533c011a | 1684 | if (PL_op) |
9014280d | 1685 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 JH |
1686 | "Argument \"%s\" isn't numeric in %s", pv, |
1687 | OP_DESC(PL_op)); | |
a0d0e21e | 1688 | else |
9014280d | 1689 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 | 1690 | "Argument \"%s\" isn't numeric", pv); |
a0d0e21e LW |
1691 | } |
1692 | ||
c2988b20 NC |
1693 | /* |
1694 | =for apidoc looks_like_number | |
1695 | ||
645c22ef DM |
1696 | Test if the content of an SV looks like a number (or is a number). |
1697 | C<Inf> and C<Infinity> are treated as numbers (so will not issue a | |
1698 | non-numeric warning), even if your atof() doesn't grok them. | |
c2988b20 NC |
1699 | |
1700 | =cut | |
1701 | */ | |
1702 | ||
1703 | I32 | |
1704 | Perl_looks_like_number(pTHX_ SV *sv) | |
1705 | { | |
a3b680e6 | 1706 | register const char *sbegin; |
c2988b20 NC |
1707 | STRLEN len; |
1708 | ||
1709 | if (SvPOK(sv)) { | |
3f7c398e | 1710 | sbegin = SvPVX_const(sv); |
c2988b20 NC |
1711 | len = SvCUR(sv); |
1712 | } | |
1713 | else if (SvPOKp(sv)) | |
83003860 | 1714 | sbegin = SvPV_const(sv, len); |
c2988b20 | 1715 | else |
e0ab1c0e | 1716 | return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK); |
c2988b20 NC |
1717 | return grok_number(sbegin, len, NULL); |
1718 | } | |
25da4f38 | 1719 | |
180488f8 NC |
1720 | STATIC char * |
1721 | S_glob_2inpuv(pTHX_ GV *gv, STRLEN *len, bool want_number) | |
1722 | { | |
1723 | const U32 wasfake = SvFLAGS(gv) & SVf_FAKE; | |
1724 | SV *const buffer = sv_newmortal(); | |
1725 | ||
1726 | /* FAKE globs can get coerced, so need to turn this off temporarily if it | |
1727 | is on. */ | |
1728 | SvFAKE_off(gv); | |
1729 | gv_efullname3(buffer, gv, "*"); | |
1730 | SvFLAGS(gv) |= wasfake; | |
1731 | ||
1732 | if (want_number) { | |
1733 | /* We know that all GVs stringify to something that is not-a-number, | |
1734 | so no need to test that. */ | |
1735 | if (ckWARN(WARN_NUMERIC)) | |
1736 | not_a_number(buffer); | |
1737 | /* We just want something true to return, so that S_sv_2iuv_common | |
1738 | can tail call us and return true. */ | |
1739 | return (char *) 1; | |
1740 | } else { | |
6a9cfe87 NC |
1741 | assert(SvPOK(buffer)); |
1742 | if (len) { | |
1743 | *len = SvCUR(buffer); | |
1744 | } | |
1745 | return SvPVX(buffer); | |
180488f8 NC |
1746 | } |
1747 | } | |
1748 | ||
25da4f38 IZ |
1749 | /* Actually, ISO C leaves conversion of UV to IV undefined, but |
1750 | until proven guilty, assume that things are not that bad... */ | |
1751 | ||
645c22ef DM |
1752 | /* |
1753 | NV_PRESERVES_UV: | |
1754 | ||
1755 | As 64 bit platforms often have an NV that doesn't preserve all bits of | |
28e5dec8 JH |
1756 | an IV (an assumption perl has been based on to date) it becomes necessary |
1757 | to remove the assumption that the NV always carries enough precision to | |
1758 | recreate the IV whenever needed, and that the NV is the canonical form. | |
1759 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
645c22ef | 1760 | precision as a side effect of conversion (which would lead to insanity |
28e5dec8 JH |
1761 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is |
1762 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
1763 | conversion where precision was lost and IV/UV/NV slots that have a | |
1764 | valid conversion which has lost no precision | |
645c22ef | 1765 | 2) to ensure that if a numeric conversion to one form is requested that |
28e5dec8 JH |
1766 | would lose precision, the precise conversion (or differently |
1767 | imprecise conversion) is also performed and cached, to prevent | |
1768 | requests for different numeric formats on the same SV causing | |
1769 | lossy conversion chains. (lossless conversion chains are perfectly | |
1770 | acceptable (still)) | |
1771 | ||
1772 | ||
1773 | flags are used: | |
1774 | SvIOKp is true if the IV slot contains a valid value | |
1775 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
1776 | SvNOKp is true if the NV slot contains a valid value | |
1777 | SvNOK is true only if the NV value is accurate | |
1778 | ||
1779 | so | |
645c22ef | 1780 | while converting from PV to NV, check to see if converting that NV to an |
28e5dec8 JH |
1781 | IV(or UV) would lose accuracy over a direct conversion from PV to |
1782 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
1783 | SV as IOK NOKp (ie not NOK). | |
1784 | ||
645c22ef | 1785 | While converting from PV to IV, check to see if converting that IV to an |
28e5dec8 JH |
1786 | NV would lose accuracy over a direct conversion from PV to NV. If it |
1787 | would, cache both conversions, flag similarly. | |
1788 | ||
1789 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
1790 | correctly because if IV & NV were set NV *always* overruled. | |
645c22ef DM |
1791 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning |
1792 | changes - now IV and NV together means that the two are interchangeable: | |
28e5dec8 | 1793 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; |
d460ef45 | 1794 | |
645c22ef DM |
1795 | The benefit of this is that operations such as pp_add know that if |
1796 | SvIOK is true for both left and right operands, then integer addition | |
1797 | can be used instead of floating point (for cases where the result won't | |
1798 | overflow). Before, floating point was always used, which could lead to | |
28e5dec8 JH |
1799 | loss of precision compared with integer addition. |
1800 | ||
1801 | * making IV and NV equal status should make maths accurate on 64 bit | |
1802 | platforms | |
1803 | * may speed up maths somewhat if pp_add and friends start to use | |
645c22ef | 1804 | integers when possible instead of fp. (Hopefully the overhead in |
28e5dec8 JH |
1805 | looking for SvIOK and checking for overflow will not outweigh the |
1806 | fp to integer speedup) | |
1807 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
1808 | values, as the change from SvIOK to SvIOKp will cause a call into | |
1809 | sv_2iv each time rather than a macro access direct to the IV slot | |
1810 | * should speed up number->string conversion on integers as IV is | |
645c22ef | 1811 | favoured when IV and NV are equally accurate |
28e5dec8 JH |
1812 | |
1813 | #################################################################### | |
645c22ef DM |
1814 | You had better be using SvIOK_notUV if you want an IV for arithmetic: |
1815 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV. | |
1816 | On the other hand, SvUOK is true iff UV. | |
28e5dec8 JH |
1817 | #################################################################### |
1818 | ||
645c22ef | 1819 | Your mileage will vary depending your CPU's relative fp to integer |
28e5dec8 JH |
1820 | performance ratio. |
1821 | */ | |
1822 | ||
1823 | #ifndef NV_PRESERVES_UV | |
645c22ef DM |
1824 | # define IS_NUMBER_UNDERFLOW_IV 1 |
1825 | # define IS_NUMBER_UNDERFLOW_UV 2 | |
1826 | # define IS_NUMBER_IV_AND_UV 2 | |
1827 | # define IS_NUMBER_OVERFLOW_IV 4 | |
1828 | # define IS_NUMBER_OVERFLOW_UV 5 | |
1829 | ||
1830 | /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */ | |
28e5dec8 JH |
1831 | |
1832 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
1833 | STATIC int | |
645c22ef | 1834 | S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype) |
28e5dec8 | 1835 | { |
97aff369 | 1836 | dVAR; |
3f7c398e | 1837 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX_const(sv), SvIVX(sv), SvNVX(sv), (UV)numtype)); |
28e5dec8 JH |
1838 | if (SvNVX(sv) < (NV)IV_MIN) { |
1839 | (void)SvIOKp_on(sv); | |
1840 | (void)SvNOK_on(sv); | |
45977657 | 1841 | SvIV_set(sv, IV_MIN); |
28e5dec8 JH |
1842 | return IS_NUMBER_UNDERFLOW_IV; |
1843 | } | |
1844 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1845 | (void)SvIOKp_on(sv); | |
1846 | (void)SvNOK_on(sv); | |
1847 | SvIsUV_on(sv); | |
607fa7f2 | 1848 | SvUV_set(sv, UV_MAX); |
28e5dec8 JH |
1849 | return IS_NUMBER_OVERFLOW_UV; |
1850 | } | |
c2988b20 NC |
1851 | (void)SvIOKp_on(sv); |
1852 | (void)SvNOK_on(sv); | |
1853 | /* Can't use strtol etc to convert this string. (See truth table in | |
1854 | sv_2iv */ | |
1855 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
45977657 | 1856 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
1857 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { |
1858 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
1859 | } else { | |
1860 | /* Integer is imprecise. NOK, IOKp */ | |
1861 | } | |
1862 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1863 | } | |
1864 | SvIsUV_on(sv); | |
607fa7f2 | 1865 | SvUV_set(sv, U_V(SvNVX(sv))); |
c2988b20 NC |
1866 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { |
1867 | if (SvUVX(sv) == UV_MAX) { | |
1868 | /* As we know that NVs don't preserve UVs, UV_MAX cannot | |
1869 | possibly be preserved by NV. Hence, it must be overflow. | |
1870 | NOK, IOKp */ | |
1871 | return IS_NUMBER_OVERFLOW_UV; | |
1872 | } | |
1873 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ | |
1874 | } else { | |
1875 | /* Integer is imprecise. NOK, IOKp */ | |
28e5dec8 | 1876 | } |
c2988b20 | 1877 | return IS_NUMBER_OVERFLOW_IV; |
28e5dec8 | 1878 | } |
645c22ef DM |
1879 | #endif /* !NV_PRESERVES_UV*/ |
1880 | ||
af359546 NC |
1881 | STATIC bool |
1882 | S_sv_2iuv_common(pTHX_ SV *sv) { | |
97aff369 | 1883 | dVAR; |
af359546 | 1884 | if (SvNOKp(sv)) { |
28e5dec8 JH |
1885 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1886 | * without also getting a cached IV/UV from it at the same time | |
1887 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
af359546 NC |
1888 | * IV or UV at same time to avoid this. */ |
1889 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
25da4f38 IZ |
1890 | |
1891 | if (SvTYPE(sv) == SVt_NV) | |
1892 | sv_upgrade(sv, SVt_PVNV); | |
1893 | ||
28e5dec8 JH |
1894 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
1895 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
1896 | certainly cast into the IV range at IV_MAX, whereas the correct | |
1897 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
1898 | cases go to UV */ | |
cab190d4 JD |
1899 | #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan) |
1900 | if (Perl_isnan(SvNVX(sv))) { | |
1901 | SvUV_set(sv, 0); | |
1902 | SvIsUV_on(sv); | |
fdbe6d7c | 1903 | return FALSE; |
cab190d4 | 1904 | } |
cab190d4 | 1905 | #endif |
28e5dec8 | 1906 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { |
45977657 | 1907 | SvIV_set(sv, I_V(SvNVX(sv))); |
28e5dec8 JH |
1908 | if (SvNVX(sv) == (NV) SvIVX(sv) |
1909 | #ifndef NV_PRESERVES_UV | |
1910 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1911 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
1912 | /* Don't flag it as "accurately an integer" if the number | |
1913 | came from a (by definition imprecise) NV operation, and | |
1914 | we're outside the range of NV integer precision */ | |
1915 | #endif | |
1916 | ) { | |
1917 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
1918 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 1919 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n", |
28e5dec8 JH |
1920 | PTR2UV(sv), |
1921 | SvNVX(sv), | |
1922 | SvIVX(sv))); | |
1923 | ||
1924 | } else { | |
1925 | /* IV not precise. No need to convert from PV, as NV | |
1926 | conversion would already have cached IV if it detected | |
1927 | that PV->IV would be better than PV->NV->IV | |
1928 | flags already correct - don't set public IOK. */ | |
1929 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 1930 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n", |
28e5dec8 JH |
1931 | PTR2UV(sv), |
1932 | SvNVX(sv), | |
1933 | SvIVX(sv))); | |
1934 | } | |
1935 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
1936 | but the cast (NV)IV_MIN rounds to a the value less (more | |
1937 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
1938 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
1939 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
1940 | (NV)UVX == NVX are both true, but the values differ. :-( | |
1941 | Hopefully for 2s complement IV_MIN is something like | |
1942 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 1943 | } |
25da4f38 | 1944 | else { |
607fa7f2 | 1945 | SvUV_set(sv, U_V(SvNVX(sv))); |
28e5dec8 JH |
1946 | if ( |
1947 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
1948 | #ifndef NV_PRESERVES_UV | |
1949 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
1950 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
1951 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
1952 | /* Don't flag it as "accurately an integer" if the number | |
1953 | came from a (by definition imprecise) NV operation, and | |
1954 | we're outside the range of NV integer precision */ | |
1955 | #endif | |
1956 | ) | |
1957 | SvIOK_on(sv); | |
25da4f38 | 1958 | SvIsUV_on(sv); |
1c846c1f | 1959 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 1960 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 1961 | PTR2UV(sv), |
57def98f JH |
1962 | SvUVX(sv), |
1963 | SvUVX(sv))); | |
25da4f38 | 1964 | } |
748a9306 LW |
1965 | } |
1966 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
c2988b20 | 1967 | UV value; |
504618e9 | 1968 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
af359546 | 1969 | /* We want to avoid a possible problem when we cache an IV/ a UV which |
25da4f38 | 1970 | may be later translated to an NV, and the resulting NV is not |
c2988b20 NC |
1971 | the same as the direct translation of the initial string |
1972 | (eg 123.456 can shortcut to the IV 123 with atol(), but we must | |
1973 | be careful to ensure that the value with the .456 is around if the | |
1974 | NV value is requested in the future). | |
1c846c1f | 1975 | |
af359546 | 1976 | This means that if we cache such an IV/a UV, we need to cache the |
25da4f38 | 1977 | NV as well. Moreover, we trade speed for space, and do not |
28e5dec8 | 1978 | cache the NV if we are sure it's not needed. |
25da4f38 | 1979 | */ |
16b7a9a4 | 1980 | |
c2988b20 NC |
1981 | /* SVt_PVNV is one higher than SVt_PVIV, hence this order */ |
1982 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
1983 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 1984 | /* It's definitely an integer, only upgrade to PVIV */ |
28e5dec8 JH |
1985 | if (SvTYPE(sv) < SVt_PVIV) |
1986 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 1987 | (void)SvIOK_on(sv); |
c2988b20 NC |
1988 | } else if (SvTYPE(sv) < SVt_PVNV) |
1989 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 | 1990 | |
f2524eef | 1991 | /* If NVs preserve UVs then we only use the UV value if we know that |
c2988b20 NC |
1992 | we aren't going to call atof() below. If NVs don't preserve UVs |
1993 | then the value returned may have more precision than atof() will | |
1994 | return, even though value isn't perfectly accurate. */ | |
1995 | if ((numtype & (IS_NUMBER_IN_UV | |
1996 | #ifdef NV_PRESERVES_UV | |
1997 | | IS_NUMBER_NOT_INT | |
1998 | #endif | |
1999 | )) == IS_NUMBER_IN_UV) { | |
2000 | /* This won't turn off the public IOK flag if it was set above */ | |
2001 | (void)SvIOKp_on(sv); | |
2002 | ||
2003 | if (!(numtype & IS_NUMBER_NEG)) { | |
2004 | /* positive */; | |
2005 | if (value <= (UV)IV_MAX) { | |
45977657 | 2006 | SvIV_set(sv, (IV)value); |
c2988b20 | 2007 | } else { |
af359546 | 2008 | /* it didn't overflow, and it was positive. */ |
607fa7f2 | 2009 | SvUV_set(sv, value); |
c2988b20 NC |
2010 | SvIsUV_on(sv); |
2011 | } | |
2012 | } else { | |
2013 | /* 2s complement assumption */ | |
2014 | if (value <= (UV)IV_MIN) { | |
45977657 | 2015 | SvIV_set(sv, -(IV)value); |
c2988b20 NC |
2016 | } else { |
2017 | /* Too negative for an IV. This is a double upgrade, but | |
d1be9408 | 2018 | I'm assuming it will be rare. */ |
c2988b20 NC |
2019 | if (SvTYPE(sv) < SVt_PVNV) |
2020 | sv_upgrade(sv, SVt_PVNV); | |
2021 | SvNOK_on(sv); | |
2022 | SvIOK_off(sv); | |
2023 | SvIOKp_on(sv); | |
9d6ce603 | 2024 | SvNV_set(sv, -(NV)value); |
45977657 | 2025 | SvIV_set(sv, IV_MIN); |
c2988b20 NC |
2026 | } |
2027 | } | |
2028 | } | |
2029 | /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we | |
2030 | will be in the previous block to set the IV slot, and the next | |
2031 | block to set the NV slot. So no else here. */ | |
2032 | ||
2033 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2034 | != IS_NUMBER_IN_UV) { | |
2035 | /* It wasn't an (integer that doesn't overflow the UV). */ | |
3f7c398e | 2036 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 | 2037 | |
c2988b20 NC |
2038 | if (! numtype && ckWARN(WARN_NUMERIC)) |
2039 | not_a_number(sv); | |
28e5dec8 | 2040 | |
65202027 | 2041 | #if defined(USE_LONG_DOUBLE) |
c2988b20 NC |
2042 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
2043 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 2044 | #else |
1779d84d | 2045 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n", |
c2988b20 | 2046 | PTR2UV(sv), SvNVX(sv))); |
65202027 | 2047 | #endif |
28e5dec8 | 2048 | |
28e5dec8 | 2049 | #ifdef NV_PRESERVES_UV |
af359546 NC |
2050 | (void)SvIOKp_on(sv); |
2051 | (void)SvNOK_on(sv); | |
2052 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2053 | SvIV_set(sv, I_V(SvNVX(sv))); | |
2054 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2055 | SvIOK_on(sv); | |
2056 | } else { | |
bb263b4e | 2057 | /*EMPTY*/; /* Integer is imprecise. NOK, IOKp */ |
af359546 NC |
2058 | } |
2059 | /* UV will not work better than IV */ | |
2060 | } else { | |
2061 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2062 | SvIsUV_on(sv); | |
2063 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2064 | SvUV_set(sv, UV_MAX); | |
af359546 NC |
2065 | } else { |
2066 | SvUV_set(sv, U_V(SvNVX(sv))); | |
2067 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs | |
2068 | NV preservse UV so can do correct comparison. */ | |
2069 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2070 | SvIOK_on(sv); | |
af359546 | 2071 | } else { |
bb263b4e | 2072 | /*EMPTY*/; /* Integer is imprecise. NOK, IOKp, is UV */ |
af359546 NC |
2073 | } |
2074 | } | |
4b0c9573 | 2075 | SvIsUV_on(sv); |
af359546 | 2076 | } |
28e5dec8 | 2077 | #else /* NV_PRESERVES_UV */ |
c2988b20 NC |
2078 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2079 | == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) { | |
af359546 | 2080 | /* The IV/UV slot will have been set from value returned by |
c2988b20 NC |
2081 | grok_number above. The NV slot has just been set using |
2082 | Atof. */ | |
560b0c46 | 2083 | SvNOK_on(sv); |
c2988b20 NC |
2084 | assert (SvIOKp(sv)); |
2085 | } else { | |
2086 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2087 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2088 | /* Small enough to preserve all bits. */ | |
2089 | (void)SvIOKp_on(sv); | |
2090 | SvNOK_on(sv); | |
45977657 | 2091 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2092 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) |
2093 | SvIOK_on(sv); | |
2094 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2095 | this NV is in the preserved range, therefore: */ | |
2096 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2097 | < (UV)IV_MAX)) { | |
32fdb065 | 2098 | Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); |
c2988b20 NC |
2099 | } |
2100 | } else { | |
2101 | /* IN_UV NOT_INT | |
2102 | 0 0 already failed to read UV. | |
2103 | 0 1 already failed to read UV. | |
2104 | 1 0 you won't get here in this case. IV/UV | |
2105 | slot set, public IOK, Atof() unneeded. | |
2106 | 1 1 already read UV. | |
2107 | so there's no point in sv_2iuv_non_preserve() attempting | |
2108 | to use atol, strtol, strtoul etc. */ | |
40a17c4c | 2109 | sv_2iuv_non_preserve (sv, numtype); |
c2988b20 NC |
2110 | } |
2111 | } | |
28e5dec8 | 2112 | #endif /* NV_PRESERVES_UV */ |
25da4f38 | 2113 | } |
af359546 NC |
2114 | } |
2115 | else { | |
f7877b28 | 2116 | if (isGV_with_GP(sv)) { |
b21a80ce | 2117 | return (bool)PTR2IV(glob_2inpuv((GV *)sv, NULL, TRUE)); |
180488f8 | 2118 | } |
180488f8 | 2119 | |
af359546 NC |
2120 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
2121 | if (!PL_localizing && ckWARN(WARN_UNINITIALIZED)) | |
2122 | report_uninit(sv); | |
2123 | } | |
25da4f38 IZ |
2124 | if (SvTYPE(sv) < SVt_IV) |
2125 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2126 | sv_upgrade(sv, SVt_IV); | |
af359546 NC |
2127 | /* Return 0 from the caller. */ |
2128 | return TRUE; | |
2129 | } | |
2130 | return FALSE; | |
2131 | } | |
2132 | ||
2133 | /* | |
2134 | =for apidoc sv_2iv_flags | |
2135 | ||
2136 | Return the integer value of an SV, doing any necessary string | |
2137 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. | |
2138 | Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros. | |
2139 | ||
2140 | =cut | |
2141 | */ | |
2142 | ||
2143 | IV | |
2144 | Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags) | |
2145 | { | |
97aff369 | 2146 | dVAR; |
af359546 | 2147 | if (!sv) |
a0d0e21e | 2148 | return 0; |
af359546 NC |
2149 | if (SvGMAGICAL(sv)) { |
2150 | if (flags & SV_GMAGIC) | |
2151 | mg_get(sv); | |
2152 | if (SvIOKp(sv)) | |
2153 | return SvIVX(sv); | |
2154 | if (SvNOKp(sv)) { | |
2155 | return I_V(SvNVX(sv)); | |
2156 | } | |
71c558c3 NC |
2157 | if (SvPOKp(sv) && SvLEN(sv)) { |
2158 | UV value; | |
2159 | const int numtype | |
2160 | = grok_number(SvPVX_const(sv), SvCUR(sv), &value); | |
2161 | ||
2162 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2163 | == IS_NUMBER_IN_UV) { | |
2164 | /* It's definitely an integer */ | |
2165 | if (numtype & IS_NUMBER_NEG) { | |
2166 | if (value < (UV)IV_MIN) | |
2167 | return -(IV)value; | |
2168 | } else { | |
2169 | if (value < (UV)IV_MAX) | |
2170 | return (IV)value; | |
2171 | } | |
2172 | } | |
2173 | if (!numtype) { | |
2174 | if (ckWARN(WARN_NUMERIC)) | |
2175 | not_a_number(sv); | |
2176 | } | |
2177 | return I_V(Atof(SvPVX_const(sv))); | |
2178 | } | |
1c7ff15e NC |
2179 | if (SvROK(sv)) { |
2180 | goto return_rok; | |
af359546 | 2181 | } |
1c7ff15e NC |
2182 | assert(SvTYPE(sv) >= SVt_PVMG); |
2183 | /* This falls through to the report_uninit inside S_sv_2iuv_common. */ | |
4cb1ec55 | 2184 | } else if (SvTHINKFIRST(sv)) { |
af359546 | 2185 | if (SvROK(sv)) { |
1c7ff15e | 2186 | return_rok: |
af359546 NC |
2187 | if (SvAMAGIC(sv)) { |
2188 | SV * const tmpstr=AMG_CALLun(sv,numer); | |
2189 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { | |
2190 | return SvIV(tmpstr); | |
2191 | } | |
2192 | } | |
2193 | return PTR2IV(SvRV(sv)); | |
2194 | } | |
2195 | if (SvIsCOW(sv)) { | |
2196 | sv_force_normal_flags(sv, 0); | |
2197 | } | |
2198 | if (SvREADONLY(sv) && !SvOK(sv)) { | |
2199 | if (ckWARN(WARN_UNINITIALIZED)) | |
2200 | report_uninit(sv); | |
2201 | return 0; | |
2202 | } | |
2203 | } | |
2204 | if (!SvIOKp(sv)) { | |
2205 | if (S_sv_2iuv_common(aTHX_ sv)) | |
2206 | return 0; | |
79072805 | 2207 | } |
1d7c1841 GS |
2208 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
2209 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 2210 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
2211 | } |
2212 | ||
645c22ef | 2213 | /* |
891f9566 | 2214 | =for apidoc sv_2uv_flags |
645c22ef DM |
2215 | |
2216 | Return the unsigned integer value of an SV, doing any necessary string | |
891f9566 YST |
2217 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. |
2218 | Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros. | |
645c22ef DM |
2219 | |
2220 | =cut | |
2221 | */ | |
2222 | ||
ff68c719 | 2223 | UV |
891f9566 | 2224 | Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags) |
ff68c719 | 2225 | { |
97aff369 | 2226 | dVAR; |
ff68c719 | 2227 | if (!sv) |
2228 | return 0; | |
2229 | if (SvGMAGICAL(sv)) { | |
891f9566 YST |
2230 | if (flags & SV_GMAGIC) |
2231 | mg_get(sv); | |
ff68c719 | 2232 | if (SvIOKp(sv)) |
2233 | return SvUVX(sv); | |
2234 | if (SvNOKp(sv)) | |
2235 | return U_V(SvNVX(sv)); | |
71c558c3 NC |
2236 | if (SvPOKp(sv) && SvLEN(sv)) { |
2237 | UV value; | |
2238 | const int numtype | |
2239 | = grok_number(SvPVX_const(sv), SvCUR(sv), &value); | |
2240 | ||
2241 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2242 | == IS_NUMBER_IN_UV) { | |
2243 | /* It's definitely an integer */ | |
2244 | if (!(numtype & IS_NUMBER_NEG)) | |
2245 | return value; | |
2246 | } | |
2247 | if (!numtype) { | |
2248 | if (ckWARN(WARN_NUMERIC)) | |
2249 | not_a_number(sv); | |
2250 | } | |
2251 | return U_V(Atof(SvPVX_const(sv))); | |
2252 | } | |
1c7ff15e NC |
2253 | if (SvROK(sv)) { |
2254 | goto return_rok; | |
3fe9a6f1 | 2255 | } |
1c7ff15e NC |
2256 | assert(SvTYPE(sv) >= SVt_PVMG); |
2257 | /* This falls through to the report_uninit inside S_sv_2iuv_common. */ | |
4cb1ec55 | 2258 | } else if (SvTHINKFIRST(sv)) { |
ff68c719 | 2259 | if (SvROK(sv)) { |
1c7ff15e | 2260 | return_rok: |
deb46114 NC |
2261 | if (SvAMAGIC(sv)) { |
2262 | SV *const tmpstr = AMG_CALLun(sv,numer); | |
2263 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { | |
2264 | return SvUV(tmpstr); | |
2265 | } | |
2266 | } | |
2267 | return PTR2UV(SvRV(sv)); | |
ff68c719 | 2268 | } |
765f542d NC |
2269 | if (SvIsCOW(sv)) { |
2270 | sv_force_normal_flags(sv, 0); | |
8a818333 | 2271 | } |
0336b60e | 2272 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2273 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2274 | report_uninit(sv); |
ff68c719 | 2275 | return 0; |
2276 | } | |
2277 | } | |
af359546 NC |
2278 | if (!SvIOKp(sv)) { |
2279 | if (S_sv_2iuv_common(aTHX_ sv)) | |
2280 | return 0; | |
ff68c719 | 2281 | } |
25da4f38 | 2282 | |
1d7c1841 GS |
2283 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2284 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2285 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2286 | } |
2287 | ||
645c22ef DM |
2288 | /* |
2289 | =for apidoc sv_2nv | |
2290 | ||
2291 | Return the num value of an SV, doing any necessary string or integer | |
2292 | conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> | |
2293 | macros. | |
2294 | ||
2295 | =cut | |
2296 | */ | |
2297 | ||
65202027 | 2298 | NV |
864dbfa3 | 2299 | Perl_sv_2nv(pTHX_ register SV *sv) |
79072805 | 2300 | { |
97aff369 | 2301 | dVAR; |
79072805 LW |
2302 | if (!sv) |
2303 | return 0.0; | |
8990e307 | 2304 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2305 | mg_get(sv); |
2306 | if (SvNOKp(sv)) | |
2307 | return SvNVX(sv); | |
0aa395f8 | 2308 | if ((SvPOKp(sv) && SvLEN(sv)) && !SvIOKp(sv)) { |
041457d9 | 2309 | if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) && |
504618e9 | 2310 | !grok_number(SvPVX_const(sv), SvCUR(sv), NULL)) |
a0d0e21e | 2311 | not_a_number(sv); |
3f7c398e | 2312 | return Atof(SvPVX_const(sv)); |
a0d0e21e | 2313 | } |
25da4f38 | 2314 | if (SvIOKp(sv)) { |
1c846c1f | 2315 | if (SvIsUV(sv)) |
65202027 | 2316 | return (NV)SvUVX(sv); |
25da4f38 | 2317 | else |
65202027 | 2318 | return (NV)SvIVX(sv); |
47a72cb8 NC |
2319 | } |
2320 | if (SvROK(sv)) { | |
2321 | goto return_rok; | |
2322 | } | |
2323 | assert(SvTYPE(sv) >= SVt_PVMG); | |
2324 | /* This falls through to the report_uninit near the end of the | |
2325 | function. */ | |
2326 | } else if (SvTHINKFIRST(sv)) { | |
a0d0e21e | 2327 | if (SvROK(sv)) { |
47a72cb8 | 2328 | return_rok: |
deb46114 NC |
2329 | if (SvAMAGIC(sv)) { |
2330 | SV *const tmpstr = AMG_CALLun(sv,numer); | |
2331 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { | |
2332 | return SvNV(tmpstr); | |
2333 | } | |
2334 | } | |
2335 | return PTR2NV(SvRV(sv)); | |
a0d0e21e | 2336 | } |
765f542d NC |
2337 | if (SvIsCOW(sv)) { |
2338 | sv_force_normal_flags(sv, 0); | |
8a818333 | 2339 | } |
0336b60e | 2340 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2341 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2342 | report_uninit(sv); |
ed6116ce LW |
2343 | return 0.0; |
2344 | } | |
79072805 LW |
2345 | } |
2346 | if (SvTYPE(sv) < SVt_NV) { | |
7e25a7e9 NC |
2347 | /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */ |
2348 | sv_upgrade(sv, SVt_NV); | |
906f284f | 2349 | #ifdef USE_LONG_DOUBLE |
097ee67d | 2350 | DEBUG_c({ |
f93f4e46 | 2351 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2352 | PerlIO_printf(Perl_debug_log, |
2353 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2354 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2355 | RESTORE_NUMERIC_LOCAL(); |
2356 | }); | |
65202027 | 2357 | #else |
572bbb43 | 2358 | DEBUG_c({ |
f93f4e46 | 2359 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2360 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n", |
1d7c1841 | 2361 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2362 | RESTORE_NUMERIC_LOCAL(); |
2363 | }); | |
572bbb43 | 2364 | #endif |
79072805 LW |
2365 | } |
2366 | else if (SvTYPE(sv) < SVt_PVNV) | |
2367 | sv_upgrade(sv, SVt_PVNV); | |
59d8ce62 NC |
2368 | if (SvNOKp(sv)) { |
2369 | return SvNVX(sv); | |
61604483 | 2370 | } |
59d8ce62 | 2371 | if (SvIOKp(sv)) { |
9d6ce603 | 2372 | SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv)); |
28e5dec8 JH |
2373 | #ifdef NV_PRESERVES_UV |
2374 | SvNOK_on(sv); | |
2375 | #else | |
2376 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2377 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
2378 | if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
2379 | : (SvIVX(sv) == I_V(SvNVX(sv)))) | |
2380 | SvNOK_on(sv); | |
2381 | else | |
2382 | SvNOKp_on(sv); | |
2383 | #endif | |
93a17b20 | 2384 | } |
748a9306 | 2385 | else if (SvPOKp(sv) && SvLEN(sv)) { |
c2988b20 | 2386 | UV value; |
3f7c398e | 2387 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
041457d9 | 2388 | if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC)) |
a0d0e21e | 2389 | not_a_number(sv); |
28e5dec8 | 2390 | #ifdef NV_PRESERVES_UV |
c2988b20 NC |
2391 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2392 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2393 | /* It's definitely an integer */ |
9d6ce603 | 2394 | SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value); |
c2988b20 | 2395 | } else |
3f7c398e | 2396 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 JH |
2397 | SvNOK_on(sv); |
2398 | #else | |
3f7c398e | 2399 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 JH |
2400 | /* Only set the public NV OK flag if this NV preserves the value in |
2401 | the PV at least as well as an IV/UV would. | |
2402 | Not sure how to do this 100% reliably. */ | |
2403 | /* if that shift count is out of range then Configure's test is | |
2404 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2405 | UV_BITS */ | |
2406 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
c2988b20 | 2407 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { |
28e5dec8 | 2408 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ |
c2988b20 NC |
2409 | } else if (!(numtype & IS_NUMBER_IN_UV)) { |
2410 | /* Can't use strtol etc to convert this string, so don't try. | |
2411 | sv_2iv and sv_2uv will use the NV to convert, not the PV. */ | |
2412 | SvNOK_on(sv); | |
2413 | } else { | |
2414 | /* value has been set. It may not be precise. */ | |
2415 | if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) { | |
2416 | /* 2s complement assumption for (UV)IV_MIN */ | |
2417 | SvNOK_on(sv); /* Integer is too negative. */ | |
2418 | } else { | |
2419 | SvNOKp_on(sv); | |
2420 | SvIOKp_on(sv); | |
6fa402ec | 2421 | |
c2988b20 | 2422 | if (numtype & IS_NUMBER_NEG) { |
45977657 | 2423 | SvIV_set(sv, -(IV)value); |
c2988b20 | 2424 | } else if (value <= (UV)IV_MAX) { |
45977657 | 2425 | SvIV_set(sv, (IV)value); |
c2988b20 | 2426 | } else { |
607fa7f2 | 2427 | SvUV_set(sv, value); |
c2988b20 NC |
2428 | SvIsUV_on(sv); |
2429 | } | |
2430 | ||
2431 | if (numtype & IS_NUMBER_NOT_INT) { | |
2432 | /* I believe that even if the original PV had decimals, | |
2433 | they are lost beyond the limit of the FP precision. | |
2434 | However, neither is canonical, so both only get p | |
2435 | flags. NWC, 2000/11/25 */ | |
2436 | /* Both already have p flags, so do nothing */ | |
2437 | } else { | |
66a1b24b | 2438 | const NV nv = SvNVX(sv); |
c2988b20 NC |
2439 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { |
2440 | if (SvIVX(sv) == I_V(nv)) { | |
2441 | SvNOK_on(sv); | |
c2988b20 | 2442 | } else { |
c2988b20 NC |
2443 | /* It had no "." so it must be integer. */ |
2444 | } | |
00b6aa41 | 2445 | SvIOK_on(sv); |
c2988b20 NC |
2446 | } else { |
2447 | /* between IV_MAX and NV(UV_MAX). | |
2448 | Could be slightly > UV_MAX */ | |
6fa402ec | 2449 | |
c2988b20 NC |
2450 | if (numtype & IS_NUMBER_NOT_INT) { |
2451 | /* UV and NV both imprecise. */ | |
2452 | } else { | |
66a1b24b | 2453 | const UV nv_as_uv = U_V(nv); |
c2988b20 NC |
2454 | |
2455 | if (value == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
2456 | SvNOK_on(sv); | |
c2988b20 | 2457 | } |
00b6aa41 | 2458 | SvIOK_on(sv); |
c2988b20 NC |
2459 | } |
2460 | } | |
2461 | } | |
2462 | } | |
2463 | } | |
28e5dec8 | 2464 | #endif /* NV_PRESERVES_UV */ |
93a17b20 | 2465 | } |
79072805 | 2466 | else { |
f7877b28 | 2467 | if (isGV_with_GP(sv)) { |
180488f8 NC |
2468 | glob_2inpuv((GV *)sv, NULL, TRUE); |
2469 | return 0.0; | |
2470 | } | |
2471 | ||
041457d9 | 2472 | if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2473 | report_uninit(sv); |
7e25a7e9 NC |
2474 | assert (SvTYPE(sv) >= SVt_NV); |
2475 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2476 | /* XXX Ilya implies that this is a bug in callers that assume this | |
2477 | and ideally should be fixed. */ | |
a0d0e21e | 2478 | return 0.0; |
79072805 | 2479 | } |
572bbb43 | 2480 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2481 | DEBUG_c({ |
f93f4e46 | 2482 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2483 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2484 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2485 | RESTORE_NUMERIC_LOCAL(); |
2486 | }); | |
65202027 | 2487 | #else |
572bbb43 | 2488 | DEBUG_c({ |
f93f4e46 | 2489 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2490 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n", |
1d7c1841 | 2491 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2492 | RESTORE_NUMERIC_LOCAL(); |
2493 | }); | |
572bbb43 | 2494 | #endif |
463ee0b2 | 2495 | return SvNVX(sv); |
79072805 LW |
2496 | } |
2497 | ||
645c22ef DM |
2498 | /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or |
2499 | * UV as a string towards the end of buf, and return pointers to start and | |
2500 | * end of it. | |
2501 | * | |
2502 | * We assume that buf is at least TYPE_CHARS(UV) long. | |
2503 | */ | |
2504 | ||
864dbfa3 | 2505 | static char * |
aec46f14 | 2506 | S_uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob) |
25da4f38 | 2507 | { |
25da4f38 | 2508 | char *ptr = buf + TYPE_CHARS(UV); |
823a54a3 | 2509 | char * const ebuf = ptr; |
25da4f38 | 2510 | int sign; |
25da4f38 IZ |
2511 | |
2512 | if (is_uv) | |
2513 | sign = 0; | |
2514 | else if (iv >= 0) { | |
2515 | uv = iv; | |
2516 | sign = 0; | |
2517 | } else { | |
2518 | uv = -iv; | |
2519 | sign = 1; | |
2520 | } | |
2521 | do { | |
eb160463 | 2522 | *--ptr = '0' + (char)(uv % 10); |
25da4f38 IZ |
2523 | } while (uv /= 10); |
2524 | if (sign) | |
2525 | *--ptr = '-'; | |
2526 | *peob = ebuf; | |
2527 | return ptr; | |
2528 | } | |
2529 | ||
9af30d34 NC |
2530 | /* stringify_regexp(): private routine for use by sv_2pv_flags(): converts |
2531 | * a regexp to its stringified form. | |
2532 | */ | |
2533 | ||
2534 | static char * | |
2535 | S_stringify_regexp(pTHX_ SV *sv, MAGIC *mg, STRLEN *lp) { | |
97aff369 | 2536 | dVAR; |
00b6aa41 | 2537 | const regexp * const re = (regexp *)mg->mg_obj; |
9af30d34 NC |
2538 | |
2539 | if (!mg->mg_ptr) { | |
2540 | const char *fptr = "msix"; | |
2541 | char reflags[6]; | |
2542 | char ch; | |
2543 | int left = 0; | |
2544 | int right = 4; | |
00b6aa41 | 2545 | bool need_newline = 0; |
9af30d34 NC |
2546 | U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12); |
2547 | ||
2548 | while((ch = *fptr++)) { | |
2549 | if(reganch & 1) { | |
2550 | reflags[left++] = ch; | |
2551 | } | |
2552 | else { | |
2553 | reflags[right--] = ch; | |
2554 | } | |
2555 | reganch >>= 1; | |
2556 | } | |
2557 | if(left != 4) { | |
2558 | reflags[left] = '-'; | |
2559 | left = 5; | |
2560 | } | |
2561 | ||
2562 | mg->mg_len = re->prelen + 4 + left; | |
2563 | /* | |
2564 | * If /x was used, we have to worry about a regex ending with a | |
2565 | * comment later being embedded within another regex. If so, we don't | |
2566 | * want this regex's "commentization" to leak out to the right part of | |
2567 | * the enclosing regex, we must cap it with a newline. | |
2568 | * | |
2569 | * So, if /x was used, we scan backwards from the end of the regex. If | |
2570 | * we find a '#' before we find a newline, we need to add a newline | |
2571 | * ourself. If we find a '\n' first (or if we don't find '#' or '\n'), | |
2572 | * we don't need to add anything. -jfriedl | |
2573 | */ | |
2574 | if (PMf_EXTENDED & re->reganch) { | |
2575 | const char *endptr = re->precomp + re->prelen; | |
2576 | while (endptr >= re->precomp) { | |
2577 | const char c = *(endptr--); | |
2578 | if (c == '\n') | |
2579 | break; /* don't need another */ | |
2580 | if (c == '#') { | |
2581 | /* we end while in a comment, so we need a newline */ | |
2582 | mg->mg_len++; /* save space for it */ | |
2583 | need_newline = 1; /* note to add it */ | |
2584 | break; | |
2585 | } | |
2586 | } | |
2587 | } | |
2588 | ||
2589 | Newx(mg->mg_ptr, mg->mg_len + 1 + left, char); | |
2590 | mg->mg_ptr[0] = '('; | |
2591 | mg->mg_ptr[1] = '?'; | |
2592 | Copy(reflags, mg->mg_ptr+2, left, char); | |
2593 | *(mg->mg_ptr+left+2) = ':'; | |
2594 | Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char); | |
2595 | if (need_newline) | |
2596 | mg->mg_ptr[mg->mg_len - 2] = '\n'; | |
2597 | mg->mg_ptr[mg->mg_len - 1] = ')'; | |
2598 | mg->mg_ptr[mg->mg_len] = 0; | |
2599 | } | |
2600 | PL_reginterp_cnt += re->program[0].next_off; | |
2601 | ||
2602 | if (re->reganch & ROPT_UTF8) | |
2603 | SvUTF8_on(sv); | |
2604 | else | |
2605 | SvUTF8_off(sv); | |
2606 | if (lp) | |
2607 | *lp = mg->mg_len; | |
2608 | return mg->mg_ptr; | |
2609 | } | |
2610 | ||
645c22ef DM |
2611 | /* |
2612 | =for apidoc sv_2pv_flags | |
2613 | ||
ff276b08 | 2614 | Returns a pointer to the string value of an SV, and sets *lp to its length. |
645c22ef DM |
2615 | If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string |
2616 | if necessary. | |
2617 | Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg> | |
2618 | usually end up here too. | |
2619 | ||
2620 | =cut | |
2621 | */ | |
2622 | ||
8d6d96c1 HS |
2623 | char * |
2624 | Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags) | |
2625 | { | |
97aff369 | 2626 | dVAR; |
79072805 | 2627 | register char *s; |
79072805 | 2628 | |
463ee0b2 | 2629 | if (!sv) { |
cdb061a3 NC |
2630 | if (lp) |
2631 | *lp = 0; | |
73d840c0 | 2632 | return (char *)""; |
463ee0b2 | 2633 | } |
8990e307 | 2634 | if (SvGMAGICAL(sv)) { |
8d6d96c1 HS |
2635 | if (flags & SV_GMAGIC) |
2636 | mg_get(sv); | |
463ee0b2 | 2637 | if (SvPOKp(sv)) { |
cdb061a3 NC |
2638 | if (lp) |
2639 | *lp = SvCUR(sv); | |
10516c54 NC |
2640 | if (flags & SV_MUTABLE_RETURN) |
2641 | return SvPVX_mutable(sv); | |
4d84ee25 NC |
2642 | if (flags & SV_CONST_RETURN) |
2643 | return (char *)SvPVX_const(sv); | |
463ee0b2 LW |
2644 | return SvPVX(sv); |
2645 | } | |
75dfc8ec NC |
2646 | if (SvIOKp(sv) || SvNOKp(sv)) { |
2647 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ | |
75dfc8ec NC |
2648 | STRLEN len; |
2649 | ||
2650 | if (SvIOKp(sv)) { | |
e8ada2d0 NC |
2651 | len = SvIsUV(sv) ? my_sprintf(tbuf,"%"UVuf, (UV)SvUVX(sv)) |
2652 | : my_sprintf(tbuf,"%"IVdf, (IV)SvIVX(sv)); | |
75dfc8ec | 2653 | } else { |
e8ada2d0 NC |
2654 | Gconvert(SvNVX(sv), NV_DIG, 0, tbuf); |
2655 | len = strlen(tbuf); | |
75dfc8ec | 2656 | } |
b5b886f0 NC |
2657 | assert(!SvROK(sv)); |
2658 | { | |
75dfc8ec NC |
2659 | dVAR; |
2660 | ||
2661 | #ifdef FIXNEGATIVEZERO | |
e8ada2d0 NC |
2662 | if (len == 2 && tbuf[0] == '-' && tbuf[1] == '0') { |
2663 | tbuf[0] = '0'; | |
2664 | tbuf[1] = 0; | |
75dfc8ec NC |
2665 | len = 1; |
2666 | } | |
2667 | #endif | |
2668 | SvUPGRADE(sv, SVt_PV); | |
2669 | if (lp) | |
2670 | *lp = len; | |
2671 | s = SvGROW_mutable(sv, len + 1); | |
2672 | SvCUR_set(sv, len); | |
2673 | SvPOKp_on(sv); | |
e8ada2d0 | 2674 | return memcpy(s, tbuf, len + 1); |
75dfc8ec | 2675 | } |
463ee0b2 | 2676 | } |
1c7ff15e NC |
2677 | if (SvROK(sv)) { |
2678 | goto return_rok; | |
2679 | } | |
2680 | assert(SvTYPE(sv) >= SVt_PVMG); | |
2681 | /* This falls through to the report_uninit near the end of the | |
2682 | function. */ | |
2683 | } else if (SvTHINKFIRST(sv)) { | |
ed6116ce | 2684 | if (SvROK(sv)) { |
1c7ff15e | 2685 | return_rok: |
deb46114 NC |
2686 | if (SvAMAGIC(sv)) { |
2687 | SV *const tmpstr = AMG_CALLun(sv,string); | |
2688 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { | |
2689 | /* Unwrap this: */ | |
2690 | /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); | |
2691 | */ | |
2692 | ||
2693 | char *pv; | |
2694 | if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) { | |
2695 | if (flags & SV_CONST_RETURN) { | |
2696 | pv = (char *) SvPVX_const(tmpstr); | |
2697 | } else { | |
2698 | pv = (flags & SV_MUTABLE_RETURN) | |
2699 | ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr); | |
2700 | } | |
2701 | if (lp) | |
2702 | *lp = SvCUR(tmpstr); | |
50adf7d2 | 2703 | } else { |
deb46114 | 2704 | pv = sv_2pv_flags(tmpstr, lp, flags); |
50adf7d2 | 2705 | } |
deb46114 NC |
2706 | if (SvUTF8(tmpstr)) |
2707 | SvUTF8_on(sv); | |
2708 | else | |
2709 | SvUTF8_off(sv); | |
2710 | return pv; | |
50adf7d2 | 2711 | } |
deb46114 NC |
2712 | } |
2713 | { | |
75dfc8ec | 2714 | SV *tsv; |
f9277f47 | 2715 | MAGIC *mg; |
d8eae41e NC |
2716 | const SV *const referent = (SV*)SvRV(sv); |
2717 | ||
2718 | if (!referent) { | |
396482e1 | 2719 | tsv = sv_2mortal(newSVpvs("NULLREF")); |
042dae7a NC |
2720 | } else if (SvTYPE(referent) == SVt_PVMG |
2721 | && ((SvFLAGS(referent) & | |
2722 | (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) | |
2723 | == (SVs_OBJECT|SVs_SMG)) | |
2724 | && (mg = mg_find(referent, PERL_MAGIC_qr))) { | |
c445ea15 | 2725 | return stringify_regexp(sv, mg, lp); |
d8eae41e NC |
2726 | } else { |
2727 | const char *const typestr = sv_reftype(referent, 0); | |
2728 | ||
2729 | tsv = sv_newmortal(); | |
2730 | if (SvOBJECT(referent)) { | |
2731 | const char *const name = HvNAME_get(SvSTASH(referent)); | |
2732 | Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")", | |
2733 | name ? name : "__ANON__" , typestr, | |
2734 | PTR2UV(referent)); | |
2735 | } | |
2736 | else | |
2737 | Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, | |
2738 | PTR2UV(referent)); | |
c080367d | 2739 | } |
042dae7a NC |
2740 | if (lp) |
2741 | *lp = SvCUR(tsv); | |
2742 | return SvPVX(tsv); | |
463ee0b2 | 2743 | } |
79072805 | 2744 | } |
0336b60e | 2745 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2746 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2747 | report_uninit(sv); |
cdb061a3 NC |
2748 | if (lp) |
2749 | *lp = 0; | |
73d840c0 | 2750 | return (char *)""; |
79072805 | 2751 | } |
79072805 | 2752 | } |
28e5dec8 JH |
2753 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
2754 | /* I'm assuming that if both IV and NV are equally valid then | |
2755 | converting the IV is going to be more efficient */ | |
e1ec3a88 AL |
2756 | const U32 isIOK = SvIOK(sv); |
2757 | const U32 isUIOK = SvIsUV(sv); | |
28e5dec8 JH |
2758 | char buf[TYPE_CHARS(UV)]; |
2759 | char *ebuf, *ptr; | |
2760 | ||
2761 | if (SvTYPE(sv) < SVt_PVIV) | |
2762 | sv_upgrade(sv, SVt_PVIV); | |
4ea1d550 | 2763 | ptr = uiv_2buf(buf, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf); |
5902b6a9 NC |
2764 | /* inlined from sv_setpvn */ |
2765 | SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1)); | |
4d84ee25 | 2766 | Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char); |
28e5dec8 JH |
2767 | SvCUR_set(sv, ebuf - ptr); |
2768 | s = SvEND(sv); | |
2769 | *s = '\0'; | |
2770 | if (isIOK) | |
2771 | SvIOK_on(sv); | |
2772 | else | |
2773 | SvIOKp_on(sv); | |
2774 | if (isUIOK) | |
2775 | SvIsUV_on(sv); | |
2776 | } | |
2777 | else if (SvNOKp(sv)) { | |
c81271c3 | 2778 | const int olderrno = errno; |
79072805 LW |
2779 | if (SvTYPE(sv) < SVt_PVNV) |
2780 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 2781 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
5902b6a9 | 2782 | s = SvGROW_mutable(sv, NV_DIG + 20); |
c81271c3 | 2783 | /* some Xenix systems wipe out errno here */ |
79072805 | 2784 | #ifdef apollo |
463ee0b2 | 2785 | if (SvNVX(sv) == 0.0) |
79072805 LW |
2786 | (void)strcpy(s,"0"); |
2787 | else | |
2788 | #endif /*apollo*/ | |
bbce6d69 | 2789 | { |
2d4389e4 | 2790 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 2791 | } |
79072805 | 2792 | errno = olderrno; |
a0d0e21e LW |
2793 | #ifdef FIXNEGATIVEZERO |
2794 | if (*s == '-' && s[1] == '0' && !s[2]) | |
2795 | strcpy(s,"0"); | |
2796 | #endif | |
79072805 LW |
2797 | while (*s) s++; |
2798 | #ifdef hcx | |
2799 | if (s[-1] == '.') | |
46fc3d4c | 2800 | *--s = '\0'; |
79072805 LW |
2801 | #endif |
2802 | } | |
79072805 | 2803 | else { |
f7877b28 | 2804 | if (isGV_with_GP(sv)) { |
180488f8 NC |
2805 | return glob_2inpuv((GV *)sv, lp, FALSE); |
2806 | } | |
2807 | ||
041457d9 | 2808 | if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2809 | report_uninit(sv); |
cdb061a3 | 2810 | if (lp) |
00b6aa41 | 2811 | *lp = 0; |
25da4f38 IZ |
2812 | if (SvTYPE(sv) < SVt_PV) |
2813 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2814 | sv_upgrade(sv, SVt_PV); | |
73d840c0 | 2815 | return (char *)""; |
79072805 | 2816 | } |
cdb061a3 | 2817 | { |
823a54a3 | 2818 | const STRLEN len = s - SvPVX_const(sv); |
cdb061a3 NC |
2819 | if (lp) |
2820 | *lp = len; | |
2821 | SvCUR_set(sv, len); | |
2822 | } | |
79072805 | 2823 | SvPOK_on(sv); |
1d7c1841 | 2824 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
3f7c398e | 2825 | PTR2UV(sv),SvPVX_const(sv))); |
4d84ee25 NC |
2826 | if (flags & SV_CONST_RETURN) |
2827 | return (char *)SvPVX_const(sv); | |
10516c54 NC |
2828 | if (flags & SV_MUTABLE_RETURN) |
2829 | return SvPVX_mutable(sv); | |
463ee0b2 LW |
2830 | return SvPVX(sv); |
2831 | } | |
2832 | ||
645c22ef | 2833 | /* |
6050d10e JP |
2834 | =for apidoc sv_copypv |
2835 | ||
2836 | Copies a stringified representation of the source SV into the | |
2837 | destination SV. Automatically performs any necessary mg_get and | |
54f0641b | 2838 | coercion of numeric values into strings. Guaranteed to preserve |
6050d10e | 2839 | UTF-8 flag even from overloaded objects. Similar in nature to |
54f0641b NIS |
2840 | sv_2pv[_flags] but operates directly on an SV instead of just the |
2841 | string. Mostly uses sv_2pv_flags to do its work, except when that | |
6050d10e JP |
2842 | would lose the UTF-8'ness of the PV. |
2843 | ||
2844 | =cut | |
2845 | */ | |
2846 | ||
2847 | void | |
2848 | Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv) | |
2849 | { | |
446eaa42 | 2850 | STRLEN len; |
53c1dcc0 | 2851 | const char * const s = SvPV_const(ssv,len); |
cb50f42d | 2852 | sv_setpvn(dsv,s,len); |
446eaa42 | 2853 | if (SvUTF8(ssv)) |
cb50f42d | 2854 | SvUTF8_on(dsv); |
446eaa42 | 2855 | else |
cb50f42d | 2856 | SvUTF8_off(dsv); |
6050d10e JP |
2857 | } |
2858 | ||
2859 | /* | |
645c22ef DM |
2860 | =for apidoc sv_2pvbyte |
2861 | ||
2862 | Return a pointer to the byte-encoded representation of the SV, and set *lp | |
1e54db1a | 2863 | to its length. May cause the SV to be downgraded from UTF-8 as a |
645c22ef DM |
2864 | side-effect. |
2865 | ||
2866 | Usually accessed via the C<SvPVbyte> macro. | |
2867 | ||
2868 | =cut | |
2869 | */ | |
2870 | ||
7340a771 GS |
2871 | char * |
2872 | Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) | |
2873 | { | |
0875d2fe | 2874 | sv_utf8_downgrade(sv,0); |
97972285 | 2875 | return lp ? SvPV(sv,*lp) : SvPV_nolen(sv); |
7340a771 GS |
2876 | } |
2877 | ||
645c22ef | 2878 | /* |
035cbb0e RGS |
2879 | =for apidoc sv_2pvutf8 |
2880 | ||
2881 | Return a pointer to the UTF-8-encoded representation of the SV, and set *lp | |
2882 | to its length. May cause the SV to be upgraded to UTF-8 as a side-effect. | |
2883 | ||
2884 | Usually accessed via the C<SvPVutf8> macro. | |
2885 | ||
2886 | =cut | |
2887 | */ | |
645c22ef | 2888 | |
7340a771 GS |
2889 | char * |
2890 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
2891 | { | |
035cbb0e RGS |
2892 | sv_utf8_upgrade(sv); |
2893 | return lp ? SvPV(sv,*lp) : SvPV_nolen(sv); | |
7340a771 | 2894 | } |
1c846c1f | 2895 | |
7ee2227d | 2896 | |
645c22ef DM |
2897 | /* |
2898 | =for apidoc sv_2bool | |
2899 | ||
2900 | This function is only called on magical items, and is only used by | |
8cf8f3d1 | 2901 | sv_true() or its macro equivalent. |
645c22ef DM |
2902 | |
2903 | =cut | |
2904 | */ | |
2905 | ||
463ee0b2 | 2906 | bool |
864dbfa3 | 2907 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 2908 | { |
97aff369 | 2909 | dVAR; |
5b295bef | 2910 | SvGETMAGIC(sv); |
463ee0b2 | 2911 | |
a0d0e21e LW |
2912 | if (!SvOK(sv)) |
2913 | return 0; | |
2914 | if (SvROK(sv)) { | |
fabdb6c0 AL |
2915 | if (SvAMAGIC(sv)) { |
2916 | SV * const tmpsv = AMG_CALLun(sv,bool_); | |
2917 | if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) | |
2918 | return (bool)SvTRUE(tmpsv); | |
2919 | } | |
2920 | return SvRV(sv) != 0; | |
a0d0e21e | 2921 | } |
463ee0b2 | 2922 | if (SvPOKp(sv)) { |
53c1dcc0 AL |
2923 | register XPV* const Xpvtmp = (XPV*)SvANY(sv); |
2924 | if (Xpvtmp && | |
339049b0 | 2925 | (*sv->sv_u.svu_pv > '0' || |
11343788 | 2926 | Xpvtmp->xpv_cur > 1 || |
339049b0 | 2927 | (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0'))) |
463ee0b2 LW |
2928 | return 1; |
2929 | else | |
2930 | return 0; | |
2931 | } | |
2932 | else { | |
2933 | if (SvIOKp(sv)) | |
2934 | return SvIVX(sv) != 0; | |
2935 | else { | |
2936 | if (SvNOKp(sv)) | |
2937 | return SvNVX(sv) != 0.0; | |
180488f8 | 2938 | else { |
f7877b28 | 2939 | if (isGV_with_GP(sv)) |
180488f8 NC |
2940 | return TRUE; |
2941 | else | |
2942 | return FALSE; | |
2943 | } | |
463ee0b2 LW |
2944 | } |
2945 | } | |
79072805 LW |
2946 | } |
2947 | ||
c461cf8f JH |
2948 | /* |
2949 | =for apidoc sv_utf8_upgrade | |
2950 | ||
78ea37eb | 2951 | Converts the PV of an SV to its UTF-8-encoded form. |
645c22ef | 2952 | Forces the SV to string form if it is not already. |
4411f3b6 NIS |
2953 | Always sets the SvUTF8 flag to avoid future validity checks even |
2954 | if all the bytes have hibit clear. | |
c461cf8f | 2955 | |
13a6c0e0 JH |
2956 | This is not as a general purpose byte encoding to Unicode interface: |
2957 | use the Encode extension for that. | |
2958 | ||
8d6d96c1 HS |
2959 | =for apidoc sv_utf8_upgrade_flags |
2960 | ||
78ea37eb | 2961 | Converts the PV of an SV to its UTF-8-encoded form. |
645c22ef | 2962 | Forces the SV to string form if it is not already. |
8d6d96c1 HS |
2963 | Always sets the SvUTF8 flag to avoid future validity checks even |
2964 | if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set, | |
2965 | will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and | |
2966 | C<sv_utf8_upgrade_nomg> are implemented in terms of this function. | |
2967 | ||
13a6c0e0 JH |
2968 | This is not as a general purpose byte encoding to Unicode interface: |
2969 | use the Encode extension for that. | |
2970 | ||
8d6d96c1 HS |
2971 | =cut |
2972 | */ | |
2973 | ||
2974 | STRLEN | |
2975 | Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags) | |
2976 | { | |
97aff369 | 2977 | dVAR; |
808c356f RGS |
2978 | if (sv == &PL_sv_undef) |
2979 | return 0; | |
e0e62c2a NIS |
2980 | if (!SvPOK(sv)) { |
2981 | STRLEN len = 0; | |
d52b7888 NC |
2982 | if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) { |
2983 | (void) sv_2pv_flags(sv,&len, flags); | |
2984 | if (SvUTF8(sv)) | |
2985 | return len; | |
2986 | } else { | |
2987 | (void) SvPV_force(sv,len); | |
2988 | } | |
e0e62c2a | 2989 | } |
4411f3b6 | 2990 | |
f5cee72b | 2991 | if (SvUTF8(sv)) { |
5fec3b1d | 2992 | return SvCUR(sv); |
f5cee72b | 2993 | } |
5fec3b1d | 2994 | |
765f542d NC |
2995 | if (SvIsCOW(sv)) { |
2996 | sv_force_normal_flags(sv, 0); | |
db42d148 NIS |
2997 | } |
2998 | ||
88632417 | 2999 | if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) |
799ef3cb | 3000 | sv_recode_to_utf8(sv, PL_encoding); |
9f4817db | 3001 | else { /* Assume Latin-1/EBCDIC */ |
c4e7c712 NC |
3002 | /* This function could be much more efficient if we |
3003 | * had a FLAG in SVs to signal if there are any hibit | |
3004 | * chars in the PV. Given that there isn't such a flag | |
3005 | * make the loop as fast as possible. */ | |
00b6aa41 | 3006 | const U8 * const s = (U8 *) SvPVX_const(sv); |
c4420975 | 3007 | const U8 * const e = (U8 *) SvEND(sv); |
93524f2b | 3008 | const U8 *t = s; |
c4e7c712 NC |
3009 | |
3010 | while (t < e) { | |
53c1dcc0 | 3011 | const U8 ch = *t++; |
00b6aa41 AL |
3012 | /* Check for hi bit */ |
3013 | if (!NATIVE_IS_INVARIANT(ch)) { | |
3014 | STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */ | |
3015 | U8 * const recoded = bytes_to_utf8((U8*)s, &len); | |
3016 | ||
3017 | SvPV_free(sv); /* No longer using what was there before. */ | |
3018 | SvPV_set(sv, (char*)recoded); | |
3019 | SvCUR_set(sv, len - 1); | |
3020 | SvLEN_set(sv, len); /* No longer know the real size. */ | |
c4e7c712 | 3021 | break; |
00b6aa41 | 3022 | } |
c4e7c712 NC |
3023 | } |
3024 | /* Mark as UTF-8 even if no hibit - saves scanning loop */ | |
3025 | SvUTF8_on(sv); | |
560a288e | 3026 | } |
4411f3b6 | 3027 | return SvCUR(sv); |
560a288e GS |
3028 | } |
3029 | ||
c461cf8f JH |
3030 | /* |
3031 | =for apidoc sv_utf8_downgrade | |
3032 | ||
78ea37eb TS |
3033 | Attempts to convert the PV of an SV from characters to bytes. |
3034 | If the PV contains a character beyond byte, this conversion will fail; | |
3035 | in this case, either returns false or, if C<fail_ok> is not | |
c461cf8f JH |
3036 | true, croaks. |
3037 | ||
13a6c0e0 JH |
3038 | This is not as a general purpose Unicode to byte encoding interface: |
3039 | use the Encode extension for that. | |
3040 | ||
c461cf8f JH |
3041 | =cut |
3042 | */ | |
3043 | ||
560a288e GS |
3044 | bool |
3045 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
3046 | { | |
97aff369 | 3047 | dVAR; |
78ea37eb | 3048 | if (SvPOKp(sv) && SvUTF8(sv)) { |
fa301091 | 3049 | if (SvCUR(sv)) { |
03cfe0ae | 3050 | U8 *s; |
652088fc | 3051 | STRLEN len; |
fa301091 | 3052 | |
765f542d NC |
3053 | if (SvIsCOW(sv)) { |
3054 | sv_force_normal_flags(sv, 0); | |
3055 | } | |
03cfe0ae NIS |
3056 | s = (U8 *) SvPV(sv, len); |
3057 | if (!utf8_to_bytes(s, &len)) { | |
fa301091 JH |
3058 | if (fail_ok) |
3059 | return FALSE; | |
3060 | else { | |
3061 | if (PL_op) | |
3062 | Perl_croak(aTHX_ "Wide character in %s", | |
53e06cf0 | 3063 | OP_DESC(PL_op)); |
fa301091 JH |
3064 | else |
3065 | Perl_croak(aTHX_ "Wide character"); | |
3066 | } | |
4b3603a4 | 3067 | } |
b162af07 | 3068 | SvCUR_set(sv, len); |
67e989fb | 3069 | } |
560a288e | 3070 | } |
ffebcc3e | 3071 | SvUTF8_off(sv); |
560a288e GS |
3072 | return TRUE; |
3073 | } | |
3074 | ||
c461cf8f JH |
3075 | /* |
3076 | =for apidoc sv_utf8_encode | |
3077 | ||
78ea37eb TS |
3078 | Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8> |
3079 | flag off so that it looks like octets again. | |
c461cf8f JH |
3080 | |
3081 | =cut | |
3082 | */ | |
3083 | ||
560a288e GS |
3084 | void |
3085 | Perl_sv_utf8_encode(pTHX_ register SV *sv) | |
3086 | { | |
4411f3b6 | 3087 | (void) sv_utf8_upgrade(sv); |
4c94c214 NC |
3088 | if (SvIsCOW(sv)) { |
3089 | sv_force_normal_flags(sv, 0); | |
3090 | } | |
3091 | if (SvREADONLY(sv)) { | |
3092 | Perl_croak(aTHX_ PL_no_modify); | |
3093 | } | |
560a288e GS |
3094 | SvUTF8_off(sv); |
3095 | } | |
3096 | ||
4411f3b6 NIS |
3097 | /* |
3098 | =for apidoc sv_utf8_decode | |
3099 | ||
78ea37eb TS |
3100 | If the PV of the SV is an octet sequence in UTF-8 |
3101 | and contains a multiple-byte character, the C<SvUTF8> flag is turned on | |
3102 | so that it looks like a character. If the PV contains only single-byte | |
3103 | characters, the C<SvUTF8> flag stays being off. | |
3104 | Scans PV for validity and returns false if the PV is invalid UTF-8. | |
4411f3b6 NIS |
3105 | |
3106 | =cut | |
3107 | */ | |
3108 | ||
560a288e GS |
3109 | bool |
3110 | Perl_sv_utf8_decode(pTHX_ register SV *sv) | |
3111 | { | |
78ea37eb | 3112 | if (SvPOKp(sv)) { |
93524f2b NC |
3113 | const U8 *c; |
3114 | const U8 *e; | |
9cbac4c7 | 3115 | |
645c22ef DM |
3116 | /* The octets may have got themselves encoded - get them back as |
3117 | * bytes | |
3118 | */ | |
3119 | if (!sv_utf8_downgrade(sv, TRUE)) | |
560a288e GS |
3120 | return FALSE; |
3121 | ||
3122 | /* it is actually just a matter of turning the utf8 flag on, but | |
3123 | * we want to make sure everything inside is valid utf8 first. | |
3124 | */ | |
93524f2b | 3125 | c = (const U8 *) SvPVX_const(sv); |
63cd0674 | 3126 | if (!is_utf8_string(c, SvCUR(sv)+1)) |
67e989fb | 3127 | return FALSE; |
93524f2b | 3128 | e = (const U8 *) SvEND(sv); |
511c2ff0 | 3129 | while (c < e) { |
b64e5050 | 3130 | const U8 ch = *c++; |
c4d5f83a | 3131 | if (!UTF8_IS_INVARIANT(ch)) { |
67e989fb JH |
3132 | SvUTF8_on(sv); |
3133 | break; | |
3134 | } | |
560a288e | 3135 | } |
560a288e GS |
3136 | } |
3137 | return TRUE; | |
3138 | } | |
3139 | ||
954c1994 GS |
3140 | /* |
3141 | =for apidoc sv_setsv | |
3142 | ||
645c22ef DM |
3143 | Copies the contents of the source SV C<ssv> into the destination SV |
3144 | C<dsv>. The source SV may be destroyed if it is mortal, so don't use this | |
3145 | function if the source SV needs to be reused. Does not handle 'set' magic. | |
3146 | Loosely speaking, it performs a copy-by-value, obliterating any previous | |
3147 | content of the destination. | |
3148 | ||
3149 | You probably want to use one of the assortment of wrappers, such as | |
3150 | C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and | |
3151 | C<SvSetMagicSV_nosteal>. | |
3152 | ||
8d6d96c1 HS |
3153 | =for apidoc sv_setsv_flags |
3154 | ||
645c22ef DM |
3155 | Copies the contents of the source SV C<ssv> into the destination SV |
3156 | C<dsv>. The source SV may be destroyed if it is mortal, so don't use this | |
3157 | function if the source SV needs to be reused. Does not handle 'set' magic. | |
3158 | Loosely speaking, it performs a copy-by-value, obliterating any previous | |
3159 | content of the destination. | |
3160 | If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on | |
5fcdf167 NC |
3161 | C<ssv> if appropriate, else not. If the C<flags> parameter has the |
3162 | C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv> | |
3163 | and C<sv_setsv_nomg> are implemented in terms of this function. | |
645c22ef DM |
3164 | |
3165 | You probably want to use one of the assortment of wrappers, such as | |
3166 | C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and | |
3167 | C<SvSetMagicSV_nosteal>. | |
3168 | ||
3169 | This is the primary function for copying scalars, and most other | |
3170 | copy-ish functions and macros use this underneath. | |
8d6d96c1 HS |
3171 | |
3172 | =cut | |
3173 | */ | |
3174 | ||
5d0301b7 | 3175 | static void |
2eb42952 | 3176 | S_glob_assign_glob(pTHX_ SV *dstr, SV *sstr, const int dtype) |
5d0301b7 NC |
3177 | { |
3178 | if (dtype != SVt_PVGV) { | |
3179 | const char * const name = GvNAME(sstr); | |
3180 | const STRLEN len = GvNAMELEN(sstr); | |
3181 | /* don't upgrade SVt_PVLV: it can hold a glob */ | |
f7877b28 NC |
3182 | if (dtype != SVt_PVLV) { |
3183 | if (dtype >= SVt_PV) { | |
3184 | SvPV_free(dstr); | |
3185 | SvPV_set(dstr, 0); | |
3186 | SvLEN_set(dstr, 0); | |
3187 | SvCUR_set(dstr, 0); | |
3188 | } | |
5d0301b7 | 3189 | sv_upgrade(dstr, SVt_PVGV); |
dedf8e73 NC |
3190 | (void)SvOK_off(dstr); |
3191 | SvSCREAM_on(dstr); | |
f7877b28 | 3192 | } |
5d0301b7 NC |
3193 | GvSTASH(dstr) = GvSTASH(sstr); |
3194 | if (GvSTASH(dstr)) | |
3195 | Perl_sv_add_backref(aTHX_ (SV*)GvSTASH(dstr), dstr); | |
ae8cc45f | 3196 | gv_name_set((GV *)dstr, name, len, GV_ADD); |
5d0301b7 NC |
3197 | SvFAKE_on(dstr); /* can coerce to non-glob */ |
3198 | } | |
3199 | ||
3200 | #ifdef GV_UNIQUE_CHECK | |
3201 | if (GvUNIQUE((GV*)dstr)) { | |
3202 | Perl_croak(aTHX_ PL_no_modify); | |
3203 | } | |
3204 | #endif | |
3205 | ||
f7877b28 NC |
3206 | gp_free((GV*)dstr); |
3207 | SvSCREAM_off(dstr); | |
5d0301b7 | 3208 | (void)SvOK_off(dstr); |
f7877b28 | 3209 | SvSCREAM_on(dstr); |
dedf8e73 | 3210 | GvINTRO_off(dstr); /* one-shot flag */ |
5d0301b7 NC |
3211 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
3212 | if (SvTAINTED(sstr)) | |
3213 | SvTAINT(dstr); | |
3214 | if (GvIMPORTED(dstr) != GVf_IMPORTED | |
3215 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3216 | { | |
3217 | GvIMPORTED_on(dstr); | |
3218 | } | |
3219 | GvMULTI_on(dstr); | |
3220 | return; | |
3221 | } | |
3222 | ||
b8473700 | 3223 | static void |
2eb42952 | 3224 | S_glob_assign_ref(pTHX_ SV *dstr, SV *sstr) { |
b8473700 NC |
3225 | SV * const sref = SvREFCNT_inc(SvRV(sstr)); |
3226 | SV *dref = NULL; | |
3227 | const int intro = GvINTRO(dstr); | |
2440974c | 3228 | SV **location; |
3386d083 | 3229 | U8 import_flag = 0; |
27242d61 NC |
3230 | const U32 stype = SvTYPE(sref); |
3231 | ||
b8473700 NC |
3232 | |
3233 | #ifdef GV_UNIQUE_CHECK | |
3234 | if (GvUNIQUE((GV*)dstr)) { | |
3235 | Perl_croak(aTHX_ PL_no_modify); | |
3236 | } | |
3237 | #endif | |
3238 | ||
3239 | if (intro) { | |
3240 | GvINTRO_off(dstr); /* one-shot flag */ | |
3241 | GvLINE(dstr) = CopLINE(PL_curcop); | |
3242 | GvEGV(dstr) = (GV*)dstr; | |
3243 | } | |
3244 | GvMULTI_on(dstr); | |
27242d61 | 3245 | switch (stype) { |
b8473700 | 3246 | case SVt_PVCV: |
27242d61 NC |
3247 | location = (SV **) &GvCV(dstr); |
3248 | import_flag = GVf_IMPORTED_CV; | |
3249 | goto common; | |
3250 | case SVt_PVHV: | |
3251 | location = (SV **) &GvHV(dstr); | |
3252 | import_flag = GVf_IMPORTED_HV; | |
3253 | goto common; | |
3254 | case SVt_PVAV: | |
3255 | location = (SV **) &GvAV(dstr); | |
3256 | import_flag = GVf_IMPORTED_AV; | |
3257 | goto common; | |
3258 | case SVt_PVIO: | |
3259 | location = (SV **) &GvIOp(dstr); | |
3260 | goto common; | |
3261 | case SVt_PVFM: | |
3262 | location = (SV **) &GvFORM(dstr); | |
3263 | default: | |
3264 | location = &GvSV(dstr); | |
3265 | import_flag = GVf_IMPORTED_SV; | |
3266 | common: | |
b8473700 | 3267 | if (intro) { |
27242d61 NC |
3268 | if (stype == SVt_PVCV) { |
3269 | if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) { | |
3270 | SvREFCNT_dec(GvCV(dstr)); | |
3271 | GvCV(dstr) = NULL; | |
3272 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ | |
3273 | PL_sub_generation++; | |
3274 | } | |
b8473700 | 3275 | } |
27242d61 | 3276 | SAVEGENERICSV(*location); |
b8473700 NC |
3277 | } |
3278 | else | |
27242d61 NC |
3279 | dref = *location; |
3280 | if (stype == SVt_PVCV && *location != sref) { | |
3281 | CV* const cv = (CV*)*location; | |
b8473700 NC |
3282 | if (cv) { |
3283 | if (!GvCVGEN((GV*)dstr) && | |
3284 | (CvROOT(cv) || CvXSUB(cv))) | |
3285 | { | |
3286 | /* Redefining a sub - warning is mandatory if | |
3287 | it was a const and its value changed. */ | |
3288 | if (CvCONST(cv) && CvCONST((CV*)sref) | |
3289 | && cv_const_sv(cv) == cv_const_sv((CV*)sref)) { | |
bb263b4e | 3290 | /*EMPTY*/ |
b8473700 NC |
3291 | /* They are 2 constant subroutines generated from |
3292 | the same constant. This probably means that | |
3293 | they are really the "same" proxy subroutine | |
3294 | instantiated in 2 places. Most likely this is | |
3295 | when a constant is exported twice. Don't warn. | |
3296 | */ | |
3297 | } | |
3298 | else if (ckWARN(WARN_REDEFINE) | |
3299 | || (CvCONST(cv) | |
3300 | && (!CvCONST((CV*)sref) | |
3301 | || sv_cmp(cv_const_sv(cv), | |
3302 | cv_const_sv((CV*)sref))))) { | |
3303 | Perl_warner(aTHX_ packWARN(WARN_REDEFINE), | |
3304 | CvCONST(cv) | |
3305 | ? "Constant subroutine %s::%s redefined" | |
3306 | : "Subroutine %s::%s redefined", | |
3307 | HvNAME_get(GvSTASH((GV*)dstr)), | |
3308 | GvENAME((GV*)dstr)); | |
3309 | } | |
3310 | } | |
3311 | if (!intro) | |
3312 | cv_ckproto(cv, (GV*)dstr, | |
bd61b366 | 3313 | SvPOK(sref) ? SvPVX_const(sref) : NULL); |
b8473700 | 3314 | } |
b8473700 NC |
3315 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3316 | GvASSUMECV_on(dstr); | |
3317 | PL_sub_generation++; | |
3318 | } | |
2440974c | 3319 | *location = sref; |
3386d083 NC |
3320 | if (import_flag && !(GvFLAGS(dstr) & import_flag) |
3321 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) { | |
3322 | GvFLAGS(dstr) |= import_flag; | |
b8473700 NC |
3323 | } |
3324 | break; | |
3325 | } | |
b37c2d43 | 3326 | SvREFCNT_dec(dref); |
b8473700 NC |
3327 | if (SvTAINTED(sstr)) |
3328 | SvTAINT(dstr); | |
3329 | return; | |
3330 | } | |
3331 | ||
8d6d96c1 HS |
3332 | void |
3333 | Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags) | |
3334 | { | |
97aff369 | 3335 | dVAR; |
8990e307 LW |
3336 | register U32 sflags; |
3337 | register int dtype; | |
3338 | register int stype; | |
463ee0b2 | 3339 | |
79072805 LW |
3340 | if (sstr == dstr) |
3341 | return; | |
765f542d | 3342 | SV_CHECK_THINKFIRST_COW_DROP(dstr); |
79072805 | 3343 | if (!sstr) |
3280af22 | 3344 | sstr = &PL_sv_undef; |
8990e307 LW |
3345 | stype = SvTYPE(sstr); |
3346 | dtype = SvTYPE(dstr); | |
79072805 | 3347 | |
a0d0e21e | 3348 | SvAMAGIC_off(dstr); |
7a5fa8a2 | 3349 | if ( SvVOK(dstr) ) |
ece467f9 JP |
3350 | { |
3351 | /* need to nuke the magic */ | |
3352 | mg_free(dstr); | |
3353 | SvRMAGICAL_off(dstr); | |
3354 | } | |
9e7bc3e8 | 3355 | |
463ee0b2 | 3356 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3357 | |
8990e307 | 3358 | switch (stype) { |
79072805 | 3359 | case SVt_NULL: |
aece5585 | 3360 | undef_sstr: |
20408e3c GS |
3361 | if (dtype != SVt_PVGV) { |
3362 | (void)SvOK_off(dstr); | |
3363 | return; | |
3364 | } | |
3365 | break; | |
463ee0b2 | 3366 | case SVt_IV: |
aece5585 GA |
3367 | if (SvIOK(sstr)) { |
3368 | switch (dtype) { | |
3369 | case SVt_NULL: | |
8990e307 | 3370 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3371 | break; |
3372 | case SVt_NV: | |
aece5585 GA |
3373 | case SVt_RV: |
3374 | case SVt_PV: | |
a0d0e21e | 3375 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 GA |
3376 | break; |
3377 | } | |
3378 | (void)SvIOK_only(dstr); | |
45977657 | 3379 | SvIV_set(dstr, SvIVX(sstr)); |
25da4f38 IZ |
3380 | if (SvIsUV(sstr)) |
3381 | SvIsUV_on(dstr); | |
37c25af0 NC |
3382 | /* SvTAINTED can only be true if the SV has taint magic, which in |
3383 | turn means that the SV type is PVMG (or greater). This is the | |
3384 | case statement for SVt_IV, so this cannot be true (whatever gcov | |
3385 | may say). */ | |
3386 | assert(!SvTAINTED(sstr)); | |
aece5585 | 3387 | return; |
8990e307 | 3388 | } |
aece5585 GA |
3389 | goto undef_sstr; |
3390 | ||
463ee0b2 | 3391 | case SVt_NV: |
aece5585 GA |
3392 | if (SvNOK(sstr)) { |
3393 | switch (dtype) { | |
3394 | case SVt_NULL: | |
3395 | case SVt_IV: | |
8990e307 | 3396 | sv_upgrade(dstr, SVt_NV); |
aece5585 GA |
3397 | break; |
3398 | case SVt_RV: | |
3399 | case SVt_PV: | |
3400 | case SVt_PVIV: | |
a0d0e21e | 3401 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3402 | break; |
3403 | } | |
9d6ce603 | 3404 | SvNV_set(dstr, SvNVX(sstr)); |
aece5585 | 3405 | (void)SvNOK_only(dstr); |
37c25af0 NC |
3406 | /* SvTAINTED can only be true if the SV has taint magic, which in |
3407 | turn means that the SV type is PVMG (or greater). This is the | |
3408 | case statement for SVt_NV, so this cannot be true (whatever gcov | |
3409 | may say). */ | |
3410 | assert(!SvTAINTED(sstr)); | |
aece5585 | 3411 | return; |
8990e307 | 3412 | } |
aece5585 GA |
3413 | goto undef_sstr; |
3414 | ||
ed6116ce | 3415 | case SVt_RV: |
8990e307 | 3416 | if (dtype < SVt_RV) |
ed6116ce | 3417 | sv_upgrade(dstr, SVt_RV); |
ed6116ce | 3418 | break; |
fc36a67e | 3419 | case SVt_PVFM: |
f8c7b90f | 3420 | #ifdef PERL_OLD_COPY_ON_WRITE |
d89fc664 NC |
3421 | if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) { |
3422 | if (dtype < SVt_PVIV) | |
3423 | sv_upgrade(dstr, SVt_PVIV); | |
3424 | break; | |
3425 | } | |
3426 | /* Fall through */ | |
3427 | #endif | |
3428 | case SVt_PV: | |
8990e307 | 3429 | if (dtype < SVt_PV) |
463ee0b2 | 3430 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3431 | break; |
3432 | case SVt_PVIV: | |
8990e307 | 3433 | if (dtype < SVt_PVIV) |
463ee0b2 | 3434 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3435 | break; |
3436 | case SVt_PVNV: | |
8990e307 | 3437 | if (dtype < SVt_PVNV) |
463ee0b2 | 3438 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3439 | break; |
489f7bfe | 3440 | default: |
a3b680e6 AL |
3441 | { |
3442 | const char * const type = sv_reftype(sstr,0); | |
533c011a | 3443 | if (PL_op) |
a3b680e6 | 3444 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op)); |
4633a7c4 | 3445 | else |
a3b680e6 AL |
3446 | Perl_croak(aTHX_ "Bizarre copy of %s", type); |
3447 | } | |
4633a7c4 LW |
3448 | break; |
3449 | ||
79072805 | 3450 | case SVt_PVGV: |
8990e307 | 3451 | if (dtype <= SVt_PVGV) { |
d4c19fe8 | 3452 | glob_assign_glob(dstr, sstr, dtype); |
b8c701c1 | 3453 | return; |
79072805 | 3454 | } |
5f66b61c | 3455 | /*FALLTHROUGH*/ |
79072805 | 3456 | |
489f7bfe NC |
3457 | case SVt_PVMG: |
3458 | case SVt_PVLV: | |
3459 | case SVt_PVBM: | |
8d6d96c1 | 3460 | if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) { |
973f89ab | 3461 | mg_get(sstr); |
eb160463 | 3462 | if ((int)SvTYPE(sstr) != stype) { |
973f89ab | 3463 | stype = SvTYPE(sstr); |
b8c701c1 | 3464 | if (stype == SVt_PVGV && dtype <= SVt_PVGV) { |
d4c19fe8 | 3465 | glob_assign_glob(dstr, sstr, dtype); |
b8c701c1 NC |
3466 | return; |
3467 | } | |
973f89ab CS |
3468 | } |
3469 | } | |
ded42b9f | 3470 | if (stype == SVt_PVLV) |
862a34c6 | 3471 | SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 3472 | else |
862a34c6 | 3473 | SvUPGRADE(dstr, (U32)stype); |
79072805 LW |
3474 | } |
3475 | ||
ff920335 NC |
3476 | /* dstr may have been upgraded. */ |
3477 | dtype = SvTYPE(dstr); | |
8990e307 LW |
3478 | sflags = SvFLAGS(sstr); |
3479 | ||
3480 | if (sflags & SVf_ROK) { | |
acaa9288 NC |
3481 | if (dtype == SVt_PVGV && |
3482 | SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) { | |
3483 | sstr = SvRV(sstr); | |
3484 | if (sstr == dstr) { | |
3485 | if (GvIMPORTED(dstr) != GVf_IMPORTED | |
3486 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3487 | { | |
3488 | GvIMPORTED_on(dstr); | |
3489 | } | |
3490 | GvMULTI_on(dstr); | |
3491 | return; | |
3492 | } | |
d4c19fe8 | 3493 | glob_assign_glob(dstr, sstr, dtype); |
acaa9288 NC |
3494 | return; |
3495 | } | |
3496 | ||
8990e307 | 3497 | if (dtype >= SVt_PV) { |
b8c701c1 | 3498 | if (dtype == SVt_PVGV) { |
d4c19fe8 | 3499 | glob_assign_ref(dstr, sstr); |
b8c701c1 NC |
3500 | return; |
3501 | } | |
3f7c398e | 3502 | if (SvPVX_const(dstr)) { |
8bd4d4c5 | 3503 | SvPV_free(dstr); |
b162af07 SP |
3504 | SvLEN_set(dstr, 0); |
3505 | SvCUR_set(dstr, 0); | |
a0d0e21e | 3506 | } |
8990e307 | 3507 | } |
a0d0e21e | 3508 | (void)SvOK_off(dstr); |
b162af07 | 3509 | SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr))); |
dfd48732 NC |
3510 | SvFLAGS(dstr) |= sflags & (SVf_ROK|SVf_AMAGIC); |
3511 | assert(!(sflags & SVp_NOK)); | |
3512 | assert(!(sflags & SVp_IOK)); | |
3513 | assert(!(sflags & SVf_NOK)); | |
3514 | assert(!(sflags & SVf_IOK)); | |
ed6116ce | 3515 | } |
c0c44674 NC |
3516 | else if (dtype == SVt_PVGV) { |
3517 | if (!(sflags & SVf_OK)) { | |
3518 | if (ckWARN(WARN_MISC)) | |
3519 | Perl_warner(aTHX_ packWARN(WARN_MISC), | |
3520 | "Undefined value assigned to typeglob"); | |
3521 | } | |
3522 | else { | |
3523 | GV *gv = gv_fetchsv(sstr, GV_ADD, SVt_PVGV); | |
3524 | if (dstr != (SV*)gv) { | |
3525 | if (GvGP(dstr)) | |
3526 | gp_free((GV*)dstr); | |
3527 | GvGP(dstr) = gp_ref(GvGP(gv)); | |
3528 | } | |
3529 | } | |
3530 | } | |
8990e307 | 3531 | else if (sflags & SVp_POK) { |
765f542d | 3532 | bool isSwipe = 0; |
79072805 LW |
3533 | |
3534 | /* | |
3535 | * Check to see if we can just swipe the string. If so, it's a | |
3536 | * possible small lose on short strings, but a big win on long ones. | |
3f7c398e SP |
3537 | * It might even be a win on short strings if SvPVX_const(dstr) |
3538 | * has to be allocated and SvPVX_const(sstr) has to be freed. | |
79072805 LW |
3539 | */ |
3540 | ||
120fac95 NC |
3541 | /* Whichever path we take through the next code, we want this true, |
3542 | and doing it now facilitates the COW check. */ | |
3543 | (void)SvPOK_only(dstr); | |
3544 | ||
765f542d | 3545 | if ( |
b8f9541a NC |
3546 | /* We're not already COW */ |
3547 | ((sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY) | |
f8c7b90f | 3548 | #ifndef PERL_OLD_COPY_ON_WRITE |
b8f9541a NC |
3549 | /* or we are, but dstr isn't a suitable target. */ |
3550 | || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS | |
3551 | #endif | |
3552 | ) | |
765f542d | 3553 | && |
765f542d NC |
3554 | !(isSwipe = |
3555 | (sflags & SVs_TEMP) && /* slated for free anyway? */ | |
3556 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ | |
5fcdf167 NC |
3557 | (!(flags & SV_NOSTEAL)) && |
3558 | /* and we're allowed to steal temps */ | |
765f542d NC |
3559 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
3560 | SvLEN(sstr) && /* and really is a string */ | |
645c22ef | 3561 | /* and won't be needed again, potentially */ |
765f542d | 3562 | !(PL_op && PL_op->op_type == OP_AASSIGN)) |
f8c7b90f | 3563 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 3564 | && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS |
120fac95 | 3565 | && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS |
765f542d NC |
3566 | && SvTYPE(sstr) >= SVt_PVIV) |
3567 | #endif | |
3568 | ) { | |
3569 | /* Failed the swipe test, and it's not a shared hash key either. | |
3570 | Have to copy the string. */ | |
3571 | STRLEN len = SvCUR(sstr); | |
3572 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ | |
3f7c398e | 3573 | Move(SvPVX_const(sstr),SvPVX(dstr),len,char); |
765f542d NC |
3574 | SvCUR_set(dstr, len); |
3575 | *SvEND(dstr) = '\0'; | |
765f542d | 3576 | } else { |
f8c7b90f | 3577 | /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always |
765f542d | 3578 | be true in here. */ |
765f542d NC |
3579 | /* Either it's a shared hash key, or it's suitable for |
3580 | copy-on-write or we can swipe the string. */ | |
46187eeb | 3581 | if (DEBUG_C_TEST) { |
ed252734 | 3582 | PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n"); |
e419cbc5 NC |
3583 | sv_dump(sstr); |
3584 | sv_dump(dstr); | |
46187eeb | 3585 | } |
f8c7b90f | 3586 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d NC |
3587 | if (!isSwipe) { |
3588 | /* I believe I should acquire a global SV mutex if | |
3589 | it's a COW sv (not a shared hash key) to stop | |
3590 | it going un copy-on-write. | |
3591 | If the source SV has gone un copy on write between up there | |
3592 | and down here, then (assert() that) it is of the correct | |
3593 | form to make it copy on write again */ | |
3594 | if ((sflags & (SVf_FAKE | SVf_READONLY)) | |
3595 | != (SVf_FAKE | SVf_READONLY)) { | |
3596 | SvREADONLY_on(sstr); | |
3597 | SvFAKE_on(sstr); | |
3598 | /* Make the source SV into a loop of 1. | |
3599 | (about to become 2) */ | |
a29f6d03 | 3600 | SV_COW_NEXT_SV_SET(sstr, sstr); |
765f542d NC |
3601 | } |
3602 | } | |
3603 | #endif | |
3604 | /* Initial code is common. */ | |
94010e71 NC |
3605 | if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */ |
3606 | SvPV_free(dstr); | |
79072805 | 3607 | } |
765f542d | 3608 | |
765f542d NC |
3609 | if (!isSwipe) { |
3610 | /* making another shared SV. */ | |
3611 | STRLEN cur = SvCUR(sstr); | |
3612 | STRLEN len = SvLEN(sstr); | |
f8c7b90f | 3613 | #ifdef PERL_OLD_COPY_ON_WRITE |
765f542d | 3614 | if (len) { |
b8f9541a | 3615 | assert (SvTYPE(dstr) >= SVt_PVIV); |
765f542d NC |
3616 | /* SvIsCOW_normal */ |
3617 | /* splice us in between source and next-after-source. */ | |
a29f6d03 NC |
3618 | SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr)); |
3619 | SV_COW_NEXT_SV_SET(sstr, dstr); | |
940132f3 | 3620 | SvPV_set(dstr, SvPVX_mutable(sstr)); |
a604c751 NC |
3621 | } else |
3622 | #endif | |
3623 | { | |
765f542d | 3624 | /* SvIsCOW_shared_hash */ |
46187eeb NC |
3625 | DEBUG_C(PerlIO_printf(Perl_debug_log, |
3626 | "Copy on write: Sharing hash\n")); | |
b8f9541a | 3627 | |
bdd68bc3 | 3628 | assert (SvTYPE(dstr) >= SVt_PV); |
765f542d | 3629 | SvPV_set(dstr, |
d1db91c6 | 3630 | HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))))); |
bdd68bc3 | 3631 | } |
87a1ef3d SP |
3632 | SvLEN_set(dstr, len); |
3633 | SvCUR_set(dstr, cur); | |
765f542d NC |
3634 | SvREADONLY_on(dstr); |
3635 | SvFAKE_on(dstr); | |
3636 | /* Relesase a global SV mutex. */ | |
3637 | } | |
3638 | else | |
765f542d | 3639 | { /* Passes the swipe test. */ |
78d1e721 | 3640 | SvPV_set(dstr, SvPVX_mutable(sstr)); |
765f542d NC |
3641 | SvLEN_set(dstr, SvLEN(sstr)); |
3642 | SvCUR_set(dstr, SvCUR(sstr)); | |
3643 | ||
3644 | SvTEMP_off(dstr); | |
3645 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ | |
6136c704 | 3646 | SvPV_set(sstr, NULL); |
765f542d NC |
3647 | SvLEN_set(sstr, 0); |
3648 | SvCUR_set(sstr, 0); | |
3649 | SvTEMP_off(sstr); | |
3650 | } | |
3651 | } | |
8990e307 | 3652 | if (sflags & SVp_NOK) { |
9d6ce603 | 3653 | SvNV_set(dstr, SvNVX(sstr)); |
79072805 | 3654 | } |
8990e307 | 3655 | if (sflags & SVp_IOK) { |
23525414 NC |
3656 | SvRELEASE_IVX(dstr); |
3657 | SvIV_set(dstr, SvIVX(sstr)); | |
3658 | /* Must do this otherwise some other overloaded use of 0x80000000 | |
3659 | gets confused. I guess SVpbm_VALID */ | |
2b1c7e3e | 3660 | if (sflags & SVf_IVisUV) |
25da4f38 | 3661 | SvIsUV_on(dstr); |
79072805 | 3662 | } |
dd2eae66 NC |
3663 | SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8 |
3664 | |SVf_AMAGIC); | |
4f2da183 NC |
3665 | { |
3666 | const MAGIC * const smg = SvVOK(sstr); | |
3667 | if (smg) { | |
3668 | sv_magic(dstr, NULL, PERL_MAGIC_vstring, | |
3669 | smg->mg_ptr, smg->mg_len); | |
3670 | SvRMAGICAL_on(dstr); | |
3671 | } | |
7a5fa8a2 | 3672 | } |
79072805 | 3673 | } |
5d581361 | 3674 | else if (sflags & (SVp_IOK|SVp_NOK)) { |
c2468cc7 | 3675 | (void)SvOK_off(dstr); |
dd2eae66 NC |
3676 | SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK |
3677 | |SVf_AMAGIC); | |
5d581361 NC |
3678 | if (sflags & SVp_IOK) { |
3679 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ | |
3680 | SvIV_set(dstr, SvIVX(sstr)); | |
3681 | } | |
3332b3c1 | 3682 | if (sflags & SVp_NOK) { |
9d6ce603 | 3683 | SvNV_set(dstr, SvNVX(sstr)); |
3332b3c1 JH |
3684 | } |
3685 | } | |
79072805 | 3686 | else { |
f7877b28 | 3687 | if (isGV_with_GP(sstr)) { |
180488f8 NC |
3688 | /* This stringification rule for globs is spread in 3 places. |
3689 | This feels bad. FIXME. */ | |
3690 | const U32 wasfake = sflags & SVf_FAKE; | |
3691 | ||
3692 | /* FAKE globs can get coerced, so need to turn this off | |
3693 | temporarily if it is on. */ | |
3694 | SvFAKE_off(sstr); | |
3695 | gv_efullname3(dstr, (GV *)sstr, "*"); | |
3696 | SvFLAGS(sstr) |= wasfake; | |
dd2eae66 | 3697 | SvFLAGS(dstr) |= sflags & SVf_AMAGIC; |
180488f8 | 3698 | } |
20408e3c GS |
3699 | else |
3700 | (void)SvOK_off(dstr); | |
a0d0e21e | 3701 | } |
27c9684d AP |
3702 | if (SvTAINTED(sstr)) |
3703 | SvTAINT(dstr); | |
79072805 LW |
3704 | } |
3705 | ||
954c1994 GS |
3706 | /* |
3707 | =for apidoc sv_setsv_mg | |
3708 | ||
3709 | Like C<sv_setsv>, but also handles 'set' magic. | |
3710 | ||
3711 | =cut | |
3712 | */ | |
3713 | ||
79072805 | 3714 | void |
864dbfa3 | 3715 | Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
3716 | { |
3717 | sv_setsv(dstr,sstr); | |
3718 | SvSETMAGIC(dstr); | |
3719 | } | |
3720 | ||
f8c7b90f | 3721 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 NC |
3722 | SV * |
3723 | Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr) | |
3724 | { | |
3725 | STRLEN cur = SvCUR(sstr); | |
3726 | STRLEN len = SvLEN(sstr); | |
3727 | register char *new_pv; | |
3728 | ||
3729 | if (DEBUG_C_TEST) { | |
3730 | PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n", | |
3731 | sstr, dstr); | |
3732 | sv_dump(sstr); | |
3733 | if (dstr) | |
3734 | sv_dump(dstr); | |
3735 | } | |
3736 | ||
3737 | if (dstr) { | |
3738 | if (SvTHINKFIRST(dstr)) | |