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 NC |
1545 | case SVt_PVGV: |
1546 | case SVt_PVAV: | |
1547 | case SVt_PVHV: | |
1548 | case SVt_PVCV: | |
1549 | case SVt_PVFM: | |
1550 | case SVt_PVIO: | |
1551 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), | |
1552 | OP_DESC(PL_op)); | |
42d0e0b7 | 1553 | default: NOOP; |
bd81e77b NC |
1554 | } |
1555 | (void)SvIOK_only(sv); /* validate number */ | |
1556 | SvIV_set(sv, i); | |
1557 | SvTAINT(sv); | |
1558 | } | |
932e9ff9 | 1559 | |
bd81e77b NC |
1560 | /* |
1561 | =for apidoc sv_setiv_mg | |
d33b2eba | 1562 | |
bd81e77b | 1563 | Like C<sv_setiv>, but also handles 'set' magic. |
1c846c1f | 1564 | |
bd81e77b NC |
1565 | =cut |
1566 | */ | |
d33b2eba | 1567 | |
bd81e77b | 1568 | void |
aad570aa | 1569 | Perl_sv_setiv_mg(pTHX_ register SV *const sv, const IV i) |
bd81e77b | 1570 | { |
7918f24d NC |
1571 | PERL_ARGS_ASSERT_SV_SETIV_MG; |
1572 | ||
bd81e77b NC |
1573 | sv_setiv(sv,i); |
1574 | SvSETMAGIC(sv); | |
1575 | } | |
727879eb | 1576 | |
bd81e77b NC |
1577 | /* |
1578 | =for apidoc sv_setuv | |
d33b2eba | 1579 | |
bd81e77b NC |
1580 | Copies an unsigned integer into the given SV, upgrading first if necessary. |
1581 | Does not handle 'set' magic. See also C<sv_setuv_mg>. | |
9b94d1dd | 1582 | |
bd81e77b NC |
1583 | =cut |
1584 | */ | |
d33b2eba | 1585 | |
bd81e77b | 1586 | void |
aad570aa | 1587 | Perl_sv_setuv(pTHX_ register SV *const sv, const UV u) |
bd81e77b | 1588 | { |
7918f24d NC |
1589 | PERL_ARGS_ASSERT_SV_SETUV; |
1590 | ||
bd81e77b NC |
1591 | /* With these two if statements: |
1592 | u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865 | |
d33b2eba | 1593 | |
bd81e77b NC |
1594 | without |
1595 | u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865 | |
1c846c1f | 1596 | |
bd81e77b NC |
1597 | If you wish to remove them, please benchmark to see what the effect is |
1598 | */ | |
1599 | if (u <= (UV)IV_MAX) { | |
1600 | sv_setiv(sv, (IV)u); | |
1601 | return; | |
1602 | } | |
1603 | sv_setiv(sv, 0); | |
1604 | SvIsUV_on(sv); | |
1605 | SvUV_set(sv, u); | |
1606 | } | |
d33b2eba | 1607 | |
bd81e77b NC |
1608 | /* |
1609 | =for apidoc sv_setuv_mg | |
727879eb | 1610 | |
bd81e77b | 1611 | Like C<sv_setuv>, but also handles 'set' magic. |
9b94d1dd | 1612 | |
bd81e77b NC |
1613 | =cut |
1614 | */ | |
5e2fc214 | 1615 | |
bd81e77b | 1616 | void |
aad570aa | 1617 | Perl_sv_setuv_mg(pTHX_ register SV *const sv, const UV u) |
bd81e77b | 1618 | { |
7918f24d NC |
1619 | PERL_ARGS_ASSERT_SV_SETUV_MG; |
1620 | ||
bd81e77b NC |
1621 | sv_setuv(sv,u); |
1622 | SvSETMAGIC(sv); | |
1623 | } | |
5e2fc214 | 1624 | |
954c1994 | 1625 | /* |
bd81e77b | 1626 | =for apidoc sv_setnv |
954c1994 | 1627 | |
bd81e77b NC |
1628 | Copies a double into the given SV, upgrading first if necessary. |
1629 | Does not handle 'set' magic. See also C<sv_setnv_mg>. | |
954c1994 GS |
1630 | |
1631 | =cut | |
1632 | */ | |
1633 | ||
63f97190 | 1634 | void |
aad570aa | 1635 | Perl_sv_setnv(pTHX_ register SV *const sv, const NV num) |
79072805 | 1636 | { |
97aff369 | 1637 | dVAR; |
7918f24d NC |
1638 | |
1639 | PERL_ARGS_ASSERT_SV_SETNV; | |
1640 | ||
bd81e77b NC |
1641 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
1642 | switch (SvTYPE(sv)) { | |
79072805 | 1643 | case SVt_NULL: |
79072805 | 1644 | case SVt_IV: |
bd81e77b | 1645 | sv_upgrade(sv, SVt_NV); |
79072805 LW |
1646 | break; |
1647 | case SVt_PV: | |
79072805 | 1648 | case SVt_PVIV: |
bd81e77b | 1649 | sv_upgrade(sv, SVt_PVNV); |
79072805 | 1650 | break; |
bd4b1eb5 | 1651 | |
bd4b1eb5 | 1652 | case SVt_PVGV: |
bd81e77b NC |
1653 | case SVt_PVAV: |
1654 | case SVt_PVHV: | |
79072805 | 1655 | case SVt_PVCV: |
bd81e77b NC |
1656 | case SVt_PVFM: |
1657 | case SVt_PVIO: | |
1658 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), | |
1659 | OP_NAME(PL_op)); | |
42d0e0b7 | 1660 | default: NOOP; |
2068cd4d | 1661 | } |
bd81e77b NC |
1662 | SvNV_set(sv, num); |
1663 | (void)SvNOK_only(sv); /* validate number */ | |
1664 | SvTAINT(sv); | |
79072805 LW |
1665 | } |
1666 | ||
645c22ef | 1667 | /* |
bd81e77b | 1668 | =for apidoc sv_setnv_mg |
645c22ef | 1669 | |
bd81e77b | 1670 | Like C<sv_setnv>, but also handles 'set' magic. |
645c22ef DM |
1671 | |
1672 | =cut | |
1673 | */ | |
1674 | ||
bd81e77b | 1675 | void |
aad570aa | 1676 | Perl_sv_setnv_mg(pTHX_ register SV *const sv, const NV num) |
79072805 | 1677 | { |
7918f24d NC |
1678 | PERL_ARGS_ASSERT_SV_SETNV_MG; |
1679 | ||
bd81e77b NC |
1680 | sv_setnv(sv,num); |
1681 | SvSETMAGIC(sv); | |
79072805 LW |
1682 | } |
1683 | ||
bd81e77b NC |
1684 | /* Print an "isn't numeric" warning, using a cleaned-up, |
1685 | * printable version of the offending string | |
1686 | */ | |
954c1994 | 1687 | |
bd81e77b | 1688 | STATIC void |
aad570aa | 1689 | S_not_a_number(pTHX_ SV *const sv) |
79072805 | 1690 | { |
97aff369 | 1691 | dVAR; |
bd81e77b NC |
1692 | SV *dsv; |
1693 | char tmpbuf[64]; | |
1694 | const char *pv; | |
94463019 | 1695 | |
7918f24d NC |
1696 | PERL_ARGS_ASSERT_NOT_A_NUMBER; |
1697 | ||
94463019 | 1698 | if (DO_UTF8(sv)) { |
84bafc02 | 1699 | dsv = newSVpvs_flags("", SVs_TEMP); |
94463019 JH |
1700 | pv = sv_uni_display(dsv, sv, 10, 0); |
1701 | } else { | |
1702 | char *d = tmpbuf; | |
551405c4 | 1703 | const char * const limit = tmpbuf + sizeof(tmpbuf) - 8; |
94463019 JH |
1704 | /* each *s can expand to 4 chars + "...\0", |
1705 | i.e. need room for 8 chars */ | |
ecdeb87c | 1706 | |
00b6aa41 AL |
1707 | const char *s = SvPVX_const(sv); |
1708 | const char * const end = s + SvCUR(sv); | |
1709 | for ( ; s < end && d < limit; s++ ) { | |
94463019 JH |
1710 | int ch = *s & 0xFF; |
1711 | if (ch & 128 && !isPRINT_LC(ch)) { | |
1712 | *d++ = 'M'; | |
1713 | *d++ = '-'; | |
1714 | ch &= 127; | |
1715 | } | |
1716 | if (ch == '\n') { | |
1717 | *d++ = '\\'; | |
1718 | *d++ = 'n'; | |
1719 | } | |
1720 | else if (ch == '\r') { | |
1721 | *d++ = '\\'; | |
1722 | *d++ = 'r'; | |
1723 | } | |
1724 | else if (ch == '\f') { | |
1725 | *d++ = '\\'; | |
1726 | *d++ = 'f'; | |
1727 | } | |
1728 | else if (ch == '\\') { | |
1729 | *d++ = '\\'; | |
1730 | *d++ = '\\'; | |
1731 | } | |
1732 | else if (ch == '\0') { | |
1733 | *d++ = '\\'; | |
1734 | *d++ = '0'; | |
1735 | } | |
1736 | else if (isPRINT_LC(ch)) | |
1737 | *d++ = ch; | |
1738 | else { | |
1739 | *d++ = '^'; | |
1740 | *d++ = toCTRL(ch); | |
1741 | } | |
1742 | } | |
1743 | if (s < end) { | |
1744 | *d++ = '.'; | |
1745 | *d++ = '.'; | |
1746 | *d++ = '.'; | |
1747 | } | |
1748 | *d = '\0'; | |
1749 | pv = tmpbuf; | |
a0d0e21e | 1750 | } |
a0d0e21e | 1751 | |
533c011a | 1752 | if (PL_op) |
9014280d | 1753 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 JH |
1754 | "Argument \"%s\" isn't numeric in %s", pv, |
1755 | OP_DESC(PL_op)); | |
a0d0e21e | 1756 | else |
9014280d | 1757 | Perl_warner(aTHX_ packWARN(WARN_NUMERIC), |
94463019 | 1758 | "Argument \"%s\" isn't numeric", pv); |
a0d0e21e LW |
1759 | } |
1760 | ||
c2988b20 NC |
1761 | /* |
1762 | =for apidoc looks_like_number | |
1763 | ||
645c22ef DM |
1764 | Test if the content of an SV looks like a number (or is a number). |
1765 | C<Inf> and C<Infinity> are treated as numbers (so will not issue a | |
1766 | non-numeric warning), even if your atof() doesn't grok them. | |
c2988b20 NC |
1767 | |
1768 | =cut | |
1769 | */ | |
1770 | ||
1771 | I32 | |
aad570aa | 1772 | Perl_looks_like_number(pTHX_ SV *const sv) |
c2988b20 | 1773 | { |
a3b680e6 | 1774 | register const char *sbegin; |
c2988b20 NC |
1775 | STRLEN len; |
1776 | ||
7918f24d NC |
1777 | PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER; |
1778 | ||
c2988b20 | 1779 | if (SvPOK(sv)) { |
3f7c398e | 1780 | sbegin = SvPVX_const(sv); |
c2988b20 NC |
1781 | len = SvCUR(sv); |
1782 | } | |
1783 | else if (SvPOKp(sv)) | |
83003860 | 1784 | sbegin = SvPV_const(sv, len); |
c2988b20 | 1785 | else |
e0ab1c0e | 1786 | return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK); |
c2988b20 NC |
1787 | return grok_number(sbegin, len, NULL); |
1788 | } | |
25da4f38 | 1789 | |
19f6321d NC |
1790 | STATIC bool |
1791 | S_glob_2number(pTHX_ GV * const gv) | |
180488f8 NC |
1792 | { |
1793 | const U32 wasfake = SvFLAGS(gv) & SVf_FAKE; | |
1794 | SV *const buffer = sv_newmortal(); | |
1795 | ||
7918f24d NC |
1796 | PERL_ARGS_ASSERT_GLOB_2NUMBER; |
1797 | ||
180488f8 NC |
1798 | /* FAKE globs can get coerced, so need to turn this off temporarily if it |
1799 | is on. */ | |
1800 | SvFAKE_off(gv); | |
1801 | gv_efullname3(buffer, gv, "*"); | |
1802 | SvFLAGS(gv) |= wasfake; | |
1803 | ||
675c862f AL |
1804 | /* We know that all GVs stringify to something that is not-a-number, |
1805 | so no need to test that. */ | |
1806 | if (ckWARN(WARN_NUMERIC)) | |
1807 | not_a_number(buffer); | |
1808 | /* We just want something true to return, so that S_sv_2iuv_common | |
1809 | can tail call us and return true. */ | |
19f6321d | 1810 | return TRUE; |
675c862f AL |
1811 | } |
1812 | ||
1813 | STATIC char * | |
19f6321d | 1814 | S_glob_2pv(pTHX_ GV * const gv, STRLEN * const len) |
675c862f AL |
1815 | { |
1816 | const U32 wasfake = SvFLAGS(gv) & SVf_FAKE; | |
1817 | SV *const buffer = sv_newmortal(); | |
1818 | ||
7918f24d NC |
1819 | PERL_ARGS_ASSERT_GLOB_2PV; |
1820 | ||
675c862f AL |
1821 | /* FAKE globs can get coerced, so need to turn this off temporarily if it |
1822 | is on. */ | |
1823 | SvFAKE_off(gv); | |
1824 | gv_efullname3(buffer, gv, "*"); | |
1825 | SvFLAGS(gv) |= wasfake; | |
1826 | ||
1827 | assert(SvPOK(buffer)); | |
a6d61a6c NC |
1828 | if (len) { |
1829 | *len = SvCUR(buffer); | |
1830 | } | |
675c862f | 1831 | return SvPVX(buffer); |
180488f8 NC |
1832 | } |
1833 | ||
25da4f38 IZ |
1834 | /* Actually, ISO C leaves conversion of UV to IV undefined, but |
1835 | until proven guilty, assume that things are not that bad... */ | |
1836 | ||
645c22ef DM |
1837 | /* |
1838 | NV_PRESERVES_UV: | |
1839 | ||
1840 | As 64 bit platforms often have an NV that doesn't preserve all bits of | |
28e5dec8 JH |
1841 | an IV (an assumption perl has been based on to date) it becomes necessary |
1842 | to remove the assumption that the NV always carries enough precision to | |
1843 | recreate the IV whenever needed, and that the NV is the canonical form. | |
1844 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
645c22ef | 1845 | precision as a side effect of conversion (which would lead to insanity |
28e5dec8 JH |
1846 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is |
1847 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
1848 | conversion where precision was lost and IV/UV/NV slots that have a | |
1849 | valid conversion which has lost no precision | |
645c22ef | 1850 | 2) to ensure that if a numeric conversion to one form is requested that |
28e5dec8 JH |
1851 | would lose precision, the precise conversion (or differently |
1852 | imprecise conversion) is also performed and cached, to prevent | |
1853 | requests for different numeric formats on the same SV causing | |
1854 | lossy conversion chains. (lossless conversion chains are perfectly | |
1855 | acceptable (still)) | |
1856 | ||
1857 | ||
1858 | flags are used: | |
1859 | SvIOKp is true if the IV slot contains a valid value | |
1860 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
1861 | SvNOKp is true if the NV slot contains a valid value | |
1862 | SvNOK is true only if the NV value is accurate | |
1863 | ||
1864 | so | |
645c22ef | 1865 | while converting from PV to NV, check to see if converting that NV to an |
28e5dec8 JH |
1866 | IV(or UV) would lose accuracy over a direct conversion from PV to |
1867 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
1868 | SV as IOK NOKp (ie not NOK). | |
1869 | ||
645c22ef | 1870 | While converting from PV to IV, check to see if converting that IV to an |
28e5dec8 JH |
1871 | NV would lose accuracy over a direct conversion from PV to NV. If it |
1872 | would, cache both conversions, flag similarly. | |
1873 | ||
1874 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
1875 | correctly because if IV & NV were set NV *always* overruled. | |
645c22ef DM |
1876 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning |
1877 | changes - now IV and NV together means that the two are interchangeable: | |
28e5dec8 | 1878 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; |
d460ef45 | 1879 | |
645c22ef DM |
1880 | The benefit of this is that operations such as pp_add know that if |
1881 | SvIOK is true for both left and right operands, then integer addition | |
1882 | can be used instead of floating point (for cases where the result won't | |
1883 | overflow). Before, floating point was always used, which could lead to | |
28e5dec8 JH |
1884 | loss of precision compared with integer addition. |
1885 | ||
1886 | * making IV and NV equal status should make maths accurate on 64 bit | |
1887 | platforms | |
1888 | * may speed up maths somewhat if pp_add and friends start to use | |
645c22ef | 1889 | integers when possible instead of fp. (Hopefully the overhead in |
28e5dec8 JH |
1890 | looking for SvIOK and checking for overflow will not outweigh the |
1891 | fp to integer speedup) | |
1892 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
1893 | values, as the change from SvIOK to SvIOKp will cause a call into | |
1894 | sv_2iv each time rather than a macro access direct to the IV slot | |
1895 | * should speed up number->string conversion on integers as IV is | |
645c22ef | 1896 | favoured when IV and NV are equally accurate |
28e5dec8 JH |
1897 | |
1898 | #################################################################### | |
645c22ef DM |
1899 | You had better be using SvIOK_notUV if you want an IV for arithmetic: |
1900 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV. | |
1901 | On the other hand, SvUOK is true iff UV. | |
28e5dec8 JH |
1902 | #################################################################### |
1903 | ||
645c22ef | 1904 | Your mileage will vary depending your CPU's relative fp to integer |
28e5dec8 JH |
1905 | performance ratio. |
1906 | */ | |
1907 | ||
1908 | #ifndef NV_PRESERVES_UV | |
645c22ef DM |
1909 | # define IS_NUMBER_UNDERFLOW_IV 1 |
1910 | # define IS_NUMBER_UNDERFLOW_UV 2 | |
1911 | # define IS_NUMBER_IV_AND_UV 2 | |
1912 | # define IS_NUMBER_OVERFLOW_IV 4 | |
1913 | # define IS_NUMBER_OVERFLOW_UV 5 | |
1914 | ||
1915 | /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */ | |
28e5dec8 JH |
1916 | |
1917 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
1918 | STATIC int | |
5de3775c | 1919 | S_sv_2iuv_non_preserve(pTHX_ register SV *const sv |
47031da6 NC |
1920 | # ifdef DEBUGGING |
1921 | , I32 numtype | |
1922 | # endif | |
1923 | ) | |
28e5dec8 | 1924 | { |
97aff369 | 1925 | dVAR; |
7918f24d NC |
1926 | |
1927 | PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE; | |
1928 | ||
3f7c398e | 1929 | 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 |
1930 | if (SvNVX(sv) < (NV)IV_MIN) { |
1931 | (void)SvIOKp_on(sv); | |
1932 | (void)SvNOK_on(sv); | |
45977657 | 1933 | SvIV_set(sv, IV_MIN); |
28e5dec8 JH |
1934 | return IS_NUMBER_UNDERFLOW_IV; |
1935 | } | |
1936 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1937 | (void)SvIOKp_on(sv); | |
1938 | (void)SvNOK_on(sv); | |
1939 | SvIsUV_on(sv); | |
607fa7f2 | 1940 | SvUV_set(sv, UV_MAX); |
28e5dec8 JH |
1941 | return IS_NUMBER_OVERFLOW_UV; |
1942 | } | |
c2988b20 NC |
1943 | (void)SvIOKp_on(sv); |
1944 | (void)SvNOK_on(sv); | |
1945 | /* Can't use strtol etc to convert this string. (See truth table in | |
1946 | sv_2iv */ | |
1947 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
45977657 | 1948 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
1949 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { |
1950 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
1951 | } else { | |
1952 | /* Integer is imprecise. NOK, IOKp */ | |
1953 | } | |
1954 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1955 | } | |
1956 | SvIsUV_on(sv); | |
607fa7f2 | 1957 | SvUV_set(sv, U_V(SvNVX(sv))); |
c2988b20 NC |
1958 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { |
1959 | if (SvUVX(sv) == UV_MAX) { | |
1960 | /* As we know that NVs don't preserve UVs, UV_MAX cannot | |
1961 | possibly be preserved by NV. Hence, it must be overflow. | |
1962 | NOK, IOKp */ | |
1963 | return IS_NUMBER_OVERFLOW_UV; | |
1964 | } | |
1965 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ | |
1966 | } else { | |
1967 | /* Integer is imprecise. NOK, IOKp */ | |
28e5dec8 | 1968 | } |
c2988b20 | 1969 | return IS_NUMBER_OVERFLOW_IV; |
28e5dec8 | 1970 | } |
645c22ef DM |
1971 | #endif /* !NV_PRESERVES_UV*/ |
1972 | ||
af359546 | 1973 | STATIC bool |
7918f24d NC |
1974 | S_sv_2iuv_common(pTHX_ SV *const sv) |
1975 | { | |
97aff369 | 1976 | dVAR; |
7918f24d NC |
1977 | |
1978 | PERL_ARGS_ASSERT_SV_2IUV_COMMON; | |
1979 | ||
af359546 | 1980 | if (SvNOKp(sv)) { |
28e5dec8 JH |
1981 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1982 | * without also getting a cached IV/UV from it at the same time | |
1983 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
af359546 NC |
1984 | * IV or UV at same time to avoid this. */ |
1985 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
25da4f38 IZ |
1986 | |
1987 | if (SvTYPE(sv) == SVt_NV) | |
1988 | sv_upgrade(sv, SVt_PVNV); | |
1989 | ||
28e5dec8 JH |
1990 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
1991 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
1992 | certainly cast into the IV range at IV_MAX, whereas the correct | |
1993 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
1994 | cases go to UV */ | |
cab190d4 JD |
1995 | #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan) |
1996 | if (Perl_isnan(SvNVX(sv))) { | |
1997 | SvUV_set(sv, 0); | |
1998 | SvIsUV_on(sv); | |
fdbe6d7c | 1999 | return FALSE; |
cab190d4 | 2000 | } |
cab190d4 | 2001 | #endif |
28e5dec8 | 2002 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { |
45977657 | 2003 | SvIV_set(sv, I_V(SvNVX(sv))); |
28e5dec8 JH |
2004 | if (SvNVX(sv) == (NV) SvIVX(sv) |
2005 | #ifndef NV_PRESERVES_UV | |
2006 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2007 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
2008 | /* Don't flag it as "accurately an integer" if the number | |
2009 | came from a (by definition imprecise) NV operation, and | |
2010 | we're outside the range of NV integer precision */ | |
2011 | #endif | |
2012 | ) { | |
a43d94f2 NC |
2013 | if (SvNOK(sv)) |
2014 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
2015 | else { | |
2016 | /* scalar has trailing garbage, eg "42a" */ | |
2017 | } | |
28e5dec8 | 2018 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
7234c960 | 2019 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n", |
28e5dec8 JH |
2020 | PTR2UV(sv), |
2021 | SvNVX(sv), | |
2022 | SvIVX(sv))); | |
2023 | ||
2024 | } else { | |
2025 | /* IV not precise. No need to convert from PV, as NV | |
2026 | conversion would already have cached IV if it detected | |
2027 | that PV->IV would be better than PV->NV->IV | |
2028 | flags already correct - don't set public IOK. */ | |
2029 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
7234c960 | 2030 | "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n", |
28e5dec8 JH |
2031 | PTR2UV(sv), |
2032 | SvNVX(sv), | |
2033 | SvIVX(sv))); | |
2034 | } | |
2035 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2036 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2037 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2038 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2039 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2040 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2041 | Hopefully for 2s complement IV_MIN is something like | |
2042 | 0x8000000000000000 which will be exact. NWC */ | |
d460ef45 | 2043 | } |
25da4f38 | 2044 | else { |
607fa7f2 | 2045 | SvUV_set(sv, U_V(SvNVX(sv))); |
28e5dec8 JH |
2046 | if ( |
2047 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2048 | #ifndef NV_PRESERVES_UV | |
2049 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2050 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2051 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2052 | /* Don't flag it as "accurately an integer" if the number | |
2053 | came from a (by definition imprecise) NV operation, and | |
2054 | we're outside the range of NV integer precision */ | |
2055 | #endif | |
a43d94f2 | 2056 | && SvNOK(sv) |
28e5dec8 JH |
2057 | ) |
2058 | SvIOK_on(sv); | |
25da4f38 | 2059 | SvIsUV_on(sv); |
1c846c1f | 2060 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 2061 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 2062 | PTR2UV(sv), |
57def98f JH |
2063 | SvUVX(sv), |
2064 | SvUVX(sv))); | |
25da4f38 | 2065 | } |
748a9306 LW |
2066 | } |
2067 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
c2988b20 | 2068 | UV value; |
504618e9 | 2069 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
af359546 | 2070 | /* We want to avoid a possible problem when we cache an IV/ a UV which |
25da4f38 | 2071 | may be later translated to an NV, and the resulting NV is not |
c2988b20 NC |
2072 | the same as the direct translation of the initial string |
2073 | (eg 123.456 can shortcut to the IV 123 with atol(), but we must | |
2074 | be careful to ensure that the value with the .456 is around if the | |
2075 | NV value is requested in the future). | |
1c846c1f | 2076 | |
af359546 | 2077 | This means that if we cache such an IV/a UV, we need to cache the |
25da4f38 | 2078 | NV as well. Moreover, we trade speed for space, and do not |
28e5dec8 | 2079 | cache the NV if we are sure it's not needed. |
25da4f38 | 2080 | */ |
16b7a9a4 | 2081 | |
c2988b20 NC |
2082 | /* SVt_PVNV is one higher than SVt_PVIV, hence this order */ |
2083 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2084 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2085 | /* It's definitely an integer, only upgrade to PVIV */ |
28e5dec8 JH |
2086 | if (SvTYPE(sv) < SVt_PVIV) |
2087 | sv_upgrade(sv, SVt_PVIV); | |
f7bbb42a | 2088 | (void)SvIOK_on(sv); |
c2988b20 NC |
2089 | } else if (SvTYPE(sv) < SVt_PVNV) |
2090 | sv_upgrade(sv, SVt_PVNV); | |
28e5dec8 | 2091 | |
f2524eef | 2092 | /* If NVs preserve UVs then we only use the UV value if we know that |
c2988b20 NC |
2093 | we aren't going to call atof() below. If NVs don't preserve UVs |
2094 | then the value returned may have more precision than atof() will | |
2095 | return, even though value isn't perfectly accurate. */ | |
2096 | if ((numtype & (IS_NUMBER_IN_UV | |
2097 | #ifdef NV_PRESERVES_UV | |
2098 | | IS_NUMBER_NOT_INT | |
2099 | #endif | |
2100 | )) == IS_NUMBER_IN_UV) { | |
2101 | /* This won't turn off the public IOK flag if it was set above */ | |
2102 | (void)SvIOKp_on(sv); | |
2103 | ||
2104 | if (!(numtype & IS_NUMBER_NEG)) { | |
2105 | /* positive */; | |
2106 | if (value <= (UV)IV_MAX) { | |
45977657 | 2107 | SvIV_set(sv, (IV)value); |
c2988b20 | 2108 | } else { |
af359546 | 2109 | /* it didn't overflow, and it was positive. */ |
607fa7f2 | 2110 | SvUV_set(sv, value); |
c2988b20 NC |
2111 | SvIsUV_on(sv); |
2112 | } | |
2113 | } else { | |
2114 | /* 2s complement assumption */ | |
2115 | if (value <= (UV)IV_MIN) { | |
45977657 | 2116 | SvIV_set(sv, -(IV)value); |
c2988b20 NC |
2117 | } else { |
2118 | /* Too negative for an IV. This is a double upgrade, but | |
d1be9408 | 2119 | I'm assuming it will be rare. */ |
c2988b20 NC |
2120 | if (SvTYPE(sv) < SVt_PVNV) |
2121 | sv_upgrade(sv, SVt_PVNV); | |
2122 | SvNOK_on(sv); | |
2123 | SvIOK_off(sv); | |
2124 | SvIOKp_on(sv); | |
9d6ce603 | 2125 | SvNV_set(sv, -(NV)value); |
45977657 | 2126 | SvIV_set(sv, IV_MIN); |
c2988b20 NC |
2127 | } |
2128 | } | |
2129 | } | |
2130 | /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we | |
2131 | will be in the previous block to set the IV slot, and the next | |
2132 | block to set the NV slot. So no else here. */ | |
2133 | ||
2134 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2135 | != IS_NUMBER_IN_UV) { | |
2136 | /* It wasn't an (integer that doesn't overflow the UV). */ | |
3f7c398e | 2137 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 | 2138 | |
c2988b20 NC |
2139 | if (! numtype && ckWARN(WARN_NUMERIC)) |
2140 | not_a_number(sv); | |
28e5dec8 | 2141 | |
65202027 | 2142 | #if defined(USE_LONG_DOUBLE) |
c2988b20 NC |
2143 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
2144 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 2145 | #else |
1779d84d | 2146 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n", |
c2988b20 | 2147 | PTR2UV(sv), SvNVX(sv))); |
65202027 | 2148 | #endif |
28e5dec8 | 2149 | |
28e5dec8 | 2150 | #ifdef NV_PRESERVES_UV |
af359546 NC |
2151 | (void)SvIOKp_on(sv); |
2152 | (void)SvNOK_on(sv); | |
2153 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2154 | SvIV_set(sv, I_V(SvNVX(sv))); | |
2155 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2156 | SvIOK_on(sv); | |
2157 | } else { | |
6f207bd3 | 2158 | NOOP; /* Integer is imprecise. NOK, IOKp */ |
af359546 NC |
2159 | } |
2160 | /* UV will not work better than IV */ | |
2161 | } else { | |
2162 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2163 | SvIsUV_on(sv); | |
2164 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2165 | SvUV_set(sv, UV_MAX); | |
af359546 NC |
2166 | } else { |
2167 | SvUV_set(sv, U_V(SvNVX(sv))); | |
2168 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs | |
2169 | NV preservse UV so can do correct comparison. */ | |
2170 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2171 | SvIOK_on(sv); | |
af359546 | 2172 | } else { |
6f207bd3 | 2173 | NOOP; /* Integer is imprecise. NOK, IOKp, is UV */ |
af359546 NC |
2174 | } |
2175 | } | |
4b0c9573 | 2176 | SvIsUV_on(sv); |
af359546 | 2177 | } |
28e5dec8 | 2178 | #else /* NV_PRESERVES_UV */ |
c2988b20 NC |
2179 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2180 | == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) { | |
af359546 | 2181 | /* The IV/UV slot will have been set from value returned by |
c2988b20 NC |
2182 | grok_number above. The NV slot has just been set using |
2183 | Atof. */ | |
560b0c46 | 2184 | SvNOK_on(sv); |
c2988b20 NC |
2185 | assert (SvIOKp(sv)); |
2186 | } else { | |
2187 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2188 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2189 | /* Small enough to preserve all bits. */ | |
2190 | (void)SvIOKp_on(sv); | |
2191 | SvNOK_on(sv); | |
45977657 | 2192 | SvIV_set(sv, I_V(SvNVX(sv))); |
c2988b20 NC |
2193 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) |
2194 | SvIOK_on(sv); | |
2195 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2196 | this NV is in the preserved range, therefore: */ | |
2197 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2198 | < (UV)IV_MAX)) { | |
32fdb065 | 2199 | 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 |
2200 | } |
2201 | } else { | |
2202 | /* IN_UV NOT_INT | |
2203 | 0 0 already failed to read UV. | |
2204 | 0 1 already failed to read UV. | |
2205 | 1 0 you won't get here in this case. IV/UV | |
2206 | slot set, public IOK, Atof() unneeded. | |
2207 | 1 1 already read UV. | |
2208 | so there's no point in sv_2iuv_non_preserve() attempting | |
2209 | to use atol, strtol, strtoul etc. */ | |
47031da6 | 2210 | # ifdef DEBUGGING |
40a17c4c | 2211 | sv_2iuv_non_preserve (sv, numtype); |
47031da6 NC |
2212 | # else |
2213 | sv_2iuv_non_preserve (sv); | |
2214 | # endif | |
c2988b20 NC |
2215 | } |
2216 | } | |
28e5dec8 | 2217 | #endif /* NV_PRESERVES_UV */ |
a43d94f2 NC |
2218 | /* It might be more code efficient to go through the entire logic above |
2219 | and conditionally set with SvIOKp_on() rather than SvIOK(), but it | |
2220 | gets complex and potentially buggy, so more programmer efficient | |
2221 | to do it this way, by turning off the public flags: */ | |
2222 | if (!numtype) | |
2223 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK); | |
25da4f38 | 2224 | } |
af359546 NC |
2225 | } |
2226 | else { | |
675c862f | 2227 | if (isGV_with_GP(sv)) |
a0933d07 | 2228 | return glob_2number((GV *)sv); |
180488f8 | 2229 | |
af359546 NC |
2230 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
2231 | if (!PL_localizing && ckWARN(WARN_UNINITIALIZED)) | |
2232 | report_uninit(sv); | |
2233 | } | |
25da4f38 IZ |
2234 | if (SvTYPE(sv) < SVt_IV) |
2235 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2236 | sv_upgrade(sv, SVt_IV); | |
af359546 NC |
2237 | /* Return 0 from the caller. */ |
2238 | return TRUE; | |
2239 | } | |
2240 | return FALSE; | |
2241 | } | |
2242 | ||
2243 | /* | |
2244 | =for apidoc sv_2iv_flags | |
2245 | ||
2246 | Return the integer value of an SV, doing any necessary string | |
2247 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. | |
2248 | Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros. | |
2249 | ||
2250 | =cut | |
2251 | */ | |
2252 | ||
2253 | IV | |
5de3775c | 2254 | Perl_sv_2iv_flags(pTHX_ register SV *const sv, const I32 flags) |
af359546 | 2255 | { |
97aff369 | 2256 | dVAR; |
af359546 | 2257 | if (!sv) |
a0d0e21e | 2258 | return 0; |
cecf5685 NC |
2259 | if (SvGMAGICAL(sv) || (SvTYPE(sv) == SVt_PVGV && SvVALID(sv))) { |
2260 | /* FBMs use the same flag bit as SVf_IVisUV, so must let them | |
50caf62e NC |
2261 | cache IVs just in case. In practice it seems that they never |
2262 | actually anywhere accessible by user Perl code, let alone get used | |
2263 | in anything other than a string context. */ | |
af359546 NC |
2264 | if (flags & SV_GMAGIC) |
2265 | mg_get(sv); | |
2266 | if (SvIOKp(sv)) | |
2267 | return SvIVX(sv); | |
2268 | if (SvNOKp(sv)) { | |
2269 | return I_V(SvNVX(sv)); | |
2270 | } | |
71c558c3 NC |
2271 | if (SvPOKp(sv) && SvLEN(sv)) { |
2272 | UV value; | |
2273 | const int numtype | |
2274 | = grok_number(SvPVX_const(sv), SvCUR(sv), &value); | |
2275 | ||
2276 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2277 | == IS_NUMBER_IN_UV) { | |
2278 | /* It's definitely an integer */ | |
2279 | if (numtype & IS_NUMBER_NEG) { | |
2280 | if (value < (UV)IV_MIN) | |
2281 | return -(IV)value; | |
2282 | } else { | |
2283 | if (value < (UV)IV_MAX) | |
2284 | return (IV)value; | |
2285 | } | |
2286 | } | |
2287 | if (!numtype) { | |
2288 | if (ckWARN(WARN_NUMERIC)) | |
2289 | not_a_number(sv); | |
2290 | } | |
2291 | return I_V(Atof(SvPVX_const(sv))); | |
2292 | } | |
1c7ff15e NC |
2293 | if (SvROK(sv)) { |
2294 | goto return_rok; | |
af359546 | 2295 | } |
1c7ff15e NC |
2296 | assert(SvTYPE(sv) >= SVt_PVMG); |
2297 | /* This falls through to the report_uninit inside S_sv_2iuv_common. */ | |
4cb1ec55 | 2298 | } else if (SvTHINKFIRST(sv)) { |
af359546 | 2299 | if (SvROK(sv)) { |
1c7ff15e | 2300 | return_rok: |
af359546 NC |
2301 | if (SvAMAGIC(sv)) { |
2302 | SV * const tmpstr=AMG_CALLun(sv,numer); | |
2303 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { | |
2304 | return SvIV(tmpstr); | |
2305 | } | |
2306 | } | |
2307 | return PTR2IV(SvRV(sv)); | |
2308 | } | |
2309 | if (SvIsCOW(sv)) { | |
2310 | sv_force_normal_flags(sv, 0); | |
2311 | } | |
2312 | if (SvREADONLY(sv) && !SvOK(sv)) { | |
2313 | if (ckWARN(WARN_UNINITIALIZED)) | |
2314 | report_uninit(sv); | |
2315 | return 0; | |
2316 | } | |
2317 | } | |
2318 | if (!SvIOKp(sv)) { | |
2319 | if (S_sv_2iuv_common(aTHX_ sv)) | |
2320 | return 0; | |
79072805 | 2321 | } |
1d7c1841 GS |
2322 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
2323 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 2324 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
2325 | } |
2326 | ||
645c22ef | 2327 | /* |
891f9566 | 2328 | =for apidoc sv_2uv_flags |
645c22ef DM |
2329 | |
2330 | Return the unsigned integer value of an SV, doing any necessary string | |
891f9566 YST |
2331 | conversion. If flags includes SV_GMAGIC, does an mg_get() first. |
2332 | Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros. | |
645c22ef DM |
2333 | |
2334 | =cut | |
2335 | */ | |
2336 | ||
ff68c719 | 2337 | UV |
5de3775c | 2338 | Perl_sv_2uv_flags(pTHX_ register SV *const sv, const I32 flags) |
ff68c719 | 2339 | { |
97aff369 | 2340 | dVAR; |
ff68c719 | 2341 | if (!sv) |
2342 | return 0; | |
cecf5685 NC |
2343 | if (SvGMAGICAL(sv) || (SvTYPE(sv) == SVt_PVGV && SvVALID(sv))) { |
2344 | /* FBMs use the same flag bit as SVf_IVisUV, so must let them | |
50caf62e | 2345 | cache IVs just in case. */ |
891f9566 YST |
2346 | if (flags & SV_GMAGIC) |
2347 | mg_get(sv); | |
ff68c719 | 2348 | if (SvIOKp(sv)) |
2349 | return SvUVX(sv); | |
2350 | if (SvNOKp(sv)) | |
2351 | return U_V(SvNVX(sv)); | |
71c558c3 NC |
2352 | if (SvPOKp(sv) && SvLEN(sv)) { |
2353 | UV value; | |
2354 | const int numtype | |
2355 | = grok_number(SvPVX_const(sv), SvCUR(sv), &value); | |
2356 | ||
2357 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) | |
2358 | == IS_NUMBER_IN_UV) { | |
2359 | /* It's definitely an integer */ | |
2360 | if (!(numtype & IS_NUMBER_NEG)) | |
2361 | return value; | |
2362 | } | |
2363 | if (!numtype) { | |
2364 | if (ckWARN(WARN_NUMERIC)) | |
2365 | not_a_number(sv); | |
2366 | } | |
2367 | return U_V(Atof(SvPVX_const(sv))); | |
2368 | } | |
1c7ff15e NC |
2369 | if (SvROK(sv)) { |
2370 | goto return_rok; | |
3fe9a6f1 | 2371 | } |
1c7ff15e NC |
2372 | assert(SvTYPE(sv) >= SVt_PVMG); |
2373 | /* This falls through to the report_uninit inside S_sv_2iuv_common. */ | |
4cb1ec55 | 2374 | } else if (SvTHINKFIRST(sv)) { |
ff68c719 | 2375 | if (SvROK(sv)) { |
1c7ff15e | 2376 | return_rok: |
deb46114 NC |
2377 | if (SvAMAGIC(sv)) { |
2378 | SV *const tmpstr = AMG_CALLun(sv,numer); | |
2379 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { | |
2380 | return SvUV(tmpstr); | |
2381 | } | |
2382 | } | |
2383 | return PTR2UV(SvRV(sv)); | |
ff68c719 | 2384 | } |
765f542d NC |
2385 | if (SvIsCOW(sv)) { |
2386 | sv_force_normal_flags(sv, 0); | |
8a818333 | 2387 | } |
0336b60e | 2388 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2389 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2390 | report_uninit(sv); |
ff68c719 | 2391 | return 0; |
2392 | } | |
2393 | } | |
af359546 NC |
2394 | if (!SvIOKp(sv)) { |
2395 | if (S_sv_2iuv_common(aTHX_ sv)) | |
2396 | return 0; | |
ff68c719 | 2397 | } |
25da4f38 | 2398 | |
1d7c1841 GS |
2399 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2400 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2401 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2402 | } |
2403 | ||
645c22ef DM |
2404 | /* |
2405 | =for apidoc sv_2nv | |
2406 | ||
2407 | Return the num value of an SV, doing any necessary string or integer | |
2408 | conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> | |
2409 | macros. | |
2410 | ||
2411 | =cut | |
2412 | */ | |
2413 | ||
65202027 | 2414 | NV |
5de3775c | 2415 | Perl_sv_2nv(pTHX_ register SV *const sv) |
79072805 | 2416 | { |
97aff369 | 2417 | dVAR; |
79072805 LW |
2418 | if (!sv) |
2419 | return 0.0; | |
cecf5685 NC |
2420 | if (SvGMAGICAL(sv) || (SvTYPE(sv) == SVt_PVGV && SvVALID(sv))) { |
2421 | /* FBMs use the same flag bit as SVf_IVisUV, so must let them | |
50caf62e | 2422 | cache IVs just in case. */ |
463ee0b2 LW |
2423 | mg_get(sv); |
2424 | if (SvNOKp(sv)) | |
2425 | return SvNVX(sv); | |
0aa395f8 | 2426 | if ((SvPOKp(sv) && SvLEN(sv)) && !SvIOKp(sv)) { |
041457d9 | 2427 | if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) && |
504618e9 | 2428 | !grok_number(SvPVX_const(sv), SvCUR(sv), NULL)) |
a0d0e21e | 2429 | not_a_number(sv); |
3f7c398e | 2430 | return Atof(SvPVX_const(sv)); |
a0d0e21e | 2431 | } |
25da4f38 | 2432 | if (SvIOKp(sv)) { |
1c846c1f | 2433 | if (SvIsUV(sv)) |
65202027 | 2434 | return (NV)SvUVX(sv); |
25da4f38 | 2435 | else |
65202027 | 2436 | return (NV)SvIVX(sv); |
47a72cb8 NC |
2437 | } |
2438 | if (SvROK(sv)) { | |
2439 | goto return_rok; | |
2440 | } | |
2441 | assert(SvTYPE(sv) >= SVt_PVMG); | |
2442 | /* This falls through to the report_uninit near the end of the | |
2443 | function. */ | |
2444 | } else if (SvTHINKFIRST(sv)) { | |
a0d0e21e | 2445 | if (SvROK(sv)) { |
47a72cb8 | 2446 | return_rok: |
deb46114 NC |
2447 | if (SvAMAGIC(sv)) { |
2448 | SV *const tmpstr = AMG_CALLun(sv,numer); | |
2449 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { | |
2450 | return SvNV(tmpstr); | |
2451 | } | |
2452 | } | |
2453 | return PTR2NV(SvRV(sv)); | |
a0d0e21e | 2454 | } |
765f542d NC |
2455 | if (SvIsCOW(sv)) { |
2456 | sv_force_normal_flags(sv, 0); | |
8a818333 | 2457 | } |
0336b60e | 2458 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2459 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2460 | report_uninit(sv); |
ed6116ce LW |
2461 | return 0.0; |
2462 | } | |
79072805 LW |
2463 | } |
2464 | if (SvTYPE(sv) < SVt_NV) { | |
7e25a7e9 NC |
2465 | /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */ |
2466 | sv_upgrade(sv, SVt_NV); | |
906f284f | 2467 | #ifdef USE_LONG_DOUBLE |
097ee67d | 2468 | DEBUG_c({ |
f93f4e46 | 2469 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2470 | PerlIO_printf(Perl_debug_log, |
2471 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2472 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2473 | RESTORE_NUMERIC_LOCAL(); |
2474 | }); | |
65202027 | 2475 | #else |
572bbb43 | 2476 | DEBUG_c({ |
f93f4e46 | 2477 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2478 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n", |
1d7c1841 | 2479 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2480 | RESTORE_NUMERIC_LOCAL(); |
2481 | }); | |
572bbb43 | 2482 | #endif |
79072805 LW |
2483 | } |
2484 | else if (SvTYPE(sv) < SVt_PVNV) | |
2485 | sv_upgrade(sv, SVt_PVNV); | |
59d8ce62 NC |
2486 | if (SvNOKp(sv)) { |
2487 | return SvNVX(sv); | |
61604483 | 2488 | } |
59d8ce62 | 2489 | if (SvIOKp(sv)) { |
9d6ce603 | 2490 | SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv)); |
28e5dec8 | 2491 | #ifdef NV_PRESERVES_UV |
a43d94f2 NC |
2492 | if (SvIOK(sv)) |
2493 | SvNOK_on(sv); | |
2494 | else | |
2495 | SvNOKp_on(sv); | |
28e5dec8 JH |
2496 | #else |
2497 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2498 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
a43d94f2 NC |
2499 | if (SvIOK(sv) && |
2500 | SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
28e5dec8 JH |
2501 | : (SvIVX(sv) == I_V(SvNVX(sv)))) |
2502 | SvNOK_on(sv); | |
2503 | else | |
2504 | SvNOKp_on(sv); | |
2505 | #endif | |
93a17b20 | 2506 | } |
748a9306 | 2507 | else if (SvPOKp(sv) && SvLEN(sv)) { |
c2988b20 | 2508 | UV value; |
3f7c398e | 2509 | const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value); |
041457d9 | 2510 | if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC)) |
a0d0e21e | 2511 | not_a_number(sv); |
28e5dec8 | 2512 | #ifdef NV_PRESERVES_UV |
c2988b20 NC |
2513 | if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) |
2514 | == IS_NUMBER_IN_UV) { | |
5e045b90 | 2515 | /* It's definitely an integer */ |
9d6ce603 | 2516 | SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value); |
c2988b20 | 2517 | } else |
3f7c398e | 2518 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
a43d94f2 NC |
2519 | if (numtype) |
2520 | SvNOK_on(sv); | |
2521 | else | |
2522 | SvNOKp_on(sv); | |
28e5dec8 | 2523 | #else |
3f7c398e | 2524 | SvNV_set(sv, Atof(SvPVX_const(sv))); |
28e5dec8 JH |
2525 | /* Only set the public NV OK flag if this NV preserves the value in |
2526 | the PV at least as well as an IV/UV would. | |
2527 | Not sure how to do this 100% reliably. */ | |
2528 | /* if that shift count is out of range then Configure's test is | |
2529 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2530 | UV_BITS */ | |
2531 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
c2988b20 | 2532 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { |
28e5dec8 | 2533 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ |
c2988b20 NC |
2534 | } else if (!(numtype & IS_NUMBER_IN_UV)) { |
2535 | /* Can't use strtol etc to convert this string, so don't try. | |
2536 | sv_2iv and sv_2uv will use the NV to convert, not the PV. */ | |
2537 | SvNOK_on(sv); | |
2538 | } else { | |
2539 | /* value has been set. It may not be precise. */ | |
2540 | if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) { | |
2541 | /* 2s complement assumption for (UV)IV_MIN */ | |
2542 | SvNOK_on(sv); /* Integer is too negative. */ | |
2543 | } else { | |
2544 | SvNOKp_on(sv); | |
2545 | SvIOKp_on(sv); | |
6fa402ec | 2546 | |
c2988b20 | 2547 | if (numtype & IS_NUMBER_NEG) { |
45977657 | 2548 | SvIV_set(sv, -(IV)value); |
c2988b20 | 2549 | } else if (value <= (UV)IV_MAX) { |
45977657 | 2550 | SvIV_set(sv, (IV)value); |
c2988b20 | 2551 | } else { |
607fa7f2 | 2552 | SvUV_set(sv, value); |
c2988b20 NC |
2553 | SvIsUV_on(sv); |
2554 | } | |
2555 | ||
2556 | if (numtype & IS_NUMBER_NOT_INT) { | |
2557 | /* I believe that even if the original PV had decimals, | |
2558 | they are lost beyond the limit of the FP precision. | |
2559 | However, neither is canonical, so both only get p | |
2560 | flags. NWC, 2000/11/25 */ | |
2561 | /* Both already have p flags, so do nothing */ | |
2562 | } else { | |
66a1b24b | 2563 | const NV nv = SvNVX(sv); |
c2988b20 NC |
2564 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { |
2565 | if (SvIVX(sv) == I_V(nv)) { | |
2566 | SvNOK_on(sv); | |
c2988b20 | 2567 | } else { |
c2988b20 NC |
2568 | /* It had no "." so it must be integer. */ |
2569 | } | |
00b6aa41 | 2570 | SvIOK_on(sv); |
c2988b20 NC |
2571 | } else { |
2572 | /* between IV_MAX and NV(UV_MAX). | |
2573 | Could be slightly > UV_MAX */ | |
6fa402ec | 2574 | |
c2988b20 NC |
2575 | if (numtype & IS_NUMBER_NOT_INT) { |
2576 | /* UV and NV both imprecise. */ | |
2577 | } else { | |
66a1b24b | 2578 | const UV nv_as_uv = U_V(nv); |
c2988b20 NC |
2579 | |
2580 | if (value == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
2581 | SvNOK_on(sv); | |
c2988b20 | 2582 | } |
00b6aa41 | 2583 | SvIOK_on(sv); |
c2988b20 NC |
2584 | } |
2585 | } | |
2586 | } | |
2587 | } | |
2588 | } | |
a43d94f2 NC |
2589 | /* It might be more code efficient to go through the entire logic above |
2590 | and conditionally set with SvNOKp_on() rather than SvNOK(), but it | |
2591 | gets complex and potentially buggy, so more programmer efficient | |
2592 | to do it this way, by turning off the public flags: */ | |
2593 | if (!numtype) | |
2594 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK); | |
28e5dec8 | 2595 | #endif /* NV_PRESERVES_UV */ |
93a17b20 | 2596 | } |
79072805 | 2597 | else { |
f7877b28 | 2598 | if (isGV_with_GP(sv)) { |
19f6321d | 2599 | glob_2number((GV *)sv); |
180488f8 NC |
2600 | return 0.0; |
2601 | } | |
2602 | ||
041457d9 | 2603 | if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 2604 | report_uninit(sv); |
7e25a7e9 NC |
2605 | assert (SvTYPE(sv) >= SVt_NV); |
2606 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2607 | /* XXX Ilya implies that this is a bug in callers that assume this | |
2608 | and ideally should be fixed. */ | |
a0d0e21e | 2609 | return 0.0; |
79072805 | 2610 | } |
572bbb43 | 2611 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2612 | DEBUG_c({ |
f93f4e46 | 2613 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2614 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2615 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2616 | RESTORE_NUMERIC_LOCAL(); |
2617 | }); | |
65202027 | 2618 | #else |
572bbb43 | 2619 | DEBUG_c({ |
f93f4e46 | 2620 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1779d84d | 2621 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n", |
1d7c1841 | 2622 | PTR2UV(sv), SvNVX(sv)); |
097ee67d JH |
2623 | RESTORE_NUMERIC_LOCAL(); |
2624 | }); | |
572bbb43 | 2625 | #endif |
463ee0b2 | 2626 | return SvNVX(sv); |
79072805 LW |
2627 | } |
2628 | ||
800401ee JH |
2629 | /* |
2630 | =for apidoc sv_2num | |
2631 | ||
2632 | Return an SV with the numeric value of the source SV, doing any necessary | |
a196a5fa JH |
2633 | reference or overload conversion. You must use the C<SvNUM(sv)> macro to |
2634 | access this function. | |
800401ee JH |
2635 | |
2636 | =cut | |
2637 | */ | |
2638 | ||
2639 | SV * | |
5de3775c | 2640 | Perl_sv_2num(pTHX_ register SV *const sv) |
800401ee | 2641 | { |
7918f24d NC |
2642 | PERL_ARGS_ASSERT_SV_2NUM; |
2643 | ||
b9ee0594 RGS |
2644 | if (!SvROK(sv)) |
2645 | return sv; | |
800401ee JH |
2646 | if (SvAMAGIC(sv)) { |
2647 | SV * const tmpsv = AMG_CALLun(sv,numer); | |
2648 | if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) | |
2649 | return sv_2num(tmpsv); | |
2650 | } | |
2651 | return sv_2mortal(newSVuv(PTR2UV(SvRV(sv)))); | |
2652 | } | |
2653 | ||
645c22ef DM |
2654 | /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or |
2655 | * UV as a string towards the end of buf, and return pointers to start and | |
2656 | * end of it. | |
2657 | * | |
2658 | * We assume that buf is at least TYPE_CHARS(UV) long. | |
2659 | */ | |
2660 | ||
864dbfa3 | 2661 | static char * |
5de3775c | 2662 | S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob) |
25da4f38 | 2663 | { |
25da4f38 | 2664 | char *ptr = buf + TYPE_CHARS(UV); |
823a54a3 | 2665 | char * const ebuf = ptr; |
25da4f38 | 2666 | int sign; |
25da4f38 | 2667 | |
7918f24d NC |
2668 | PERL_ARGS_ASSERT_UIV_2BUF; |
2669 | ||
25da4f38 IZ |
2670 | if (is_uv) |
2671 | sign = 0; | |
2672 | else if (iv >= 0) { | |
2673 | uv = iv; | |
2674 | sign = 0; | |
2675 | } else { | |
2676 | uv = -iv; | |
2677 | sign = 1; | |
2678 | } | |
2679 | do { | |
eb160463 | 2680 | *--ptr = '0' + (char)(uv % 10); |
25da4f38 IZ |
2681 | } while (uv /= 10); |
2682 | if (sign) | |
2683 | *--ptr = '-'; | |
2684 | *peob = ebuf; | |
2685 | return ptr; | |
2686 | } | |
2687 | ||
645c22ef DM |
2688 | /* |
2689 | =for apidoc sv_2pv_flags | |
2690 | ||
ff276b08 | 2691 | Returns a pointer to the string value of an SV, and sets *lp to its length. |
645c22ef DM |
2692 | If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string |
2693 | if necessary. | |
2694 | Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg> | |
2695 | usually end up here too. | |
2696 | ||
2697 | =cut | |
2698 | */ | |
2699 | ||
8d6d96c1 | 2700 | char * |
5de3775c | 2701 | Perl_sv_2pv_flags(pTHX_ register SV *const sv, STRLEN *const lp, const I32 flags) |
8d6d96c1 | 2702 | { |
97aff369 | 2703 | dVAR; |
79072805 | 2704 | register char *s; |
79072805 | 2705 | |
463ee0b2 | 2706 | if (!sv) { |
cdb061a3 NC |
2707 | if (lp) |
2708 | *lp = 0; | |
73d840c0 | 2709 | return (char *)""; |
463ee0b2 | 2710 | } |
8990e307 | 2711 | if (SvGMAGICAL(sv)) { |
8d6d96c1 HS |
2712 | if (flags & SV_GMAGIC) |
2713 | mg_get(sv); | |
463ee0b2 | 2714 | if (SvPOKp(sv)) { |
cdb061a3 NC |
2715 | if (lp) |
2716 | *lp = SvCUR(sv); | |
10516c54 NC |
2717 | if (flags & SV_MUTABLE_RETURN) |
2718 | return SvPVX_mutable(sv); | |
4d84ee25 NC |
2719 | if (flags & SV_CONST_RETURN) |
2720 | return (char *)SvPVX_const(sv); | |
463ee0b2 LW |
2721 | return SvPVX(sv); |
2722 | } | |
75dfc8ec NC |
2723 | if (SvIOKp(sv) || SvNOKp(sv)) { |
2724 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ | |
75dfc8ec NC |
2725 | STRLEN len; |
2726 | ||
2727 | if (SvIOKp(sv)) { | |
e80fed9d | 2728 | len = SvIsUV(sv) |
d9fad198 JH |
2729 | ? my_snprintf(tbuf, sizeof(tbuf), "%"UVuf, (UV)SvUVX(sv)) |
2730 | : my_snprintf(tbuf, sizeof(tbuf), "%"IVdf, (IV)SvIVX(sv)); | |
75dfc8ec | 2731 | } else { |
e8ada2d0 NC |
2732 | Gconvert(SvNVX(sv), NV_DIG, 0, tbuf); |
2733 | len = strlen(tbuf); | |
75dfc8ec | 2734 | } |
b5b886f0 NC |
2735 | assert(!SvROK(sv)); |
2736 | { | |
75dfc8ec NC |
2737 | dVAR; |
2738 | ||
2739 | #ifdef FIXNEGATIVEZERO | |
e8ada2d0 NC |
2740 | if (len == 2 && tbuf[0] == '-' && tbuf[1] == '0') { |
2741 | tbuf[0] = '0'; | |
2742 | tbuf[1] = 0; | |
75dfc8ec NC |
2743 | len = 1; |
2744 | } | |
2745 | #endif | |
2746 | SvUPGRADE(sv, SVt_PV); | |
2747 | if (lp) | |
2748 | *lp = len; | |
2749 | s = SvGROW_mutable(sv, len + 1); | |
2750 | SvCUR_set(sv, len); | |
2751 | SvPOKp_on(sv); | |
10edeb5d | 2752 | return (char*)memcpy(s, tbuf, len + 1); |
75dfc8ec | 2753 | } |
463ee0b2 | 2754 | } |
1c7ff15e NC |
2755 | if (SvROK(sv)) { |
2756 | goto return_rok; | |
2757 | } | |
2758 | assert(SvTYPE(sv) >= SVt_PVMG); | |
2759 | /* This falls through to the report_uninit near the end of the | |
2760 | function. */ | |
2761 | } else if (SvTHINKFIRST(sv)) { | |
ed6116ce | 2762 | if (SvROK(sv)) { |
1c7ff15e | 2763 | return_rok: |
deb46114 NC |
2764 | if (SvAMAGIC(sv)) { |
2765 | SV *const tmpstr = AMG_CALLun(sv,string); | |
2766 | if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) { | |
2767 | /* Unwrap this: */ | |
2768 | /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); | |
2769 | */ | |
2770 | ||
2771 | char *pv; | |
2772 | if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) { | |
2773 | if (flags & SV_CONST_RETURN) { | |
2774 | pv = (char *) SvPVX_const(tmpstr); | |
2775 | } else { | |
2776 | pv = (flags & SV_MUTABLE_RETURN) | |
2777 | ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr); | |
2778 | } | |
2779 | if (lp) | |
2780 | *lp = SvCUR(tmpstr); | |
50adf7d2 | 2781 | } else { |
deb46114 | 2782 | pv = sv_2pv_flags(tmpstr, lp, flags); |
50adf7d2 | 2783 | } |
deb46114 NC |
2784 | if (SvUTF8(tmpstr)) |
2785 | SvUTF8_on(sv); | |
2786 | else | |
2787 | SvUTF8_off(sv); | |
2788 | return pv; | |
50adf7d2 | 2789 | } |
deb46114 NC |
2790 | } |
2791 | { | |
fafee734 NC |
2792 | STRLEN len; |
2793 | char *retval; | |
2794 | char *buffer; | |
d8eae41e NC |
2795 | const SV *const referent = (SV*)SvRV(sv); |
2796 | ||
2797 | if (!referent) { | |
fafee734 NC |
2798 | len = 7; |
2799 | retval = buffer = savepvn("NULLREF", len); | |
5c35adbb | 2800 | } else if (SvTYPE(referent) == SVt_REGEXP) { |
67d2d14d AB |
2801 | const REGEXP * const re = (REGEXP *)referent; |
2802 | I32 seen_evals = 0; | |
2803 | ||
2804 | assert(re); | |
2805 | ||
2806 | /* If the regex is UTF-8 we want the containing scalar to | |
2807 | have an UTF-8 flag too */ | |
2808 | if (RX_UTF8(re)) | |
2809 | SvUTF8_on(sv); | |
2810 | else | |
2811 | SvUTF8_off(sv); | |
2812 | ||
2813 | if ((seen_evals = RX_SEEN_EVALS(re))) | |
2814 | PL_reginterp_cnt += seen_evals; | |
2815 | ||
2816 | if (lp) | |
2817 | *lp = RX_WRAPLEN(re); | |
2818 | ||
2819 | return RX_WRAPPED(re); | |
d8eae41e NC |
2820 | } else { |
2821 | const char *const typestr = sv_reftype(referent, 0); | |
fafee734 NC |
2822 | const STRLEN typelen = strlen(typestr); |
2823 | UV addr = PTR2UV(referent); | |
2824 | const char *stashname = NULL; | |
2825 | STRLEN stashnamelen = 0; /* hush, gcc */ | |
2826 | const char *buffer_end; | |
d8eae41e | 2827 | |
d8eae41e | 2828 | if (SvOBJECT(referent)) { |
fafee734 NC |
2829 | const HEK *const name = HvNAME_HEK(SvSTASH(referent)); |
2830 | ||
2831 | if (name) { | |
2832 | stashname = HEK_KEY(name); | |
2833 | stashnamelen = HEK_LEN(name); | |
2834 | ||
2835 | if (HEK_UTF8(name)) { | |
2836 | SvUTF8_on(sv); | |
2837 | } else { | |
2838 | SvUTF8_off(sv); | |
2839 | } | |
2840 | } else { | |
2841 | stashname = "__ANON__"; | |
2842 | stashnamelen = 8; | |
2843 | } | |
2844 | len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */ | |
2845 | + 2 * sizeof(UV) + 2 /* )\0 */; | |
2846 | } else { | |
2847 | len = typelen + 3 /* (0x */ | |
2848 | + 2 * sizeof(UV) + 2 /* )\0 */; | |
d8eae41e | 2849 | } |
fafee734 NC |
2850 | |
2851 | Newx(buffer, len, char); | |
2852 | buffer_end = retval = buffer + len; | |
2853 | ||
2854 | /* Working backwards */ | |
2855 | *--retval = '\0'; | |
2856 | *--retval = ')'; | |
2857 | do { | |
2858 | *--retval = PL_hexdigit[addr & 15]; | |
2859 | } while (addr >>= 4); | |
2860 | *--retval = 'x'; | |
2861 | *--retval = '0'; | |
2862 | *--retval = '('; | |
2863 | ||
2864 | retval -= typelen; | |
2865 | memcpy(retval, typestr, typelen); | |
2866 | ||
2867 | if (stashname) { | |
2868 | *--retval = '='; | |
2869 | retval -= stashnamelen; | |
2870 | memcpy(retval, stashname, stashnamelen); | |
2871 | } | |
2872 | /* retval may not neccesarily have reached the start of the | |
2873 | buffer here. */ | |
2874 | assert (retval >= buffer); | |
2875 | ||
2876 | len = buffer_end - retval - 1; /* -1 for that \0 */ | |
c080367d | 2877 | } |
042dae7a | 2878 | if (lp) |
fafee734 NC |
2879 | *lp = len; |
2880 | SAVEFREEPV(buffer); | |
2881 | return retval; | |
463ee0b2 | 2882 | } |
79072805 | 2883 | } |
0336b60e | 2884 | if (SvREADONLY(sv) && !SvOK(sv)) { |
cdb061a3 NC |
2885 | if (lp) |
2886 | *lp = 0; | |
9f621bb0 NC |
2887 | if (flags & SV_UNDEF_RETURNS_NULL) |
2888 | return NULL; | |
2889 | if (ckWARN(WARN_UNINITIALIZED)) | |
2890 | report_uninit(sv); | |
73d840c0 | 2891 | return (char *)""; |
79072805 | 2892 | } |
79072805 | 2893 | } |
28e5dec8 JH |
2894 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
2895 | /* I'm assuming that if both IV and NV are equally valid then | |
2896 | converting the IV is going to be more efficient */ | |
e1ec3a88 | 2897 | const U32 isUIOK = SvIsUV(sv); |
28e5dec8 JH |
2898 | char buf[TYPE_CHARS(UV)]; |
2899 | char *ebuf, *ptr; | |
97a130b8 | 2900 | STRLEN len; |
28e5dec8 JH |
2901 | |
2902 | if (SvTYPE(sv) < SVt_PVIV) | |
2903 | sv_upgrade(sv, SVt_PVIV); | |
4ea1d550 | 2904 | ptr = uiv_2buf(buf, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf); |
97a130b8 | 2905 | len = ebuf - ptr; |
5902b6a9 | 2906 | /* inlined from sv_setpvn */ |
97a130b8 NC |
2907 | s = SvGROW_mutable(sv, len + 1); |
2908 | Move(ptr, s, len, char); | |
2909 | s += len; | |
28e5dec8 | 2910 | *s = '\0'; |
28e5dec8 JH |
2911 | } |
2912 | else if (SvNOKp(sv)) { | |
c81271c3 | 2913 | const int olderrno = errno; |
79072805 LW |
2914 | if (SvTYPE(sv) < SVt_PVNV) |
2915 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 2916 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
5902b6a9 | 2917 | s = SvGROW_mutable(sv, NV_DIG + 20); |
c81271c3 | 2918 | /* some Xenix systems wipe out errno here */ |
79072805 | 2919 | #ifdef apollo |
463ee0b2 | 2920 | if (SvNVX(sv) == 0.0) |
d1307786 | 2921 | my_strlcpy(s, "0", SvLEN(sv)); |
79072805 LW |
2922 | else |
2923 | #endif /*apollo*/ | |
bbce6d69 | 2924 | { |
2d4389e4 | 2925 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 2926 | } |
79072805 | 2927 | errno = olderrno; |
a0d0e21e | 2928 | #ifdef FIXNEGATIVEZERO |
20773dcd NC |
2929 | if (*s == '-' && s[1] == '0' && !s[2]) { |
2930 | s[0] = '0'; | |
2931 | s[1] = 0; | |
2932 | } | |
a0d0e21e | 2933 | #endif |
79072805 LW |
2934 | while (*s) s++; |
2935 | #ifdef hcx | |
2936 | if (s[-1] == '.') | |
46fc3d4c | 2937 | *--s = '\0'; |
79072805 LW |
2938 | #endif |
2939 | } | |
79072805 | 2940 | else { |
675c862f | 2941 | if (isGV_with_GP(sv)) |
19f6321d | 2942 | return glob_2pv((GV *)sv, lp); |
180488f8 | 2943 | |
cdb061a3 | 2944 | if (lp) |
00b6aa41 | 2945 | *lp = 0; |
9f621bb0 NC |
2946 | if (flags & SV_UNDEF_RETURNS_NULL) |
2947 | return NULL; | |
2948 | if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED)) | |
2949 | report_uninit(sv); | |
25da4f38 IZ |
2950 | if (SvTYPE(sv) < SVt_PV) |
2951 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2952 | sv_upgrade(sv, SVt_PV); | |
73d840c0 | 2953 | return (char *)""; |
79072805 | 2954 | } |
cdb061a3 | 2955 | { |
823a54a3 | 2956 | const STRLEN len = s - SvPVX_const(sv); |
cdb061a3 NC |
2957 | if (lp) |
2958 | *lp = len; | |
2959 | SvCUR_set(sv, len); | |
2960 | } | |
79072805 | 2961 | SvPOK_on(sv); |
1d7c1841 | 2962 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
3f7c398e | 2963 | PTR2UV(sv),SvPVX_const(sv))); |
4d84ee25 NC |
2964 | if (flags & SV_CONST_RETURN) |
2965 | return (char *)SvPVX_const(sv); | |
10516c54 NC |
2966 | if (flags & SV_MUTABLE_RETURN) |
2967 | return SvPVX_mutable(sv); | |
463ee0b2 LW |
2968 | return SvPVX(sv); |
2969 | } | |
2970 | ||
645c22ef | 2971 | /* |
6050d10e JP |
2972 | =for apidoc sv_copypv |
2973 | ||
2974 | Copies a stringified representation of the source SV into the | |
2975 | destination SV. Automatically performs any necessary mg_get and | |
54f0641b | 2976 | coercion of numeric values into strings. Guaranteed to preserve |
2575c402 | 2977 | UTF8 flag even from overloaded objects. Similar in nature to |
54f0641b NIS |
2978 | sv_2pv[_flags] but operates directly on an SV instead of just the |
2979 | string. Mostly uses sv_2pv_flags to do its work, except when that | |
6050d10e JP |
2980 | would lose the UTF-8'ness of the PV. |
2981 | ||
2982 | =cut | |
2983 | */ | |
2984 | ||
2985 | void | |
5de3775c | 2986 | Perl_sv_copypv(pTHX_ SV *const dsv, register SV *const ssv) |
6050d10e | 2987 | { |
446eaa42 | 2988 | STRLEN len; |
53c1dcc0 | 2989 | const char * const s = SvPV_const(ssv,len); |
7918f24d NC |
2990 | |
2991 | PERL_ARGS_ASSERT_SV_COPYPV; | |
2992 | ||
cb50f42d | 2993 | sv_setpvn(dsv,s,len); |
446eaa42 | 2994 | if (SvUTF8(ssv)) |
cb50f42d | 2995 | SvUTF8_on(dsv); |
446eaa42 | 2996 | else |
cb50f42d | 2997 | SvUTF8_off(dsv); |
6050d10e JP |
2998 | } |
2999 | ||
3000 | /* | |
645c22ef DM |
3001 | =for apidoc sv_2pvbyte |
3002 | ||
3003 | Return a pointer to the byte-encoded representation of the SV, and set *lp | |
1e54db1a | 3004 | to its length. May cause the SV to be downgraded from UTF-8 as a |
645c22ef DM |
3005 | side-effect. |
3006 | ||
3007 | Usually accessed via the C<SvPVbyte> macro. | |
3008 | ||
3009 | =cut | |
3010 | */ | |
3011 | ||
7340a771 | 3012 | char * |
5de3775c | 3013 | Perl_sv_2pvbyte(pTHX_ register SV *const sv, STRLEN *const lp) |
7340a771 | 3014 | { |
7918f24d NC |
3015 | PERL_ARGS_ASSERT_SV_2PVBYTE; |
3016 | ||
0875d2fe | 3017 | sv_utf8_downgrade(sv,0); |
97972285 | 3018 | return lp ? SvPV(sv,*lp) : SvPV_nolen(sv); |
7340a771 GS |
3019 | } |
3020 | ||
645c22ef | 3021 | /* |
035cbb0e RGS |
3022 | =for apidoc sv_2pvutf8 |
3023 | ||
3024 | Return a pointer to the UTF-8-encoded representation of the SV, and set *lp | |
3025 | to its length. May cause the SV to be upgraded to UTF-8 as a side-effect. | |
3026 | ||
3027 | Usually accessed via the C<SvPVutf8> macro. | |
3028 | ||
3029 | =cut | |
3030 | */ | |
645c22ef | 3031 | |
7340a771 | 3032 | char * |
7bc54cea | 3033 | Perl_sv_2pvutf8(pTHX_ register SV *const sv, STRLEN *const lp) |
7340a771 | 3034 | { |
7918f24d NC |
3035 | PERL_ARGS_ASSERT_SV_2PVUTF8; |
3036 | ||
035cbb0e RGS |
3037 | sv_utf8_upgrade(sv); |
3038 | return lp ? SvPV(sv,*lp) : SvPV_nolen(sv); | |
7340a771 | 3039 | } |
1c846c1f | 3040 | |
7ee2227d | 3041 | |
645c22ef DM |
3042 | /* |
3043 | =for apidoc sv_2bool | |
3044 | ||
3045 | This function is only called on magical items, and is only used by | |
8cf8f3d1 | 3046 | sv_true() or its macro equivalent. |
645c22ef DM |
3047 | |
3048 | =cut | |
3049 | */ | |
3050 | ||
463ee0b2 | 3051 | bool |
7bc54cea | 3052 | Perl_sv_2bool(pTHX_ register SV *const sv) |
463ee0b2 | 3053 | { |
97aff369 | 3054 | dVAR; |
7918f24d NC |
3055 | |
3056 | PERL_ARGS_ASSERT_SV_2BOOL; | |
3057 | ||
5b295bef | 3058 | SvGETMAGIC(sv); |
463ee0b2 | 3059 | |
a0d0e21e LW |
3060 | if (!SvOK(sv)) |
3061 | return 0; | |
3062 | if (SvROK(sv)) { | |
fabdb6c0 AL |
3063 | if (SvAMAGIC(sv)) { |
3064 | SV * const tmpsv = AMG_CALLun(sv,bool_); | |
3065 | if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) | |
3066 | return (bool)SvTRUE(tmpsv); | |
3067 | } | |
3068 | return SvRV(sv) != 0; | |
a0d0e21e | 3069 | } |
463ee0b2 | 3070 | if (SvPOKp(sv)) { |
53c1dcc0 AL |
3071 | register XPV* const Xpvtmp = (XPV*)SvANY(sv); |
3072 | if (Xpvtmp && | |
339049b0 | 3073 | (*sv->sv_u.svu_pv > '0' || |
11343788 | 3074 | Xpvtmp->xpv_cur > 1 || |
339049b0 | 3075 | (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0'))) |
463ee0b2 LW |
3076 | return 1; |
3077 | else | |
3078 | return 0; | |
3079 | } | |
3080 | else { | |
3081 | if (SvIOKp(sv)) | |
3082 | return SvIVX(sv) != 0; | |
3083 | else { | |
3084 | if (SvNOKp(sv)) | |
3085 | return SvNVX(sv) != 0.0; | |
180488f8 | 3086 | else { |
f7877b28 | 3087 | if (isGV_with_GP(sv)) |
180488f8 NC |
3088 | return TRUE; |
3089 | else | |
3090 | return FALSE; | |
3091 | } | |
463ee0b2 LW |
3092 | } |
3093 | } | |
79072805 LW |
3094 | } |
3095 | ||
c461cf8f JH |
3096 | /* |
3097 | =for apidoc sv_utf8_upgrade | |
3098 | ||
78ea37eb | 3099 | Converts the PV of an SV to its UTF-8-encoded form. |
645c22ef | 3100 | Forces the SV to string form if it is not already. |
4411f3b6 NIS |
3101 | Always sets the SvUTF8 flag to avoid future validity checks even |
3102 | if all the bytes have hibit clear. | |
c461cf8f | 3103 | |
13a6c0e0 JH |
3104 | This is not as a general purpose byte encoding to Unicode interface: |
3105 | use the Encode extension for that. | |
3106 | ||
8d6d96c1 HS |
3107 | =for apidoc sv_utf8_upgrade_flags |
3108 | ||
78ea37eb | 3109 | Converts the PV of an SV to its UTF-8-encoded form. |
645c22ef | 3110 | Forces the SV to string form if it is not already. |
8d6d96c1 HS |
3111 | Always sets the SvUTF8 flag to avoid future validity checks even |
3112 | if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set, | |
3113 | will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and | |
3114 | C<sv_utf8_upgrade_nomg> are implemented in terms of this function. | |
3115 | ||
13a6c0e0 JH |
3116 | This is not as a general purpose byte encoding to Unicode interface: |
3117 | use the Encode extension for that. | |
3118 | ||
8d6d96c1 HS |
3119 | =cut |
3120 | */ | |
3121 | ||
3122 | STRLEN | |
7bc54cea | 3123 | Perl_sv_utf8_upgrade_flags(pTHX_ register SV *const sv, const I32 flags) |
8d6d96c1 | 3124 | { |
97aff369 | 3125 | dVAR; |
7918f24d NC |
3126 | |
3127 | PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS; | |
3128 | ||
808c356f RGS |
3129 | if (sv == &PL_sv_undef) |
3130 | return 0; | |
e0e62c2a NIS |
3131 | if (!SvPOK(sv)) { |
3132 | STRLEN len = 0; | |
d52b7888 NC |
3133 | if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) { |
3134 | (void) sv_2pv_flags(sv,&len, flags); | |
3135 | if (SvUTF8(sv)) | |
3136 | return len; | |
3137 | } else { | |
3138 | (void) SvPV_force(sv,len); | |
3139 | } | |
e0e62c2a | 3140 | } |
4411f3b6 | 3141 | |
f5cee72b | 3142 | if (SvUTF8(sv)) { |
5fec3b1d | 3143 | return SvCUR(sv); |
f5cee72b | 3144 | } |
5fec3b1d | 3145 | |
765f542d NC |
3146 | if (SvIsCOW(sv)) { |
3147 | sv_force_normal_flags(sv, 0); | |
db42d148 NIS |
3148 | } |
3149 | ||
88632417 | 3150 | if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) |
799ef3cb | 3151 | sv_recode_to_utf8(sv, PL_encoding); |
9f4817db | 3152 | else { /* Assume Latin-1/EBCDIC */ |
c4e7c712 NC |
3153 | /* This function could be much more efficient if we |
3154 | * had a FLAG in SVs to signal if there are any hibit | |
3155 | * chars in the PV. Given that there isn't such a flag | |
3156 | * make the loop as fast as possible. */ | |
00b6aa41 | 3157 | const U8 * const s = (U8 *) SvPVX_const(sv); |
c4420975 | 3158 | const U8 * const e = (U8 *) SvEND(sv); |
93524f2b | 3159 | const U8 *t = s; |
c4e7c712 NC |
3160 | |
3161 | while (t < e) { | |
53c1dcc0 | 3162 | const U8 ch = *t++; |
00b6aa41 AL |
3163 | /* Check for hi bit */ |
3164 | if (!NATIVE_IS_INVARIANT(ch)) { | |
3165 | STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */ | |
3166 | U8 * const recoded = bytes_to_utf8((U8*)s, &len); | |
3167 | ||
3168 | SvPV_free(sv); /* No longer using what was there before. */ | |
3169 | SvPV_set(sv, (char*)recoded); | |
3170 | SvCUR_set(sv, len - 1); | |
3171 | SvLEN_set(sv, len); /* No longer know the real size. */ | |
c4e7c712 | 3172 | break; |
00b6aa41 | 3173 | } |
c4e7c712 NC |
3174 | } |
3175 | /* Mark as UTF-8 even if no hibit - saves scanning loop */ | |
3176 | SvUTF8_on(sv); | |
560a288e | 3177 | } |
4411f3b6 | 3178 | return SvCUR(sv); |
560a288e GS |
3179 | } |
3180 | ||
c461cf8f JH |
3181 | /* |
3182 | =for apidoc sv_utf8_downgrade | |
3183 | ||
78ea37eb TS |
3184 | Attempts to convert the PV of an SV from characters to bytes. |
3185 | If the PV contains a character beyond byte, this conversion will fail; | |
3186 | in this case, either returns false or, if C<fail_ok> is not | |
c461cf8f JH |
3187 | true, croaks. |
3188 | ||
13a6c0e0 JH |
3189 | This is not as a general purpose Unicode to byte encoding interface: |
3190 | use the Encode extension for that. | |
3191 | ||
c461cf8f JH |
3192 | =cut |
3193 | */ | |
3194 | ||
560a288e | 3195 | bool |
7bc54cea | 3196 | Perl_sv_utf8_downgrade(pTHX_ register SV *const sv, const bool fail_ok) |
560a288e | 3197 | { |
97aff369 | 3198 | dVAR; |
7918f24d NC |
3199 | |
3200 | PERL_ARGS_ASSERT_SV_UTF8_DOWNGRADE; | |
3201 | ||
78ea37eb | 3202 | if (SvPOKp(sv) && SvUTF8(sv)) { |
fa301091 | 3203 | if (SvCUR(sv)) { |
03cfe0ae | 3204 | U8 *s; |
652088fc | 3205 | STRLEN len; |
fa301091 | 3206 | |
765f542d NC |
3207 | if (SvIsCOW(sv)) { |
3208 | sv_force_normal_flags(sv, 0); | |
3209 | } | |
03cfe0ae NIS |
3210 | s = (U8 *) SvPV(sv, len); |
3211 | if (!utf8_to_bytes(s, &len)) { | |
fa301091 JH |
3212 | if (fail_ok) |
3213 | return FALSE; | |
3214 | else { | |
3215 | if (PL_op) | |
3216 | Perl_croak(aTHX_ "Wide character in %s", | |
53e06cf0 | 3217 | OP_DESC(PL_op)); |
fa301091 JH |
3218 | else |
3219 | Perl_croak(aTHX_ "Wide character"); | |
3220 | } | |
4b3603a4 | 3221 | } |
b162af07 | 3222 | SvCUR_set(sv, len); |
67e989fb | 3223 | } |
560a288e | 3224 | } |
ffebcc3e | 3225 | SvUTF8_off(sv); |
560a288e GS |
3226 | return TRUE; |
3227 | } | |
3228 | ||
c461cf8f JH |
3229 | /* |
3230 | =for apidoc sv_utf8_encode | |
3231 | ||
78ea37eb TS |
3232 | Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8> |
3233 | flag off so that it looks like octets again. | |
c461cf8f JH |
3234 | |
3235 | =cut | |
3236 | */ | |
3237 | ||
560a288e | 3238 | void |
7bc54cea | 3239 | Perl_sv_utf8_encode(pTHX_ register SV *const sv) |
560a288e | 3240 | { |
7918f24d NC |
3241 | PERL_ARGS_ASSERT_SV_UTF8_ENCODE; |
3242 | ||
4c94c214 NC |
3243 | if (SvIsCOW(sv)) { |
3244 | sv_force_normal_flags(sv, 0); | |
3245 | } | |
3246 | if (SvREADONLY(sv)) { | |
3247 | Perl_croak(aTHX_ PL_no_modify); | |
3248 | } | |
a5f5288a | 3249 | (void) sv_utf8_upgrade(sv); |
560a288e GS |
3250 | SvUTF8_off(sv); |
3251 | } | |
3252 | ||
4411f3b6 NIS |
3253 | /* |
3254 | =for apidoc sv_utf8_decode | |
3255 | ||
78ea37eb TS |
3256 | If the PV of the SV is an octet sequence in UTF-8 |
3257 | and contains a multiple-byte character, the C<SvUTF8> flag is turned on | |
3258 | so that it looks like a character. If the PV contains only single-byte | |
3259 | characters, the C<SvUTF8> flag stays being off. | |
3260 | Scans PV for validity and returns false if the PV is invalid UTF-8. | |
4411f3b6 NIS |
3261 | |
3262 | =cut | |
3263 | */ | |
3264 | ||
560a288e | 3265 | bool |
7bc54cea | 3266 | Perl_sv_utf8_decode(pTHX_ register SV *const sv) |
560a288e | 3267 | { |
7918f24d NC |
3268 | PERL_ARGS_ASSERT_SV_UTF8_DECODE; |
3269 | ||
78ea37eb | 3270 | if (SvPOKp(sv)) { |
93524f2b NC |
3271 | const U8 *c; |
3272 | const U8 *e; | |
9cbac4c7 | 3273 | |
645c22ef DM |
3274 | /* The octets may have got themselves encoded - get them back as |
3275 | * bytes | |
3276 | */ | |
3277 | if (!sv_utf8_downgrade(sv, TRUE)) | |
560a288e GS |
3278 | return FALSE; |
3279 | ||
3280 | /* it is actually just a matter of turning the utf8 flag on, but | |
3281 | * we want to make sure everything inside is valid utf8 first. | |
3282 | */ | |
93524f2b | 3283 | c = (const U8 *) SvPVX_const(sv); |
63cd0674 | 3284 | if (!is_utf8_string(c, SvCUR(sv)+1)) |
67e989fb | 3285 | return FALSE; |
93524f2b | 3286 | e = (const U8 *) SvEND(sv); |
511c2ff0 | 3287 | while (c < e) { |
b64e5050 | 3288 | const U8 ch = *c++; |
c4d5f83a | 3289 | if (!UTF8_IS_INVARIANT(ch)) { |
67e989fb JH |
3290 | SvUTF8_on(sv); |
3291 | break; | |
3292 | } | |
560a288e | 3293 | } |
560a288e GS |
3294 | } |
3295 | return TRUE; | |
3296 | } | |
3297 | ||
954c1994 GS |
3298 | /* |
3299 | =for apidoc sv_setsv | |
3300 | ||
645c22ef DM |
3301 | Copies the contents of the source SV C<ssv> into the destination SV |
3302 | C<dsv>. The source SV may be destroyed if it is mortal, so don't use this | |
3303 | function if the source SV needs to be reused. Does not handle 'set' magic. | |
3304 | Loosely speaking, it performs a copy-by-value, obliterating any previous | |
3305 | content of the destination. | |
3306 | ||
3307 | You probably want to use one of the assortment of wrappers, such as | |
3308 | C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and | |
3309 | C<SvSetMagicSV_nosteal>. | |
3310 | ||
8d6d96c1 HS |
3311 | =for apidoc sv_setsv_flags |
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 | If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on | |
5fcdf167 NC |
3319 | C<ssv> if appropriate, else not. If the C<flags> parameter has the |
3320 | C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv> | |
3321 | and C<sv_setsv_nomg> are implemented in terms of this function. | |
645c22ef DM |
3322 | |
3323 | You probably want to use one of the assortment of wrappers, such as | |
3324 | C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and | |
3325 | C<SvSetMagicSV_nosteal>. | |
3326 | ||
3327 | This is the primary function for copying scalars, and most other | |
3328 | copy-ish functions and macros use this underneath. | |
8d6d96c1 HS |
3329 | |
3330 | =cut | |
3331 | */ | |
3332 | ||
5d0301b7 | 3333 | static void |
7bc54cea | 3334 | S_glob_assign_glob(pTHX_ SV *const dstr, SV *const sstr, const int dtype) |
5d0301b7 | 3335 | { |
70cd14a1 | 3336 | I32 mro_changes = 0; /* 1 = method, 2 = isa */ |
dd69841b | 3337 | |
7918f24d NC |
3338 | PERL_ARGS_ASSERT_GLOB_ASSIGN_GLOB; |
3339 | ||
5d0301b7 NC |
3340 | if (dtype != SVt_PVGV) { |
3341 | const char * const name = GvNAME(sstr); | |
3342 | const STRLEN len = GvNAMELEN(sstr); | |
0d092c36 | 3343 | { |
f7877b28 NC |
3344 | if (dtype >= SVt_PV) { |
3345 | SvPV_free(dstr); | |
3346 | SvPV_set(dstr, 0); | |
3347 | SvLEN_set(dstr, 0); | |
3348 | SvCUR_set(dstr, 0); | |
3349 | } | |
0d092c36 | 3350 | SvUPGRADE(dstr, SVt_PVGV); |
dedf8e73 | 3351 | (void)SvOK_off(dstr); |
2e5b91de NC |
3352 | /* FIXME - why are we doing this, then turning it off and on again |
3353 | below? */ | |
3354 | isGV_with_GP_on(dstr); | |
f7877b28 | 3355 | } |
5d0301b7 NC |
3356 | GvSTASH(dstr) = GvSTASH(sstr); |
3357 | if (GvSTASH(dstr)) | |
3358 | Perl_sv_add_backref(aTHX_ (SV*)GvSTASH(dstr), dstr); | |
ae8cc45f | 3359 | gv_name_set((GV *)dstr, name, len, GV_ADD); |
5d0301b7 NC |
3360 | SvFAKE_on(dstr); /* can coerce to non-glob */ |
3361 | } | |
3362 | ||
3363 | #ifdef GV_UNIQUE_CHECK | |
3364 | if (GvUNIQUE((GV*)dstr)) { | |
3365 | Perl_croak(aTHX_ PL_no_modify); | |
3366 | } | |
3367 | #endif | |
3368 | ||
dd69841b BB |
3369 | if(GvGP((GV*)sstr)) { |
3370 | /* If source has method cache entry, clear it */ | |
3371 | if(GvCVGEN(sstr)) { | |
3372 | SvREFCNT_dec(GvCV(sstr)); | |
3373 | GvCV(sstr) = NULL; | |
3374 | GvCVGEN(sstr) = 0; | |
3375 | } | |
3376 | /* If source has a real method, then a method is | |
3377 | going to change */ | |
3378 | else if(GvCV((GV*)sstr)) { | |
70cd14a1 | 3379 | mro_changes = 1; |
dd69841b BB |
3380 | } |
3381 | } | |
3382 | ||
3383 | /* If dest already had a real method, that's a change as well */ | |
70cd14a1 CB |
3384 | if(!mro_changes && GvGP((GV*)dstr) && GvCVu((GV*)dstr)) { |
3385 | mro_changes = 1; | |
dd69841b BB |
3386 | } |
3387 | ||
70cd14a1 CB |
3388 | if(strEQ(GvNAME((GV*)dstr),"ISA")) |
3389 | mro_changes = 2; | |
3390 | ||
f7877b28 | 3391 | gp_free((GV*)dstr); |
2e5b91de | 3392 | isGV_with_GP_off(dstr); |
5d0301b7 | 3393 | (void)SvOK_off(dstr); |
2e5b91de | 3394 | isGV_with_GP_on(dstr); |
dedf8e73 | 3395 | GvINTRO_off(dstr); /* one-shot flag */ |
5d0301b7 NC |
3396 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
3397 | if (SvTAINTED(sstr)) | |
3398 | SvTAINT(dstr); | |
3399 | if (GvIMPORTED(dstr) != GVf_IMPORTED | |
3400 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3401 | { | |
3402 | GvIMPORTED_on(dstr); | |
3403 | } | |
3404 | GvMULTI_on(dstr); | |
70cd14a1 CB |
3405 | if(mro_changes == 2) mro_isa_changed_in(GvSTASH(dstr)); |
3406 | else if(mro_changes) mro_method_changed_in(GvSTASH(dstr)); | |
5d0301b7 NC |
3407 | return; |
3408 | } | |
3409 | ||
b8473700 | 3410 | static void |
7bc54cea | 3411 | S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr) |
7918f24d | 3412 | { |
b8473700 NC |
3413 | SV * const sref = SvREFCNT_inc(SvRV(sstr)); |
3414 | SV *dref = NULL; | |
3415 | const int intro = GvINTRO(dstr); | |
2440974c | 3416 | SV **location; |
3386d083 | 3417 | U8 import_flag = 0; |
27242d61 NC |
3418 | const U32 stype = SvTYPE(sref); |
3419 | ||
7918f24d | 3420 | PERL_ARGS_ASSERT_GLOB_ASSIGN_REF; |
b8473700 NC |
3421 | |
3422 | #ifdef GV_UNIQUE_CHECK | |
3423 | if (GvUNIQUE((GV*)dstr)) { | |
3424 | Perl_croak(aTHX_ PL_no_modify); | |
3425 | } | |
3426 | #endif | |
3427 | ||
3428 | if (intro) { | |
3429 | GvINTRO_off(dstr); /* one-shot flag */ | |
3430 | GvLINE(dstr) = CopLINE(PL_curcop); | |
3431 | GvEGV(dstr) = (GV*)dstr; | |
3432 | } | |
3433 | GvMULTI_on(dstr); | |
27242d61 | 3434 | switch (stype) { |
b8473700 | 3435 | case SVt_PVCV: |
27242d61 NC |
3436 | location = (SV **) &GvCV(dstr); |
3437 | import_flag = GVf_IMPORTED_CV; | |
3438 | goto common; | |
3439 | case SVt_PVHV: | |
3440 | location = (SV **) &GvHV(dstr); | |
3441 | import_flag = GVf_IMPORTED_HV; | |
3442 | goto common; | |
3443 | case SVt_PVAV: | |
3444 | location = (SV **) &GvAV(dstr); | |
3445 | import_flag = GVf_IMPORTED_AV; | |
3446 | goto common; | |
3447 | case SVt_PVIO: | |
3448 | location = (SV **) &GvIOp(dstr); | |
3449 | goto common; | |
3450 | case SVt_PVFM: | |
3451 | location = (SV **) &GvFORM(dstr); | |
3452 | default: | |
3453 | location = &GvSV(dstr); | |
3454 | import_flag = GVf_IMPORTED_SV; | |
3455 | common: | |
b8473700 | 3456 | if (intro) { |
27242d61 | 3457 | if (stype == SVt_PVCV) { |
5f2fca8a BB |
3458 | /*if (GvCVGEN(dstr) && (GvCV(dstr) != (CV*)sref || GvCVGEN(dstr))) {*/ |
3459 | if (GvCVGEN(dstr)) { | |
27242d61 NC |
3460 | SvREFCNT_dec(GvCV(dstr)); |
3461 | GvCV(dstr) = NULL; | |
3462 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ | |
27242d61 | 3463 | } |
b8473700 | 3464 | } |
27242d61 | 3465 | SAVEGENERICSV(*location); |
b8473700 NC |
3466 | } |
3467 | else | |
27242d61 | 3468 | dref = *location; |
5f2fca8a | 3469 | if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dstr))) { |
27242d61 | 3470 | CV* const cv = (CV*)*location; |
b8473700 NC |
3471 | if (cv) { |
3472 | if (!GvCVGEN((GV*)dstr) && | |
3473 | (CvROOT(cv) || CvXSUB(cv))) | |
3474 | { | |
3475 | /* Redefining a sub - warning is mandatory if | |
3476 | it was a const and its value changed. */ | |
3477 | if (CvCONST(cv) && CvCONST((CV*)sref) | |
3478 | && cv_const_sv(cv) == cv_const_sv((CV*)sref)) { | |
6f207bd3 | 3479 | NOOP; |
b8473700 NC |
3480 | /* They are 2 constant subroutines generated from |
3481 | the same constant. This probably means that | |
3482 | they are really the "same" proxy subroutine | |
3483 | instantiated in 2 places. Most likely this is | |
3484 | when a constant is exported twice. Don't warn. | |
3485 | */ | |
3486 | } | |
3487 | else if (ckWARN(WARN_REDEFINE) | |
3488 | || (CvCONST(cv) | |
3489 | && (!CvCONST((CV*)sref) | |
3490 | || sv_cmp(cv_const_sv(cv), | |
3491 | cv_const_sv((CV*)sref))))) { | |
3492 | Perl_warner(aTHX_ packWARN(WARN_REDEFINE), | |
10edeb5d JH |
3493 | (const char *) |
3494 | (CvCONST(cv) | |
3495 | ? "Constant subroutine %s::%s redefined" | |
3496 | : "Subroutine %s::%s redefined"), | |
b8473700 NC |
3497 | HvNAME_get(GvSTASH((GV*)dstr)), |
3498 | GvENAME((GV*)dstr)); | |
3499 | } | |
3500 | } | |
3501 | if (!intro) | |
cbf82dd0 NC |
3502 | cv_ckproto_len(cv, (GV*)dstr, |
3503 | SvPOK(sref) ? SvPVX_const(sref) : NULL, | |
3504 | SvPOK(sref) ? SvCUR(sref) : 0); | |
b8473700 | 3505 | } |
b8473700 NC |
3506 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3507 | GvASSUMECV_on(dstr); | |
dd69841b | 3508 | if(GvSTASH(dstr)) mro_method_changed_in(GvSTASH(dstr)); /* sub foo { 1 } sub bar { 2 } *bar = \&foo */ |
b8473700 | 3509 | } |
2440974c | 3510 | *location = sref; |
3386d083 NC |
3511 | if (import_flag && !(GvFLAGS(dstr) & import_flag) |
3512 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) { | |
3513 | GvFLAGS(dstr) |= import_flag; | |
b8473700 NC |
3514 | } |
3515 | break; | |
3516 | } | |
b37c2d43 | 3517 | SvREFCNT_dec(dref); |
b8473700 NC |
3518 | if (SvTAINTED(sstr)) |
3519 | SvTAINT(dstr); | |
3520 | return; | |
3521 | } | |
3522 | ||
8d6d96c1 | 3523 | void |
7bc54cea | 3524 | Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV* sstr, const I32 flags) |
8d6d96c1 | 3525 | { |
97aff369 | 3526 | dVAR; |
8990e307 LW |
3527 | register U32 sflags; |
3528 | register int dtype; | |
42d0e0b7 | 3529 | register svtype stype; |
463ee0b2 | 3530 | |
7918f24d NC |
3531 | PERL_ARGS_ASSERT_SV_SETSV_FLAGS; |
3532 | ||
79072805 LW |
3533 | if (sstr == dstr) |
3534 | return; | |
29f4f0ab NC |
3535 | |
3536 | if (SvIS_FREED(dstr)) { | |
3537 | Perl_croak(aTHX_ "panic: attempt to copy value %" SVf | |
be2597df | 3538 | " to a freed scalar %p", SVfARG(sstr), (void *)dstr); |
29f4f0ab | 3539 | } |
765f542d | 3540 | SV_CHECK_THINKFIRST_COW_DROP(dstr); |
79072805 | 3541 | if (!sstr) |
3280af22 | 3542 | sstr = &PL_sv_undef; |
29f4f0ab | 3543 | if (SvIS_FREED(sstr)) { |
6c9570dc MHM |
3544 | Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p to %p", |
3545 | (void*)sstr, (void*)dstr); | |
29f4f0ab | 3546 | } |
8990e307 LW |
3547 | stype = SvTYPE(sstr); |
3548 | dtype = SvTYPE(dstr); | |
79072805 | 3549 | |
52944de8 | 3550 | (void)SvAMAGIC_off(dstr); |
7a5fa8a2 | 3551 | if ( SvVOK(dstr) ) |
ece467f9 JP |
3552 | { |
3553 | /* need to nuke the magic */ | |
3554 | mg_free(dstr); | |
ece467f9 | 3555 | } |
9e7bc3e8 | 3556 | |
463ee0b2 | 3557 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3558 | |
8990e307 | 3559 | switch (stype) { |
79072805 | 3560 | case SVt_NULL: |
aece5585 | 3561 | undef_sstr: |
20408e3c GS |
3562 | if (dtype != SVt_PVGV) { |
3563 | (void)SvOK_off(dstr); | |
3564 | return; | |
3565 | } | |
3566 | break; | |
463ee0b2 | 3567 | case SVt_IV: |
aece5585 GA |
3568 | if (SvIOK(sstr)) { |
3569 | switch (dtype) { | |
3570 | case SVt_NULL: | |
8990e307 | 3571 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3572 | break; |
3573 | case SVt_NV: | |
aece5585 | 3574 | case SVt_PV: |
a0d0e21e | 3575 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 | 3576 | break; |
010be86b NC |
3577 | case SVt_PVGV: |
3578 | goto end_of_first_switch; | |
aece5585 GA |
3579 | } |
3580 | (void)SvIOK_only(dstr); | |
45977657 | 3581 | SvIV_set(dstr, SvIVX(sstr)); |
25da4f38 IZ |
3582 | if (SvIsUV(sstr)) |
3583 | SvIsUV_on(dstr); | |
37c25af0 NC |
3584 | /* SvTAINTED can only be true if the SV has taint magic, which in |
3585 | turn means that the SV type is PVMG (or greater). This is the | |
3586 | case statement for SVt_IV, so this cannot be true (whatever gcov | |
3587 | may say). */ | |
3588 | assert(!SvTAINTED(sstr)); | |
aece5585 | 3589 | return; |
8990e307 | 3590 | } |
4df7f6af NC |
3591 | if (!SvROK(sstr)) |
3592 | goto undef_sstr; | |
3593 | if (dtype < SVt_PV && dtype != SVt_IV) | |
3594 | sv_upgrade(dstr, SVt_IV); | |
3595 | break; | |
aece5585 | 3596 | |
463ee0b2 | 3597 | case SVt_NV: |
aece5585 GA |
3598 | if (SvNOK(sstr)) { |
3599 | switch (dtype) { | |
3600 | case SVt_NULL: | |
3601 | case SVt_IV: | |
8990e307 | 3602 | sv_upgrade(dstr, SVt_NV); |
aece5585 | 3603 | break; |
aece5585 GA |
3604 | case SVt_PV: |
3605 | case SVt_PVIV: | |
a0d0e21e | 3606 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 | 3607 | break; |
010be86b NC |
3608 | case SVt_PVGV: |
3609 | goto end_of_first_switch; | |
aece5585 | 3610 | } |
9d6ce603 | 3611 | SvNV_set(dstr, SvNVX(sstr)); |
aece5585 | 3612 | (void)SvNOK_only(dstr); |
37c25af0 NC |
3613 | /* SvTAINTED can only be true if the SV has taint magic, which in |
3614 | turn means that the SV type is PVMG (or greater). This is the | |
3615 | case statement for SVt_NV, so this cannot be true (whatever gcov | |
3616 | may say). */ | |
3617 | assert(!SvTAINTED(sstr)); | |
aece5585 | 3618 | return; |
8990e307 | 3619 | } |
aece5585 GA |
3620 | goto undef_sstr; |
3621 | ||
fc36a67e | 3622 | case SVt_PVFM: |
f8c7b90f | 3623 | #ifdef PERL_OLD_COPY_ON_WRITE |
d89fc664 NC |
3624 | if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) { |
3625 | if (dtype < SVt_PVIV) | |
3626 | sv_upgrade(dstr, SVt_PVIV); | |
3627 | break; | |
3628 | } | |
3629 | /* Fall through */ | |
3630 | #endif | |
fd44068c | 3631 | case SVt_REGEXP: |
d89fc664 | 3632 | case SVt_PV: |
8990e307 | 3633 | if (dtype < SVt_PV) |
463ee0b2 | 3634 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3635 | break; |
3636 | case SVt_PVIV: | |
8990e307 | 3637 | if (dtype < SVt_PVIV) |
463ee0b2 | 3638 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3639 | break; |
3640 | case SVt_PVNV: | |
8990e307 | 3641 | if (dtype < SVt_PVNV) |
463ee0b2 | 3642 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3643 |