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