2 * Store and retrieve mechanism.
6 * $Id: Storable.xs,v 1.0 2000/09/01 19:40:41 ram Exp $
8 * Copyright (c) 1995-2000, Raphael Manfredi
10 * You may redistribute only under the same terms as Perl 5, as specified
11 * in the README file that comes with the distribution.
13 * $Log: Storable.xs,v $
14 * Revision 1.0 2000/09/01 19:40:41 ram
15 * Baseline for first official release.
21 #include <patchlevel.h> /* Perl's one, needed since 5.6 */
25 #define DEBUGME /* Debug mode, turns assertions on as well */
26 #define DASSERT /* Assertion mode */
30 * Pre PerlIO time when none of USE_PERLIO and PERLIO_IS_STDIO is defined
31 * Provide them with the necessary defines so they can build with pre-5.004.
34 #ifndef PERLIO_IS_STDIO
36 #define PerlIO_getc(x) getc(x)
37 #define PerlIO_putc(f,x) putc(x,f)
38 #define PerlIO_read(x,y,z) fread(y,1,z,x)
39 #define PerlIO_write(x,y,z) fwrite(y,1,z,x)
40 #define PerlIO_stdoutf printf
41 #endif /* PERLIO_IS_STDIO */
42 #endif /* USE_PERLIO */
45 * Earlier versions of perl might be used, we can't assume they have the latest!
48 #ifndef PERL_VERSION /* For perls < 5.6 */
49 #define PERL_VERSION PATCHLEVEL
51 #define newRV_noinc(sv) ((Sv = newRV(sv)), --SvREFCNT(SvRV(Sv)), Sv)
53 #if (PATCHLEVEL <= 4) /* Older perls (<= 5.004) lack PL_ namespace */
54 #define PL_sv_yes sv_yes
55 #define PL_sv_no sv_no
56 #define PL_sv_undef sv_undef
58 #ifndef HvSHAREKEYS_off
59 #define HvSHAREKEYS_off(hv) /* Ignore */
61 #ifndef AvFILLp /* Older perls (<=5.003) lack AvFILLp */
62 #define AvFILLp AvFILL
64 typedef double NV; /* Older perls lack the NV type */
65 #define IVdf "ld" /* Various printf formats for Perl types */
69 #define INT2PTR(t,v) (t)(IV)(v)
70 #define PTR2UV(v) (unsigned long)(v)
71 #endif /* PERL_VERSION -- perls < 5.6 */
73 #ifndef NVef /* The following were not part of perl 5.6 */
74 #if defined(USE_LONG_DOUBLE) && \
75 defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
76 #define NVef PERL_PRIeldbl
77 #define NVff PERL_PRIfldbl
78 #define NVgf PERL_PRIgldbl
90 #define TRACEME(x) do { PerlIO_stdoutf x; PerlIO_stdoutf("\n"); } while (0)
96 #define ASSERT(x,y) do { \
98 PerlIO_stdoutf("ASSERT FAILED (\"%s\", line %d): ", \
99 __FILE__, __LINE__); \
100 PerlIO_stdoutf y; PerlIO_stdoutf("\n"); \
111 #define C(x) ((char) (x)) /* For markers with dynamic retrieval handling */
113 #define SX_OBJECT C(0) /* Already stored object */
114 #define SX_LSCALAR C(1) /* Scalar (string) forthcoming (length, data) */
115 #define SX_ARRAY C(2) /* Array forthcominng (size, item list) */
116 #define SX_HASH C(3) /* Hash forthcoming (size, key/value pair list) */
117 #define SX_REF C(4) /* Reference to object forthcoming */
118 #define SX_UNDEF C(5) /* Undefined scalar */
119 #define SX_INTEGER C(6) /* Integer forthcoming */
120 #define SX_DOUBLE C(7) /* Double forthcoming */
121 #define SX_BYTE C(8) /* (signed) byte forthcoming */
122 #define SX_NETINT C(9) /* Integer in network order forthcoming */
123 #define SX_SCALAR C(10) /* Scalar (small) forthcoming (length, data) */
124 #define SX_TIED_ARRAY C(11) /* Tied array forthcoming */
125 #define SX_TIED_HASH C(12) /* Tied hash forthcoming */
126 #define SX_TIED_SCALAR C(13) /* Tied scalar forthcoming */
127 #define SX_SV_UNDEF C(14) /* Perl's immortal PL_sv_undef */
128 #define SX_SV_YES C(15) /* Perl's immortal PL_sv_yes */
129 #define SX_SV_NO C(16) /* Perl's immortal PL_sv_no */
130 #define SX_BLESS C(17) /* Object is blessed */
131 #define SX_IX_BLESS C(18) /* Object is blessed, classname given by index */
132 #define SX_HOOK C(19) /* Stored via hook, user-defined */
133 #define SX_OVERLOAD C(20) /* Overloaded reference */
134 #define SX_TIED_KEY C(21) /* Tied magic key forthcoming */
135 #define SX_TIED_IDX C(22) /* Tied magic index forthcoming */
136 #define SX_ERROR C(23) /* Error */
139 * Those are only used to retrieve "old" pre-0.6 binary images.
141 #define SX_ITEM 'i' /* An array item introducer */
142 #define SX_IT_UNDEF 'I' /* Undefined array item */
143 #define SX_KEY 'k' /* An hash key introducer */
144 #define SX_VALUE 'v' /* An hash value introducer */
145 #define SX_VL_UNDEF 'V' /* Undefined hash value */
148 * Those are only used to retrieve "old" pre-0.7 binary images
151 #define SX_CLASS 'b' /* Object is blessed, class name length <255 */
152 #define SX_LG_CLASS 'B' /* Object is blessed, class name length >255 */
153 #define SX_STORED 'X' /* End of object */
156 * Limits between short/long length representation.
159 #define LG_SCALAR 255 /* Large scalar length limit */
160 #define LG_BLESS 127 /* Large classname bless limit */
166 #define ST_STORE 0x1 /* Store operation */
167 #define ST_RETRIEVE 0x2 /* Retrieval operation */
168 #define ST_CLONE 0x4 /* Deep cloning operation */
171 * The following structure is used for hash table key retrieval. Since, when
172 * retrieving objects, we'll be facing blessed hash references, it's best
173 * to pre-allocate that buffer once and resize it as the need arises, never
174 * freeing it (keys will be saved away someplace else anyway, so even large
175 * keys are not enough a motivation to reclaim that space).
177 * This structure is also used for memory store/retrieve operations which
178 * happen in a fixed place before being malloc'ed elsewhere if persistency
179 * is required. Hence the aptr pointer.
182 char *arena; /* Will hold hash key strings, resized as needed */
183 STRLEN asiz; /* Size of aforementionned buffer */
184 char *aptr; /* Arena pointer, for in-place read/write ops */
185 char *aend; /* First invalid address */
190 * An hash table records the objects which have already been stored.
191 * Those are referred to as SX_OBJECT in the file, and their "tag" (i.e.
192 * an arbitrary sequence number) is used to identify them.
195 * An array table records the objects which have already been retrieved,
196 * as seen by the tag determind by counting the objects themselves. The
197 * reference to that retrieved object is kept in the table, and is returned
198 * when an SX_OBJECT is found bearing that same tag.
200 * The same processing is used to record "classname" for blessed objects:
201 * indexing by a hash at store time, and via an array at retrieve time.
204 typedef unsigned long stag_t; /* Used by pre-0.6 binary format */
207 * The following "thread-safe" related defines were contributed by
208 * Murray Nesbitt <murray@activestate.com> and integrated by RAM, who
209 * only renamed things a little bit to ensure consistency with surrounding
210 * code. -- RAM, 14/09/1999
212 * The original patch suffered from the fact that the stcxt_t structure
213 * was global. Murray tried to minimize the impact on the code as much as
216 * Starting with 0.7, Storable can be re-entrant, via the STORABLE_xxx hooks
217 * on objects. Therefore, the notion of context needs to be generalized,
221 #define MY_VERSION "Storable(" XS_VERSION ")"
223 typedef struct stcxt {
224 int entry; /* flags recursion */
225 int optype; /* type of traversal operation */
226 HV *hseen; /* which objects have been seen, store time */
227 AV *aseen; /* which objects have been seen, retrieve time */
228 HV *hclass; /* which classnames have been seen, store time */
229 AV *aclass; /* which classnames have been seen, retrieve time */
230 HV *hook; /* cache for hook methods per class name */
231 I32 tagnum; /* incremented at store time for each seen object */
232 I32 classnum; /* incremented at store time for each seen classname */
233 int netorder; /* true if network order used */
234 int forgive_me; /* whether to be forgiving... */
235 int canonical; /* whether to store hashes sorted by key */
236 int dirty; /* context is dirty due to CROAK() -- can be cleaned */
237 struct extendable keybuf; /* for hash key retrieval */
238 struct extendable membuf; /* for memory store/retrieve operations */
239 PerlIO *fio; /* where I/O are performed, NULL for memory */
240 int ver_major; /* major of version for retrieved object */
241 int ver_minor; /* minor of version for retrieved object */
242 SV *(**retrieve_vtbl)(); /* retrieve dispatch table */
243 struct stcxt *prev; /* contexts chained backwards in real recursion */
246 #if defined(MULTIPLICITY) || defined(PERL_OBJECT) || defined(PERL_CAPI)
248 #if (PATCHLEVEL <= 4) && (SUBVERSION < 68)
250 SV *perinterp_sv = perl_get_sv(MY_VERSION, FALSE)
251 #else /* >= perl5.004_68 */
253 SV *perinterp_sv = *hv_fetch(PL_modglobal, \
254 MY_VERSION, sizeof(MY_VERSION)-1, TRUE)
255 #endif /* < perl5.004_68 */
257 #define dSTCXT_PTR(T,name) \
258 T name = (perinterp_sv && SvIOK(perinterp_sv) \
259 ? INT2PTR(T, SvIVX(perinterp_sv)) : (T) 0)
262 dSTCXT_PTR(stcxt_t *, cxt)
266 Newz(0, cxt, 1, stcxt_t); \
267 sv_setiv(perinterp_sv, PTR2IV(cxt))
269 #define SET_STCXT(x) do { \
271 sv_setiv(perinterp_sv, PTR2IV(x)); \
274 #else /* !MULTIPLICITY && !PERL_OBJECT && !PERL_CAPI */
276 static stcxt_t Context;
277 static stcxt_t *Context_ptr = &Context;
278 #define dSTCXT stcxt_t *cxt = Context_ptr
279 #define INIT_STCXT dSTCXT
280 #define SET_STCXT(x) Context_ptr = x
282 #endif /* MULTIPLICITY || PERL_OBJECT || PERL_CAPI */
286 * Croaking implies a memory leak, since we don't use setjmp/longjmp
287 * to catch the exit and free memory used during store or retrieve
288 * operations. This is not too difficult to fix, but I need to understand
289 * how Perl does it, and croaking is exceptional anyway, so I lack the
290 * motivation to do it.
292 * The current workaround is to mark the context as dirty when croaking,
293 * so that data structures can be freed whenever we renter Storable code
294 * (but only *then*: it's a workaround, not a fix).
296 * This is also imperfect, because we don't really know how far they trapped
297 * the croak(), and when we were recursing, we won't be able to clean anything
298 * but the topmost context stacked.
301 #define CROAK(x) do { cxt->dirty = 1; croak x; } while (0)
304 * End of "thread-safe" related definitions.
310 * Keep only the low 32 bits of a pointer (used for tags, which are not
315 #define LOW_32BITS(x) ((I32) (x))
317 #define LOW_32BITS(x) ((I32) ((unsigned long) (x) & 0xffffffffUL))
323 * Hack for Crays, where sizeof(I32) == 8, and which are big-endians.
324 * Used in the WLEN and RLEN macros.
328 #define oI(x) ((I32 *) ((char *) (x) + 4))
329 #define oS(x) ((x) - 4)
330 #define oC(x) (x = 0)
339 * key buffer handling
341 #define kbuf (cxt->keybuf).arena
342 #define ksiz (cxt->keybuf).asiz
343 #define KBUFINIT() do { \
345 TRACEME(("** allocating kbuf of 128 bytes")); \
346 New(10003, kbuf, 128, char); \
350 #define KBUFCHK(x) do { \
352 TRACEME(("** extending kbuf to %d bytes", x+1)); \
353 Renew(kbuf, x+1, char); \
359 * memory buffer handling
361 #define mbase (cxt->membuf).arena
362 #define msiz (cxt->membuf).asiz
363 #define mptr (cxt->membuf).aptr
364 #define mend (cxt->membuf).aend
366 #define MGROW (1 << 13)
367 #define MMASK (MGROW - 1)
369 #define round_mgrow(x) \
370 ((unsigned long) (((unsigned long) (x) + MMASK) & ~MMASK))
371 #define trunc_int(x) \
372 ((unsigned long) ((unsigned long) (x) & ~(sizeof(int)-1)))
373 #define int_aligned(x) \
374 ((unsigned long) (x) == trunc_int(x))
376 #define MBUF_INIT(x) do { \
378 TRACEME(("** allocating mbase of %d bytes", MGROW)); \
379 New(10003, mbase, MGROW, char); \
386 mend = mbase + msiz; \
389 #define MBUF_TRUNC(x) mptr = mbase + x
390 #define MBUF_SIZE() (mptr - mbase)
393 * Use SvPOKp(), because SvPOK() fails on tainted scalars.
394 * See store_scalar() for other usage of this workaround.
396 #define MBUF_LOAD(v) do { \
398 CROAK(("Not a scalar string")); \
399 mptr = mbase = SvPV(v, msiz); \
400 mend = mbase + msiz; \
403 #define MBUF_XTEND(x) do { \
404 int nsz = (int) round_mgrow((x)+msiz); \
405 int offset = mptr - mbase; \
406 TRACEME(("** extending mbase to %d bytes", nsz)); \
407 Renew(mbase, nsz, char); \
409 mptr = mbase + offset; \
410 mend = mbase + nsz; \
413 #define MBUF_CHK(x) do { \
414 if ((mptr + (x)) > mend) \
418 #define MBUF_GETC(x) do { \
420 x = (int) (unsigned char) *mptr++; \
426 #define MBUF_GETINT(x) do { \
428 if ((mptr + 4) <= mend) { \
429 memcpy(oI(&x), mptr, 4); \
435 #define MBUF_GETINT(x) do { \
436 if ((mptr + sizeof(int)) <= mend) { \
437 if (int_aligned(mptr)) \
440 memcpy(&x, mptr, sizeof(int)); \
441 mptr += sizeof(int); \
447 #define MBUF_READ(x,s) do { \
448 if ((mptr + (s)) <= mend) { \
449 memcpy(x, mptr, s); \
455 #define MBUF_SAFEREAD(x,s,z) do { \
456 if ((mptr + (s)) <= mend) { \
457 memcpy(x, mptr, s); \
465 #define MBUF_PUTC(c) do { \
467 *mptr++ = (char) c; \
470 *mptr++ = (char) c; \
475 #define MBUF_PUTINT(i) do { \
477 memcpy(mptr, oI(&i), 4); \
481 #define MBUF_PUTINT(i) do { \
482 MBUF_CHK(sizeof(int)); \
483 if (int_aligned(mptr)) \
486 memcpy(mptr, &i, sizeof(int)); \
487 mptr += sizeof(int); \
491 #define MBUF_WRITE(x,s) do { \
493 memcpy(mptr, x, s); \
498 * Possible return values for sv_type().
502 #define svis_SCALAR 1
506 #define svis_TIED_ITEM 5
513 #define SHF_TYPE_MASK 0x03
514 #define SHF_LARGE_CLASSLEN 0x04
515 #define SHF_LARGE_STRLEN 0x08
516 #define SHF_LARGE_LISTLEN 0x10
517 #define SHF_IDX_CLASSNAME 0x20
518 #define SHF_NEED_RECURSE 0x40
519 #define SHF_HAS_LIST 0x80
522 * Types for SX_HOOK (2 bits).
530 * Before 0.6, the magic string was "perl-store" (binary version number 0).
532 * Since 0.6 introduced many binary incompatibilities, the magic string has
533 * been changed to "pst0" to allow an old image to be properly retrieved by
534 * a newer Storable, but ensure a newer image cannot be retrieved with an
537 * At 0.7, objects are given the ability to serialize themselves, and the
538 * set of markers is extended, backward compatibility is not jeopardized,
539 * so the binary version number could have remained unchanged. To correctly
540 * spot errors if a file making use of 0.7-specific extensions is given to
541 * 0.6 for retrieval, the binary version was moved to "2". And I'm introducing
542 * a "minor" version, to better track this kind of evolution from now on.
545 static char old_magicstr[] = "perl-store"; /* Magic number before 0.6 */
546 static char magicstr[] = "pst0"; /* Used as a magic number */
548 #define STORABLE_BIN_MAJOR 2 /* Binary major "version" */
549 #define STORABLE_BIN_MINOR 2 /* Binary minor "version" */
552 * Useful store shortcuts...
555 #define PUTMARK(x) do { \
558 else if (PerlIO_putc(cxt->fio, x) == EOF) \
562 #define WRITE_I32(x) do { \
563 ASSERT(sizeof(x) == sizeof(I32), ("writing an I32")); \
566 else if (PerlIO_write(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
571 #define WLEN(x) do { \
572 if (cxt->netorder) { \
573 int y = (int) htonl(x); \
576 else if (PerlIO_write(cxt->fio,oI(&y),oS(sizeof(y))) != oS(sizeof(y))) \
581 else if (PerlIO_write(cxt->fio,oI(&x),oS(sizeof(x))) != oS(sizeof(x))) \
586 #define WLEN(x) WRITE_I32(x)
589 #define WRITE(x,y) do { \
592 else if (PerlIO_write(cxt->fio, x, y) != y) \
596 #define STORE_SCALAR(pv, len) do { \
597 if (len <= LG_SCALAR) { \
598 unsigned char clen = (unsigned char) len; \
599 PUTMARK(SX_SCALAR); \
604 PUTMARK(SX_LSCALAR); \
611 * Store undef in arrays and hashes without recursing through store().
613 #define STORE_UNDEF() do { \
619 * Useful retrieve shortcuts...
623 (cxt->fio ? PerlIO_getc(cxt->fio) : (mptr >= mend ? EOF : (int) *mptr++))
625 #define GETMARK(x) do { \
628 else if ((x = PerlIO_getc(cxt->fio)) == EOF) \
632 #define READ_I32(x) do { \
633 ASSERT(sizeof(x) == sizeof(I32), ("reading an I32")); \
637 else if (PerlIO_read(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
642 #define RLEN(x) do { \
646 else if (PerlIO_read(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
649 x = (int) ntohl(x); \
652 #define RLEN(x) READ_I32(x)
655 #define READ(x,y) do { \
658 else if (PerlIO_read(cxt->fio, x, y) != y) \
662 #define SAFEREAD(x,y,z) do { \
664 MBUF_SAFEREAD(x,y,z); \
665 else if (PerlIO_read(cxt->fio, x, y) != y) { \
672 * This macro is used at retrieve time, to remember where object 'y', bearing a
673 * given tag 'tagnum', has been retrieved. Next time we see an SX_OBJECT marker,
674 * we'll therefore know where it has been retrieved and will be able to
675 * share the same reference, as in the original stored memory image.
677 #define SEEN(y) do { \
680 if (av_store(cxt->aseen, cxt->tagnum++, SvREFCNT_inc(y)) == 0) \
682 TRACEME(("aseen(#%d) = 0x%"UVxf" (refcnt=%d)", cxt->tagnum-1, \
683 PTR2UV(y), SvREFCNT(y)-1)); \
687 * Bless `s' in `p', via a temporary reference, required by sv_bless().
689 #define BLESS(s,p) do { \
692 TRACEME(("blessing 0x%"UVxf" in %s", PTR2UV(s), (p))); \
693 stash = gv_stashpv((p), TRUE); \
694 ref = newRV_noinc(s); \
695 (void) sv_bless(ref, stash); \
701 static SV *retrieve();
704 * Dynamic dispatching table for SV store.
707 static int store_ref(stcxt_t *cxt, SV *sv);
708 static int store_scalar(stcxt_t *cxt, SV *sv);
709 static int store_array(stcxt_t *cxt, AV *av);
710 static int store_hash(stcxt_t *cxt, HV *hv);
711 static int store_tied(stcxt_t *cxt, SV *sv);
712 static int store_tied_item(stcxt_t *cxt, SV *sv);
713 static int store_other(stcxt_t *cxt, SV *sv);
714 static int store_blessed(stcxt_t *cxt, SV *sv, int type, HV *pkg);
716 static int (*sv_store[])() = {
717 store_ref, /* svis_REF */
718 store_scalar, /* svis_SCALAR */
719 store_array, /* svis_ARRAY */
720 store_hash, /* svis_HASH */
721 store_tied, /* svis_TIED */
722 store_tied_item, /* svis_TIED_ITEM */
723 store_other, /* svis_OTHER */
726 #define SV_STORE(x) (*sv_store[x])
729 * Dynamic dispatching tables for SV retrieval.
732 static SV *retrieve_lscalar(stcxt_t *cxt);
733 static SV *old_retrieve_array(stcxt_t *cxt);
734 static SV *old_retrieve_hash(stcxt_t *cxt);
735 static SV *retrieve_ref(stcxt_t *cxt);
736 static SV *retrieve_undef(stcxt_t *cxt);
737 static SV *retrieve_integer(stcxt_t *cxt);
738 static SV *retrieve_double(stcxt_t *cxt);
739 static SV *retrieve_byte(stcxt_t *cxt);
740 static SV *retrieve_netint(stcxt_t *cxt);
741 static SV *retrieve_scalar(stcxt_t *cxt);
742 static SV *retrieve_tied_array(stcxt_t *cxt);
743 static SV *retrieve_tied_hash(stcxt_t *cxt);
744 static SV *retrieve_tied_scalar(stcxt_t *cxt);
745 static SV *retrieve_other(stcxt_t *cxt);
747 static SV *(*sv_old_retrieve[])() = {
748 0, /* SX_OBJECT -- entry unused dynamically */
749 retrieve_lscalar, /* SX_LSCALAR */
750 old_retrieve_array, /* SX_ARRAY -- for pre-0.6 binaries */
751 old_retrieve_hash, /* SX_HASH -- for pre-0.6 binaries */
752 retrieve_ref, /* SX_REF */
753 retrieve_undef, /* SX_UNDEF */
754 retrieve_integer, /* SX_INTEGER */
755 retrieve_double, /* SX_DOUBLE */
756 retrieve_byte, /* SX_BYTE */
757 retrieve_netint, /* SX_NETINT */
758 retrieve_scalar, /* SX_SCALAR */
759 retrieve_tied_array, /* SX_ARRAY */
760 retrieve_tied_hash, /* SX_HASH */
761 retrieve_tied_scalar, /* SX_SCALAR */
762 retrieve_other, /* SX_SV_UNDEF not supported */
763 retrieve_other, /* SX_SV_YES not supported */
764 retrieve_other, /* SX_SV_NO not supported */
765 retrieve_other, /* SX_BLESS not supported */
766 retrieve_other, /* SX_IX_BLESS not supported */
767 retrieve_other, /* SX_HOOK not supported */
768 retrieve_other, /* SX_OVERLOADED not supported */
769 retrieve_other, /* SX_TIED_KEY not supported */
770 retrieve_other, /* SX_TIED_IDX not supported */
771 retrieve_other, /* SX_ERROR */
774 static SV *retrieve_array(stcxt_t *cxt);
775 static SV *retrieve_hash(stcxt_t *cxt);
776 static SV *retrieve_sv_undef(stcxt_t *cxt);
777 static SV *retrieve_sv_yes(stcxt_t *cxt);
778 static SV *retrieve_sv_no(stcxt_t *cxt);
779 static SV *retrieve_blessed(stcxt_t *cxt);
780 static SV *retrieve_idx_blessed(stcxt_t *cxt);
781 static SV *retrieve_hook(stcxt_t *cxt);
782 static SV *retrieve_overloaded(stcxt_t *cxt);
783 static SV *retrieve_tied_key(stcxt_t *cxt);
784 static SV *retrieve_tied_idx(stcxt_t *cxt);
786 static SV *(*sv_retrieve[])() = {
787 0, /* SX_OBJECT -- entry unused dynamically */
788 retrieve_lscalar, /* SX_LSCALAR */
789 retrieve_array, /* SX_ARRAY */
790 retrieve_hash, /* SX_HASH */
791 retrieve_ref, /* SX_REF */
792 retrieve_undef, /* SX_UNDEF */
793 retrieve_integer, /* SX_INTEGER */
794 retrieve_double, /* SX_DOUBLE */
795 retrieve_byte, /* SX_BYTE */
796 retrieve_netint, /* SX_NETINT */
797 retrieve_scalar, /* SX_SCALAR */
798 retrieve_tied_array, /* SX_ARRAY */
799 retrieve_tied_hash, /* SX_HASH */
800 retrieve_tied_scalar, /* SX_SCALAR */
801 retrieve_sv_undef, /* SX_SV_UNDEF */
802 retrieve_sv_yes, /* SX_SV_YES */
803 retrieve_sv_no, /* SX_SV_NO */
804 retrieve_blessed, /* SX_BLESS */
805 retrieve_idx_blessed, /* SX_IX_BLESS */
806 retrieve_hook, /* SX_HOOK */
807 retrieve_overloaded, /* SX_OVERLOAD */
808 retrieve_tied_key, /* SX_TIED_KEY */
809 retrieve_tied_idx, /* SX_TIED_IDX */
810 retrieve_other, /* SX_ERROR */
813 #define RETRIEVE(c,x) (*(c)->retrieve_vtbl[(x) >= SX_ERROR ? SX_ERROR : (x)])
815 static SV *mbuf2sv(void);
818 *** Context management.
824 * Called once per "thread" (interpreter) to initialize some global context.
826 static void init_perinterp(void)
830 cxt->netorder = 0; /* true if network order used */
831 cxt->forgive_me = -1; /* whether to be forgiving... */
837 * Initialize a new store context for real recursion.
839 static void init_store_context(
845 TRACEME(("init_store_context"));
847 cxt->netorder = network_order;
848 cxt->forgive_me = -1; /* Fetched from perl if needed */
849 cxt->canonical = -1; /* Idem */
850 cxt->tagnum = -1; /* Reset tag numbers */
851 cxt->classnum = -1; /* Reset class numbers */
852 cxt->fio = f; /* Where I/O are performed */
853 cxt->optype = optype; /* A store, or a deep clone */
854 cxt->entry = 1; /* No recursion yet */
857 * The `hseen' table is used to keep track of each SV stored and their
858 * associated tag numbers is special. It is "abused" because the
859 * values stored are not real SV, just integers cast to (SV *),
860 * which explains the freeing below.
862 * It is also one possible bottlneck to achieve good storing speed,
863 * so the "shared keys" optimization is turned off (unlikely to be
864 * of any use here), and the hash table is "pre-extended". Together,
865 * those optimizations increase the throughput by 12%.
868 cxt->hseen = newHV(); /* Table where seen objects are stored */
869 HvSHAREKEYS_off(cxt->hseen);
872 * The following does not work well with perl5.004_04, and causes
873 * a core dump later on, in a completely unrelated spot, which
874 * makes me think there is a memory corruption going on.
876 * Calling hv_ksplit(hseen, HBUCKETS) instead of manually hacking
877 * it below does not make any difference. It seems to work fine
878 * with perl5.004_68 but given the probable nature of the bug,
879 * that does not prove anything.
881 * It's a shame because increasing the amount of buckets raises
882 * store() throughput by 5%, but until I figure this out, I can't
883 * allow for this to go into production.
885 * It is reported fixed in 5.005, hence the #if.
887 #if PERL_VERSION >= 5
888 #define HBUCKETS 4096 /* Buckets for %hseen */
889 HvMAX(cxt->hseen) = HBUCKETS - 1; /* keys %hseen = $HBUCKETS; */
893 * The `hclass' hash uses the same settings as `hseen' above, but it is
894 * used to assign sequential tags (numbers) to class names for blessed
897 * We turn the shared key optimization on.
900 cxt->hclass = newHV(); /* Where seen classnames are stored */
902 #if PERL_VERSION >= 5
903 HvMAX(cxt->hclass) = HBUCKETS - 1; /* keys %hclass = $HBUCKETS; */
907 * The `hook' hash table is used to keep track of the references on
908 * the STORABLE_freeze hook routines, when found in some class name.
910 * It is assumed that the inheritance tree will not be changed during
911 * storing, and that no new method will be dynamically created by the
915 cxt->hook = newHV(); /* Table where hooks are cached */
919 * clean_store_context
921 * Clean store context by
923 static void clean_store_context(stcxt_t *cxt)
927 TRACEME(("clean_store_context"));
929 ASSERT(cxt->optype & ST_STORE, ("was performing a store()"));
932 * Insert real values into hashes where we stored faked pointers.
935 hv_iterinit(cxt->hseen);
936 while (he = hv_iternext(cxt->hseen))
937 HeVAL(he) = &PL_sv_undef;
939 hv_iterinit(cxt->hclass);
940 while (he = hv_iternext(cxt->hclass))
941 HeVAL(he) = &PL_sv_undef;
944 * And now dispose of them...
947 hv_undef(cxt->hseen);
948 sv_free((SV *) cxt->hseen);
950 hv_undef(cxt->hclass);
951 sv_free((SV *) cxt->hclass);
954 sv_free((SV *) cxt->hook);
961 * init_retrieve_context
963 * Initialize a new retrieve context for real recursion.
965 static void init_retrieve_context(cxt, optype)
969 TRACEME(("init_retrieve_context"));
972 * The hook hash table is used to keep track of the references on
973 * the STORABLE_thaw hook routines, when found in some class name.
975 * It is assumed that the inheritance tree will not be changed during
976 * storing, and that no new method will be dynamically created by the
980 cxt->hook = newHV(); /* Caches STORABLE_thaw */
983 * If retrieving an old binary version, the cxt->retrieve_vtbl variable
984 * was set to sv_old_retrieve. We'll need a hash table to keep track of
985 * the correspondance between the tags and the tag number used by the
986 * new retrieve routines.
989 cxt->hseen = (cxt->retrieve_vtbl == sv_old_retrieve) ? newHV() : 0;
991 cxt->aseen = newAV(); /* Where retrieved objects are kept */
992 cxt->aclass = newAV(); /* Where seen classnames are kept */
993 cxt->tagnum = 0; /* Have to count objects... */
994 cxt->classnum = 0; /* ...and class names as well */
995 cxt->optype = optype;
996 cxt->entry = 1; /* No recursion yet */
1000 * clean_retrieve_context
1002 * Clean retrieve context by
1004 static void clean_retrieve_context(cxt)
1007 TRACEME(("clean_retrieve_context"));
1009 ASSERT(cxt->optype & ST_RETRIEVE, ("was performing a retrieve()"));
1011 av_undef(cxt->aseen);
1012 sv_free((SV *) cxt->aseen);
1014 av_undef(cxt->aclass);
1015 sv_free((SV *) cxt->aclass);
1017 hv_undef(cxt->hook);
1018 sv_free((SV *) cxt->hook);
1021 sv_free((SV *) cxt->hseen); /* optional HV, for backward compat. */
1030 * A workaround for the CROAK bug: cleanup the last context.
1032 static void clean_context(cxt)
1035 TRACEME(("clean_context"));
1037 ASSERT(cxt->dirty, ("dirty context"));
1039 if (cxt->optype & ST_RETRIEVE)
1040 clean_retrieve_context(cxt);
1042 clean_store_context(cxt);
1048 * Allocate a new context and push it on top of the parent one.
1049 * This new context is made globally visible via SET_STCXT().
1051 static stcxt_t *allocate_context(parent_cxt)
1052 stcxt_t *parent_cxt;
1056 TRACEME(("allocate_context"));
1058 ASSERT(!parent_cxt->dirty, ("parent context clean"));
1060 Newz(0, cxt, 1, stcxt_t);
1061 cxt->prev = parent_cxt;
1070 * Free current context, which cannot be the "root" one.
1071 * Make the context underneath globally visible via SET_STCXT().
1073 static void free_context(cxt)
1076 stcxt_t *prev = cxt->prev;
1078 TRACEME(("free_context"));
1080 ASSERT(!cxt->dirty, ("clean context"));
1081 ASSERT(prev, ("not freeing root context"));
1099 * Tells whether we're in the middle of a store operation.
1101 int is_storing(void)
1105 return cxt->entry && (cxt->optype & ST_STORE);
1111 * Tells whether we're in the middle of a retrieve operation.
1113 int is_retrieving(void)
1117 return cxt->entry && (cxt->optype & ST_RETRIEVE);
1121 * last_op_in_netorder
1123 * Returns whether last operation was made using network order.
1125 * This is typically out-of-band information that might prove useful
1126 * to people wishing to convert native to network order data when used.
1128 int last_op_in_netorder(void)
1132 return cxt->netorder;
1136 *** Hook lookup and calling routines.
1142 * A wrapper on gv_fetchmethod_autoload() which caches results.
1144 * Returns the routine reference as an SV*, or null if neither the package
1145 * nor its ancestors know about the method.
1147 static SV *pkg_fetchmeth(
1157 * The following code is the same as the one performed by UNIVERSAL::can
1161 gv = gv_fetchmethod_autoload(pkg, method, FALSE);
1162 if (gv && isGV(gv)) {
1163 sv = newRV((SV*) GvCV(gv));
1164 TRACEME(("%s->%s: 0x%"UVxf, HvNAME(pkg), method, PTR2UV(sv)));
1166 sv = newSVsv(&PL_sv_undef);
1167 TRACEME(("%s->%s: not found", HvNAME(pkg), method));
1171 * Cache the result, ignoring failure: if we can't store the value,
1172 * it just won't be cached.
1175 (void) hv_store(cache, HvNAME(pkg), strlen(HvNAME(pkg)), sv, 0);
1177 return SvOK(sv) ? sv : (SV *) 0;
1183 * Force cached value to be undef: hook ignored even if present.
1185 static void pkg_hide(
1190 (void) hv_store(cache,
1191 HvNAME(pkg), strlen(HvNAME(pkg)), newSVsv(&PL_sv_undef), 0);
1197 * Our own "UNIVERSAL::can", which caches results.
1199 * Returns the routine reference as an SV*, or null if the object does not
1200 * know about the method.
1210 TRACEME(("pkg_can for %s->%s", HvNAME(pkg), method));
1213 * Look into the cache to see whether we already have determined
1214 * where the routine was, if any.
1216 * NOTA BENE: we don't use `method' at all in our lookup, since we know
1217 * that only one hook (i.e. always the same) is cached in a given cache.
1220 svh = hv_fetch(cache, HvNAME(pkg), strlen(HvNAME(pkg)), FALSE);
1224 TRACEME(("cached %s->%s: not found", HvNAME(pkg), method));
1227 TRACEME(("cached %s->%s: 0x%"UVxf,
1228 HvNAME(pkg), method, PTR2UV(sv)));
1233 TRACEME(("not cached yet"));
1234 return pkg_fetchmeth(cache, pkg, method); /* Fetch and cache */
1240 * Call routine as obj->hook(av) in scalar context.
1241 * Propagates the single returned value if not called in void context.
1243 static SV *scalar_call(
1254 TRACEME(("scalar_call (cloning=%d)", cloning));
1261 XPUSHs(sv_2mortal(newSViv(cloning))); /* Cloning flag */
1263 SV **ary = AvARRAY(av);
1264 int cnt = AvFILLp(av) + 1;
1266 XPUSHs(ary[0]); /* Frozen string */
1267 for (i = 1; i < cnt; i++) {
1268 TRACEME(("pushing arg #%d (0x%"UVxf")...",
1269 i, PTR2UV(ary[i])));
1270 XPUSHs(sv_2mortal(newRV(ary[i])));
1275 TRACEME(("calling..."));
1276 count = perl_call_sv(hook, flags); /* Go back to Perl code */
1277 TRACEME(("count = %d", count));
1283 SvREFCNT_inc(sv); /* We're returning it, must stay alive! */
1296 * Call routine obj->hook(cloning) in list context.
1297 * Returns the list of returned values in an array.
1299 static AV *array_call(
1309 TRACEME(("array_call (cloning=%d)", cloning));
1315 XPUSHs(obj); /* Target object */
1316 XPUSHs(sv_2mortal(newSViv(cloning))); /* Cloning flag */
1319 count = perl_call_sv(hook, G_ARRAY); /* Go back to Perl code */
1324 for (i = count - 1; i >= 0; i--) {
1326 av_store(av, i, SvREFCNT_inc(sv));
1339 * Lookup the class name in the `hclass' table and either assign it a new ID
1340 * or return the existing one, by filling in `classnum'.
1342 * Return true if the class was known, false if the ID was just generated.
1344 static int known_class(
1346 char *name, /* Class name */
1347 int len, /* Name length */
1351 HV *hclass = cxt->hclass;
1353 TRACEME(("known_class (%s)", name));
1356 * Recall that we don't store pointers in this hash table, but tags.
1357 * Therefore, we need LOW_32BITS() to extract the relevant parts.
1360 svh = hv_fetch(hclass, name, len, FALSE);
1362 *classnum = LOW_32BITS(*svh);
1367 * Unknown classname, we need to record it.
1371 if (!hv_store(hclass, name, len, INT2PTR(SV*, cxt->classnum), 0))
1372 CROAK(("Unable to record new classname"));
1374 *classnum = cxt->classnum;
1379 *** Sepcific store routines.
1385 * Store a reference.
1386 * Layout is SX_REF <object> or SX_OVERLOAD <object>.
1388 static int store_ref(stcxt_t *cxt, SV *sv)
1390 TRACEME(("store_ref (0x%"UVxf")", PTR2UV(sv)));
1393 * Follow reference, and check if target is overloaded.
1399 HV *stash = (HV *) SvSTASH(sv);
1400 if (stash && Gv_AMG(stash)) {
1401 TRACEME(("ref (0x%"UVxf") is overloaded", PTR2UV(sv)));
1402 PUTMARK(SX_OVERLOAD);
1408 return store(cxt, sv);
1416 * Layout is SX_LSCALAR <length> <data>, SX_SCALAR <lenght> <data> or SX_UNDEF.
1417 * The <data> section is omitted if <length> is 0.
1419 * If integer or double, the layout is SX_INTEGER <data> or SX_DOUBLE <data>.
1420 * Small integers (within [-127, +127]) are stored as SX_BYTE <byte>.
1422 static int store_scalar(stcxt_t *cxt, SV *sv)
1427 U32 flags = SvFLAGS(sv); /* "cc -O" may put it in register */
1429 TRACEME(("store_scalar (0x%"UVxf")", PTR2UV(sv)));
1432 * For efficiency, break the SV encapsulation by peaking at the flags
1433 * directly without using the Perl macros to avoid dereferencing
1434 * sv->sv_flags each time we wish to check the flags.
1437 if (!(flags & SVf_OK)) { /* !SvOK(sv) */
1438 if (sv == &PL_sv_undef) {
1439 TRACEME(("immortal undef"));
1440 PUTMARK(SX_SV_UNDEF);
1442 TRACEME(("undef at 0x%"UVxf, PTR2UV(sv)));
1449 * Always store the string representation of a scalar if it exists.
1450 * Gisle Aas provided me with this test case, better than a long speach:
1452 * perl -MDevel::Peek -le '$a="abc"; $a+0; Dump($a)'
1453 * SV = PVNV(0x80c8520)
1455 * FLAGS = (NOK,POK,pNOK,pPOK)
1458 * PV = 0x80c83d0 "abc"\0
1462 * Write SX_SCALAR, length, followed by the actual data.
1464 * Otherwise, write an SX_BYTE, SX_INTEGER or an SX_DOUBLE as
1465 * appropriate, followed by the actual (binary) data. A double
1466 * is written as a string if network order, for portability.
1468 * NOTE: instead of using SvNOK(sv), we test for SvNOKp(sv).
1469 * The reason is that when the scalar value is tainted, the SvNOK(sv)
1472 * The test for a read-only scalar with both POK and NOK set is meant
1473 * to quickly detect &PL_sv_yes and &PL_sv_no without having to pay the
1474 * address comparison for each scalar we store.
1477 #define SV_MAYBE_IMMORTAL (SVf_READONLY|SVf_POK|SVf_NOK)
1479 if ((flags & SV_MAYBE_IMMORTAL) == SV_MAYBE_IMMORTAL) {
1480 if (sv == &PL_sv_yes) {
1481 TRACEME(("immortal yes"));
1483 } else if (sv == &PL_sv_no) {
1484 TRACEME(("immortal no"));
1487 pv = SvPV(sv, len); /* We know it's SvPOK */
1488 goto string; /* Share code below */
1490 } else if (flags & SVp_POK) { /* SvPOKp(sv) => string */
1491 I32 wlen; /* For 64-bit machines */
1495 * Will come here from below with pv and len set if double & netorder,
1496 * or from above if it was readonly, POK and NOK but neither &PL_sv_yes
1501 wlen = (I32) len; /* WLEN via STORE_SCALAR expects I32 */
1502 STORE_SCALAR(pv, wlen);
1503 TRACEME(("ok (scalar 0x%"UVxf" '%s', length = %"IVdf")",
1504 PTR2UV(sv), SvPVX(sv), (IV)len));
1506 } else if (flags & SVp_NOK) { /* SvNOKp(sv) => double */
1510 * Watch for number being an integer in disguise.
1512 if (nv == (NV) (iv = I_V(nv))) {
1513 TRACEME(("double %"NVff" is actually integer %"IVdf, nv, iv));
1514 goto integer; /* Share code below */
1517 if (cxt->netorder) {
1518 TRACEME(("double %"NVff" stored as string", nv));
1520 goto string; /* Share code above */
1524 WRITE(&nv, sizeof(nv));
1526 TRACEME(("ok (double 0x%"UVxf", value = %"NVff")", PTR2UV(sv), nv));
1528 } else if (flags & SVp_IOK) { /* SvIOKp(sv) => integer */
1532 * Will come here from above with iv set if double is an integer.
1537 * Optimize small integers into a single byte, otherwise store as
1538 * a real integer (converted into network order if they asked).
1541 if (iv >= -128 && iv <= 127) {
1542 unsigned char siv = (unsigned char) (iv + 128); /* [0,255] */
1545 TRACEME(("small integer stored as %d", siv));
1546 } else if (cxt->netorder) {
1549 niv = (I32) htonl(iv);
1550 TRACEME(("using network order"));
1553 TRACEME(("as-is for network order"));
1558 PUTMARK(SX_INTEGER);
1559 WRITE(&iv, sizeof(iv));
1562 TRACEME(("ok (integer 0x%"UVxf", value = %"IVdf")", PTR2UV(sv), iv));
1565 CROAK(("Can't determine type of %s(0x%"UVxf")",
1566 sv_reftype(sv, FALSE),
1569 return 0; /* Ok, no recursion on scalars */
1577 * Layout is SX_ARRAY <size> followed by each item, in increading index order.
1578 * Each item is stored as <object>.
1580 static int store_array(stcxt_t *cxt, AV *av)
1583 I32 len = av_len(av) + 1;
1587 TRACEME(("store_array (0x%"UVxf")", PTR2UV(av)));
1590 * Signal array by emitting SX_ARRAY, followed by the array length.
1595 TRACEME(("size = %d", len));
1598 * Now store each item recursively.
1601 for (i = 0; i < len; i++) {
1602 sav = av_fetch(av, i, 0);
1604 TRACEME(("(#%d) undef item", i));
1608 TRACEME(("(#%d) item", i));
1609 if (ret = store(cxt, *sav))
1613 TRACEME(("ok (array)"));
1622 * Borrowed from perl source file pp_ctl.c, where it is used by pp_sort.
1625 sortcmp(const void *a, const void *b)
1627 return sv_cmp(*(SV * const *) a, *(SV * const *) b);
1634 * Store an hash table.
1636 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
1637 * Values are stored as <object>.
1638 * Keys are stored as <length> <data>, the <data> section being omitted
1641 static int store_hash(stcxt_t *cxt, HV *hv)
1643 I32 len = HvKEYS(hv);
1649 TRACEME(("store_hash (0x%"UVxf")", PTR2UV(hv)));
1652 * Signal hash by emitting SX_HASH, followed by the table length.
1657 TRACEME(("size = %d", len));
1660 * Save possible iteration state via each() on that table.
1663 riter = HvRITER(hv);
1664 eiter = HvEITER(hv);
1668 * Now store each item recursively.
1670 * If canonical is defined to some true value then store each
1671 * key/value pair in sorted order otherwise the order is random.
1672 * Canonical order is irrelevant when a deep clone operation is performed.
1674 * Fetch the value from perl only once per store() operation, and only
1679 !(cxt->optype & ST_CLONE) && (cxt->canonical == 1 ||
1680 (cxt->canonical < 0 && (cxt->canonical =
1681 SvTRUE(perl_get_sv("Storable::canonical", TRUE)) ? 1 : 0)))
1684 * Storing in order, sorted by key.
1685 * Run through the hash, building up an array of keys in a
1686 * mortal array, sort the array and then run through the
1692 TRACEME(("using canonical order"));
1694 for (i = 0; i < len; i++) {
1695 HE *he = hv_iternext(hv);
1696 SV *key = hv_iterkeysv(he);
1697 av_store(av, AvFILLp(av)+1, key); /* av_push(), really */
1700 qsort((char *) AvARRAY(av), len, sizeof(SV *), sortcmp);
1702 for (i = 0; i < len; i++) {
1705 SV *key = av_shift(av);
1706 HE *he = hv_fetch_ent(hv, key, 0, 0);
1707 SV *val = HeVAL(he);
1709 return 1; /* Internal error, not I/O error */
1712 * Store value first.
1715 TRACEME(("(#%d) value 0x%"UVxf, i, PTR2UV(val)));
1717 if (ret = store(cxt, val))
1722 * Keys are written after values to make sure retrieval
1723 * can be optimal in terms of memory usage, where keys are
1724 * read into a fixed unique buffer called kbuf.
1725 * See retrieve_hash() for details.
1728 keyval = hv_iterkey(he, &keylen);
1729 TRACEME(("(#%d) key '%s'", i, keyval));
1732 WRITE(keyval, keylen);
1736 * Free up the temporary array
1745 * Storing in "random" order (in the order the keys are stored
1746 * within the the hash). This is the default and will be faster!
1749 for (i = 0; i < len; i++) {
1752 SV *val = hv_iternextsv(hv, &key, &len);
1755 return 1; /* Internal error, not I/O error */
1758 * Store value first.
1761 TRACEME(("(#%d) value 0x%"UVxf, i, PTR2UV(val)));
1763 if (ret = store(cxt, val))
1768 * Keys are written after values to make sure retrieval
1769 * can be optimal in terms of memory usage, where keys are
1770 * read into a fixed unique buffer called kbuf.
1771 * See retrieve_hash() for details.
1774 TRACEME(("(#%d) key '%s'", i, key));
1781 TRACEME(("ok (hash 0x%"UVxf")", PTR2UV(hv)));
1784 HvRITER(hv) = riter; /* Restore hash iterator state */
1785 HvEITER(hv) = eiter;
1793 * When storing a tied object (be it a tied scalar, array or hash), we lay out
1794 * a special mark, followed by the underlying tied object. For instance, when
1795 * dealing with a tied hash, we store SX_TIED_HASH <hash object>, where
1796 * <hash object> stands for the serialization of the tied hash.
1798 static int store_tied(stcxt_t *cxt, SV *sv)
1802 int svt = SvTYPE(sv);
1805 TRACEME(("store_tied (0x%"UVxf")", PTR2UV(sv)));
1808 * We have a small run-time penalty here because we chose to factorise
1809 * all tieds objects into the same routine, and not have a store_tied_hash,
1810 * a store_tied_array, etc...
1812 * Don't use a switch() statement, as most compilers don't optimize that
1813 * well for 2/3 values. An if() else if() cascade is just fine. We put
1814 * tied hashes first, as they are the most likely beasts.
1817 if (svt == SVt_PVHV) {
1818 TRACEME(("tied hash"));
1819 PUTMARK(SX_TIED_HASH); /* Introduces tied hash */
1820 } else if (svt == SVt_PVAV) {
1821 TRACEME(("tied array"));
1822 PUTMARK(SX_TIED_ARRAY); /* Introduces tied array */
1824 TRACEME(("tied scalar"));
1825 PUTMARK(SX_TIED_SCALAR); /* Introduces tied scalar */
1829 if (!(mg = mg_find(sv, mtype)))
1830 CROAK(("No magic '%c' found while storing tied %s", mtype,
1831 (svt == SVt_PVHV) ? "hash" :
1832 (svt == SVt_PVAV) ? "array" : "scalar"));
1835 * The mg->mg_obj found by mg_find() above actually points to the
1836 * underlying tied Perl object implementation. For instance, if the
1837 * original SV was that of a tied array, then mg->mg_obj is an AV.
1839 * Note that we store the Perl object as-is. We don't call its FETCH
1840 * method along the way. At retrieval time, we won't call its STORE
1841 * method either, but the tieing magic will be re-installed. In itself,
1842 * that ensures that the tieing semantics are preserved since futher
1843 * accesses on the retrieved object will indeed call the magic methods...
1846 if (ret = store(cxt, mg->mg_obj))
1849 TRACEME(("ok (tied)"));
1857 * Stores a reference to an item within a tied structure:
1859 * . \$h{key}, stores both the (tied %h) object and 'key'.
1860 * . \$a[idx], stores both the (tied @a) object and 'idx'.
1862 * Layout is therefore either:
1863 * SX_TIED_KEY <object> <key>
1864 * SX_TIED_IDX <object> <index>
1866 static int store_tied_item(stcxt_t *cxt, SV *sv)
1871 TRACEME(("store_tied_item (0x%"UVxf")", PTR2UV(sv)));
1873 if (!(mg = mg_find(sv, 'p')))
1874 CROAK(("No magic 'p' found while storing reference to tied item"));
1877 * We discriminate between \$h{key} and \$a[idx] via mg_ptr.
1881 TRACEME(("store_tied_item: storing a ref to a tied hash item"));
1882 PUTMARK(SX_TIED_KEY);
1883 TRACEME(("store_tied_item: storing OBJ 0x%"UVxf, PTR2UV(mg->mg_obj)));
1885 if (ret = store(cxt, mg->mg_obj))
1888 TRACEME(("store_tied_item: storing PTR 0x%"UVxf, PTR2UV(mg->mg_ptr)));
1890 if (ret = store(cxt, (SV *) mg->mg_ptr))
1893 I32 idx = mg->mg_len;
1895 TRACEME(("store_tied_item: storing a ref to a tied array item "));
1896 PUTMARK(SX_TIED_IDX);
1897 TRACEME(("store_tied_item: storing OBJ 0x%"UVxf, PTR2UV(mg->mg_obj)));
1899 if (ret = store(cxt, mg->mg_obj))
1902 TRACEME(("store_tied_item: storing IDX %d", idx));
1907 TRACEME(("ok (tied item)"));
1913 * store_hook -- dispatched manually, not via sv_store[]
1915 * The blessed SV is serialized by a hook.
1919 * SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
1921 * where <flags> indicates how long <len>, <len2> and <len3> are, whether
1922 * the trailing part [] is present, the type of object (scalar, array or hash).
1923 * There is also a bit which says how the classname is stored between:
1928 * and when the <index> form is used (classname already seen), the "large
1929 * classname" bit in <flags> indicates how large the <index> is.
1931 * The serialized string returned by the hook is of length <len2> and comes
1932 * next. It is an opaque string for us.
1934 * Those <len3> object IDs which are listed last represent the extra references
1935 * not directly serialized by the hook, but which are linked to the object.
1937 * When recursion is mandated to resolve object-IDs not yet seen, we have
1938 * instead, with <header> being flags with bits set to indicate the object type
1939 * and that recursion was indeed needed:
1941 * SX_HOOK <header> <object> <header> <object> <flags>
1943 * that same header being repeated between serialized objects obtained through
1944 * recursion, until we reach flags indicating no recursion, at which point
1945 * we know we've resynchronized with a single layout, after <flags>.
1947 static int store_hook(
1960 int count; /* really len3 + 1 */
1961 unsigned char flags;
1964 int recursed = 0; /* counts recursion */
1965 int obj_type; /* object type, on 2 bits */
1968 int clone = cxt->optype & ST_CLONE;
1970 TRACEME(("store_hook, class \"%s\", tagged #%d", HvNAME(pkg), cxt->tagnum));
1973 * Determine object type on 2 bits.
1978 obj_type = SHT_SCALAR;
1981 obj_type = SHT_ARRAY;
1984 obj_type = SHT_HASH;
1987 CROAK(("Unexpected object type (%d) in store_hook()", type));
1989 flags = SHF_NEED_RECURSE | obj_type;
1991 class = HvNAME(pkg);
1992 len = strlen(class);
1995 * To call the hook, we need to fake a call like:
1997 * $object->STORABLE_freeze($cloning);
1999 * but we don't have the $object here. For instance, if $object is
2000 * a blessed array, what we have in `sv' is the array, and we can't
2001 * call a method on those.
2003 * Therefore, we need to create a temporary reference to the object and
2004 * make the call on that reference.
2007 TRACEME(("about to call STORABLE_freeze on class %s", class));
2009 ref = newRV_noinc(sv); /* Temporary reference */
2010 av = array_call(ref, hook, clone); /* @a = $object->STORABLE_freeze($c) */
2012 SvREFCNT_dec(ref); /* Reclaim temporary reference */
2014 count = AvFILLp(av) + 1;
2015 TRACEME(("store_hook, array holds %d items", count));
2018 * If they return an empty list, it means they wish to ignore the
2019 * hook for this class (and not just this instance -- that's for them
2020 * to handle if they so wish).
2022 * Simply disable the cached entry for the hook (it won't be recomputed
2023 * since it's present in the cache) and recurse to store_blessed().
2028 * They must not change their mind in the middle of a serialization.
2031 if (hv_fetch(cxt->hclass, class, len, FALSE))
2032 CROAK(("Too late to ignore hooks for %s class \"%s\"",
2033 (cxt->optype & ST_CLONE) ? "cloning" : "storing", class));
2035 pkg_hide(cxt->hook, pkg, "STORABLE_freeze");
2037 ASSERT(!pkg_can(cxt->hook, pkg, "STORABLE_freeze"), ("hook invisible"));
2038 TRACEME(("ignoring STORABLE_freeze in class \"%s\"", class));
2040 return store_blessed(cxt, sv, type, pkg);
2044 * Get frozen string.
2048 pv = SvPV(ary[0], len2);
2051 * Allocate a class ID if not already done.
2054 if (!known_class(cxt, class, len, &classnum)) {
2055 TRACEME(("first time we see class %s, ID = %d", class, classnum));
2056 classnum = -1; /* Mark: we must store classname */
2058 TRACEME(("already seen class %s, ID = %d", class, classnum));
2062 * If they returned more than one item, we need to serialize some
2063 * extra references if not already done.
2065 * Loop over the array, starting at postion #1, and for each item,
2066 * ensure it is a reference, serialize it if not already done, and
2067 * replace the entry with the tag ID of the corresponding serialized
2070 * We CHEAT by not calling av_fetch() and read directly within the
2074 for (i = 1; i < count; i++) {
2079 CROAK(("Item #%d from hook in %s is not a reference", i, class));
2080 xsv = SvRV(xsv); /* Follow ref to know what to look for */
2083 * Look in hseen and see if we have a tag already.
2084 * Serialize entry if not done already, and get its tag.
2087 if (svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE))
2088 goto sv_seen; /* Avoid moving code too far to the right */
2090 TRACEME(("listed object %d at 0x%"UVxf" is unknown", i-1, PTR2UV(xsv)));
2093 * We need to recurse to store that object and get it to be known
2094 * so that we can resolve the list of object-IDs at retrieve time.
2096 * The first time we do this, we need to emit the proper header
2097 * indicating that we recursed, and what the type of object is (the
2098 * object we're storing via a user-hook). Indeed, during retrieval,
2099 * we'll have to create the object before recursing to retrieve the
2100 * others, in case those would point back at that object.
2103 /* [SX_HOOK] <flags> <object>*/
2108 if (ret = store(cxt, xsv)) /* Given by hook for us to store */
2111 svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE);
2113 CROAK(("Could not serialize item #%d from hook in %s", i, class));
2116 * Replace entry with its tag (not a real SV, so no refcnt increment)
2122 TRACEME(("listed object %d at 0x%"UVxf" is tag #%"UVuf,
2123 i-1, PTR2UV(xsv), PTR2UV(*svh)));
2127 * Compute leading flags.
2131 if (((classnum == -1) ? len : classnum) > LG_SCALAR)
2132 flags |= SHF_LARGE_CLASSLEN;
2134 flags |= SHF_IDX_CLASSNAME;
2135 if (len2 > LG_SCALAR)
2136 flags |= SHF_LARGE_STRLEN;
2138 flags |= SHF_HAS_LIST;
2139 if (count > (LG_SCALAR + 1))
2140 flags |= SHF_LARGE_LISTLEN;
2143 * We're ready to emit either serialized form:
2145 * SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
2146 * SX_HOOK <flags> <index> <len2> <str> [<len3> <object-IDs>]
2148 * If we recursed, the SX_HOOK has already been emitted.
2151 TRACEME(("SX_HOOK (recursed=%d) flags=0x%x "
2152 "class=%"IVdf" len=%"IVdf" len2=%"IVdf" len3=%d",
2153 recursed, flags, (IV)classnum, (IV)len, (IV)len2, count-1));
2155 /* SX_HOOK <flags> */
2160 /* <len> <classname> or <index> */
2161 if (flags & SHF_IDX_CLASSNAME) {
2162 if (flags & SHF_LARGE_CLASSLEN)
2165 unsigned char cnum = (unsigned char) classnum;
2169 if (flags & SHF_LARGE_CLASSLEN)
2172 unsigned char clen = (unsigned char) len;
2175 WRITE(class, len); /* Final \0 is omitted */
2178 /* <len2> <frozen-str> */
2179 if (flags & SHF_LARGE_STRLEN) {
2180 I32 wlen2 = len2; /* STRLEN might be 8 bytes */
2181 WLEN(wlen2); /* Must write an I32 for 64-bit machines */
2183 unsigned char clen = (unsigned char) len2;
2187 WRITE(pv, len2); /* Final \0 is omitted */
2189 /* [<len3> <object-IDs>] */
2190 if (flags & SHF_HAS_LIST) {
2191 int len3 = count - 1;
2192 if (flags & SHF_LARGE_LISTLEN)
2195 unsigned char clen = (unsigned char) len3;
2200 * NOTA BENE, for 64-bit machines: the ary[i] below does not yield a
2201 * real pointer, rather a tag number, well under the 32-bit limit.
2204 for (i = 1; i < count; i++) {
2205 I32 tagval = htonl(LOW_32BITS(ary[i]));
2207 TRACEME(("object %d, tag #%d", i-1, ntohl(tagval)));
2212 * Free the array. We need extra care for indices after 0, since they
2213 * don't hold real SVs but integers cast.
2217 AvFILLp(av) = 0; /* Cheat, nothing after 0 interests us */
2225 * store_blessed -- dispatched manually, not via sv_store[]
2227 * Check whether there is a STORABLE_xxx hook defined in the class or in one
2228 * of its ancestors. If there is, then redispatch to store_hook();
2230 * Otherwise, the blessed SV is stored using the following layout:
2232 * SX_BLESS <flag> <len> <classname> <object>
2234 * where <flag> indicates whether <len> is stored on 0 or 4 bytes, depending
2235 * on the high-order bit in flag: if 1, then length follows on 4 bytes.
2236 * Otherwise, the low order bits give the length, thereby giving a compact
2237 * representation for class names less than 127 chars long.
2239 * Each <classname> seen is remembered and indexed, so that the next time
2240 * an object in the blessed in the same <classname> is stored, the following
2243 * SX_IX_BLESS <flag> <index> <object>
2245 * where <index> is the classname index, stored on 0 or 4 bytes depending
2246 * on the high-order bit in flag (same encoding as above for <len>).
2248 static int store_blessed(
2259 TRACEME(("store_blessed, type %d, class \"%s\"", type, HvNAME(pkg)));
2262 * Look for a hook for this blessed SV and redirect to store_hook()
2266 hook = pkg_can(cxt->hook, pkg, "STORABLE_freeze");
2268 return store_hook(cxt, sv, type, pkg, hook);
2271 * This is a blessed SV without any serialization hook.
2274 class = HvNAME(pkg);
2275 len = strlen(class);
2277 TRACEME(("blessed 0x%"UVxf" in %s, no hook: tagged #%d",
2278 PTR2UV(sv), class, cxt->tagnum));
2281 * Determine whether it is the first time we see that class name (in which
2282 * case it will be stored in the SX_BLESS form), or whether we already
2283 * saw that class name before (in which case the SX_IX_BLESS form will be
2287 if (known_class(cxt, class, len, &classnum)) {
2288 TRACEME(("already seen class %s, ID = %d", class, classnum));
2289 PUTMARK(SX_IX_BLESS);
2290 if (classnum <= LG_BLESS) {
2291 unsigned char cnum = (unsigned char) classnum;
2294 unsigned char flag = (unsigned char) 0x80;
2299 TRACEME(("first time we see class %s, ID = %d", class, classnum));
2301 if (len <= LG_BLESS) {
2302 unsigned char clen = (unsigned char) len;
2305 unsigned char flag = (unsigned char) 0x80;
2307 WLEN(len); /* Don't BER-encode, this should be rare */
2309 WRITE(class, len); /* Final \0 is omitted */
2313 * Now emit the <object> part.
2316 return SV_STORE(type)(cxt, sv);
2322 * We don't know how to store the item we reached, so return an error condition.
2323 * (it's probably a GLOB, some CODE reference, etc...)
2325 * If they defined the `forgive_me' variable at the Perl level to some
2326 * true value, then don't croak, just warn, and store a placeholder string
2329 static int store_other(stcxt_t *cxt, SV *sv)
2332 static char buf[80];
2334 TRACEME(("store_other"));
2337 * Fetch the value from perl only once per store() operation.
2341 cxt->forgive_me == 0 ||
2342 (cxt->forgive_me < 0 && !(cxt->forgive_me =
2343 SvTRUE(perl_get_sv("Storable::forgive_me", TRUE)) ? 1 : 0))
2345 CROAK(("Can't store %s items", sv_reftype(sv, FALSE)));
2347 warn("Can't store item %s(0x%"UVxf")",
2348 sv_reftype(sv, FALSE), PTR2UV(sv));
2351 * Store placeholder string as a scalar instead...
2354 (void) sprintf(buf, "You lost %s(0x%"UVxf")\0", sv_reftype(sv, FALSE),
2358 STORE_SCALAR(buf, len);
2359 TRACEME(("ok (dummy \"%s\", length = %"IVdf")", buf, len));
2365 *** Store driving routines
2371 * WARNING: partially duplicates Perl's sv_reftype for speed.
2373 * Returns the type of the SV, identified by an integer. That integer
2374 * may then be used to index the dynamic routine dispatch table.
2376 static int sv_type(SV *sv)
2378 switch (SvTYPE(sv)) {
2383 * No need to check for ROK, that can't be set here since there
2384 * is no field capable of hodling the xrv_rv reference.
2392 * Starting from SVt_PV, it is possible to have the ROK flag
2393 * set, the pointer to the other SV being either stored in
2394 * the xrv_rv (in the case of a pure SVt_RV), or as the
2395 * xpv_pv field of an SVt_PV and its heirs.
2397 * However, those SV cannot be magical or they would be an
2398 * SVt_PVMG at least.
2400 return SvROK(sv) ? svis_REF : svis_SCALAR;
2402 case SVt_PVLV: /* Workaround for perl5.004_04 "LVALUE" bug */
2403 if (SvRMAGICAL(sv) && (mg_find(sv, 'p')))
2404 return svis_TIED_ITEM;
2407 if (SvRMAGICAL(sv) && (mg_find(sv, 'q')))
2409 return SvROK(sv) ? svis_REF : svis_SCALAR;
2411 if (SvRMAGICAL(sv) && (mg_find(sv, 'P')))
2415 if (SvRMAGICAL(sv) && (mg_find(sv, 'P')))
2428 * Recursively store objects pointed to by the sv to the specified file.
2430 * Layout is <content> or SX_OBJECT <tagnum> if we reach an already stored
2431 * object (one for which storage has started -- it may not be over if we have
2432 * a self-referenced structure). This data set forms a stored <object>.
2434 static int store(stcxt_t *cxt, SV *sv)
2440 HV *hseen = cxt->hseen;
2442 TRACEME(("store (0x%"UVxf")", PTR2UV(sv)));
2445 * If object has already been stored, do not duplicate data.
2446 * Simply emit the SX_OBJECT marker followed by its tag data.
2447 * The tag is always written in network order.
2449 * NOTA BENE, for 64-bit machines: the "*svh" below does not yield a
2450 * real pointer, rather a tag number (watch the insertion code below).
2451 * That means it pobably safe to assume it is well under the 32-bit limit,
2452 * and makes the truncation safe.
2453 * -- RAM, 14/09/1999
2456 svh = hv_fetch(hseen, (char *) &sv, sizeof(sv), FALSE);
2458 I32 tagval = htonl(LOW_32BITS(*svh));
2460 TRACEME(("object 0x%"UVxf" seen as #%d", PTR2UV(sv), ntohl(tagval)));
2468 * Allocate a new tag and associate it with the address of the sv being
2469 * stored, before recursing...
2471 * In order to avoid creating new SvIVs to hold the tagnum we just
2472 * cast the tagnum to a SV pointer and store that in the hash. This
2473 * means that we must clean up the hash manually afterwards, but gives
2474 * us a 15% throughput increase.
2479 if (!hv_store(hseen,
2480 (char *) &sv, sizeof(sv), INT2PTR(SV*, cxt->tagnum), 0))
2484 * Store `sv' and everything beneath it, using appropriate routine.
2485 * Abort immediately if we get a non-zero status back.
2490 TRACEME(("storing 0x%"UVxf" tag #%d, type %d...",
2491 PTR2UV(sv), cxt->tagnum, type));
2494 HV *pkg = SvSTASH(sv);
2495 ret = store_blessed(cxt, sv, type, pkg);
2497 ret = SV_STORE(type)(cxt, sv);
2499 TRACEME(("%s (stored 0x%"UVxf", refcnt=%d, %s)",
2500 ret ? "FAILED" : "ok", PTR2UV(sv),
2501 SvREFCNT(sv), sv_reftype(sv, FALSE)));
2509 * Write magic number and system information into the file.
2510 * Layout is <magic> <network> [<len> <byteorder> <sizeof int> <sizeof long>
2511 * <sizeof ptr>] where <len> is the length of the byteorder hexa string.
2512 * All size and lenghts are written as single characters here.
2514 * Note that no byte ordering info is emitted when <network> is true, since
2515 * integers will be emitted in network order in that case.
2517 static int magic_write(stcxt_t *cxt)
2519 char buf[256]; /* Enough room for 256 hexa digits */
2521 int use_network_order = cxt->netorder;
2523 TRACEME(("magic_write on fd=%d", cxt->fio ? fileno(cxt->fio) : -1));
2526 WRITE(magicstr, strlen(magicstr)); /* Don't write final \0 */
2529 * Starting with 0.6, the "use_network_order" byte flag is also used to
2530 * indicate the version number of the binary image, encoded in the upper
2531 * bits. The bit 0 is always used to indicate network order.
2535 ((use_network_order ? 0x1 : 0x0) | (STORABLE_BIN_MAJOR << 1));
2539 * Starting with 0.7, a full byte is dedicated to the minor version of
2540 * the binary format, which is incremented only when new markers are
2541 * introduced, for instance, but when backward compatibility is preserved.
2544 PUTMARK((unsigned char) STORABLE_BIN_MINOR);
2546 if (use_network_order)
2547 return 0; /* Don't bother with byte ordering */
2549 sprintf(buf, "%lx", (unsigned long) BYTEORDER);
2550 c = (unsigned char) strlen(buf);
2552 WRITE(buf, (unsigned int) c); /* Don't write final \0 */
2553 PUTMARK((unsigned char) sizeof(int));
2554 PUTMARK((unsigned char) sizeof(long));
2555 PUTMARK((unsigned char) sizeof(char *));
2556 PUTMARK((unsigned char) sizeof(NV));
2558 TRACEME(("ok (magic_write byteorder = 0x%lx [%d], I%d L%d P%d D%d)",
2559 (unsigned long) BYTEORDER, (int) c,
2560 (int) sizeof(int), (int) sizeof(long),
2561 (int) sizeof(char *), (int) sizeof(NV)));
2569 * Common code for store operations.
2571 * When memory store is requested (f = NULL) and a non null SV* is given in
2572 * `res', it is filled with a new SV created out of the memory buffer.
2574 * It is required to provide a non-null `res' when the operation type is not
2575 * dclone() and store() is performed to memory.
2577 static int do_store(
2587 ASSERT(!(f == 0 && !(optype & ST_CLONE)) || res,
2588 ("must supply result SV pointer for real recursion to memory"));
2590 TRACEME(("do_store (optype=%d, netorder=%d)",
2591 optype, network_order));
2596 * Workaround for CROAK leak: if they enter with a "dirty" context,
2597 * free up memory for them now.
2604 * Now that STORABLE_xxx hooks exist, it is possible that they try to
2605 * re-enter store() via the hooks. We need to stack contexts.
2609 cxt = allocate_context(cxt);
2613 ASSERT(cxt->entry == 1, ("starting new recursion"));
2614 ASSERT(!cxt->dirty, ("clean context"));
2617 * Ensure sv is actually a reference. From perl, we called something
2619 * pstore(FILE, \@array);
2620 * so we must get the scalar value behing that reference.
2624 CROAK(("Not a reference"));
2625 sv = SvRV(sv); /* So follow it to know what to store */
2628 * If we're going to store to memory, reset the buffer.
2635 * Prepare context and emit headers.
2638 init_store_context(cxt, f, optype, network_order);
2640 if (-1 == magic_write(cxt)) /* Emit magic and ILP info */
2641 return 0; /* Error */
2644 * Recursively store object...
2647 ASSERT(is_storing(), ("within store operation"));
2649 status = store(cxt, sv); /* Just do it! */
2652 * If they asked for a memory store and they provided an SV pointer,
2653 * make an SV string out of the buffer and fill their pointer.
2655 * When asking for ST_REAL, it's MANDATORY for the caller to provide
2656 * an SV, since context cleanup might free the buffer if we did recurse.
2657 * (unless caller is dclone(), which is aware of that).
2660 if (!cxt->fio && res)
2666 * The "root" context is never freed, since it is meant to be always
2667 * handy for the common case where no recursion occurs at all (i.e.
2668 * we enter store() outside of any Storable code and leave it, period).
2669 * We know it's the "root" context because there's nothing stacked
2674 * When deep cloning, we don't free the context: doing so would force
2675 * us to copy the data in the memory buffer. Sicne we know we're
2676 * about to enter do_retrieve...
2679 clean_store_context(cxt);
2680 if (cxt->prev && !(cxt->optype & ST_CLONE))
2683 TRACEME(("do_store returns %d", status));
2691 * Store the transitive data closure of given object to disk.
2692 * Returns 0 on error, a true value otherwise.
2694 int pstore(PerlIO *f, SV *sv)
2696 TRACEME(("pstore"));
2697 return do_store(f, sv, 0, FALSE, (SV**) 0);
2704 * Same as pstore(), but network order is used for integers and doubles are
2705 * emitted as strings.
2707 int net_pstore(PerlIO *f, SV *sv)
2709 TRACEME(("net_pstore"));
2710 return do_store(f, sv, 0, TRUE, (SV**) 0);
2720 * Build a new SV out of the content of the internal memory buffer.
2722 static SV *mbuf2sv(void)
2726 return newSVpv(mbase, MBUF_SIZE());
2732 * Store the transitive data closure of given object to memory.
2733 * Returns undef on error, a scalar value containing the data otherwise.
2740 TRACEME(("mstore"));
2742 if (!do_store((PerlIO*) 0, sv, 0, FALSE, &out))
2743 return &PL_sv_undef;
2751 * Same as mstore(), but network order is used for integers and doubles are
2752 * emitted as strings.
2754 SV *net_mstore(SV *sv)
2759 TRACEME(("net_mstore"));
2761 if (!do_store((PerlIO*) 0, sv, 0, TRUE, &out))
2762 return &PL_sv_undef;
2768 *** Specific retrieve callbacks.
2774 * Return an error via croak, since it is not possible that we get here
2775 * under normal conditions, when facing a file produced via pstore().
2777 static SV *retrieve_other(stcxt_t *cxt)
2780 cxt->ver_major != STORABLE_BIN_MAJOR &&
2781 cxt->ver_minor != STORABLE_BIN_MINOR
2783 CROAK(("Corrupted storable %s (binary v%d.%d), current is v%d.%d",
2784 cxt->fio ? "file" : "string",
2785 cxt->ver_major, cxt->ver_minor,
2786 STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR));
2788 CROAK(("Corrupted storable %s (binary v%d.%d)",
2789 cxt->fio ? "file" : "string",
2790 cxt->ver_major, cxt->ver_minor));
2793 return (SV *) 0; /* Just in case */
2797 * retrieve_idx_blessed
2799 * Layout is SX_IX_BLESS <index> <object> with SX_IX_BLESS already read.
2800 * <index> can be coded on either 1 or 5 bytes.
2802 static SV *retrieve_idx_blessed(stcxt_t *cxt)
2809 TRACEME(("retrieve_idx_blessed (#%d)", cxt->tagnum));
2811 GETMARK(idx); /* Index coded on a single char? */
2816 * Fetch classname in `aclass'
2819 sva = av_fetch(cxt->aclass, idx, FALSE);
2821 CROAK(("Class name #%d should have been seen already", idx));
2823 class = SvPVX(*sva); /* We know it's a PV, by construction */
2825 TRACEME(("class ID %d => %s", idx, class));
2828 * Retrieve object and bless it.
2841 * Layout is SX_BLESS <len> <classname> <object> with SX_BLESS already read.
2842 * <len> can be coded on either 1 or 5 bytes.
2844 static SV *retrieve_blessed(stcxt_t *cxt)
2848 char buf[LG_BLESS + 1]; /* Avoid malloc() if possible */
2851 TRACEME(("retrieve_blessed (#%d)", cxt->tagnum));
2854 * Decode class name length and read that name.
2856 * Short classnames have two advantages: their length is stored on one
2857 * single byte, and the string can be read on the stack.
2860 GETMARK(len); /* Length coded on a single char? */
2863 TRACEME(("** allocating %d bytes for class name", len+1));
2864 New(10003, class, len+1, char);
2867 class[len] = '\0'; /* Mark string end */
2870 * It's a new classname, otherwise it would have been an SX_IX_BLESS.
2873 if (!av_store(cxt->aclass, cxt->classnum++, newSVpvn(class, len)))
2877 * Retrieve object and bless it.
2893 * Layout: SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
2894 * with leading mark already read, as usual.
2896 * When recursion was involved during serialization of the object, there
2897 * is an unknown amount of serialized objects after the SX_HOOK mark. Until
2898 * we reach a <flags> marker with the recursion bit cleared.
2900 static SV *retrieve_hook(stcxt_t *cxt)
2903 char buf[LG_BLESS + 1]; /* Avoid malloc() if possible */
2915 int clone = cxt->optype & ST_CLONE;
2917 TRACEME(("retrieve_hook (#%d)", cxt->tagnum));
2920 * Read flags, which tell us about the type, and whether we need to recurse.
2926 * Create the (empty) object, and mark it as seen.
2928 * This must be done now, because tags are incremented, and during
2929 * serialization, the object tag was affected before recursion could
2933 obj_type = flags & SHF_TYPE_MASK;
2939 sv = (SV *) newAV();
2942 sv = (SV *) newHV();
2945 return retrieve_other(cxt); /* Let it croak */
2950 * Whilst flags tell us to recurse, do so.
2952 * We don't need to remember the addresses returned by retrieval, because
2953 * all the references will be obtained through indirection via the object
2954 * tags in the object-ID list.
2957 while (flags & SHF_NEED_RECURSE) {
2958 TRACEME(("retrieve_hook recursing..."));
2962 TRACEME(("retrieve_hook back with rv=0x%"UVxf,
2967 if (flags & SHF_IDX_CLASSNAME) {
2972 * Fetch index from `aclass'
2975 if (flags & SHF_LARGE_CLASSLEN)
2980 sva = av_fetch(cxt->aclass, idx, FALSE);
2982 CROAK(("Class name #%d should have been seen already", idx));
2984 class = SvPVX(*sva); /* We know it's a PV, by construction */
2985 TRACEME(("class ID %d => %s", idx, class));
2989 * Decode class name length and read that name.
2991 * NOTA BENE: even if the length is stored on one byte, we don't read
2992 * on the stack. Just like retrieve_blessed(), we limit the name to
2993 * LG_BLESS bytes. This is an arbitrary decision.
2996 if (flags & SHF_LARGE_CLASSLEN)
3001 if (len > LG_BLESS) {
3002 TRACEME(("** allocating %d bytes for class name", len+1));
3003 New(10003, class, len+1, char);
3007 class[len] = '\0'; /* Mark string end */
3010 * Record new classname.
3013 if (!av_store(cxt->aclass, cxt->classnum++, newSVpvn(class, len)))
3017 TRACEME(("class name: %s", class));
3020 * Decode user-frozen string length and read it in a SV.
3022 * For efficiency reasons, we read data directly into the SV buffer.
3023 * To understand that code, read retrieve_scalar()
3026 if (flags & SHF_LARGE_STRLEN)
3031 frozen = NEWSV(10002, len2);
3033 SAFEREAD(SvPVX(frozen), len2, frozen);
3034 SvCUR_set(frozen, len2);
3035 *SvEND(frozen) = '\0';
3037 (void) SvPOK_only(frozen); /* Validates string pointer */
3040 TRACEME(("frozen string: %d bytes", len2));
3043 * Decode object-ID list length, if present.
3046 if (flags & SHF_HAS_LIST) {
3047 if (flags & SHF_LARGE_LISTLEN)
3053 av_extend(av, len3 + 1); /* Leave room for [0] */
3054 AvFILLp(av) = len3; /* About to be filled anyway */
3058 TRACEME(("has %d object IDs to link", len3));
3061 * Read object-ID list into array.
3062 * Because we pre-extended it, we can cheat and fill it manually.
3064 * We read object tags and we can convert them into SV* on the fly
3065 * because we know all the references listed in there (as tags)
3066 * have been already serialized, hence we have a valid correspondance
3067 * between each of those tags and the recreated SV.
3071 SV **ary = AvARRAY(av);
3073 for (i = 1; i <= len3; i++) { /* We leave [0] alone */
3080 svh = av_fetch(cxt->aseen, tag, FALSE);
3082 CROAK(("Object #%d should have been retrieved already", tag));
3084 ary[i] = SvREFCNT_inc(xsv);
3089 * Bless the object and look up the STORABLE_thaw hook.
3093 hook = pkg_can(cxt->hook, SvSTASH(sv), "STORABLE_thaw");
3095 CROAK(("No STORABLE_thaw defined for objects of class %s", class));
3098 * If we don't have an `av' yet, prepare one.
3099 * Then insert the frozen string as item [0].
3107 AvARRAY(av)[0] = SvREFCNT_inc(frozen);
3112 * $object->STORABLE_thaw($cloning, $frozen, @refs);
3114 * where $object is our blessed (empty) object, $cloning is a boolean
3115 * telling whether we're running a deep clone, $frozen is the frozen
3116 * string the user gave us in his serializing hook, and @refs, which may
3117 * be empty, is the list of extra references he returned along for us
3120 * In effect, the hook is an alternate creation routine for the class,
3121 * the object itself being already created by the runtime.
3124 TRACEME(("calling STORABLE_thaw on %s at 0x%"UVxf" (%"IVdf" args)",
3125 class, PTR2UV(sv), AvFILLp(av) + 1));
3128 (void) scalar_call(rv, hook, clone, av, G_SCALAR|G_DISCARD);
3135 SvREFCNT_dec(frozen);
3138 if (!(flags & SHF_IDX_CLASSNAME) && class != buf)
3147 * Retrieve reference to some other scalar.
3148 * Layout is SX_REF <object>, with SX_REF already read.
3150 static SV *retrieve_ref(stcxt_t *cxt)
3155 TRACEME(("retrieve_ref (#%d)", cxt->tagnum));
3158 * We need to create the SV that holds the reference to the yet-to-retrieve
3159 * object now, so that we may record the address in the seen table.
3160 * Otherwise, if the object to retrieve references us, we won't be able
3161 * to resolve the SX_OBJECT we'll see at that point! Hence we cannot
3162 * do the retrieve first and use rv = newRV(sv) since it will be too late
3163 * for SEEN() recording.
3166 rv = NEWSV(10002, 0);
3167 SEEN(rv); /* Will return if rv is null */
3168 sv = retrieve(cxt); /* Retrieve <object> */
3170 return (SV *) 0; /* Failed */
3173 * WARNING: breaks RV encapsulation.
3175 * Now for the tricky part. We have to upgrade our existing SV, so that
3176 * it is now an RV on sv... Again, we cheat by duplicating the code
3177 * held in newSVrv(), since we already got our SV from retrieve().
3181 * SvRV(rv) = SvREFCNT_inc(sv);
3183 * here because the reference count we got from retrieve() above is
3184 * already correct: if the object was retrieved from the file, then
3185 * its reference count is one. Otherwise, if it was retrieved via
3186 * an SX_OBJECT indication, a ref count increment was done.
3189 sv_upgrade(rv, SVt_RV);
3190 SvRV(rv) = sv; /* $rv = \$sv */
3193 TRACEME(("ok (retrieve_ref at 0x%"UVxf")", PTR2UV(rv)));
3199 * retrieve_overloaded
3201 * Retrieve reference to some other scalar with overloading.
3202 * Layout is SX_OVERLOAD <object>, with SX_OVERLOAD already read.
3204 static SV *retrieve_overloaded(stcxt_t *cxt)
3210 TRACEME(("retrieve_overloaded (#%d)", cxt->tagnum));
3213 * Same code as retrieve_ref(), duplicated to avoid extra call.
3216 rv = NEWSV(10002, 0);
3217 SEEN(rv); /* Will return if rv is null */
3218 sv = retrieve(cxt); /* Retrieve <object> */
3220 return (SV *) 0; /* Failed */
3223 * WARNING: breaks RV encapsulation.
3226 sv_upgrade(rv, SVt_RV);
3227 SvRV(rv) = sv; /* $rv = \$sv */
3231 * Restore overloading magic.
3234 stash = (HV *) SvSTASH (sv);
3235 if (!stash || !Gv_AMG(stash))
3236 CROAK(("Cannot restore overloading on %s(0x%"UVxf")",
3237 sv_reftype(sv, FALSE),
3242 TRACEME(("ok (retrieve_overloaded at 0x%"UVxf")", PTR2UV(rv)));
3248 * retrieve_tied_array
3250 * Retrieve tied array
3251 * Layout is SX_TIED_ARRAY <object>, with SX_TIED_ARRAY already read.
3253 static SV *retrieve_tied_array(stcxt_t *cxt)
3258 TRACEME(("retrieve_tied_array (#%d)", cxt->tagnum));
3260 tv = NEWSV(10002, 0);
3261 SEEN(tv); /* Will return if tv is null */
3262 sv = retrieve(cxt); /* Retrieve <object> */
3264 return (SV *) 0; /* Failed */
3266 sv_upgrade(tv, SVt_PVAV);
3267 AvREAL_off((AV *)tv);
3268 sv_magic(tv, sv, 'P', Nullch, 0);
3269 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
3271 TRACEME(("ok (retrieve_tied_array at 0x%"UVxf")", PTR2UV(tv)));
3277 * retrieve_tied_hash
3279 * Retrieve tied hash
3280 * Layout is SX_TIED_HASH <object>, with SX_TIED_HASH already read.
3282 static SV *retrieve_tied_hash(stcxt_t *cxt)
3287 TRACEME(("retrieve_tied_hash (#%d)", cxt->tagnum));
3289 tv = NEWSV(10002, 0);
3290 SEEN(tv); /* Will return if tv is null */
3291 sv = retrieve(cxt); /* Retrieve <object> */
3293 return (SV *) 0; /* Failed */
3295 sv_upgrade(tv, SVt_PVHV);
3296 sv_magic(tv, sv, 'P', Nullch, 0);
3297 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
3299 TRACEME(("ok (retrieve_tied_hash at 0x%"UVxf")", PTR2UV(tv)));
3305 * retrieve_tied_scalar
3307 * Retrieve tied scalar
3308 * Layout is SX_TIED_SCALAR <object>, with SX_TIED_SCALAR already read.
3310 static SV *retrieve_tied_scalar(cxt)
3316 TRACEME(("retrieve_tied_scalar (#%d)", cxt->tagnum));
3318 tv = NEWSV(10002, 0);
3319 SEEN(tv); /* Will return if rv is null */
3320 sv = retrieve(cxt); /* Retrieve <object> */
3322 return (SV *) 0; /* Failed */
3324 sv_upgrade(tv, SVt_PVMG);
3325 sv_magic(tv, sv, 'q', Nullch, 0);
3326 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
3328 TRACEME(("ok (retrieve_tied_scalar at 0x%"UVxf")", PTR2UV(tv)));
3336 * Retrieve reference to value in a tied hash.
3337 * Layout is SX_TIED_KEY <object> <key>, with SX_TIED_KEY already read.
3339 static SV *retrieve_tied_key(stcxt_t *cxt)
3345 TRACEME(("retrieve_tied_key (#%d)", cxt->tagnum));
3347 tv = NEWSV(10002, 0);
3348 SEEN(tv); /* Will return if tv is null */
3349 sv = retrieve(cxt); /* Retrieve <object> */
3351 return (SV *) 0; /* Failed */
3353 key = retrieve(cxt); /* Retrieve <key> */
3355 return (SV *) 0; /* Failed */
3357 sv_upgrade(tv, SVt_PVMG);
3358 sv_magic(tv, sv, 'p', (char *)key, HEf_SVKEY);
3359 SvREFCNT_dec(key); /* Undo refcnt inc from sv_magic() */
3360 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
3368 * Retrieve reference to value in a tied array.
3369 * Layout is SX_TIED_IDX <object> <idx>, with SX_TIED_IDX already read.
3371 static SV *retrieve_tied_idx(stcxt_t *cxt)
3377 TRACEME(("retrieve_tied_idx (#%d)", cxt->tagnum));
3379 tv = NEWSV(10002, 0);
3380 SEEN(tv); /* Will return if tv is null */
3381 sv = retrieve(cxt); /* Retrieve <object> */
3383 return (SV *) 0; /* Failed */
3385 RLEN(idx); /* Retrieve <idx> */
3387 sv_upgrade(tv, SVt_PVMG);
3388 sv_magic(tv, sv, 'p', Nullch, idx);
3389 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
3398 * Retrieve defined long (string) scalar.
3400 * Layout is SX_LSCALAR <length> <data>, with SX_LSCALAR already read.
3401 * The scalar is "long" in that <length> is larger than LG_SCALAR so it
3402 * was not stored on a single byte.
3404 static SV *retrieve_lscalar(stcxt_t *cxt)
3410 TRACEME(("retrieve_lscalar (#%d), len = %"IVdf, cxt->tagnum, len));
3413 * Allocate an empty scalar of the suitable length.
3416 sv = NEWSV(10002, len);
3417 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3420 * WARNING: duplicates parts of sv_setpv and breaks SV data encapsulation.
3422 * Now, for efficiency reasons, read data directly inside the SV buffer,
3423 * and perform the SV final settings directly by duplicating the final
3424 * work done by sv_setpv. Since we're going to allocate lots of scalars
3425 * this way, it's worth the hassle and risk.
3428 SAFEREAD(SvPVX(sv), len, sv);
3429 SvCUR_set(sv, len); /* Record C string length */
3430 *SvEND(sv) = '\0'; /* Ensure it's null terminated anyway */
3431 (void) SvPOK_only(sv); /* Validate string pointer */
3432 SvTAINT(sv); /* External data cannot be trusted */
3434 TRACEME(("large scalar len %"IVdf" '%s'", len, SvPVX(sv)));
3435 TRACEME(("ok (retrieve_lscalar at 0x%"UVxf")", PTR2UV(sv)));
3443 * Retrieve defined short (string) scalar.
3445 * Layout is SX_SCALAR <length> <data>, with SX_SCALAR already read.
3446 * The scalar is "short" so <length> is single byte. If it is 0, there
3447 * is no <data> section.
3449 static SV *retrieve_scalar(stcxt_t *cxt)
3455 TRACEME(("retrieve_scalar (#%d), len = %d", cxt->tagnum, len));
3458 * Allocate an empty scalar of the suitable length.
3461 sv = NEWSV(10002, len);
3462 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3465 * WARNING: duplicates parts of sv_setpv and breaks SV data encapsulation.
3470 * newSV did not upgrade to SVt_PV so the scalar is undefined.
3471 * To make it defined with an empty length, upgrade it now...
3473 sv_upgrade(sv, SVt_PV);
3475 *SvEND(sv) = '\0'; /* Ensure it's null terminated anyway */
3476 TRACEME(("ok (retrieve_scalar empty at 0x%"UVxf")", PTR2UV(sv)));
3479 * Now, for efficiency reasons, read data directly inside the SV buffer,
3480 * and perform the SV final settings directly by duplicating the final
3481 * work done by sv_setpv. Since we're going to allocate lots of scalars
3482 * this way, it's worth the hassle and risk.
3484 SAFEREAD(SvPVX(sv), len, sv);
3485 SvCUR_set(sv, len); /* Record C string length */
3486 *SvEND(sv) = '\0'; /* Ensure it's null terminated anyway */
3487 TRACEME(("small scalar len %d '%s'", len, SvPVX(sv)));
3490 (void) SvPOK_only(sv); /* Validate string pointer */
3491 SvTAINT(sv); /* External data cannot be trusted */
3493 TRACEME(("ok (retrieve_scalar at 0x%"UVxf")", PTR2UV(sv)));
3500 * Retrieve defined integer.
3501 * Layout is SX_INTEGER <data>, whith SX_INTEGER already read.
3503 static SV *retrieve_integer(stcxt_t *cxt)
3508 TRACEME(("retrieve_integer (#%d)", cxt->tagnum));
3510 READ(&iv, sizeof(iv));
3512 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3514 TRACEME(("integer %"IVdf, iv));
3515 TRACEME(("ok (retrieve_integer at 0x%"UVxf")", PTR2UV(sv)));
3523 * Retrieve defined integer in network order.
3524 * Layout is SX_NETINT <data>, whith SX_NETINT already read.
3526 static SV *retrieve_netint(stcxt_t *cxt)
3531 TRACEME(("retrieve_netint (#%d)", cxt->tagnum));
3535 sv = newSViv((int) ntohl(iv));
3536 TRACEME(("network integer %d", (int) ntohl(iv)));
3539 TRACEME(("network integer (as-is) %d", iv));
3541 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3543 TRACEME(("ok (retrieve_netint at 0x%"UVxf")", PTR2UV(sv)));
3551 * Retrieve defined double.
3552 * Layout is SX_DOUBLE <data>, whith SX_DOUBLE already read.
3554 static SV *retrieve_double(stcxt_t *cxt)
3559 TRACEME(("retrieve_double (#%d)", cxt->tagnum));
3561 READ(&nv, sizeof(nv));
3563 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3565 TRACEME(("double %"NVff, nv));
3566 TRACEME(("ok (retrieve_double at 0x%"UVxf")", PTR2UV(sv)));
3574 * Retrieve defined byte (small integer within the [-128, +127] range).
3575 * Layout is SX_BYTE <data>, whith SX_BYTE already read.
3577 static SV *retrieve_byte(stcxt_t *cxt)
3582 TRACEME(("retrieve_byte (#%d)", cxt->tagnum));
3585 TRACEME(("small integer read as %d", (unsigned char) siv));
3586 sv = newSViv((unsigned char) siv - 128);
3587 SEEN(sv); /* Associate this new scalar with tag "tagnum" */
3589 TRACEME(("byte %d", (unsigned char) siv - 128));
3590 TRACEME(("ok (retrieve_byte at 0x%"UVxf")", PTR2UV(sv)));
3598 * Return the undefined value.
3600 static SV *retrieve_undef(stcxt_t *cxt)
3604 TRACEME(("retrieve_undef"));
3615 * Return the immortal undefined value.
3617 static SV *retrieve_sv_undef(stcxt_t *cxt)
3619 SV *sv = &PL_sv_undef;
3621 TRACEME(("retrieve_sv_undef"));
3630 * Return the immortal yes value.
3632 static SV *retrieve_sv_yes(stcxt_t *cxt)
3634 SV *sv = &PL_sv_yes;
3636 TRACEME(("retrieve_sv_yes"));
3645 * Return the immortal no value.
3647 static SV *retrieve_sv_no(stcxt_t *cxt)
3651 TRACEME(("retrieve_sv_no"));
3660 * Retrieve a whole array.
3661 * Layout is SX_ARRAY <size> followed by each item, in increading index order.
3662 * Each item is stored as <object>.
3664 * When we come here, SX_ARRAY has been read already.
3666 static SV *retrieve_array(stcxt_t *cxt)
3673 TRACEME(("retrieve_array (#%d)", cxt->tagnum));
3676 * Read length, and allocate array, then pre-extend it.
3680 TRACEME(("size = %d", len));
3682 SEEN(av); /* Will return if array not allocated nicely */
3686 return (SV *) av; /* No data follow if array is empty */
3689 * Now get each item in turn...
3692 for (i = 0; i < len; i++) {
3693 TRACEME(("(#%d) item", i));
3694 sv = retrieve(cxt); /* Retrieve item */
3697 if (av_store(av, i, sv) == 0)
3701 TRACEME(("ok (retrieve_array at 0x%"UVxf")", PTR2UV(av)));
3709 * Retrieve a whole hash table.
3710 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
3711 * Keys are stored as <length> <data>, the <data> section being omitted
3713 * Values are stored as <object>.
3715 * When we come here, SX_HASH has been read already.
3717 static SV *retrieve_hash(stcxt_t *cxt)
3724 static SV *sv_h_undef = (SV *) 0; /* hv_store() bug */
3726 TRACEME(("retrieve_hash (#%d)", cxt->tagnum));
3729 * Read length, allocate table.
3733 TRACEME(("size = %d", len));
3735 SEEN(hv); /* Will return if table not allocated properly */
3737 return (SV *) hv; /* No data follow if table empty */
3740 * Now get each key/value pair in turn...
3743 for (i = 0; i < len; i++) {
3748 TRACEME(("(#%d) value", i));
3755 * Since we're reading into kbuf, we must ensure we're not
3756 * recursing between the read and the hv_store() where it's used.
3757 * Hence the key comes after the value.
3760 RLEN(size); /* Get key size */
3761 KBUFCHK(size); /* Grow hash key read pool if needed */
3764 kbuf[size] = '\0'; /* Mark string end, just in case */
3765 TRACEME(("(#%d) key '%s'", i, kbuf));
3768 * Enter key/value pair into hash table.
3771 if (hv_store(hv, kbuf, (U32) size, sv, 0) == 0)
3775 TRACEME(("ok (retrieve_hash at 0x%"UVxf")", PTR2UV(hv)));
3781 * old_retrieve_array
3783 * Retrieve a whole array in pre-0.6 binary format.
3785 * Layout is SX_ARRAY <size> followed by each item, in increading index order.
3786 * Each item is stored as SX_ITEM <object> or SX_IT_UNDEF for "holes".
3788 * When we come here, SX_ARRAY has been read already.
3790 static SV *old_retrieve_array(stcxt_t *cxt)
3798 TRACEME(("old_retrieve_array (#%d)", cxt->tagnum));
3801 * Read length, and allocate array, then pre-extend it.
3805 TRACEME(("size = %d", len));
3807 SEEN(av); /* Will return if array not allocated nicely */
3811 return (SV *) av; /* No data follow if array is empty */
3814 * Now get each item in turn...
3817 for (i = 0; i < len; i++) {
3819 if (c == SX_IT_UNDEF) {
3820 TRACEME(("(#%d) undef item", i));
3821 continue; /* av_extend() already filled us with undef */
3824 (void) retrieve_other((stcxt_t *) 0); /* Will croak out */
3825 TRACEME(("(#%d) item", i));
3826 sv = retrieve(cxt); /* Retrieve item */
3829 if (av_store(av, i, sv) == 0)
3833 TRACEME(("ok (old_retrieve_array at 0x%"UVxf")", PTR2UV(av)));
3841 * Retrieve a whole hash table in pre-0.6 binary format.
3843 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
3844 * Keys are stored as SX_KEY <length> <data>, the <data> section being omitted
3846 * Values are stored as SX_VALUE <object> or SX_VL_UNDEF for "holes".
3848 * When we come here, SX_HASH has been read already.
3850 static SV *old_retrieve_hash(stcxt_t *cxt)
3858 static SV *sv_h_undef = (SV *) 0; /* hv_store() bug */
3860 TRACEME(("old_retrieve_hash (#%d)", cxt->tagnum));
3863 * Read length, allocate table.
3867 TRACEME(("size = %d", len));
3869 SEEN(hv); /* Will return if table not allocated properly */
3871 return (SV *) hv; /* No data follow if table empty */
3874 * Now get each key/value pair in turn...
3877 for (i = 0; i < len; i++) {
3883 if (c == SX_VL_UNDEF) {
3884 TRACEME(("(#%d) undef value", i));
3886 * Due to a bug in hv_store(), it's not possible to pass
3887 * &PL_sv_undef to hv_store() as a value, otherwise the
3888 * associated key will not be creatable any more. -- RAM, 14/01/97
3891 sv_h_undef = newSVsv(&PL_sv_undef);
3892 sv = SvREFCNT_inc(sv_h_undef);
3893 } else if (c == SX_VALUE) {
3894 TRACEME(("(#%d) value", i));
3899 (void) retrieve_other((stcxt_t *) 0); /* Will croak out */
3903 * Since we're reading into kbuf, we must ensure we're not
3904 * recursing between the read and the hv_store() where it's used.
3905 * Hence the key comes after the value.
3910 (void) retrieve_other((stcxt_t *) 0); /* Will croak out */
3911 RLEN(size); /* Get key size */
3912 KBUFCHK(size); /* Grow hash key read pool if needed */
3915 kbuf[size] = '\0'; /* Mark string end, just in case */
3916 TRACEME(("(#%d) key '%s'", i, kbuf));
3919 * Enter key/value pair into hash table.
3922 if (hv_store(hv, kbuf, (U32) size, sv, 0) == 0)
3926 TRACEME(("ok (retrieve_hash at 0x%"UVxf")", PTR2UV(hv)));
3932 *** Retrieval engine.
3938 * Make sure the stored data we're trying to retrieve has been produced
3939 * on an ILP compatible system with the same byteorder. It croaks out in
3940 * case an error is detected. [ILP = integer-long-pointer sizes]
3941 * Returns null if error is detected, &PL_sv_undef otherwise.
3943 * Note that there's no byte ordering info emitted when network order was
3944 * used at store time.
3946 static SV *magic_check(stcxt_t *cxt)
3949 char byteorder[256];
3951 int use_network_order;
3953 int version_minor = 0;
3955 TRACEME(("magic_check"));
3958 * The "magic number" is only for files, not when freezing in memory.
3962 STRLEN len = sizeof(magicstr) - 1;
3965 READ(buf, len); /* Not null-terminated */
3966 buf[len] = '\0'; /* Is now */
3968 if (0 == strcmp(buf, magicstr))
3972 * Try to read more bytes to check for the old magic number, which
3976 old_len = sizeof(old_magicstr) - 1;
3977 READ(&buf[len], old_len - len);
3978 buf[old_len] = '\0'; /* Is now null-terminated */
3980 if (strcmp(buf, old_magicstr))
3981 CROAK(("File is not a perl storable"));
3986 * Starting with 0.6, the "use_network_order" byte flag is also used to
3987 * indicate the version number of the binary, and therefore governs the
3988 * setting of sv_retrieve_vtbl. See magic_write().
3991 GETMARK(use_network_order);
3992 version_major = use_network_order >> 1;
3993 cxt->retrieve_vtbl = version_major ? sv_retrieve : sv_old_retrieve;
3995 TRACEME(("magic_check: netorder = 0x%x", use_network_order));
3999 * Starting with 0.7 (binary major 2), a full byte is dedicated to the
4000 * minor version of the protocol. See magic_write().
4003 if (version_major > 1)
4004 GETMARK(version_minor);
4006 cxt->ver_major = version_major;
4007 cxt->ver_minor = version_minor;
4009 TRACEME(("binary image version is %d.%d", version_major, version_minor));
4012 * Inter-operability sanity check: we can't retrieve something stored
4013 * using a format more recent than ours, because we have no way to
4014 * know what has changed, and letting retrieval go would mean a probable
4015 * failure reporting a "corrupted" storable file.
4019 version_major > STORABLE_BIN_MAJOR ||
4020 (version_major == STORABLE_BIN_MAJOR &&
4021 version_minor > STORABLE_BIN_MINOR)
4023 CROAK(("Storable binary image v%d.%d more recent than I am (v%d.%d)",
4024 version_major, version_minor,
4025 STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR));
4028 * If they stored using network order, there's no byte ordering
4029 * information to check.
4032 if (cxt->netorder = (use_network_order & 0x1))
4033 return &PL_sv_undef; /* No byte ordering info */
4035 sprintf(byteorder, "%lx", (unsigned long) BYTEORDER);
4037 READ(buf, c); /* Not null-terminated */
4038 buf[c] = '\0'; /* Is now */
4040 if (strcmp(buf, byteorder))
4041 CROAK(("Byte order is not compatible"));
4043 GETMARK(c); /* sizeof(int) */
4044 if ((int) c != sizeof(int))
4045 CROAK(("Integer size is not compatible"));
4047 GETMARK(c); /* sizeof(long) */
4048 if ((int) c != sizeof(long))
4049 CROAK(("Long integer size is not compatible"));
4051 GETMARK(c); /* sizeof(char *) */
4052 if ((int) c != sizeof(char *))
4053 CROAK(("Pointer integer size is not compatible"));
4055 if (version_major >= 2 && version_minor >= 2) {
4056 GETMARK(c); /* sizeof(NV) */
4057 if ((int) c != sizeof(NV))
4058 CROAK(("Double size is not compatible"));
4061 return &PL_sv_undef; /* OK */
4067 * Recursively retrieve objects from the specified file and return their
4068 * root SV (which may be an AV or an HV for what we care).
4069 * Returns null if there is a problem.
4071 static SV *retrieve(stcxt_t *cxt)
4077 TRACEME(("retrieve"));
4080 * Grab address tag which identifies the object if we are retrieving
4081 * an older format. Since the new binary format counts objects and no
4082 * longer explicitely tags them, we must keep track of the correspondance
4085 * The following section will disappear one day when the old format is
4086 * no longer supported, hence the final "goto" in the "if" block.
4089 if (cxt->hseen) { /* Retrieving old binary */
4091 if (cxt->netorder) {
4093 READ(&nettag, sizeof(I32)); /* Ordered sequence of I32 */
4094 tag = (stag_t) nettag;
4096 READ(&tag, sizeof(stag_t)); /* Original address of the SV */
4099 if (type == SX_OBJECT) {
4101 svh = hv_fetch(cxt->hseen, (char *) &tag, sizeof(tag), FALSE);
4103 CROAK(("Old tag 0x%x should have been mapped already", tag));
4104 tagn = SvIV(*svh); /* Mapped tag number computed earlier below */
4107 * The following code is common with the SX_OBJECT case below.
4110 svh = av_fetch(cxt->aseen, tagn, FALSE);
4112 CROAK(("Object #%d should have been retrieved already", tagn));
4114 TRACEME(("has retrieved #%d at 0x%"UVxf, tagn, PTR2UV(sv)));
4115 SvREFCNT_inc(sv); /* One more reference to this same sv */
4116 return sv; /* The SV pointer where object was retrieved */
4120 * Map new object, but don't increase tagnum. This will be done
4121 * by each of the retrieve_* functions when they call SEEN().
4123 * The mapping associates the "tag" initially present with a unique
4124 * tag number. See test for SX_OBJECT above to see how this is perused.
4127 if (!hv_store(cxt->hseen, (char *) &tag, sizeof(tag),
4128 newSViv(cxt->tagnum), 0))
4135 * Regular post-0.6 binary format.
4141 TRACEME(("retrieve type = %d", type));
4144 * Are we dealing with an object we should have already retrieved?
4147 if (type == SX_OBJECT) {
4151 svh = av_fetch(cxt->aseen, tag, FALSE);
4153 CROAK(("Object #%d should have been retrieved already", tag));
4155 TRACEME(("had retrieved #%d at 0x%"UVxf, tag, PTR2UV(sv)));
4156 SvREFCNT_inc(sv); /* One more reference to this same sv */
4157 return sv; /* The SV pointer where object was retrieved */
4160 first_time: /* Will disappear when support for old format is dropped */
4163 * Okay, first time through for this one.
4166 sv = RETRIEVE(cxt, type)(cxt);
4168 return (SV *) 0; /* Failed */
4171 * Old binary formats (pre-0.7).
4173 * Final notifications, ended by SX_STORED may now follow.
4174 * Currently, the only pertinent notification to apply on the
4175 * freshly retrieved object is either:
4176 * SX_CLASS <char-len> <classname> for short classnames.
4177 * SX_LG_CLASS <int-len> <classname> for larger one (rare!).
4178 * Class name is then read into the key buffer pool used by
4179 * hash table key retrieval.
4182 if (cxt->ver_major < 2) {
4183 while ((type = GETCHAR()) != SX_STORED) {
4187 GETMARK(len); /* Length coded on a single char */
4189 case SX_LG_CLASS: /* Length coded on a regular integer */
4194 return (SV *) 0; /* Failed */
4196 KBUFCHK(len); /* Grow buffer as necessary */
4199 kbuf[len] = '\0'; /* Mark string end */
4204 TRACEME(("ok (retrieved 0x%"UVxf", refcnt=%d, %s)", PTR2UV(sv),
4205 SvREFCNT(sv) - 1, sv_reftype(sv, FALSE)));
4213 * Retrieve data held in file and return the root object.
4214 * Common routine for pretrieve and mretrieve.
4216 static SV *do_retrieve(
4223 struct extendable msave; /* Where potentially valid mbuf is saved */
4225 TRACEME(("do_retrieve (optype = 0x%x)", optype));
4227 optype |= ST_RETRIEVE;
4230 * Sanity assertions for retrieve dispatch tables.
4233 ASSERT(sizeof(sv_old_retrieve) == sizeof(sv_retrieve),
4234 ("old and new retrieve dispatch table have same size"));
4235 ASSERT(sv_old_retrieve[SX_ERROR] == retrieve_other,
4236 ("SX_ERROR entry correctly initialized in old dispatch table"));
4237 ASSERT(sv_retrieve[SX_ERROR] == retrieve_other,
4238 ("SX_ERROR entry correctly initialized in new dispatch table"));
4241 * Workaround for CROAK leak: if they enter with a "dirty" context,
4242 * free up memory for them now.
4249 * Now that STORABLE_xxx hooks exist, it is possible that they try to
4250 * re-enter retrieve() via the hooks.
4254 cxt = allocate_context(cxt);
4258 ASSERT(cxt->entry == 1, ("starting new recursion"));
4259 ASSERT(!cxt->dirty, ("clean context"));
4264 * Data is loaded into the memory buffer when f is NULL, unless `in' is
4265 * also NULL, in which case we're expecting the data to already lie
4266 * in the buffer (dclone case).
4269 KBUFINIT(); /* Allocate hash key reading pool once */
4272 StructCopy(&cxt->membuf, &msave, struct extendable);
4278 * Magic number verifications.
4280 * This needs to be done before calling init_retrieve_context()
4281 * since the format indication in the file are necessary to conduct
4282 * some of the initializations.
4285 cxt->fio = f; /* Where I/O are performed */
4287 if (!magic_check(cxt))
4288 CROAK(("Magic number checking on storable %s failed",
4289 cxt->fio ? "file" : "string"));
4291 TRACEME(("data stored in %s format",
4292 cxt->netorder ? "net order" : "native"));
4294 init_retrieve_context(cxt, optype);
4296 ASSERT(is_retrieving(), ("within retrieve operation"));
4298 sv = retrieve(cxt); /* Recursively retrieve object, get root SV */
4305 StructCopy(&msave, &cxt->membuf, struct extendable);
4308 * The "root" context is never freed.
4311 clean_retrieve_context(cxt);
4312 if (cxt->prev) /* This context was stacked */
4313 free_context(cxt); /* It was not the "root" context */
4316 * Prepare returned value.
4320 TRACEME(("retrieve ERROR"));
4321 return &PL_sv_undef; /* Something went wrong, return undef */
4324 TRACEME(("retrieve got %s(0x%"UVxf")",
4325 sv_reftype(sv, FALSE), PTR2UV(sv)));
4328 * Backward compatibility with Storable-0.5@9 (which we know we
4329 * are retrieving if hseen is non-null): don't create an extra RV
4330 * for objects since we special-cased it at store time.
4332 * Build a reference to the SV returned by pretrieve even if it is
4333 * already one and not a scalar, for consistency reasons.
4335 * NB: although context might have been cleaned, the value of `cxt->hseen'
4336 * remains intact, and can be used as a flag.
4339 if (cxt->hseen) { /* Was not handling overloading by then */
4341 if (sv_type(sv) == svis_REF && (rv = SvRV(sv)) && SvOBJECT(rv))
4346 * If reference is overloaded, restore behaviour.
4348 * NB: minor glitch here: normally, overloaded refs are stored specially
4349 * so that we can croak when behaviour cannot be re-installed, and also
4350 * avoid testing for overloading magic at each reference retrieval.
4352 * Unfortunately, the root reference is implicitely stored, so we must
4353 * check for possible overloading now. Furthermore, if we don't restore
4354 * overloading, we cannot croak as if the original ref was, because we
4355 * have no way to determine whether it was an overloaded ref or not in
4358 * It's a pity that overloading magic is attached to the rv, and not to
4359 * the underlying sv as blessing is.
4363 HV *stash = (HV *) SvSTASH (sv);
4364 SV *rv = newRV_noinc(sv);
4365 if (stash && Gv_AMG(stash)) {
4367 TRACEME(("restored overloading on root reference"));
4372 return newRV_noinc(sv);
4378 * Retrieve data held in file and return the root object, undef on error.
4380 SV *pretrieve(PerlIO *f)
4382 TRACEME(("pretrieve"));
4383 return do_retrieve(f, Nullsv, 0);
4389 * Retrieve data held in scalar and return the root object, undef on error.
4391 SV *mretrieve(SV *sv)
4393 TRACEME(("mretrieve"));
4394 return do_retrieve((PerlIO*) 0, sv, 0);
4404 * Deep clone: returns a fresh copy of the original referenced SV tree.
4406 * This is achieved by storing the object in memory and restoring from
4407 * there. Not that efficient, but it should be faster than doing it from
4414 stcxt_t *real_context;
4417 TRACEME(("dclone"));
4420 * Workaround for CROAK leak: if they enter with a "dirty" context,
4421 * free up memory for them now.
4428 * do_store() optimizes for dclone by not freeing its context, should
4429 * we need to allocate one because we're deep cloning from a hook.
4432 if (!do_store((PerlIO*) 0, sv, ST_CLONE, FALSE, (SV**) 0))
4433 return &PL_sv_undef; /* Error during store */
4436 * Because of the above optimization, we have to refresh the context,
4437 * since a new one could have been allocated and stacked by do_store().
4440 { dSTCXT; real_context = cxt; } /* Sub-block needed for macro */
4441 cxt = real_context; /* And we need this temporary... */
4444 * Now, `cxt' may refer to a new context.
4447 ASSERT(!cxt->dirty, ("clean context"));
4448 ASSERT(!cxt->entry, ("entry will not cause new context allocation"));
4451 TRACEME(("dclone stored %d bytes", size));
4454 out = do_retrieve((PerlIO*) 0, Nullsv, ST_CLONE); /* Will free non-root context */
4456 TRACEME(("dclone returns 0x%"UVxf, PTR2UV(out)));
4466 * The Perl IO GV object distinguishes between input and output for sockets
4467 * but not for plain files. To allow Storable to transparently work on
4468 * plain files and sockets transparently, we have to ask xsubpp to fetch the
4469 * right object for us. Hence the OutputStream and InputStream declarations.
4471 * Before perl 5.004_05, those entries in the standard typemap are not
4472 * defined in perl include files, so we do that here.
4475 #ifndef OutputStream
4476 #define OutputStream PerlIO *
4477 #define InputStream PerlIO *
4478 #endif /* !OutputStream */
4480 MODULE = Storable PACKAGE = Storable
4518 last_op_in_netorder()