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