2 * Store and retrieve mechanism.
6 * $Id: Storable.xs,v 0.7.1.3 2000/08/23 23:00:41 ram Exp $
8 * Copyright (c) 1995-2000, Raphael Manfredi
10 * You may redistribute only under the terms of the Artistic License,
11 * as specified in the README file that comes with the distribution.
13 * $Log: Storable.xs,v $
14 * Revision 0.7.1.3 2000/08/23 23:00:41 ram
15 * patch3: ANSI-fied most of the code, preparing for Perl core integration
16 * patch3: dispatch tables moved upfront to relieve some compilers
17 * patch3: merged 64-bit fixes from perl5-porters
19 * Revision 0.7.1.2 2000/08/14 07:19:27 ram
20 * patch2: added a refcnt dec in retrieve_tied_key()
22 * Revision 0.7.1.1 2000/08/13 20:10:06 ram
23 * patch1: was wrongly optimizing for "undef" values in hashes
24 * patch1: added support for ref to tied items in hash/array
25 * patch1: added overloading support
27 * Revision 0.7 2000/08/03 22:04:44 ram
28 * Baseline for second beta release.
34 #include <patchlevel.h> /* Perl's one, needed since 5.6 */
37 /*#define DEBUGME /* Debug mode, turns assertions on as well */
38 /*#define DASSERT /* Assertion mode */
41 * Pre PerlIO time when none of USE_PERLIO and PERLIO_IS_STDIO is defined
42 * Provide them with the necessary defines so they can build with pre-5.004.
45 #ifndef PERLIO_IS_STDIO
47 #define PerlIO_getc(x) getc(x)
48 #define PerlIO_putc(f,x) putc(x,f)
49 #define PerlIO_read(x,y,z) fread(y,1,z,x)
50 #define PerlIO_write(x,y,z) fwrite(y,1,z,x)
51 #define PerlIO_stdoutf printf
52 #endif /* PERLIO_IS_STDIO */
53 #endif /* USE_PERLIO */
56 * Earlier versions of perl might be used, we can't assume they have the latest!
59 #ifndef PERL_VERSION /* For perls < 5.6 */
60 #define PERL_VERSION PATCHLEVEL
62 #define newRV_noinc(sv) ((Sv = newRV(sv)), --SvREFCNT(SvRV(Sv)), Sv)
64 #if (PATCHLEVEL <= 4) /* Older perls (<= 5.004) lack PL_ namespace */
65 #define PL_sv_yes sv_yes
66 #define PL_sv_no sv_no
67 #define PL_sv_undef sv_undef
69 #ifndef HvSHAREKEYS_off
70 #define HvSHAREKEYS_off(hv) /* Ignore */
73 #define INT2PTR(t,v) (t)(IV)(v)
75 #ifndef AvFILLp /* Older perls (<=5.003) lack AvFILLp */
76 #define AvFILLp AvFILL
78 typedef double NV; /* Older perls lack the NV type */
79 #endif /* PERL_VERSION -- perls < 5.6 */
85 #define TRACEME(x) do { PerlIO_stdoutf x; PerlIO_stdoutf("\n"); } while (0)
91 #define ASSERT(x,y) do { \
93 PerlIO_stdoutf("ASSERT FAILED (\"%s\", line %d): ", \
94 __FILE__, __LINE__); \
95 PerlIO_stdoutf y; PerlIO_stdoutf("\n"); \
106 #define C(x) ((char) (x)) /* For markers with dynamic retrieval handling */
108 #define SX_OBJECT C(0) /* Already stored object */
109 #define SX_LSCALAR C(1) /* Scalar (string) forthcoming (length, data) */
110 #define SX_ARRAY C(2) /* Array forthcominng (size, item list) */
111 #define SX_HASH C(3) /* Hash forthcoming (size, key/value pair list) */
112 #define SX_REF C(4) /* Reference to object forthcoming */
113 #define SX_UNDEF C(5) /* Undefined scalar */
114 #define SX_INTEGER C(6) /* Integer forthcoming */
115 #define SX_DOUBLE C(7) /* Double forthcoming */
116 #define SX_BYTE C(8) /* (signed) byte forthcoming */
117 #define SX_NETINT C(9) /* Integer in network order forthcoming */
118 #define SX_SCALAR C(10) /* Scalar (small) forthcoming (length, data) */
119 #define SX_TIED_ARRAY C(11) /* Tied array forthcoming */
120 #define SX_TIED_HASH C(12) /* Tied hash forthcoming */
121 #define SX_TIED_SCALAR C(13) /* Tied scalar forthcoming */
122 #define SX_SV_UNDEF C(14) /* Perl's immortal PL_sv_undef */
123 #define SX_SV_YES C(15) /* Perl's immortal PL_sv_yes */
124 #define SX_SV_NO C(16) /* Perl's immortal PL_sv_no */
125 #define SX_BLESS C(17) /* Object is blessed */
126 #define SX_IX_BLESS C(18) /* Object is blessed, classname given by index */
127 #define SX_HOOK C(19) /* Stored via hook, user-defined */
128 #define SX_OVERLOAD C(20) /* Overloaded reference */
129 #define SX_TIED_KEY C(21) /* Tied magic key forthcoming */
130 #define SX_TIED_IDX C(22) /* Tied magic index forthcoming */
131 #define SX_ERROR C(23) /* Error */
134 * Those are only used to retrieve "old" pre-0.6 binary images.
136 #define SX_ITEM 'i' /* An array item introducer */
137 #define SX_IT_UNDEF 'I' /* Undefined array item */
138 #define SX_KEY 'k' /* An hash key introducer */
139 #define SX_VALUE 'v' /* An hash value introducer */
140 #define SX_VL_UNDEF 'V' /* Undefined hash value */
143 * Those are only used to retrieve "old" pre-0.7 binary images
146 #define SX_CLASS 'b' /* Object is blessed, class name length <255 */
147 #define SX_LG_CLASS 'B' /* Object is blessed, class name length >255 */
148 #define SX_STORED 'X' /* End of object */
151 * Limits between short/long length representation.
154 #define LG_SCALAR 255 /* Large scalar length limit */
155 #define LG_BLESS 127 /* Large classname bless limit */
161 #define ST_STORE 0x1 /* Store operation */
162 #define ST_RETRIEVE 0x2 /* Retrieval operation */
163 #define ST_CLONE 0x4 /* Deep cloning operation */
166 * The following structure is used for hash table key retrieval. Since, when
167 * retrieving objects, we'll be facing blessed hash references, it's best
168 * to pre-allocate that buffer once and resize it as the need arises, never
169 * freeing it (keys will be saved away someplace else anyway, so even large
170 * keys are not enough a motivation to reclaim that space).
172 * This structure is also used for memory store/retrieve operations which
173 * happen in a fixed place before being malloc'ed elsewhere if persistency
174 * is required. Hence the aptr pointer.
177 char *arena; /* Will hold hash key strings, resized as needed */
178 STRLEN asiz; /* Size of aforementionned buffer */
179 char *aptr; /* Arena pointer, for in-place read/write ops */
180 char *aend; /* First invalid address */
185 * An hash table records the objects which have already been stored.
186 * Those are referred to as SX_OBJECT in the file, and their "tag" (i.e.
187 * an arbitrary sequence number) is used to identify them.
190 * An array table records the objects which have already been retrieved,
191 * as seen by the tag determind by counting the objects themselves. The
192 * reference to that retrieved object is kept in the table, and is returned
193 * when an SX_OBJECT is found bearing that same tag.
195 * The same processing is used to record "classname" for blessed objects:
196 * indexing by a hash at store time, and via an array at retrieve time.
199 typedef unsigned long stag_t; /* Used by pre-0.6 binary format */
202 * The following "thread-safe" related defines were contributed by
203 * Murray Nesbitt <murray@activestate.com> and integrated by RAM, who
204 * only renamed things a little bit to ensure consistency with surrounding
205 * code. -- RAM, 14/09/1999
207 * The original patch suffered from the fact that the stcxt_t structure
208 * was global. Murray tried to minimize the impact on the code as much as
211 * Starting with 0.7, Storable can be re-entrant, via the STORABLE_xxx hooks
212 * on objects. Therefore, the notion of context needs to be generalized,
216 #define MY_VERSION "Storable(" XS_VERSION ")"
218 typedef struct stcxt {
219 int entry; /* flags recursion */
220 int optype; /* type of traversal operation */
221 HV *hseen; /* which objects have been seen, store time */
222 AV *aseen; /* which objects have been seen, retrieve time */
223 HV *hclass; /* which classnames have been seen, store time */
224 AV *aclass; /* which classnames have been seen, retrieve time */
225 HV *hook; /* cache for hook methods per class name */
226 I32 tagnum; /* incremented at store time for each seen object */
227 I32 classnum; /* incremented at store time for each seen classname */
228 int netorder; /* true if network order used */
229 int forgive_me; /* whether to be forgiving... */
230 int canonical; /* whether to store hashes sorted by key */
231 int dirty; /* context is dirty due to CROAK() -- can be cleaned */
232 struct extendable keybuf; /* for hash key retrieval */
233 struct extendable membuf; /* for memory store/retrieve operations */
234 PerlIO *fio; /* where I/O are performed, NULL for memory */
235 int ver_major; /* major of version for retrieved object */
236 int ver_minor; /* minor of version for retrieved object */
237 SV *(**retrieve_vtbl)(); /* retrieve dispatch table */
238 struct stcxt *prev; /* contexts chained backwards in real recursion */
241 #if defined(MULTIPLICITY) || defined(PERL_OBJECT) || defined(PERL_CAPI)
243 #if (PATCHLEVEL <= 4) && (SUBVERSION < 68)
245 SV *perinterp_sv = perl_get_sv(MY_VERSION, FALSE)
246 #else /* >= perl5.004_68 */
248 SV *perinterp_sv = *hv_fetch(PL_modglobal, \
249 MY_VERSION, sizeof(MY_VERSION)-1, TRUE)
250 #endif /* < perl5.004_68 */
252 #define dSTCXT_PTR(T,name) \
253 T name = (T)(perinterp_sv && SvIOK(perinterp_sv)\
254 ? SvIVX(perinterp_sv) : NULL)
257 dSTCXT_PTR(stcxt_t *, cxt)
261 Newz(0, cxt, 1, stcxt_t); \
262 sv_setiv(perinterp_sv, PTR2IV(cxt))
264 #define SET_STCXT(x) do { \
266 sv_setiv(perinterp_sv, PTR2IV(x)); \
269 #else /* !MULTIPLICITY && !PERL_OBJECT && !PERL_CAPI */
271 static stcxt_t Context;
272 static stcxt_t *Context_ptr = &Context;
273 #define dSTCXT stcxt_t *cxt = Context_ptr
274 #define INIT_STCXT dSTCXT
275 #define SET_STCXT(x) Context_ptr = x
277 #endif /* MULTIPLICITY || PERL_OBJECT || PERL_CAPI */
281 * Croaking implies a memory leak, since we don't use setjmp/longjmp
282 * to catch the exit and free memory used during store or retrieve
283 * operations. This is not too difficult to fix, but I need to understand
284 * how Perl does it, and croaking is exceptional anyway, so I lack the
285 * motivation to do it.
287 * The current workaround is to mark the context as dirty when croaking,
288 * so that data structures can be freed whenever we renter Storable code
289 * (but only *then*: it's a workaround, not a fix).
291 * This is also imperfect, because we don't really know how far they trapped
292 * the croak(), and when we were recursing, we won't be able to clean anything
293 * but the topmost context stacked.
296 #define CROAK(x) do { cxt->dirty = 1; croak x; } while (0)
299 * End of "thread-safe" related definitions.
303 * key buffer handling
305 #define kbuf (cxt->keybuf).arena
306 #define ksiz (cxt->keybuf).asiz
307 #define KBUFINIT() do { \
309 TRACEME(("** allocating kbuf of 128 bytes")); \
310 New(10003, kbuf, 128, char); \
314 #define KBUFCHK(x) do { \
316 TRACEME(("** extending kbuf to %d bytes", x+1)); \
317 Renew(kbuf, x+1, char); \
323 * memory buffer handling
325 #define mbase (cxt->membuf).arena
326 #define msiz (cxt->membuf).asiz
327 #define mptr (cxt->membuf).aptr
328 #define mend (cxt->membuf).aend
330 #define MGROW (1 << 13)
331 #define MMASK (MGROW - 1)
333 #define round_mgrow(x) \
334 ((unsigned long) (((unsigned long) (x) + MMASK) & ~MMASK))
335 #define trunc_int(x) \
336 ((unsigned long) ((unsigned long) (x) & ~(sizeof(int)-1)))
337 #define int_aligned(x) \
338 ((unsigned long) (x) == trunc_int(x))
340 #define MBUF_INIT(x) do { \
342 TRACEME(("** allocating mbase of %d bytes", MGROW)); \
343 New(10003, mbase, MGROW, char); \
350 mend = mbase + msiz; \
353 #define MBUF_TRUNC(x) mptr = mbase + x
354 #define MBUF_SIZE() (mptr - mbase)
357 * Use SvPOKp(), because SvPOK() fails on tainted scalars.
358 * See store_scalar() for other usage of this workaround.
360 #define MBUF_LOAD(v) do { \
362 CROAK(("Not a scalar string")); \
363 mptr = mbase = SvPV(v, msiz); \
364 mend = mbase + msiz; \
367 #define MBUF_XTEND(x) do { \
368 int nsz = (int) round_mgrow((x)+msiz); \
369 int offset = mptr - mbase; \
370 TRACEME(("** extending mbase to %d bytes", nsz)); \
371 Renew(mbase, nsz, char); \
373 mptr = mbase + offset; \
374 mend = mbase + nsz; \
377 #define MBUF_CHK(x) do { \
378 if ((mptr + (x)) > mend) \
382 #define MBUF_GETC(x) do { \
384 x = (int) (unsigned char) *mptr++; \
389 #define MBUF_GETINT(x) do { \
390 if ((mptr + sizeof(int)) <= mend) { \
391 if (int_aligned(mptr)) \
394 memcpy(&x, mptr, sizeof(int)); \
395 mptr += sizeof(int); \
400 #define MBUF_READ(x,s) do { \
401 if ((mptr + (s)) <= mend) { \
402 memcpy(x, mptr, s); \
408 #define MBUF_SAFEREAD(x,s,z) do { \
409 if ((mptr + (s)) <= mend) { \
410 memcpy(x, mptr, s); \
418 #define MBUF_PUTC(c) do { \
420 *mptr++ = (char) c; \
423 *mptr++ = (char) c; \
427 #define MBUF_PUTINT(i) do { \
428 MBUF_CHK(sizeof(int)); \
429 if (int_aligned(mptr)) \
432 memcpy(mptr, &i, sizeof(int)); \
433 mptr += sizeof(int); \
436 #define MBUF_WRITE(x,s) do { \
438 memcpy(mptr, x, s); \
445 * Keep only the low 32 bits of a pointer (used for tags, which are not
450 #define LOW_32BITS(x) ((I32) (x))
452 #define LOW_32BITS(x) ((I32) ((unsigned long) (x) & 0xffffffffUL))
456 * Possible return values for sv_type().
460 #define svis_SCALAR 1
464 #define svis_TIED_ITEM 5
471 #define SHF_TYPE_MASK 0x03
472 #define SHF_LARGE_CLASSLEN 0x04
473 #define SHF_LARGE_STRLEN 0x08
474 #define SHF_LARGE_LISTLEN 0x10
475 #define SHF_IDX_CLASSNAME 0x20
476 #define SHF_NEED_RECURSE 0x40
477 #define SHF_HAS_LIST 0x80
480 * Types for SX_HOOK (2 bits).
488 * Before 0.6, the magic string was "perl-store" (binary version number 0).
490 * Since 0.6 introduced many binary incompatibilities, the magic string has
491 * been changed to "pst0" to allow an old image to be properly retrieved by
492 * a newer Storable, but ensure a newer image cannot be retrieved with an
495 * At 0.7, objects are given the ability to serialize themselves, and the
496 * set of markers is extended, backward compatibility is not jeopardized,
497 * so the binary version number could have remained unchanged. To correctly
498 * spot errors if a file making use of 0.7-specific extensions is given to
499 * 0.6 for retrieval, the binary version was moved to "2". And I'm introducing
500 * a "minor" version, to better track this kind of evolution from now on.
503 static char old_magicstr[] = "perl-store"; /* Magic number before 0.6 */
504 static char magicstr[] = "pst0"; /* Used as a magic number */
506 #define STORABLE_BIN_MAJOR 2 /* Binary major "version" */
507 #define STORABLE_BIN_MINOR 1 /* Binary minor "version" */
510 * Useful store shortcuts...
513 #define PUTMARK(x) do { \
516 else if (PerlIO_putc(cxt->fio, x) == EOF) \
521 #define WLEN(x) do { \
522 if (cxt->netorder) { \
523 int y = (int) htonl(x); \
526 else if (PerlIO_write(cxt->fio, &y, sizeof(y)) != sizeof(y)) \
531 else if (PerlIO_write(cxt->fio, &x, sizeof(x)) != sizeof(x)) \
536 #define WLEN(x) do { \
539 else if (PerlIO_write(cxt->fio, &x, sizeof(x)) != sizeof(x)) \
544 #define WRITE(x,y) do { \
547 else if (PerlIO_write(cxt->fio, x, y) != y) \
551 #define STORE_SCALAR(pv, len) do { \
552 if (len <= LG_SCALAR) { \
553 unsigned char clen = (unsigned char) len; \
554 PUTMARK(SX_SCALAR); \
559 PUTMARK(SX_LSCALAR); \
566 * Store undef in arrays and hashes without recursing through store().
568 #define STORE_UNDEF() do { \
574 * Useful retrieve shortcuts...
578 (cxt->fio ? PerlIO_getc(cxt->fio) : (mptr >= mend ? EOF : (int) *mptr++))
580 #define GETMARK(x) do { \
583 else if ((x = PerlIO_getc(cxt->fio)) == EOF) \
588 #define RLEN(x) do { \
591 else if (PerlIO_read(cxt->fio, &x, sizeof(x)) != sizeof(x)) \
594 x = (int) ntohl(x); \
597 #define RLEN(x) do { \
600 else if (PerlIO_read(cxt->fio, &x, sizeof(x)) != sizeof(x)) \
605 #define READ(x,y) do { \
608 else if (PerlIO_read(cxt->fio, x, y) != y) \
612 #define SAFEREAD(x,y,z) do { \
614 MBUF_SAFEREAD(x,y,z); \
615 else if (PerlIO_read(cxt->fio, x, y) != y) { \
622 * This macro is used at retrieve time, to remember where object 'y', bearing a
623 * given tag 'tagnum', has been retrieved. Next time we see an SX_OBJECT marker,
624 * we'll therefore know where it has been retrieved and will be able to
625 * share the same reference, as in the original stored memory image.
627 #define SEEN(y) do { \
630 if (av_store(cxt->aseen, cxt->tagnum++, SvREFCNT_inc(y)) == 0) \
632 TRACEME(("aseen(#%d) = 0x%"UVxf" (refcnt=%d)", cxt->tagnum-1, \
633 PTR2UV(y), SvREFCNT(y)-1)); \
637 * Bless `s' in `p', via a temporary reference, required by sv_bless().
639 #define BLESS(s,p) do { \
642 TRACEME(("blessing 0x%"UVxf" in %s", PTR2UV(s), (p))); \
643 stash = gv_stashpv((p), TRUE); \
644 ref = newRV_noinc(s); \
645 (void) sv_bless(ref, stash); \
651 static SV *retrieve();
654 * Dynamic dispatching table for SV store.
657 static int store_ref(stcxt_t *cxt, SV *sv);
658 static int store_scalar(stcxt_t *cxt, SV *sv);
659 static int store_array(stcxt_t *cxt, AV *av);
660 static int store_hash(stcxt_t *cxt, HV *hv);
661 static int store_tied(stcxt_t *cxt, SV *sv);
662 static int store_tied_item(stcxt_t *cxt, SV *sv);
663 static int store_other(stcxt_t *cxt, SV *sv);
664 static int store_blessed(stcxt_t *cxt, SV *sv, int type, HV *pkg);
666 static int (*sv_store[])() = {
667 store_ref, /* svis_REF */
668 store_scalar, /* svis_SCALAR */
669 store_array, /* svis_ARRAY */
670 store_hash, /* svis_HASH */
671 store_tied, /* svis_TIED */
672 store_tied_item, /* svis_TIED_ITEM */
673 store_other, /* svis_OTHER */
676 #define SV_STORE(x) (*sv_store[x])
679 * Dynamic dispatching tables for SV retrieval.
682 static SV *retrieve_lscalar(stcxt_t *cxt);
683 static SV *old_retrieve_array(stcxt_t *cxt);
684 static SV *old_retrieve_hash(stcxt_t *cxt);
685 static SV *retrieve_ref(stcxt_t *cxt);
686 static SV *retrieve_undef(stcxt_t *cxt);
687 static SV *retrieve_integer(stcxt_t *cxt);
688 static SV *retrieve_double(stcxt_t *cxt);
689 static SV *retrieve_byte(stcxt_t *cxt);
690 static SV *retrieve_netint(stcxt_t *cxt);
691 static SV *retrieve_scalar(stcxt_t *cxt);
692 static SV *retrieve_tied_array(stcxt_t *cxt);
693 static SV *retrieve_tied_hash(stcxt_t *cxt);
694 static SV *retrieve_tied_scalar(stcxt_t *cxt);
695 static SV *retrieve_other(stcxt_t *cxt);
697 static SV *(*sv_old_retrieve[])() = {
698 0, /* SX_OBJECT -- entry unused dynamically */
699 retrieve_lscalar, /* SX_LSCALAR */
700 old_retrieve_array, /* SX_ARRAY -- for pre-0.6 binaries */
701 old_retrieve_hash, /* SX_HASH -- for pre-0.6 binaries */
702 retrieve_ref, /* SX_REF */
703 retrieve_undef, /* SX_UNDEF */
704 retrieve_integer, /* SX_INTEGER */
705 retrieve_double, /* SX_DOUBLE */
706 retrieve_byte, /* SX_BYTE */
707 retrieve_netint, /* SX_NETINT */
708 retrieve_scalar, /* SX_SCALAR */
709 retrieve_tied_array, /* SX_ARRAY */
710 retrieve_tied_hash, /* SX_HASH */
711 retrieve_tied_scalar, /* SX_SCALAR */
712 retrieve_other, /* SX_SV_UNDEF not supported */
713 retrieve_other, /* SX_SV_YES not supported */
714 retrieve_other, /* SX_SV_NO not supported */
715 retrieve_other, /* SX_BLESS not supported */
716 retrieve_other, /* SX_IX_BLESS not supported */
717 retrieve_other, /* SX_HOOK not supported */
718 retrieve_other, /* SX_OVERLOADED not supported */
719 retrieve_other, /* SX_TIED_KEY not supported */
720 retrieve_other, /* SX_TIED_IDX not supported */
721 retrieve_other, /* SX_ERROR */
724 static SV *retrieve_array(stcxt_t *cxt);
725 static SV *retrieve_hash(stcxt_t *cxt);
726 static SV *retrieve_sv_undef(stcxt_t *cxt);
727 static SV *retrieve_sv_yes(stcxt_t *cxt);
728 static SV *retrieve_sv_no(stcxt_t *cxt);
729 static SV *retrieve_blessed(stcxt_t *cxt);
730 static SV *retrieve_idx_blessed(stcxt_t *cxt);
731 static SV *retrieve_hook(stcxt_t *cxt);
732 static SV *retrieve_overloaded(stcxt_t *cxt);
733 static SV *retrieve_tied_key(stcxt_t *cxt);
734 static SV *retrieve_tied_idx(stcxt_t *cxt);
736 static SV *(*sv_retrieve[])() = {
737 0, /* SX_OBJECT -- entry unused dynamically */
738 retrieve_lscalar, /* SX_LSCALAR */
739 retrieve_array, /* SX_ARRAY */
740 retrieve_hash, /* SX_HASH */
741 retrieve_ref, /* SX_REF */
742 retrieve_undef, /* SX_UNDEF */
743 retrieve_integer, /* SX_INTEGER */
744 retrieve_double, /* SX_DOUBLE */
745 retrieve_byte, /* SX_BYTE */
746 retrieve_netint, /* SX_NETINT */
747 retrieve_scalar, /* SX_SCALAR */
748 retrieve_tied_array, /* SX_ARRAY */
749 retrieve_tied_hash, /* SX_HASH */
750 retrieve_tied_scalar, /* SX_SCALAR */
751 retrieve_sv_undef, /* SX_SV_UNDEF */
752 retrieve_sv_yes, /* SX_SV_YES */
753 retrieve_sv_no, /* SX_SV_NO */
754 retrieve_blessed, /* SX_BLESS */
755 retrieve_idx_blessed, /* SX_IX_BLESS */
756 retrieve_hook, /* SX_HOOK */
757 retrieve_overloaded, /* SX_OVERLOAD */
758 retrieve_tied_key, /* SX_TIED_KEY */
759 retrieve_tied_idx, /* SX_TIED_IDX */
760 retrieve_other, /* SX_ERROR */
763 #define RETRIEVE(c,x) (*(c)->retrieve_vtbl[(x) >= SX_ERROR ? SX_ERROR : (x)])
765 static SV *mbuf2sv(void);
768 *** Context management.
774 * Called once per "thread" (interpreter) to initialize some global context.
776 static void init_perinterp(void)
780 cxt->netorder = 0; /* true if network order used */
781 cxt->forgive_me = -1; /* whether to be forgiving... */
787 * Initialize a new store context for real recursion.
789 static void init_store_context(
795 TRACEME(("init_store_context"));
797 cxt->netorder = network_order;
798 cxt->forgive_me = -1; /* Fetched from perl if needed */
799 cxt->canonical = -1; /* Idem */
800 cxt->tagnum = -1; /* Reset tag numbers */
801 cxt->classnum = -1; /* Reset class numbers */
802 cxt->fio = f; /* Where I/O are performed */
803 cxt->optype = optype; /* A store, or a deep clone */
804 cxt->entry = 1; /* No recursion yet */
807 * The `hseen' table is used to keep track of each SV stored and their
808 * associated tag numbers is special. It is "abused" because the
809 * values stored are not real SV, just integers cast to (SV *),
810 * which explains the freeing below.
812 * It is also one possible bottlneck to achieve good storing speed,
813 * so the "shared keys" optimization is turned off (unlikely to be
814 * of any use here), and the hash table is "pre-extended". Together,
815 * those optimizations increase the throughput by 12%.
818 cxt->hseen = newHV(); /* Table where seen objects are stored */
819 HvSHAREKEYS_off(cxt->hseen);
822 * The following does not work well with perl5.004_04, and causes
823 * a core dump later on, in a completely unrelated spot, which
824 * makes me think there is a memory corruption going on.
826 * Calling hv_ksplit(hseen, HBUCKETS) instead of manually hacking
827 * it below does not make any difference. It seems to work fine
828 * with perl5.004_68 but given the probable nature of the bug,
829 * that does not prove anything.
831 * It's a shame because increasing the amount of buckets raises
832 * store() throughput by 5%, but until I figure this out, I can't
833 * allow for this to go into production.
835 * It is reported fixed in 5.005, hence the #if.
837 #if PERL_VERSION >= 5
838 #define HBUCKETS 4096 /* Buckets for %hseen */
839 HvMAX(cxt->hseen) = HBUCKETS - 1; /* keys %hseen = $HBUCKETS; */
843 * The `hclass' hash uses the same settings as `hseen' above, but it is
844 * used to assign sequential tags (numbers) to class names for blessed
847 * We turn the shared key optimization on.
850 cxt->hclass = newHV(); /* Where seen classnames are stored */
852 #if PERL_VERSION >= 5
853 HvMAX(cxt->hclass) = HBUCKETS - 1; /* keys %hclass = $HBUCKETS; */
857 * The `hook' hash table is used to keep track of the references on
858 * the STORABLE_freeze hook routines, when found in some class name.
860 * It is assumed that the inheritance tree will not be changed during
861 * storing, and that no new method will be dynamically created by the
865 cxt->hook = newHV(); /* Table where hooks are cached */
869 * clean_store_context
871 * Clean store context by
873 static void clean_store_context(stcxt_t *cxt)
877 TRACEME(("clean_store_context"));
879 ASSERT(cxt->optype & ST_STORE, ("was performing a store()"));
882 * Insert real values into hashes where we stored faked pointers.
885 hv_iterinit(cxt->hseen);
886 while (he = hv_iternext(cxt->hseen))
887 HeVAL(he) = &PL_sv_undef;
889 hv_iterinit(cxt->hclass);
890 while (he = hv_iternext(cxt->hclass))
891 HeVAL(he) = &PL_sv_undef;
894 * And now dispose of them...
897 hv_undef(cxt->hseen);
898 sv_free((SV *) cxt->hseen);
900 hv_undef(cxt->hclass);
901 sv_free((SV *) cxt->hclass);
904 sv_free((SV *) cxt->hook);
911 * init_retrieve_context
913 * Initialize a new retrieve context for real recursion.
915 static void init_retrieve_context(cxt, optype)
919 TRACEME(("init_retrieve_context"));
922 * The hook hash table is used to keep track of the references on
923 * the STORABLE_thaw hook routines, when found in some class name.
925 * It is assumed that the inheritance tree will not be changed during
926 * storing, and that no new method will be dynamically created by the
930 cxt->hook = newHV(); /* Caches STORABLE_thaw */
933 * If retrieving an old binary version, the cxt->retrieve_vtbl variable
934 * was set to sv_old_retrieve. We'll need a hash table to keep track of
935 * the correspondance between the tags and the tag number used by the
936 * new retrieve routines.
939 cxt->hseen = (cxt->retrieve_vtbl == sv_old_retrieve) ? newHV() : 0;
941 cxt->aseen = newAV(); /* Where retrieved objects are kept */
942 cxt->aclass = newAV(); /* Where seen classnames are kept */
943 cxt->tagnum = 0; /* Have to count objects... */
944 cxt->classnum = 0; /* ...and class names as well */
945 cxt->optype = optype;
946 cxt->entry = 1; /* No recursion yet */
950 * clean_retrieve_context
952 * Clean retrieve context by
954 static void clean_retrieve_context(cxt)
957 TRACEME(("clean_retrieve_context"));
959 ASSERT(cxt->optype & ST_RETRIEVE, ("was performing a retrieve()"));
961 av_undef(cxt->aseen);
962 sv_free((SV *) cxt->aseen);
964 av_undef(cxt->aclass);
965 sv_free((SV *) cxt->aclass);
968 sv_free((SV *) cxt->hook);
971 sv_free((SV *) cxt->hseen); /* optional HV, for backward compat. */
980 * A workaround for the CROAK bug: cleanup the last context.
982 static void clean_context(cxt)
985 TRACEME(("clean_context"));
987 ASSERT(cxt->dirty, ("dirty context"));
989 if (cxt->optype & ST_RETRIEVE)
990 clean_retrieve_context(cxt);
992 clean_store_context(cxt);
998 * Allocate a new context and push it on top of the parent one.
999 * This new context is made globally visible via SET_STCXT().
1001 static stcxt_t *allocate_context(parent_cxt)
1002 stcxt_t *parent_cxt;
1006 TRACEME(("allocate_context"));
1008 ASSERT(!parent_cxt->dirty, ("parent context clean"));
1010 Newz(0, cxt, 1, stcxt_t);
1011 cxt->prev = parent_cxt;
1020 * Free current context, which cannot be the "root" one.
1021 * Make the context underneath globally visible via SET_STCXT().
1023 static void free_context(cxt)
1026 stcxt_t *prev = cxt->prev;
1028 TRACEME(("free_context"));
1030 ASSERT(!cxt->dirty, ("clean context"));
1031 ASSERT(prev, ("not freeing root context"));
1049 * Tells whether we're in the middle of a store operation.
1051 int is_storing(void)
1055 return cxt->entry && (cxt->optype & ST_STORE);
1061 * Tells whether we're in the middle of a retrieve operation.
1063 int is_retrieving(void)
1067 return cxt->entry && (cxt->optype & ST_RETRIEVE);
1071 * last_op_in_netorder
1073 * Returns whether last operation was made using network order.
1075 * This is typically out-of-band information that might prove useful
1076 * to people wishing to convert native to network order data when used.
1078 int last_op_in_netorder(void)
1082 return cxt->netorder;
1086 *** Hook lookup and calling routines.
1092 * A wrapper on gv_fetchmethod_autoload() which caches results.
1094 * Returns the routine reference as an SV*, or null if neither the package
1095 * nor its ancestors know about the method.
1097 static SV *pkg_fetchmeth(
1107 * The following code is the same as the one performed by UNIVERSAL::can
1111 gv = gv_fetchmethod_autoload(pkg, method, FALSE);
1112 if (gv && isGV(gv)) {
1113 sv = newRV((SV*) GvCV(gv));
1114 TRACEME(("%s->%s: 0x%"UVxf,
1115 HvNAME(pkg), method,
1118 sv = newSVsv(&PL_sv_undef);
1119 TRACEME(("%s->%s: not found", HvNAME(pkg), method));
1123 * Cache the result, ignoring failure: if we can't store the value,
1124 * it just won't be cached.
1127 (void) hv_store(cache, HvNAME(pkg), strlen(HvNAME(pkg)), sv, 0);
1129 return SvOK(sv) ? sv : (SV *) 0;
1135 * Force cached value to be undef: hook ignored even if present.
1137 static void pkg_hide(
1142 (void) hv_store(cache,
1143 HvNAME(pkg), strlen(HvNAME(pkg)), newSVsv(&PL_sv_undef), 0);
1149 * Our own "UNIVERSAL::can", which caches results.
1151 * Returns the routine reference as an SV*, or null if the object does not
1152 * know about the method.
1162 TRACEME(("pkg_can for %s->%s", HvNAME(pkg), method));
1165 * Look into the cache to see whether we already have determined
1166 * where the routine was, if any.
1168 * NOTA BENE: we don't use `method' at all in our lookup, since we know
1169 * that only one hook (i.e. always the same) is cached in a given cache.
1172 svh = hv_fetch(cache, HvNAME(pkg), strlen(HvNAME(pkg)), FALSE);
1176 TRACEME(("cached %s->%s: not found", HvNAME(pkg), method));
1179 TRACEME(("cached %s->%s: 0x%"UVxf,
1180 HvNAME(pkg), method,
1186 TRACEME(("not cached yet"));
1187 return pkg_fetchmeth(cache, pkg, method); /* Fetch and cache */
1193 * Call routine as obj->hook(av) in scalar context.
1194 * Propagates the single returned value if not called in void context.
1196 static SV *scalar_call(
1207 TRACEME(("scalar_call (cloning=%d)", cloning));
1214 XPUSHs(sv_2mortal(newSViv(cloning))); /* Cloning flag */
1216 SV **ary = AvARRAY(av);
1217 int cnt = AvFILLp(av) + 1;
1219 XPUSHs(ary[0]); /* Frozen string */
1220 for (i = 1; i < cnt; i++) {
1221 TRACEME(("pushing arg #%d (0x%"UVxf")...",
1222 i, PTR2UV(ary[i])));
1223 XPUSHs(sv_2mortal(newRV(ary[i])));
1228 TRACEME(("calling..."));
1229 count = perl_call_sv(hook, flags); /* Go back to Perl code */
1230 TRACEME(("count = %d", count));
1236 SvREFCNT_inc(sv); /* We're returning it, must stay alive! */
1249 * Call routine obj->hook(cloning) in list context.
1250 * Returns the list of returned values in an array.
1252 static AV *array_call(
1262 TRACEME(("array_call (cloning=%d)", cloning));
1268 XPUSHs(obj); /* Target object */
1269 XPUSHs(sv_2mortal(newSViv(cloning))); /* Cloning flag */
1272 count = perl_call_sv(hook, G_ARRAY); /* Go back to Perl code */
1277 for (i = count - 1; i >= 0; i--) {
1279 av_store(av, i, SvREFCNT_inc(sv));
1292 * Lookup the class name in the `hclass' table and either assign it a new ID
1293 * or return the existing one, by filling in `classnum'.
1295 * Return true if the class was known, false if the ID was just generated.
1297 static int known_class(
1299 char *name, /* Class name */
1300 int len, /* Name length */
1304 HV *hclass = cxt->hclass;
1306 TRACEME(("known_class (%s)", name));
1309 * Recall that we don't store pointers in this hash table, but tags.
1310 * Therefore, we need LOW_32BITS() to extract the relevant parts.
1313 svh = hv_fetch(hclass, name, len, FALSE);
1315 *classnum = LOW_32BITS(*svh);
1320 * Unknown classname, we need to record it.
1324 if (!hv_store(hclass, name, len, INT2PTR(SV*, cxt->classnum), 0))
1325 CROAK(("Unable to record new classname"));
1327 *classnum = cxt->classnum;
1332 *** Sepcific store routines.
1338 * Store a reference.
1339 * Layout is SX_REF <object> or SX_OVERLOAD <object>.
1341 static int store_ref(stcxt_t *cxt, SV *sv)
1343 TRACEME(("store_ref (0x%"UVxf")", PTR2UV(sv)));
1346 * Follow reference, and check if target is overloaded.
1352 HV *stash = (HV *) SvSTASH(sv);
1353 if (stash && Gv_AMG(stash)) {
1354 TRACEME(("ref (0x%"UVxf") is overloaded",
1356 PUTMARK(SX_OVERLOAD);
1362 return store(cxt, sv);
1370 * Layout is SX_LSCALAR <length> <data>, SX_SCALAR <lenght> <data> or SX_UNDEF.
1371 * The <data> section is omitted if <length> is 0.
1373 * If integer or double, the layout is SX_INTEGER <data> or SX_DOUBLE <data>.
1374 * Small integers (within [-127, +127]) are stored as SX_BYTE <byte>.
1376 static int store_scalar(stcxt_t *cxt, SV *sv)
1381 U32 flags = SvFLAGS(sv); /* "cc -O" may put it in register */
1383 TRACEME(("store_scalar (0x%"UVxf")", PTR2UV(sv)));
1386 * For efficiency, break the SV encapsulation by peaking at the flags
1387 * directly without using the Perl macros to avoid dereferencing
1388 * sv->sv_flags each time we wish to check the flags.
1391 if (!(flags & SVf_OK)) { /* !SvOK(sv) */
1392 if (sv == &PL_sv_undef) {
1393 TRACEME(("immortal undef"));
1394 PUTMARK(SX_SV_UNDEF);
1396 TRACEME(("undef at 0x%x", sv));
1403 * Always store the string representation of a scalar if it exists.
1404 * Gisle Aas provided me with this test case, better than a long speach:
1406 * perl -MDevel::Peek -le '$a="abc"; $a+0; Dump($a)'
1407 * SV = PVNV(0x80c8520)
1409 * FLAGS = (NOK,POK,pNOK,pPOK)
1412 * PV = 0x80c83d0 "abc"\0
1416 * Write SX_SCALAR, length, followed by the actual data.
1418 * Otherwise, write an SX_BYTE, SX_INTEGER or an SX_DOUBLE as
1419 * appropriate, followed by the actual (binary) data. A double
1420 * is written as a string if network order, for portability.
1422 * NOTE: instead of using SvNOK(sv), we test for SvNOKp(sv).
1423 * The reason is that when the scalar value is tainted, the SvNOK(sv)
1426 * The test for a read-only scalar with both POK and NOK set is meant
1427 * to quickly detect &PL_sv_yes and &PL_sv_no without having to pay the
1428 * address comparison for each scalar we store.
1431 #define SV_MAYBE_IMMORTAL (SVf_READONLY|SVf_POK|SVf_NOK)
1433 if ((flags & SV_MAYBE_IMMORTAL) == SV_MAYBE_IMMORTAL) {
1434 if (sv == &PL_sv_yes) {
1435 TRACEME(("immortal yes"));
1437 } else if (sv == &PL_sv_no) {
1438 TRACEME(("immortal no"));
1441 pv = SvPV(sv, len); /* We know it's SvPOK */
1442 goto string; /* Share code below */
1444 } else if (flags & SVp_POK) { /* SvPOKp(sv) => string */
1448 * Will come here from below with pv and len set if double & netorder,
1449 * or from above if it was readonly, POK and NOK but neither &PL_sv_yes
1454 STORE_SCALAR(pv, len);
1455 TRACEME(("ok (scalar 0x%"UVxf" '%s', length = %d)",
1456 PTR2UV(sv), SvPVX(sv), len));
1458 } else if (flags & SVp_NOK) { /* SvNOKp(sv) => double */
1462 * Watch for number being an integer in disguise.
1464 if (nv == (NV) (iv = I_V(nv))) {
1465 TRACEME(("double %"NVff" is actually integer %ld", nv, iv));
1466 goto integer; /* Share code below */
1469 if (cxt->netorder) {
1470 TRACEME(("double %"NVff" stored as string", nv));
1472 goto string; /* Share code above */
1476 WRITE(&nv, sizeof(nv));
1478 TRACEME(("ok (double 0x%"UVxf", value = %"NVff")",
1481 } else if (flags & SVp_IOK) { /* SvIOKp(sv) => integer */
1485 * Will come here from above with iv set if double is an integer.
1490 * Optimize small integers into a single byte, otherwise store as
1491 * a real integer (converted into network order if they asked).
1494 if (iv >= -128 && iv <= 127) {
1495 unsigned char siv = (unsigned char) (iv + 128); /* [0,255] */
1498 TRACEME(("small integer stored as %d", siv));
1499 } else if (cxt->netorder) {
1502 niv = (int) htonl(iv);
1503 TRACEME(("using network order"));
1506 TRACEME(("as-is for network order"));
1509 WRITE(&niv, sizeof(niv));
1511 PUTMARK(SX_INTEGER);
1512 WRITE(&iv, sizeof(iv));
1515 TRACEME(("ok (integer 0x%"UVxf", value = %d)",
1519 CROAK(("Can't determine type of %s(0x%"UVxf")",
1520 sv_reftype(sv, FALSE),
1523 return 0; /* Ok, no recursion on scalars */
1531 * Layout is SX_ARRAY <size> followed by each item, in increading index order.
1532 * Each item is stored as <object>.
1534 static int store_array(stcxt_t *cxt, AV *av)
1537 I32 len = av_len(av) + 1;
1541 TRACEME(("store_array (0x%"UVxf")", PTR2UV(av)));
1544 * Signal array by emitting SX_ARRAY, followed by the array length.
1549 TRACEME(("size = %d", len));
1552 * Now store each item recursively.
1555 for (i = 0; i < len; i++) {
1556 sav = av_fetch(av, i, 0);
1558 TRACEME(("(#%d) undef item", i));
1562 TRACEME(("(#%d) item", i));
1563 if (ret = store(cxt, *sav))
1567 TRACEME(("ok (array)"));
1576 * Borrowed from perl source file pp_ctl.c, where it is used by pp_sort.
1579 sortcmp(const void *a, const void *b)
1581 return sv_cmp(*(SV * const *) a, *(SV * const *) b);
1588 * Store an hash table.
1590 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
1591 * Values are stored as <object>.
1592 * Keys are stored as <length> <data>, the <data> section being omitted
1595 static int store_hash(stcxt_t *cxt, HV *hv)
1597 I32 len = HvKEYS(hv);
1603 TRACEME(("store_hash (0x%"UVxf")", PTR2UV(hv)));
1606 * Signal hash by emitting SX_HASH, followed by the table length.
1611 TRACEME(("size = %d", len));
1614 * Save possible iteration state via each() on that table.
1617 riter = HvRITER(hv);
1618 eiter = HvEITER(hv);
1622 * Now store each item recursively.
1624 * If canonical is defined to some true value then store each
1625 * key/value pair in sorted order otherwise the order is random.
1626 * Canonical order is irrelevant when a deep clone operation is performed.
1628 * Fetch the value from perl only once per store() operation, and only
1633 !(cxt->optype & ST_CLONE) && (cxt->canonical == 1 ||
1634 (cxt->canonical < 0 && (cxt->canonical =
1635 SvTRUE(perl_get_sv("Storable::canonical", TRUE)) ? 1 : 0)))
1638 * Storing in order, sorted by key.
1639 * Run through the hash, building up an array of keys in a
1640 * mortal array, sort the array and then run through the
1646 TRACEME(("using canonical order"));
1648 for (i = 0; i < len; i++) {
1649 HE *he = hv_iternext(hv);
1650 SV *key = hv_iterkeysv(he);
1651 av_store(av, AvFILLp(av)+1, key); /* av_push(), really */
1654 qsort((char *) AvARRAY(av), len, sizeof(SV *), sortcmp);
1656 for (i = 0; i < len; i++) {
1659 SV *key = av_shift(av);
1660 HE *he = hv_fetch_ent(hv, key, 0, 0);
1661 SV *val = HeVAL(he);
1663 return 1; /* Internal error, not I/O error */
1666 * Store value first.
1669 TRACEME(("(#%d) value 0x%"UVxf,
1672 if (ret = store(cxt, val))
1677 * Keys are written after values to make sure retrieval
1678 * can be optimal in terms of memory usage, where keys are
1679 * read into a fixed unique buffer called kbuf.
1680 * See retrieve_hash() for details.
1683 keyval = hv_iterkey(he, &keylen);
1684 TRACEME(("(#%d) key '%s'", i, keyval));
1687 WRITE(keyval, keylen);
1691 * Free up the temporary array
1700 * Storing in "random" order (in the order the keys are stored
1701 * within the the hash). This is the default and will be faster!
1704 for (i = 0; i < len; i++) {
1707 SV *val = hv_iternextsv(hv, &key, &len);
1710 return 1; /* Internal error, not I/O error */
1713 * Store value first.
1716 TRACEME(("(#%d) value 0x%"UVxf,
1719 if (ret = store(cxt, val))
1724 * Keys are written after values to make sure retrieval
1725 * can be optimal in terms of memory usage, where keys are
1726 * read into a fixed unique buffer called kbuf.
1727 * See retrieve_hash() for details.
1730 TRACEME(("(#%d) key '%s'", i, key));
1737 TRACEME(("ok (hash 0x%"UVxf")", PTR2UV(hv)));
1740 HvRITER(hv) = riter; /* Restore hash iterator state */
1741 HvEITER(hv) = eiter;
1749 * When storing a tied object (be it a tied scalar, array or hash), we lay out
1750 * a special mark, followed by the underlying tied object. For instance, when
1751 * dealing with a tied hash, we store SX_TIED_HASH <hash object>, where
1752 * <hash object> stands for the serialization of the tied hash.
1754 static int store_tied(stcxt_t *cxt, SV *sv)
1758 int svt = SvTYPE(sv);
1761 TRACEME(("store_tied (0x%"UVxf")", PTR2UV(sv)));
1764 * We have a small run-time penalty here because we chose to factorise
1765 * all tieds objects into the same routine, and not have a store_tied_hash,
1766 * a store_tied_array, etc...
1768 * Don't use a switch() statement, as most compilers don't optimize that
1769 * well for 2/3 values. An if() else if() cascade is just fine. We put
1770 * tied hashes first, as they are the most likely beasts.
1773 if (svt == SVt_PVHV) {
1774 TRACEME(("tied hash"));
1775 PUTMARK(SX_TIED_HASH); /* Introduces tied hash */
1776 } else if (svt == SVt_PVAV) {
1777 TRACEME(("tied array"));
1778 PUTMARK(SX_TIED_ARRAY); /* Introduces tied array */
1780 TRACEME(("tied scalar"));
1781 PUTMARK(SX_TIED_SCALAR); /* Introduces tied scalar */
1785 if (!(mg = mg_find(sv, mtype)))
1786 CROAK(("No magic '%c' found while storing tied %s", mtype,
1787 (svt == SVt_PVHV) ? "hash" :
1788 (svt == SVt_PVAV) ? "array" : "scalar"));
1791 * The mg->mg_obj found by mg_find() above actually points to the
1792 * underlying tied Perl object implementation. For instance, if the
1793 * original SV was that of a tied array, then mg->mg_obj is an AV.
1795 * Note that we store the Perl object as-is. We don't call its FETCH
1796 * method along the way. At retrieval time, we won't call its STORE
1797 * method either, but the tieing magic will be re-installed. In itself,
1798 * that ensures that the tieing semantics are preserved since futher
1799 * accesses on the retrieved object will indeed call the magic methods...
1802 if (ret = store(cxt, mg->mg_obj))
1805 TRACEME(("ok (tied)"));
1813 * Stores a reference to an item within a tied structure:
1815 * . \$h{key}, stores both the (tied %h) object and 'key'.
1816 * . \$a[idx], stores both the (tied @a) object and 'idx'.
1818 * Layout is therefore either:
1819 * SX_TIED_KEY <object> <key>
1820 * SX_TIED_IDX <object> <index>
1822 static int store_tied_item(stcxt_t *cxt, SV *sv)
1827 TRACEME(("store_tied_item (0x%"UVxf")", PTR2UV(sv)));
1829 if (!(mg = mg_find(sv, 'p')))
1830 CROAK(("No magic 'p' found while storing reference to tied item"));
1833 * We discriminate between \$h{key} and \$a[idx] via mg_ptr.
1837 TRACEME(("store_tied_item: storing a ref to a tied hash item"));
1838 PUTMARK(SX_TIED_KEY);
1839 TRACEME(("store_tied_item: storing OBJ 0x%"UVxf,
1840 PTR2UV(mg->mg_obj)));
1842 if (ret = store(cxt, mg->mg_obj))
1845 TRACEME(("store_tied_item: storing PTR 0x%"UVxf,
1846 PTR2UV(mg->mg_ptr)));
1848 if (ret = store(cxt, (SV *) mg->mg_ptr))
1851 I32 idx = mg->mg_len;
1853 TRACEME(("store_tied_item: storing a ref to a tied array item "));
1854 PUTMARK(SX_TIED_IDX);
1855 TRACEME(("store_tied_item: storing OBJ 0x%"UVxf,
1856 PTR2UV(mg->mg_obj)));
1858 if (ret = store(cxt, mg->mg_obj))
1861 TRACEME(("store_tied_item: storing IDX %d", idx));
1866 TRACEME(("ok (tied item)"));
1872 * store_hook -- dispatched manually, not via sv_store[]
1874 * The blessed SV is serialized by a hook.
1878 * SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
1880 * where <flags> indicates how long <len>, <len2> and <len3> are, whether
1881 * the trailing part [] is present, the type of object (scalar, array or hash).
1882 * There is also a bit which says how the classname is stored between:
1887 * and when the <index> form is used (classname already seen), the "large
1888 * classname" bit in <flags> indicates how large the <index> is.
1890 * The serialized string returned by the hook is of length <len2> and comes
1891 * next. It is an opaque string for us.
1893 * Those <len3> object IDs which are listed last represent the extra references
1894 * not directly serialized by the hook, but which are linked to the object.
1896 * When recursion is mandated to resolve object-IDs not yet seen, we have
1897 * instead, with <header> being flags with bits set to indicate the object type
1898 * and that recursion was indeed needed:
1900 * SX_HOOK <header> <object> <header> <object> <flags>
1902 * that same header being repeated between serialized objects obtained through
1903 * recursion, until we reach flags indicating no recursion, at which point
1904 * we know we've resynchronized with a single layout, after <flags>.
1906 static int store_hook(
1919 int count; /* really len3 + 1 */
1920 unsigned char flags;
1923 int recursed = 0; /* counts recursion */
1924 int obj_type; /* object type, on 2 bits */
1927 int clone = cxt->optype & ST_CLONE;
1929 TRACEME(("store_hook, class \"%s\", tagged #%d", HvNAME(pkg), cxt->tagnum));
1932 * Determine object type on 2 bits.
1937 obj_type = SHT_SCALAR;
1940 obj_type = SHT_ARRAY;
1943 obj_type = SHT_HASH;
1946 CROAK(("Unexpected object type (%d) in store_hook()", type));
1948 flags = SHF_NEED_RECURSE | obj_type;
1950 class = HvNAME(pkg);
1951 len = strlen(class);
1954 * To call the hook, we need to fake a call like:
1956 * $object->STORABLE_freeze($cloning);
1958 * but we don't have the $object here. For instance, if $object is
1959 * a blessed array, what we have in `sv' is the array, and we can't
1960 * call a method on those.
1962 * Therefore, we need to create a temporary reference to the object and
1963 * make the call on that reference.
1966 TRACEME(("about to call STORABLE_freeze on class %s", class));
1968 ref = newRV_noinc(sv); /* Temporary reference */
1969 av = array_call(ref, hook, clone); /* @a = $object->STORABLE_freeze($c) */
1971 SvREFCNT_dec(ref); /* Reclaim temporary reference */
1973 count = AvFILLp(av) + 1;
1974 TRACEME(("store_hook, array holds %d items", count));
1977 * If they return an empty list, it means they wish to ignore the
1978 * hook for this class (and not just this instance -- that's for them
1979 * to handle if they so wish).
1981 * Simply disable the cached entry for the hook (it won't be recomputed
1982 * since it's present in the cache) and recurse to store_blessed().
1987 * They must not change their mind in the middle of a serialization.
1990 if (hv_fetch(cxt->hclass, class, len, FALSE))
1991 CROAK(("Too late to ignore hooks for %s class \"%s\"",
1992 (cxt->optype & ST_CLONE) ? "cloning" : "storing", class));
1994 pkg_hide(cxt->hook, pkg, "STORABLE_freeze");
1996 ASSERT(!pkg_can(cxt->hook, pkg, "STORABLE_freeze"), ("hook invisible"));
1997 TRACEME(("Ignoring STORABLE_freeze in class \"%s\"", class));
1999 return store_blessed(cxt, sv, type, pkg);
2003 * Get frozen string.
2007 pv = SvPV(ary[0], len2);
2010 * Allocate a class ID if not already done.
2013 if (!known_class(cxt, class, len, &classnum)) {
2014 TRACEME(("first time we see class %s, ID = %d", class, classnum));
2015 classnum = -1; /* Mark: we must store classname */
2017 TRACEME(("already seen class %s, ID = %d", class, classnum));
2021 * If they returned more than one item, we need to serialize some
2022 * extra references if not already done.
2024 * Loop over the array, starting at postion #1, and for each item,
2025 * ensure it is a reference, serialize it if not already done, and
2026 * replace the entry with the tag ID of the corresponding serialized
2029 * We CHEAT by not calling av_fetch() and read directly within the
2033 for (i = 1; i < count; i++) {
2038 CROAK(("Item #%d from hook in %s is not a reference", i, class));
2039 xsv = SvRV(xsv); /* Follow ref to know what to look for */
2042 * Look in hseen and see if we have a tag already.
2043 * Serialize entry if not done already, and get its tag.
2046 if (svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE))
2047 goto sv_seen; /* Avoid moving code too far to the right */
2049 TRACEME(("listed object %d at 0x%"UVxf" is unknown",
2053 * We need to recurse to store that object and get it to be known
2054 * so that we can resolve the list of object-IDs at retrieve time.
2056 * The first time we do this, we need to emit the proper header
2057 * indicating that we recursed, and what the type of object is (the
2058 * object we're storing via a user-hook). Indeed, during retrieval,
2059 * we'll have to create the object before recursing to retrieve the
2060 * others, in case those would point back at that object.
2063 /* [SX_HOOK] <flags> <object>*/
2068 if (ret = store(cxt, xsv)) /* Given by hook for us to store */
2071 svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE);
2073 CROAK(("Could not serialize item #%d from hook in %s", i, class));
2076 * Replace entry with its tag (not a real SV, so no refcnt increment)
2082 TRACEME(("listed object %d at 0x%"UVxf" is tag #%d",
2083 i-1, PTR2UV(xsv), (I32) *svh));
2087 * Compute leading flags.
2091 if (((classnum == -1) ? len : classnum) > LG_SCALAR)
2092 flags |= SHF_LARGE_CLASSLEN;
2094 flags |= SHF_IDX_CLASSNAME;
2095 if (len2 > LG_SCALAR)
2096 flags |= SHF_LARGE_STRLEN;
2098 flags |= SHF_HAS_LIST;
2099 if (count > (LG_SCALAR + 1))
2100 flags |= SHF_LARGE_LISTLEN;
2103 * We're ready to emit either serialized form:
2105 * SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
2106 * SX_HOOK <flags> <index> <len2> <str> [<len3> <object-IDs>]
2108 * If we recursed, the SX_HOOK has already been emitted.
2111 TRACEME(("SX_HOOK (recursed=%d) flags=0x%x class=%d len=%d len2=%d len3=%d",
2112 recursed, flags, classnum, len, len2, count-1));
2114 /* SX_HOOK <flags> */
2119 /* <len> <classname> or <index> */
2120 if (flags & SHF_IDX_CLASSNAME) {
2121 if (flags & SHF_LARGE_CLASSLEN)
2124 unsigned char cnum = (unsigned char) classnum;
2128 if (flags & SHF_LARGE_CLASSLEN)
2131 unsigned char clen = (unsigned char) len;
2134 WRITE(class, len); /* Final \0 is omitted */
2137 /* <len2> <frozen-str> */
2138 if (flags & SHF_LARGE_STRLEN)
2141 unsigned char clen = (unsigned char) len2;
2145 WRITE(pv, len2); /* Final \0 is omitted */
2147 /* [<len3> <object-IDs>] */
2148 if (flags & SHF_HAS_LIST) {
2149 int len3 = count - 1;
2150 if (flags & SHF_LARGE_LISTLEN)
2153 unsigned char clen = (unsigned char) len3;
2158 * NOTA BENE, for 64-bit machines: the ary[i] below does not yield a
2159 * real pointer, rather a tag number, well under the 32-bit limit.
2162 for (i = 1; i < count; i++) {
2163 I32 tagval = htonl(LOW_32BITS(ary[i]));
2164 WRITE(&tagval, sizeof(I32));
2165 TRACEME(("object %d, tag #%d", i-1, ntohl(tagval)));
2170 * Free the array. We need extra care for indices after 0, since they
2171 * don't hold real SVs but integers cast.
2175 AvFILLp(av) = 0; /* Cheat, nothing after 0 interests us */
2183 * store_blessed -- dispatched manually, not via sv_store[]
2185 * Check whether there is a STORABLE_xxx hook defined in the class or in one
2186 * of its ancestors. If there is, then redispatch to store_hook();
2188 * Otherwise, the blessed SV is stored using the following layout:
2190 * SX_BLESS <flag> <len> <classname> <object>
2192 * where <flag> indicates whether <len> is stored on 0 or 4 bytes, depending
2193 * on the high-order bit in flag: if 1, then length follows on 4 bytes.
2194 * Otherwise, the low order bits give the length, thereby giving a compact
2195 * representation for class names less than 127 chars long.
2197 * Each <classname> seen is remembered and indexed, so that the next time
2198 * an object in the blessed in the same <classname> is stored, the following
2201 * SX_IX_BLESS <flag> <index> <object>
2203 * where <index> is the classname index, stored on 0 or 4 bytes depending
2204 * on the high-order bit in flag (same encoding as above for <len>).
2206 static int store_blessed(
2217 TRACEME(("store_blessed, type %d, class \"%s\"", type, HvNAME(pkg)));
2220 * Look for a hook for this blessed SV and redirect to store_hook()
2224 hook = pkg_can(cxt->hook, pkg, "STORABLE_freeze");
2226 return store_hook(cxt, sv, type, pkg, hook);
2229 * This is a blessed SV without any serialization hook.
2232 class = HvNAME(pkg);
2233 len = strlen(class);
2235 TRACEME(("blessed 0x%"UVxf" in %s, no hook: tagged #%d",
2236 PTR2UV(sv), class, cxt->tagnum));
2239 * Determine whether it is the first time we see that class name (in which
2240 * case it will be stored in the SX_BLESS form), or whether we already
2241 * saw that class name before (in which case the SX_IX_BLESS form will be
2245 if (known_class(cxt, class, len, &classnum)) {
2246 TRACEME(("already seen class %s, ID = %d", class, classnum));
2247 PUTMARK(SX_IX_BLESS);
2248 if (classnum <= LG_BLESS) {
2249 unsigned char cnum = (unsigned char) classnum;
2252 unsigned char flag = (unsigned char) 0x80;
2257 TRACEME(("first time we see class %s, ID = %d", class, classnum));
2259 if (len <= LG_BLESS) {
2260 unsigned char clen = (unsigned char) len;
2263 unsigned char flag = (unsigned char) 0x80;
2265 WLEN(len); /* Don't BER-encode, this should be rare */
2267 WRITE(class, len); /* Final \0 is omitted */
2271 * Now emit the <object> part.
2274 return SV_STORE(type)(cxt, sv);
2280 * We don't know how to store the item we reached, so return an error condition.
2281 * (it's probably a GLOB, some CODE reference, etc...)
2283 * If they defined the `forgive_me' variable at the Perl level to some
2284 * true value, then don't croak, just warn, and store a placeholder string
2287 static int store_other(stcxt_t *cxt, SV *sv)
2290 static char buf[80];
2292 TRACEME(("store_other"));
2295 * Fetch the value from perl only once per store() operation.
2299 cxt->forgive_me == 0 ||
2300 (cxt->forgive_me < 0 && !(cxt->forgive_me =
2301 SvTRUE(perl_get_sv("Storable::forgive_me", TRUE)) ? 1 : 0))
2303 CROAK(("Can't store %s items", sv_reftype(sv, FALSE)));
2305 warn("Can't store item %s(0x%"UVxf")",
2306 sv_reftype(sv, FALSE), PTR2UV(sv));
2309 * Store placeholder string as a scalar instead...
2312 (void) sprintf(buf, "You lost %s(0x%"UVxf")\0", sv_reftype(sv, FALSE),
2316 STORE_SCALAR(buf, len);
2317 TRACEME(("ok (dummy \"%s\", length = %d)", buf, len));
2323 *** Store driving routines
2329 * WARNING: partially duplicates Perl's sv_reftype for speed.
2331 * Returns the type of the SV, identified by an integer. That integer
2332 * may then be used to index the dynamic routine dispatch table.
2334 static int sv_type(SV *sv)
2336 switch (SvTYPE(sv)) {
2341 * No need to check for ROK, that can't be set here since there
2342 * is no field capable of hodling the xrv_rv reference.
2350 * Starting from SVt_PV, it is possible to have the ROK flag
2351 * set, the pointer to the other SV being either stored in
2352 * the xrv_rv (in the case of a pure SVt_RV), or as the
2353 * xpv_pv field of an SVt_PV and its heirs.
2355 * However, those SV cannot be magical or they would be an
2356 * SVt_PVMG at least.
2358 return SvROK(sv) ? svis_REF : svis_SCALAR;
2360 case SVt_PVLV: /* Workaround for perl5.004_04 "LVALUE" bug */
2361 if (SvRMAGICAL(sv) && (mg_find(sv, 'p')))
2362 return svis_TIED_ITEM;
2365 if (SvRMAGICAL(sv) && (mg_find(sv, 'q')))
2367 return SvROK(sv) ? svis_REF : svis_SCALAR;
2369 if (SvRMAGICAL(sv) && (mg_find(sv, 'P')))
2373 if (SvRMAGICAL(sv) && (mg_find(sv, 'P')))
2386 * Recursively store objects pointed to by the sv to the specified file.
2388 * Layout is <content> or SX_OBJECT <tagnum> if we reach an already stored
2389 * object (one for which storage has started -- it may not be over if we have
2390 * a self-referenced structure). This data set forms a stored <object>.
2392 static int store(stcxt_t *cxt, SV *sv)
2398 HV *hseen = cxt->hseen;
2400 TRACEME(("store (0x%"UVxf")", PTR2UV(sv)));
2403 * If object has already been stored, do not duplicate data.
2404 * Simply emit the SX_OBJECT marker followed by its tag data.
2405 * The tag is always written in network order.
2407 * NOTA BENE, for 64-bit machines: the "*svh" below does not yield a
2408 * real pointer, rather a tag number (watch the insertion code below).
2409 * That means it pobably safe to assume it is well under the 32-bit limit,
2410 * and makes the truncation safe.
2411 * -- RAM, 14/09/1999
2414 svh = hv_fetch(hseen, (char *) &sv, sizeof(sv), FALSE);
2416 I32 tagval = htonl(LOW_32BITS(*svh));
2418 TRACEME(("object 0x%"UVxf" seen as #%d",
2419 PTR2UV(sv), ntohl(tagval)));
2422 WRITE(&tagval, sizeof(I32));
2427 * Allocate a new tag and associate it with the address of the sv being
2428 * stored, before recursing...
2430 * In order to avoid creating new SvIVs to hold the tagnum we just
2431 * cast the tagnum to a SV pointer and store that in the hash. This
2432 * means that we must clean up the hash manually afterwards, but gives
2433 * us a 15% throughput increase.
2438 if (!hv_store(hseen,
2439 (char *) &sv, sizeof(sv), INT2PTR(SV*, cxt->tagnum), 0))
2443 * Store `sv' and everything beneath it, using appropriate routine.
2444 * Abort immediately if we get a non-zero status back.
2449 TRACEME(("storing 0x%"UVxf" tag #%d, type %d...",
2450 PTR2UV(sv), cxt->tagnum, type));
2453 HV *pkg = SvSTASH(sv);
2454 ret = store_blessed(cxt, sv, type, pkg);
2456 ret = SV_STORE(type)(cxt, sv);
2458 TRACEME(("%s (stored 0x%"UVxf", refcnt=%d, %s)",
2459 ret ? "FAILED" : "ok", PTR2UV(sv),
2460 SvREFCNT(sv), sv_reftype(sv, FALSE)));
2468 * Write magic number and system information into the file.
2469 * Layout is <magic> <network> [<len> <byteorder> <sizeof int> <sizeof long>
2470 * <sizeof ptr>] where <len> is the length of the byteorder hexa string.
2471 * All size and lenghts are written as single characters here.
2473 * Note that no byte ordering info is emitted when <network> is true, since
2474 * integers will be emitted in network order in that case.
2476 static int magic_write(stcxt_t *cxt)
2478 char buf[256]; /* Enough room for 256 hexa digits */
2480 int use_network_order = cxt->netorder;
2482 TRACEME(("magic_write on fd=%d", cxt->fio ? fileno(cxt->fio) : -1));
2485 WRITE(magicstr, strlen(magicstr)); /* Don't write final \0 */
2488 * Starting with 0.6, the "use_network_order" byte flag is also used to
2489 * indicate the version number of the binary image, encoded in the upper
2490 * bits. The bit 0 is always used to indicate network order.
2494 ((use_network_order ? 0x1 : 0x0) | (STORABLE_BIN_MAJOR << 1));
2498 * Starting with 0.7, a full byte is dedicated to the minor version of
2499 * the binary format, which is incremented only when new markers are
2500 * introduced, for instance, but when backward compatibility is preserved.
2503 PUTMARK((unsigned char) STORABLE_BIN_MINOR);
2505 if (use_network_order)
2506 return 0; /* Don't bother with byte ordering */
2508 sprintf(buf, "%lx", (unsigned long) BYTEORDER);
2509 c = (unsigned char) strlen(buf);
2511 WRITE(buf, (unsigned int) c); /* Don't write final \0 */
2512 PUTMARK((unsigned char) sizeof(int));
2513 PUTMARK((unsigned char) sizeof(long));
2514 PUTMARK((unsigned char) sizeof(char *));
2516 TRACEME(("ok (magic_write byteorder = 0x%lx [%d], I%d L%d P%d)",
2517 (unsigned long) BYTEORDER, (int) c,
2518 sizeof(int), sizeof(long), sizeof(char *)));
2526 * Common code for store operations.
2528 * When memory store is requested (f = NULL) and a non null SV* is given in
2529 * `res', it is filled with a new SV created out of the memory buffer.
2531 * It is required to provide a non-null `res' when the operation type is not
2532 * dclone() and store() is performed to memory.
2534 static int do_store(
2544 ASSERT(!(f == 0 && !(optype & ST_CLONE)) || res,
2545 ("must supply result SV pointer for real recursion to memory"));
2547 TRACEME(("do_store (optype=%d, netorder=%d)",
2548 optype, network_order));
2553 * Workaround for CROAK leak: if they enter with a "dirty" context,
2554 * free up memory for them now.
2561 * Now that STORABLE_xxx hooks exist, it is possible that they try to
2562 * re-enter store() via the hooks. We need to stack contexts.
2566 cxt = allocate_context(cxt);
2570 ASSERT(cxt->entry == 1, ("starting new recursion"));
2571 ASSERT(!cxt->dirty, ("clean context"));
2574 * Ensure sv is actually a reference. From perl, we called something
2576 * pstore(FILE, \@array);
2577 * so we must get the scalar value behing that reference.
2581 CROAK(("Not a reference"));
2582 sv = SvRV(sv); /* So follow it to know what to store */
2585 * If we're going to store to memory, reset the buffer.
2592 * Prepare context and emit headers.
2595 init_store_context(cxt, f, optype, network_order);
2597 if (-1 == magic_write(cxt)) /* Emit magic and ILP info */
2598 return 0; /* Error */
2601 * Recursively store object...
2604 ASSERT(is_storing(), ("within store operation"));
2606 status = store(cxt, sv); /* Just do it! */
2609 * If they asked for a memory store and they provided an SV pointer,
2610 * make an SV string out of the buffer and fill their pointer.
2612 * When asking for ST_REAL, it's MANDATORY for the caller to provide
2613 * an SV, since context cleanup might free the buffer if we did recurse.
2614 * (unless caller is dclone(), which is aware of that).
2617 if (!cxt->fio && res)
2623 * The "root" context is never freed, since it is meant to be always
2624 * handy for the common case where no recursion occurs at all (i.e.
2625 * we enter store() outside of any Storable code and leave it, period).
2626 * We know it's the "root" context because there's nothing stacked
2631 * When deep cloning, we don't free the context: doing so would force
2632 * us to copy the data in the memory buffer. Sicne we know we're
2633 * about to enter do_retrieve...
2636 clean_store_context(cxt);
2637 if (cxt->prev && !(cxt->optype & ST_CLONE))
2640 TRACEME(("do_store returns %d", status));
2648 * Store the transitive data closure of given object to disk.
2649 * Returns 0 on error, a true value otherwise.
2651 int pstore(PerlIO *f, SV *sv)
2653 TRACEME(("pstore"));
2654 return do_store(f, sv, 0, FALSE, (SV**) 0);
2661 * Same as pstore(), but network order is used for integers and doubles are
2662 * emitted as strings.
2664 int net_pstore(PerlIO *f, SV *sv)
2666 TRACEME(("net_pstore"));
2667 return do_store(f, sv, 0, TRUE, (SV**) 0);
2677 * Build a new SV out of the content of the internal memory buffer.
2679 static SV *mbuf2sv(void)
2683 return newSVpv(mbase, MBUF_SIZE());
2689 * Store the transitive data closure of given object to memory.
2690 * Returns undef on error, a scalar value containing the data otherwise.
2697 TRACEME(("mstore"));
2699 if (!do_store((PerlIO*) 0, sv, 0, FALSE, &out))
2700 return &PL_sv_undef;
2708 * Same as mstore(), but network order is used for integers and doubles are
2709 * emitted as strings.
2711 SV *net_mstore(SV *sv)
2716 TRACEME(("net_mstore"));
2718 if (!do_store((PerlIO*) 0, sv, 0, TRUE, &out))
2719 return &PL_sv_undef;
2725 *** Specific retrieve callbacks.
2731 * Return an error via croak, since it is not possible that we get here
2732 * under normal conditions, when facing a file produced via pstore().
2734 static SV *retrieve_other(stcxt_t *cxt)
2737 cxt->ver_major != STORABLE_BIN_MAJOR &&
2738 cxt->ver_minor != STORABLE_BIN_MINOR
2740 CROAK(("Corrupted storable %s (binary v%d.%d), current is v%d.%d",
2741 cxt->fio ? "file" : "string",
2742 cxt->ver_major, cxt->ver_minor,
2743 STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR));
2745 CROAK(("Corrupted storable %s (binary v%d.%d)",
2746 cxt->fio ? "file" : "string",
2747 cxt->ver_major, cxt->ver_minor));
2750 return (SV *) 0; /* Just in case */
2754 * retrieve_idx_blessed
2756 * Layout is SX_IX_BLESS <index> <object> with SX_IX_BLESS already read.
2757 * <index> can be coded on either 1 or 5 bytes.
2759 static SV *retrieve_idx_blessed(stcxt_t *cxt)
2766 TRACEME(("retrieve_idx_blessed (#%d)", cxt->tagnum));
2768 GETMARK(idx); /* Index coded on a single char? */
2773 * Fetch classname in `aclass'
2776 sva = av_fetch(cxt->aclass, idx, FALSE);
2778 CROAK(("Class name #%d should have been seen already", idx));
2780 class = SvPVX(*sva); /* We know it's a PV, by construction */
2782 TRACEME(("class ID %d => %s", idx, class));
2785 * Retrieve object and bless it.
2798 * Layout is SX_BLESS <len> <classname> <object> with SX_BLESS already read.
2799 * <len> can be coded on either 1 or 5 bytes.
2801 static SV *retrieve_blessed(stcxt_t *cxt)
2805 char buf[LG_BLESS + 1]; /* Avoid malloc() if possible */
2808 TRACEME(("retrieve_blessed (#%d)", cxt->tagnum));
2811 * Decode class name length and read that name.
2813 * Short classnames have two advantages: their length is stored on one
2814 * single byte, and the string can be read on the stack.
2817 GETMARK(len); /* Length coded on a single char? */
2820 TRACEME(("** allocating %d bytes for class name", len+1));
2821 New(10003, class, len+1, char);
2824 class[len] = '\0'; /* Mark string end */
2827 * It's a new classname, otherwise it would have been an SX_IX_BLESS.
2830 if (!av_store(cxt->aclass, cxt->classnum++, newSVpvn(class, len)))
2834 * Retrieve object and bless it.
2850 * Layout: SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
2851 * with leading mark already read, as usual.
2853 * When recursion was involved during serialization of the object, there
2854 * is an unknown amount of serialized objects after the SX_HOOK mark. Until
2855 * we reach a <flags> marker with the recursion bit cleared.
2857 static SV *retrieve_hook(stcxt_t *cxt)
2860 char buf[LG_BLESS + 1]; /* Avoid malloc() if possible */
2872 int clone = cxt->optype & ST_CLONE;
2874 TRACEME(("retrieve_hook (#%d)", cxt->tagnum));
2877 * Read flags, which tell us about the type, and whether we need to recurse.
2883 * Create the (empty) object, and mark it as seen.
2885 * This must be done now, because tags are incremented, and during
2886 * serialization, the object tag was affected before recursion could
2890 obj_type = flags & SHF_TYPE_MASK;
2896 sv = (SV *) newAV();
2899 sv = (SV *) newHV();
2902 return retrieve_other(cxt); /* Let it croak */
2907 * Whilst flags tell us to recurse, do so.
2909 * We don't need to remember the addresses returned by retrieval, because
2910 * all the references will be obtained through indirection via the object
2911 * tags in the object-ID list.
2914 while (flags & SHF_NEED_RECURSE) {
2915 TRACEME(("retrieve_hook recursing..."));
2919 TRACEME(("retrieve_hook back with rv=0x%"UVxf,
2924 if (flags & SHF_IDX_CLASSNAME) {
2929 * Fetch index from `aclass'
2932 if (flags & SHF_LARGE_CLASSLEN)
2937 sva = av_fetch(cxt->aclass, idx, FALSE);
2939 CROAK(("Class name #%d should have been seen already", idx));
2941 class = SvPVX(*sva); /* We know it's a PV, by construction */
2942 TRACEME(("class ID %d => %s", idx, class));
2946 * Decode class name length and read that name.
2948 * NOTA BENE: even if the length is stored on one byte, we don't read
2949 * on the stack. Just like retrieve_blessed(), we limit the name to
2950 * LG_BLESS bytes. This is an arbitrary decision.
2953 if (flags & SHF_LARGE_CLASSLEN)
2958 if (len > LG_BLESS) {
2959 TRACEME(("** allocating %d bytes for class name", len+1));
2960 New(10003, class, len+1, char);
2964 class[len] = '\0'; /* Mark string end */
2967 * Record new classname.
2970 if (!av_store(cxt->aclass, cxt->classnum++, newSVpvn(class, len)))
2974 TRACEME(("class name: %s", class));
2977 * Decode user-frozen string length and read it in a SV.
2979 * For efficiency reasons, we read data directly into the SV buffer.
2980 * To understand that code, read retrieve_scalar()
2983 if (flags & SHF_LARGE_STRLEN)
2988 frozen = NEWSV(10002, len2);
2990 SAFEREAD(SvPVX(frozen), len2, frozen);
2991 SvCUR_set(frozen, len2);
2992 *SvEND(frozen) = '\0';
2994 (void) SvPOK_only(frozen); /* Validates string pointer */
2997 TRACEME(("frozen string: %d bytes", len2));
3000 * Decode object-ID list length, if present.
3003 if (flags & SHF_HAS_LIST) {
3004 if (flags & SHF_LARGE_LISTLEN)
3010 av_extend(av, len3 + 1); /* Leave room for [0] */
3011 AvFILLp(av) = len3; /* About to be filled anyway */
3015 TRACEME(("has %d object IDs to link", len3));
3018 * Read object-ID list into array.
3019 * Because we pre-extended it, we can cheat and fill it manually.
3021 * We read object tags and we can convert them into SV* on the fly
3022 * because we know all the references listed in there (as tags)
3023 * have been already serialized, hence we have a valid correspondance
3024 * between each of those tags and the recreated SV.
3028 SV **ary = AvARRAY(av);
3030 for (i = 1; i <= len3; i++) { /* We leave [0] alone */
3035 READ(&tag, sizeof(I32));
3037 svh = av_fetch(cxt->aseen, tag, FALSE);
3039 CROAK(("Object #%d should have been retrieved already", tag));
3041 ary[i] = SvREFCNT_inc(xsv);
3046 * Bless the object and look up the STORABLE_thaw hook.
3050 hook = pkg_can(cxt->hook, SvSTASH(sv), "STORABLE_thaw");
3052 CROAK(("No STORABLE_thaw defined for objects of class %s", class));
3055 * If we don't have an `av' yet, prepare one.
3056 * Then insert the frozen string as item [0].
3064 AvARRAY(av)[0] = SvREFCNT_inc(frozen);
3069 * $object->STORABLE_thaw($cloning, $frozen, @refs);
3071 * where $object is our blessed (empty) object, $cloning is a boolean
3072 * telling whether we're running a deep clone, $frozen is the frozen
3073 * string the user gave us in his serializing hook, and @refs, which may
3074 * be empty, is the list of extra references he returned along for us
3077 * In effect, the hook is an alternate creation routine for the class,
3078 * the object itself being already created by the runtime.
3081 TRACEME(("calling STORABLE_thaw on %s at 0x%"UVxf" (%d args)",
3082 class, PTR2UV(sv), AvFILLp(av) + 1));
3085 (void) scalar_call(rv, hook, clone, av, G_SCALAR|G_DISCARD);
3092 SvREFCNT_dec(frozen);
3095 if (!(flags & SHF_IDX_CLASSNAME) && class != buf)
3104 * Retrieve reference to some other scalar.
3105 * Layout is SX_REF <object>, with SX_REF already read.
3107 static SV *retrieve_ref(stcxt_t *cxt)
3112 TRACEME(("retrieve_ref (#%d)", cxt->tagnum));
3115 * We need to create the SV that holds the reference to the yet-to-retrieve
3116 * object now, so that we may record the address in the seen table.
3117 * Otherwise, if the object to retrieve references us, we won't be able
3118 * to resolve the SX_OBJECT we'll see at that point! Hence we cannot
3119 * do the retrieve first and use rv = newRV(sv) since it will be too late
3120 * for SEEN() recording.
3123 rv = NEWSV(10002, 0);
3124 SEEN(rv); /* Will return if rv is null */
3125 sv = retrieve(cxt); /* Retrieve <object> */
3127 return (SV *) 0; /* Failed */
3130 * WARNING: breaks RV encapsulation.
3132 * Now for the tricky part. We have to upgrade our existing SV, so that
3133 * it is now an RV on sv... Again, we cheat by duplicating the code
3134 * held in newSVrv(), since we already got our SV from retrieve().
3138 * SvRV(rv) = SvREFCNT_inc(sv);
3140 * here because the reference count we got from retrieve() above is
3141 * already correct: if the object was retrieved from the file, then
3142 * its reference count is one. Otherwise, if it was retrieved via
3143 * an SX_OBJECT indication, a ref count increment was done.
3146 sv_upgrade(rv, SVt_RV);
3147 SvRV(rv) = sv; /* $rv = \$sv */
3150 TRACEME(("ok (retrieve_ref at 0x%"UVxf")", PTR2UV(rv)));
3156 * retrieve_overloaded
3158 * Retrieve reference to some other scalar with overloading.
3159 * Layout is SX_OVERLOAD <object>, with SX_OVERLOAD already read.
3161 static SV *retrieve_overloaded(stcxt_t *cxt)
3167 TRACEME(("retrieve_overloaded (#%d)", cxt->tagnum));
3170 * Same code as retrieve_ref(), duplicated to avoid extra call.
3173 rv = NEWSV(10002, 0);
3174 SEEN(rv); /* Will return if rv is null */
3175 sv = retrieve(cxt); /* Retrieve <object> */
3177 return (SV *) 0; /* Failed */
3180 * WARNING: breaks RV encapsulation.
3183 sv_upgrade(rv, SVt_RV);
3184 SvRV(rv) = sv; /* $rv = \$sv */
3188 * Restore overloading magic.
3191 stash = (HV *) SvSTASH (sv);
3192 if (!stash || !Gv_AMG(stash))
3193 CROAK(("Cannot restore overloading on %s(0x%"UVxf")",
3194 sv_reftype(sv, FALSE),
3199 TRACEME(("ok (retrieve_overloaded at 0x%"UVxf")", PTR2UV(rv)));
3205 * retrieve_tied_array
3207 * Retrieve tied array
3208 * Layout is SX_TIED_ARRAY <object>, with SX_TIED_ARRAY already read.
3210 static SV *retrieve_tied_array(stcxt_t *cxt)
3215 TRACEME(("retrieve_tied_array (#%d)", cxt->tagnum));
3217 tv = NEWSV(10002, 0);
3218 SEEN(tv); /* Will return if tv is null */
3219 sv = retrieve(cxt); /* Retrieve <object> */
3221 return (SV *) 0; /* Failed */
3223 sv_upgrade(tv, SVt_PVAV);
3224 AvREAL_off((AV *)tv);
3225 sv_magic(tv, sv, 'P', Nullch, 0);
3226 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
3228 TRACEME(("ok (retrieve_tied_array at 0x%"UVxf")", PTR2UV(tv)));
3234 * retrieve_tied_hash
3236 * Retrieve tied hash
3237 * Layout is SX_TIED_HASH <object>, with SX_TIED_HASH already read.
3239 static SV *retrieve_tied_hash(stcxt_t *cxt)
3244 TRACEME(("retrieve_tied_hash (#%d)", cxt->tagnum));
3246 tv = NEWSV(10002, 0);
3247 SEEN(tv); /* Will return if tv is null */
3248 sv = retrieve(cxt); /* Retrieve <object> */
3250 return (SV *) 0; /* Failed */
3252 sv_upgrade(tv, SVt_PVHV);
3253 sv_magic(tv, sv, 'P', Nullch, 0);
3254 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
3256 TRACEME(("ok (retrieve_tied_hash at 0x%"UVxf")", PTR2UV(tv)));
3262 * retrieve_tied_scalar
3264 * Retrieve tied scalar
3265 * Layout is SX_TIED_SCALAR <object>, with SX_TIED_SCALAR already read.
3267 static SV *retrieve_tied_scalar(cxt)
3273 TRACEME(("retrieve_tied_scalar (#%d)", cxt->tagnum));
3275 tv = NEWSV(10002, 0);
3276 SEEN(tv); /* Will return if rv is null */
3277 sv = retrieve(cxt); /* Retrieve <object> */
3279 return (SV *) 0; /* Failed */
3281 sv_upgrade(tv, SVt_PVMG);
3282 sv_magic(tv, sv, 'q', Nullch, 0);
3283 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
3285 TRACEME(("ok (retrieve_tied_scalar at 0x%"UVxf")", PTR2UV(tv)));
3293 * Retrieve reference to value in a tied hash.
3294 * Layout is SX_TIED_KEY <object> <key>, with SX_TIED_KEY already read.
3296 static SV *retrieve_tied_key(stcxt_t *cxt)
3302 TRACEME(("retrieve_tied_key (#%d)", cxt->tagnum));
3304 tv = NEWSV(10002, 0);
3305 SEEN(tv); /* Will return if tv is null */
3306 sv = retrieve(cxt); /* Retrieve <object> */
3308 return (SV *) 0; /* Failed */
3310 key = retrieve(cxt); /* Retrieve <key> */
3312 return (SV *) 0; /* Failed */
3314 sv_upgrade(tv, SVt_PVMG);
3315 sv_magic(tv, sv, 'p', (char *)key, HEf_SVKEY);
3316 SvREFCNT_dec(key); /* Undo refcnt inc from sv_magic() */
3317 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
3325 * Retrieve reference to value in a tied array.
3326 * Layout is SX_TIED_IDX <object> <idx>, with SX_TIED_IDX already read.
3328 static SV *retrieve_tied_idx(stcxt_t *cxt)
3334 TRACEME(("retrieve_tied_idx (#%d)", cxt->tagnum));
3336 tv = NEWSV(10002, 0);
3337 SEEN(tv); /* Will return if tv is null */
3338 sv = retrieve(cxt); /* Retrieve <object> */
3340 return (SV *) 0; /* Failed */
3342 RLEN(idx); /* Retrieve <idx> */
3344 sv_upgrade(tv, SVt_PVMG);
3345 sv_magic(tv, sv, 'p', Nullch, idx);
3346 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
3355 * Retrieve defined long (string) scalar.
3357 * Layout is SX_LSCALAR <length> <data>, with SX_LSCALAR already read.
3358 * The scalar is "long" in that <length> is larger than LG_SCALAR so it
3359 * was not stored on a single byte.
3361 static SV *retrieve_lscalar(stcxt_t *cxt)
3367 TRACEME(("retrieve_lscalar (#%d), len = %d", cxt->tagnum, len));
3370 * Allocate an empty scalar of the suitable length.
3373 sv = NEWSV(10002, len);
3374 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3377 * WARNING: duplicates parts of sv_setpv and breaks SV data encapsulation.
3379 * Now, for efficiency reasons, read data directly inside the SV buffer,
3380 * and perform the SV final settings directly by duplicating the final
3381 * work done by sv_setpv. Since we're going to allocate lots of scalars
3382 * this way, it's worth the hassle and risk.
3385 SAFEREAD(SvPVX(sv), len, sv);
3386 SvCUR_set(sv, len); /* Record C string length */
3387 *SvEND(sv) = '\0'; /* Ensure it's null terminated anyway */
3388 (void) SvPOK_only(sv); /* Validate string pointer */
3389 SvTAINT(sv); /* External data cannot be trusted */
3391 TRACEME(("large scalar len %d '%s'", len, SvPVX(sv)));
3392 TRACEME(("ok (retrieve_lscalar at 0x%"UVxf")", PTR2UV(sv)));
3400 * Retrieve defined short (string) scalar.
3402 * Layout is SX_SCALAR <length> <data>, with SX_SCALAR already read.
3403 * The scalar is "short" so <length> is single byte. If it is 0, there
3404 * is no <data> section.
3406 static SV *retrieve_scalar(stcxt_t *cxt)
3412 TRACEME(("retrieve_scalar (#%d), len = %d", cxt->tagnum, len));
3415 * Allocate an empty scalar of the suitable length.
3418 sv = NEWSV(10002, len);
3419 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3422 * WARNING: duplicates parts of sv_setpv and breaks SV data encapsulation.
3427 * newSV did not upgrade to SVt_PV so the scalar is undefined.
3428 * To make it defined with an empty length, upgrade it now...
3430 sv_upgrade(sv, SVt_PV);
3432 *SvEND(sv) = '\0'; /* Ensure it's null terminated anyway */
3433 TRACEME(("ok (retrieve_scalar empty at 0x%"UVxf")", PTR2UV(sv)));
3436 * Now, for efficiency reasons, read data directly inside the SV buffer,
3437 * and perform the SV final settings directly by duplicating the final
3438 * work done by sv_setpv. Since we're going to allocate lots of scalars
3439 * this way, it's worth the hassle and risk.
3441 SAFEREAD(SvPVX(sv), len, sv);
3442 SvCUR_set(sv, len); /* Record C string length */
3443 *SvEND(sv) = '\0'; /* Ensure it's null terminated anyway */
3444 TRACEME(("small scalar len %d '%s'", len, SvPVX(sv)));
3447 (void) SvPOK_only(sv); /* Validate string pointer */
3448 SvTAINT(sv); /* External data cannot be trusted */
3450 TRACEME(("ok (retrieve_scalar at 0x%"UVxf")", PTR2UV(sv)));
3457 * Retrieve defined integer.
3458 * Layout is SX_INTEGER <data>, whith SX_INTEGER already read.
3460 static SV *retrieve_integer(stcxt_t *cxt)
3465 TRACEME(("retrieve_integer (#%d)", cxt->tagnum));
3467 READ(&iv, sizeof(iv));
3469 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3471 TRACEME(("integer %d", iv));
3472 TRACEME(("ok (retrieve_integer at 0x%"UVxf")", PTR2UV(sv)));
3480 * Retrieve defined integer in network order.
3481 * Layout is SX_NETINT <data>, whith SX_NETINT already read.
3483 static SV *retrieve_netint(stcxt_t *cxt)
3488 TRACEME(("retrieve_netint (#%d)", cxt->tagnum));
3490 READ(&iv, sizeof(iv));
3492 sv = newSViv((int) ntohl(iv));
3493 TRACEME(("network integer %d", (int) ntohl(iv)));
3496 TRACEME(("network integer (as-is) %d", iv));
3498 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3500 TRACEME(("ok (retrieve_netint at 0x%"UVxf")", PTR2UV(sv)));
3508 * Retrieve defined double.
3509 * Layout is SX_DOUBLE <data>, whith SX_DOUBLE already read.
3511 static SV *retrieve_double(stcxt_t *cxt)
3516 TRACEME(("retrieve_double (#%d)", cxt->tagnum));
3518 READ(&nv, sizeof(nv));
3520 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3522 TRACEME(("double %"NVff, nv));
3523 TRACEME(("ok (retrieve_double at 0x%"UVxf")", PTR2UV(sv)));
3531 * Retrieve defined byte (small integer within the [-128, +127] range).
3532 * Layout is SX_BYTE <data>, whith SX_BYTE already read.
3534 static SV *retrieve_byte(stcxt_t *cxt)
3539 TRACEME(("retrieve_byte (#%d)", cxt->tagnum));
3542 TRACEME(("small integer read as %d", (unsigned char) siv));
3543 sv = newSViv((unsigned char) siv - 128);
3544 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3546 TRACEME(("byte %d", (unsigned char) siv - 128));
3547 TRACEME(("ok (retrieve_byte at 0x%"UVxf")", PTR2UV(sv)));
3555 * Return the undefined value.
3557 static SV *retrieve_undef(stcxt_t *cxt)
3561 TRACEME(("retrieve_undef"));
3572 * Return the immortal undefined value.
3574 static SV *retrieve_sv_undef(stcxt_t *cxt)
3576 SV *sv = &PL_sv_undef;
3578 TRACEME(("retrieve_sv_undef"));
3587 * Return the immortal yes value.
3589 static SV *retrieve_sv_yes(stcxt_t *cxt)
3591 SV *sv = &PL_sv_yes;
3593 TRACEME(("retrieve_sv_yes"));
3602 * Return the immortal no value.
3604 static SV *retrieve_sv_no(stcxt_t *cxt)
3608 TRACEME(("retrieve_sv_no"));
3617 * Retrieve a whole array.
3618 * Layout is SX_ARRAY <size> followed by each item, in increading index order.
3619 * Each item is stored as <object>.
3621 * When we come here, SX_ARRAY has been read already.
3623 static SV *retrieve_array(stcxt_t *cxt)
3630 TRACEME(("retrieve_array (#%d)", cxt->tagnum));
3633 * Read length, and allocate array, then pre-extend it.
3637 TRACEME(("size = %d", len));
3639 SEEN(av); /* Will return if array not allocated nicely */
3643 return (SV *) av; /* No data follow if array is empty */
3646 * Now get each item in turn...
3649 for (i = 0; i < len; i++) {
3650 TRACEME(("(#%d) item", i));
3651 sv = retrieve(cxt); /* Retrieve item */
3654 if (av_store(av, i, sv) == 0)
3658 TRACEME(("ok (retrieve_array at 0x%"UVxf")", PTR2UV(av)));
3666 * Retrieve a whole hash table.
3667 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
3668 * Keys are stored as <length> <data>, the <data> section being omitted
3670 * Values are stored as <object>.
3672 * When we come here, SX_HASH has been read already.
3674 static SV *retrieve_hash(stcxt_t *cxt)
3681 static SV *sv_h_undef = (SV *) 0; /* hv_store() bug */
3683 TRACEME(("retrieve_hash (#%d)", cxt->tagnum));
3686 * Read length, allocate table.
3690 TRACEME(("size = %d", len));
3692 SEEN(hv); /* Will return if table not allocated properly */
3694 return (SV *) hv; /* No data follow if table empty */
3697 * Now get each key/value pair in turn...
3700 for (i = 0; i < len; i++) {
3705 TRACEME(("(#%d) value", i));
3712 * Since we're reading into kbuf, we must ensure we're not
3713 * recursing between the read and the hv_store() where it's used.
3714 * Hence the key comes after the value.
3717 RLEN(size); /* Get key size */
3718 KBUFCHK(size); /* Grow hash key read pool if needed */
3721 kbuf[size] = '\0'; /* Mark string end, just in case */
3722 TRACEME(("(#%d) key '%s'", i, kbuf));
3725 * Enter key/value pair into hash table.
3728 if (hv_store(hv, kbuf, (U32) size, sv, 0) == 0)
3732 TRACEME(("ok (retrieve_hash at 0x%"UVxf")", PTR2UV(hv)));
3738 * old_retrieve_array
3740 * Retrieve a whole array in pre-0.6 binary format.
3742 * Layout is SX_ARRAY <size> followed by each item, in increading index order.
3743 * Each item is stored as SX_ITEM <object> or SX_IT_UNDEF for "holes".
3745 * When we come here, SX_ARRAY has been read already.
3747 static SV *old_retrieve_array(stcxt_t *cxt)
3755 TRACEME(("old_retrieve_array (#%d)", cxt->tagnum));
3758 * Read length, and allocate array, then pre-extend it.
3762 TRACEME(("size = %d", len));
3764 SEEN(av); /* Will return if array not allocated nicely */
3768 return (SV *) av; /* No data follow if array is empty */
3771 * Now get each item in turn...
3774 for (i = 0; i < len; i++) {
3776 if (c == SX_IT_UNDEF) {
3777 TRACEME(("(#%d) undef item", i));
3778 continue; /* av_extend() already filled us with undef */
3781 (void) retrieve_other((stcxt_t *) 0); /* Will croak out */
3782 TRACEME(("(#%d) item", i));
3783 sv = retrieve(cxt); /* Retrieve item */
3786 if (av_store(av, i, sv) == 0)
3790 TRACEME(("ok (old_retrieve_array at 0x%"UVxf")", PTR2UV(av)));
3798 * Retrieve a whole hash table in pre-0.6 binary format.
3800 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
3801 * Keys are stored as SX_KEY <length> <data>, the <data> section being omitted
3803 * Values are stored as SX_VALUE <object> or SX_VL_UNDEF for "holes".
3805 * When we come here, SX_HASH has been read already.
3807 static SV *old_retrieve_hash(stcxt_t *cxt)
3815 static SV *sv_h_undef = (SV *) 0; /* hv_store() bug */
3817 TRACEME(("old_retrieve_hash (#%d)", cxt->tagnum));
3820 * Read length, allocate table.
3824 TRACEME(("size = %d", len));
3826 SEEN(hv); /* Will return if table not allocated properly */
3828 return (SV *) hv; /* No data follow if table empty */
3831 * Now get each key/value pair in turn...
3834 for (i = 0; i < len; i++) {
3840 if (c == SX_VL_UNDEF) {
3841 TRACEME(("(#%d) undef value", i));
3843 * Due to a bug in hv_store(), it's not possible to pass
3844 * &PL_sv_undef to hv_store() as a value, otherwise the
3845 * associated key will not be creatable any more. -- RAM, 14/01/97
3848 sv_h_undef = newSVsv(&PL_sv_undef);
3849 sv = SvREFCNT_inc(sv_h_undef);
3850 } else if (c == SX_VALUE) {
3851 TRACEME(("(#%d) value", i));
3856 (void) retrieve_other((stcxt_t *) 0); /* Will croak out */
3860 * Since we're reading into kbuf, we must ensure we're not
3861 * recursing between the read and the hv_store() where it's used.
3862 * Hence the key comes after the value.
3867 (void) retrieve_other((stcxt_t *) 0); /* Will croak out */
3868 RLEN(size); /* Get key size */
3869 KBUFCHK(size); /* Grow hash key read pool if needed */
3872 kbuf[size] = '\0'; /* Mark string end, just in case */
3873 TRACEME(("(#%d) key '%s'", i, kbuf));
3876 * Enter key/value pair into hash table.
3879 if (hv_store(hv, kbuf, (U32) size, sv, 0) == 0)
3883 TRACEME(("ok (retrieve_hash at 0x%"UVxf")", PTR2UV(hv)));
3889 *** Retrieval engine.
3895 * Make sure the stored data we're trying to retrieve has been produced
3896 * on an ILP compatible system with the same byteorder. It croaks out in
3897 * case an error is detected. [ILP = integer-long-pointer sizes]
3898 * Returns null if error is detected, &PL_sv_undef otherwise.
3900 * Note that there's no byte ordering info emitted when network order was
3901 * used at store time.
3903 static SV *magic_check(stcxt_t *cxt)
3906 char byteorder[256];
3908 int use_network_order;
3910 int version_minor = 0;
3912 TRACEME(("magic_check"));
3915 * The "magic number" is only for files, not when freezing in memory.
3919 STRLEN len = sizeof(magicstr) - 1;
3922 READ(buf, len); /* Not null-terminated */
3923 buf[len] = '\0'; /* Is now */
3925 if (0 == strcmp(buf, magicstr))
3929 * Try to read more bytes to check for the old magic number, which
3933 old_len = sizeof(old_magicstr) - 1;
3934 READ(&buf[len], old_len - len);
3935 buf[old_len] = '\0'; /* Is now null-terminated */
3937 if (strcmp(buf, old_magicstr))
3938 CROAK(("File is not a perl storable"));
3943 * Starting with 0.6, the "use_network_order" byte flag is also used to
3944 * indicate the version number of the binary, and therefore governs the
3945 * setting of sv_retrieve_vtbl. See magic_write().
3948 GETMARK(use_network_order);
3949 version_major = use_network_order >> 1;
3950 cxt->retrieve_vtbl = version_major ? sv_retrieve : sv_old_retrieve;
3952 TRACEME(("magic_check: netorder = 0x%x", use_network_order));
3956 * Starting with 0.7 (binary major 2), a full byte is dedicated to the
3957 * minor version of the protocol. See magic_write().
3960 if (version_major > 1)
3961 GETMARK(version_minor);
3963 cxt->ver_major = version_major;
3964 cxt->ver_minor = version_minor;
3966 TRACEME(("binary image version is %d.%d", version_major, version_minor));
3969 * Inter-operability sanity check: we can't retrieve something stored
3970 * using a format more recent than ours, because we have no way to
3971 * know what has changed, and letting retrieval go would mean a probable
3972 * failure reporting a "corrupted" storable file.
3976 version_major > STORABLE_BIN_MAJOR ||
3977 (version_major == STORABLE_BIN_MAJOR &&
3978 version_minor > STORABLE_BIN_MINOR)
3980 CROAK(("Storable binary image v%d.%d more recent than I am (v%d.%d)",
3981 version_major, version_minor,
3982 STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR));
3985 * If they stored using network order, there's no byte ordering
3986 * information to check.
3989 if (cxt->netorder = (use_network_order & 0x1))
3990 return &PL_sv_undef; /* No byte ordering info */
3992 sprintf(byteorder, "%lx", (unsigned long) BYTEORDER);
3994 READ(buf, c); /* Not null-terminated */
3995 buf[c] = '\0'; /* Is now */
3997 if (strcmp(buf, byteorder))
3998 CROAK(("Byte order is not compatible"));
4000 GETMARK(c); /* sizeof(int) */
4001 if ((int) c != sizeof(int))
4002 CROAK(("Integer size is not compatible"));
4004 GETMARK(c); /* sizeof(long) */
4005 if ((int) c != sizeof(long))
4006 CROAK(("Long integer size is not compatible"));
4008 GETMARK(c); /* sizeof(char *) */
4009 if ((int) c != sizeof(char *))
4010 CROAK(("Pointer integer size is not compatible"));
4012 return &PL_sv_undef; /* OK */
4018 * Recursively retrieve objects from the specified file and return their
4019 * root SV (which may be an AV or an HV for what we care).
4020 * Returns null if there is a problem.
4022 static SV *retrieve(stcxt_t *cxt)
4028 TRACEME(("retrieve"));
4031 * Grab address tag which identifies the object if we are retrieving
4032 * an older format. Since the new binary format counts objects and no
4033 * longer explicitely tags them, we must keep track of the correspondance
4036 * The following section will disappear one day when the old format is
4037 * no longer supported, hence the final "goto" in the "if" block.
4040 if (cxt->hseen) { /* Retrieving old binary */
4042 if (cxt->netorder) {
4044 READ(&nettag, sizeof(I32)); /* Ordered sequence of I32 */
4045 tag = (stag_t) nettag;
4047 READ(&tag, sizeof(stag_t)); /* Original address of the SV */
4050 if (type == SX_OBJECT) {
4052 svh = hv_fetch(cxt->hseen, (char *) &tag, sizeof(tag), FALSE);
4054 CROAK(("Old tag 0x%x should have been mapped already", tag));
4055 tagn = SvIV(*svh); /* Mapped tag number computed earlier below */
4058 * The following code is common with the SX_OBJECT case below.
4061 svh = av_fetch(cxt->aseen, tagn, FALSE);
4063 CROAK(("Object #%d should have been retrieved already", tagn));
4065 TRACEME(("has retrieved #%d at 0x%"UVxf, tagn, PTR2UV(sv)));
4066 SvREFCNT_inc(sv); /* One more reference to this same sv */
4067 return sv; /* The SV pointer where object was retrieved */
4071 * Map new object, but don't increase tagnum. This will be done
4072 * by each of the retrieve_* functions when they call SEEN().
4074 * The mapping associates the "tag" initially present with a unique
4075 * tag number. See test for SX_OBJECT above to see how this is perused.
4078 if (!hv_store(cxt->hseen, (char *) &tag, sizeof(tag),
4079 newSViv(cxt->tagnum), 0))
4086 * Regular post-0.6 binary format.
4092 TRACEME(("retrieve type = %d", type));
4095 * Are we dealing with an object we should have already retrieved?
4098 if (type == SX_OBJECT) {
4100 READ(&tag, sizeof(I32));
4102 svh = av_fetch(cxt->aseen, tag, FALSE);
4104 CROAK(("Object #%d should have been retrieved already", tag));
4106 TRACEME(("had retrieved #%d at 0x%"UVxf, tag, PTR2UV(sv)));
4107 SvREFCNT_inc(sv); /* One more reference to this same sv */
4108 return sv; /* The SV pointer where object was retrieved */
4111 first_time: /* Will disappear when support for old format is dropped */
4114 * Okay, first time through for this one.
4117 sv = RETRIEVE(cxt, type)(cxt);
4119 return (SV *) 0; /* Failed */
4122 * Old binary formats (pre-0.7).
4124 * Final notifications, ended by SX_STORED may now follow.
4125 * Currently, the only pertinent notification to apply on the
4126 * freshly retrieved object is either:
4127 * SX_CLASS <char-len> <classname> for short classnames.
4128 * SX_LG_CLASS <int-len> <classname> for larger one (rare!).
4129 * Class name is then read into the key buffer pool used by
4130 * hash table key retrieval.
4133 if (cxt->ver_major < 2) {
4134 while ((type = GETCHAR()) != SX_STORED) {
4138 GETMARK(len); /* Length coded on a single char */
4140 case SX_LG_CLASS: /* Length coded on a regular integer */
4145 return (SV *) 0; /* Failed */
4147 KBUFCHK(len); /* Grow buffer as necessary */
4150 kbuf[len] = '\0'; /* Mark string end */
4155 TRACEME(("ok (retrieved 0x%"UVxf", refcnt=%d, %s)", PTR2UV(sv),
4156 SvREFCNT(sv) - 1, sv_reftype(sv, FALSE)));
4164 * Retrieve data held in file and return the root object.
4165 * Common routine for pretrieve and mretrieve.
4167 static SV *do_retrieve(
4174 struct extendable msave; /* Where potentially valid mbuf is saved */
4176 TRACEME(("do_retrieve (optype = 0x%x)", optype));
4178 optype |= ST_RETRIEVE;
4181 * Sanity assertions for retrieve dispatch tables.
4184 ASSERT(sizeof(sv_old_retrieve) == sizeof(sv_retrieve),
4185 ("old and new retrieve dispatch table have same size"));
4186 ASSERT(sv_old_retrieve[SX_ERROR] == retrieve_other,
4187 ("SX_ERROR entry correctly initialized in old dispatch table"));
4188 ASSERT(sv_retrieve[SX_ERROR] == retrieve_other,
4189 ("SX_ERROR entry correctly initialized in new dispatch table"));
4192 * Workaround for CROAK leak: if they enter with a "dirty" context,
4193 * free up memory for them now.
4200 * Now that STORABLE_xxx hooks exist, it is possible that they try to
4201 * re-enter retrieve() via the hooks.
4205 cxt = allocate_context(cxt);
4209 ASSERT(cxt->entry == 1, ("starting new recursion"));
4210 ASSERT(!cxt->dirty, ("clean context"));
4215 * Data is loaded into the memory buffer when f is NULL, unless `in' is
4216 * also NULL, in which case we're expecting the data to already lie
4217 * in the buffer (dclone case).
4220 KBUFINIT(); /* Allocate hash key reading pool once */
4223 StructCopy(&cxt->membuf, &msave, struct extendable);
4229 * Magic number verifications.
4231 * This needs to be done before calling init_retrieve_context()
4232 * since the format indication in the file are necessary to conduct
4233 * some of the initializations.
4236 cxt->fio = f; /* Where I/O are performed */
4238 if (!magic_check(cxt))
4239 CROAK(("Magic number checking on storable %s failed",
4240 cxt->fio ? "file" : "string"));
4242 TRACEME(("data stored in %s format",
4243 cxt->netorder ? "net order" : "native"));
4245 init_retrieve_context(cxt, optype);
4247 ASSERT(is_retrieving(), ("within retrieve operation"));
4249 sv = retrieve(cxt); /* Recursively retrieve object, get root SV */
4256 StructCopy(&msave, &cxt->membuf, struct extendable);
4259 * The "root" context is never freed.
4262 clean_retrieve_context(cxt);
4263 if (cxt->prev) /* This context was stacked */
4264 free_context(cxt); /* It was not the "root" context */
4267 * Prepare returned value.
4271 TRACEME(("retrieve ERROR"));
4272 return &PL_sv_undef; /* Something went wrong, return undef */
4275 TRACEME(("retrieve got %s(0x%"UVxf")",
4276 sv_reftype(sv, FALSE), PTR2UV(sv)));
4279 * Backward compatibility with Storable-0.5@9 (which we know we
4280 * are retrieving if hseen is non-null): don't create an extra RV
4281 * for objects since we special-cased it at store time.
4283 * Build a reference to the SV returned by pretrieve even if it is
4284 * already one and not a scalar, for consistency reasons.
4286 * NB: although context might have been cleaned, the value of `cxt->hseen'
4287 * remains intact, and can be used as a flag.
4290 if (cxt->hseen) { /* Was not handling overloading by then */
4292 if (sv_type(sv) == svis_REF && (rv = SvRV(sv)) && SvOBJECT(rv))
4297 * If reference is overloaded, restore behaviour.
4299 * NB: minor glitch here: normally, overloaded refs are stored specially
4300 * so that we can croak when behaviour cannot be re-installed, and also
4301 * avoid testing for overloading magic at each reference retrieval.
4303 * Unfortunately, the root reference is implicitely stored, so we must
4304 * check for possible overloading now. Furthermore, if we don't restore
4305 * overloading, we cannot croak as if the original ref was, because we
4306 * have no way to determine whether it was an overloaded ref or not in
4309 * It's a pity that overloading magic is attached to the rv, and not to
4310 * the underlying sv as blessing is.
4314 HV *stash = (HV *) SvSTASH (sv);
4315 SV *rv = newRV_noinc(sv);
4316 if (stash && Gv_AMG(stash)) {
4318 TRACEME(("restored overloading on root reference"));
4323 return newRV_noinc(sv);
4329 * Retrieve data held in file and return the root object, undef on error.
4331 SV *pretrieve(PerlIO *f)
4333 TRACEME(("pretrieve"));
4334 return do_retrieve(f, Nullsv, 0);
4340 * Retrieve data held in scalar and return the root object, undef on error.
4342 SV *mretrieve(SV *sv)
4344 TRACEME(("mretrieve"));
4345 return do_retrieve((PerlIO*) 0, sv, 0);
4355 * Deep clone: returns a fresh copy of the original referenced SV tree.
4357 * This is achieved by storing the object in memory and restoring from
4358 * there. Not that efficient, but it should be faster than doing it from
4365 stcxt_t *real_context;
4368 TRACEME(("dclone"));
4371 * Workaround for CROAK leak: if they enter with a "dirty" context,
4372 * free up memory for them now.
4379 * do_store() optimizes for dclone by not freeing its context, should
4380 * we need to allocate one because we're deep cloning from a hook.
4383 if (!do_store((PerlIO*) 0, sv, ST_CLONE, FALSE, (SV**) 0))
4384 return &PL_sv_undef; /* Error during store */
4387 * Because of the above optimization, we have to refresh the context,
4388 * since a new one could have been allocated and stacked by do_store().
4391 { dSTCXT; real_context = cxt; } /* Sub-block needed for macro */
4392 cxt = real_context; /* And we need this temporary... */
4395 * Now, `cxt' may refer to a new context.
4398 ASSERT(!cxt->dirty, ("clean context"));
4399 ASSERT(!cxt->entry, ("entry will not cause new context allocation"));
4402 TRACEME(("dclone stored %d bytes", size));
4405 out = do_retrieve((PerlIO*) 0, Nullsv, ST_CLONE); /* Will free non-root context */
4407 TRACEME(("dclone returns 0x%"UVxf, PTR2UV(out)));
4417 * The Perl IO GV object distinguishes between input and output for sockets
4418 * but not for plain files. To allow Storable to transparently work on
4419 * plain files and sockets transparently, we have to ask xsubpp to fetch the
4420 * right object for us. Hence the OutputStream and InputStream declarations.
4422 * Before perl 5.004_05, those entries in the standard typemap are not
4423 * defined in perl include files, so we do that here.
4426 #ifndef OutputStream
4427 #define OutputStream PerlIO *
4428 #define InputStream PerlIO *
4429 #endif /* !OutputStream */
4431 MODULE = Storable PACKAGE = Storable
4469 last_op_in_netorder()