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