/* * Store and retrieve mechanism. * * Copyright (c) 1995-2000, Raphael Manfredi * * You may redistribute only under the same terms as Perl 5, as specified * in the README file that comes with the distribution. * */ #define PERL_NO_GET_CONTEXT /* we want efficiency */ #include #include #include #ifndef PATCHLEVEL #include /* Perl's one, needed since 5.6 */ #endif #if !defined(PERL_VERSION) || PERL_VERSION < 8 #include "ppport.h" /* handle old perls */ #endif #if 0 #define DEBUGME /* Debug mode, turns assertions on as well */ #define DASSERT /* Assertion mode */ #endif /* * Pre PerlIO time when none of USE_PERLIO and PERLIO_IS_STDIO is defined * Provide them with the necessary defines so they can build with pre-5.004. */ #ifndef USE_PERLIO #ifndef PERLIO_IS_STDIO #define PerlIO FILE #define PerlIO_getc(x) getc(x) #define PerlIO_putc(f,x) putc(x,f) #define PerlIO_read(x,y,z) fread(y,1,z,x) #define PerlIO_write(x,y,z) fwrite(y,1,z,x) #define PerlIO_stdoutf printf #endif /* PERLIO_IS_STDIO */ #endif /* USE_PERLIO */ /* * Earlier versions of perl might be used, we can't assume they have the latest! */ #ifndef PERL_VERSION /* For perls < 5.6 */ #define PERL_VERSION PATCHLEVEL #ifndef newRV_noinc #define newRV_noinc(sv) ((Sv = newRV(sv)), --SvREFCNT(SvRV(Sv)), Sv) #endif #if (PATCHLEVEL <= 4) /* Older perls (<= 5.004) lack PL_ namespace */ #define PL_sv_yes sv_yes #define PL_sv_no sv_no #define PL_sv_undef sv_undef #if (SUBVERSION <= 4) /* 5.004_04 has been reported to lack newSVpvn */ #define newSVpvn newSVpv #endif #endif /* PATCHLEVEL <= 4 */ #ifndef HvSHAREKEYS_off #define HvSHAREKEYS_off(hv) /* Ignore */ #endif #ifndef AvFILLp /* Older perls (<=5.003) lack AvFILLp */ #define AvFILLp AvFILL #endif typedef double NV; /* Older perls lack the NV type */ #define IVdf "ld" /* Various printf formats for Perl types */ #define UVuf "lu" #define UVof "lo" #define UVxf "lx" #define INT2PTR(t,v) (t)(IV)(v) #define PTR2UV(v) (unsigned long)(v) #endif /* PERL_VERSION -- perls < 5.6 */ #ifndef NVef /* The following were not part of perl 5.6 */ #if defined(USE_LONG_DOUBLE) && \ defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl) #define NVef PERL_PRIeldbl #define NVff PERL_PRIfldbl #define NVgf PERL_PRIgldbl #else #define NVef "e" #define NVff "f" #define NVgf "g" #endif #endif #ifndef SvRV_set #define SvRV_set(sv, val) \ STMT_START { \ assert(SvTYPE(sv) >= SVt_RV); \ (((XRV*)SvANY(sv))->xrv_rv = (val)); \ } STMT_END #endif #ifndef PERL_UNUSED_DECL # ifdef HASATTRIBUTE # if (defined(__GNUC__) && defined(__cplusplus)) || defined(__INTEL_COMPILER) # define PERL_UNUSED_DECL # else # define PERL_UNUSED_DECL __attribute__((unused)) # endif # else # define PERL_UNUSED_DECL # endif #endif #ifndef dNOOP #define dNOOP extern int Perl___notused PERL_UNUSED_DECL #endif #ifndef dVAR #define dVAR dNOOP #endif #ifndef HvRITER_set # define HvRITER_set(hv,r) (HvRITER(hv) = r) #endif #ifndef HvEITER_set # define HvEITER_set(hv,r) (HvEITER(hv) = r) #endif #ifndef HvRITER_get # define HvRITER_get HvRITER #endif #ifndef HvEITER_get # define HvEITER_get HvEITER #endif #ifndef HvNAME_get #define HvNAME_get HvNAME #endif #ifndef HvPLACEHOLDERS_get # define HvPLACEHOLDERS_get HvPLACEHOLDERS #endif #ifdef DEBUGME #ifndef DASSERT #define DASSERT #endif /* * TRACEME() will only output things when the $Storable::DEBUGME is true. */ #define TRACEME(x) \ STMT_START { \ if (SvTRUE(perl_get_sv("Storable::DEBUGME", TRUE))) \ { PerlIO_stdoutf x; PerlIO_stdoutf("\n"); } \ } STMT_END #else #define TRACEME(x) #endif /* DEBUGME */ #ifdef DASSERT #define ASSERT(x,y) \ STMT_START { \ if (!(x)) { \ PerlIO_stdoutf("ASSERT FAILED (\"%s\", line %d): ", \ __FILE__, __LINE__); \ PerlIO_stdoutf y; PerlIO_stdoutf("\n"); \ } \ } STMT_END #else #define ASSERT(x,y) #endif /* * Type markers. */ #define C(x) ((char) (x)) /* For markers with dynamic retrieval handling */ #define SX_OBJECT C(0) /* Already stored object */ #define SX_LSCALAR C(1) /* Scalar (large binary) follows (length, data) */ #define SX_ARRAY C(2) /* Array forthcominng (size, item list) */ #define SX_HASH C(3) /* Hash forthcoming (size, key/value pair list) */ #define SX_REF C(4) /* Reference to object forthcoming */ #define SX_UNDEF C(5) /* Undefined scalar */ #define SX_INTEGER C(6) /* Integer forthcoming */ #define SX_DOUBLE C(7) /* Double forthcoming */ #define SX_BYTE C(8) /* (signed) byte forthcoming */ #define SX_NETINT C(9) /* Integer in network order forthcoming */ #define SX_SCALAR C(10) /* Scalar (binary, small) follows (length, data) */ #define SX_TIED_ARRAY C(11) /* Tied array forthcoming */ #define SX_TIED_HASH C(12) /* Tied hash forthcoming */ #define SX_TIED_SCALAR C(13) /* Tied scalar forthcoming */ #define SX_SV_UNDEF C(14) /* Perl's immortal PL_sv_undef */ #define SX_SV_YES C(15) /* Perl's immortal PL_sv_yes */ #define SX_SV_NO C(16) /* Perl's immortal PL_sv_no */ #define SX_BLESS C(17) /* Object is blessed */ #define SX_IX_BLESS C(18) /* Object is blessed, classname given by index */ #define SX_HOOK C(19) /* Stored via hook, user-defined */ #define SX_OVERLOAD C(20) /* Overloaded reference */ #define SX_TIED_KEY C(21) /* Tied magic key forthcoming */ #define SX_TIED_IDX C(22) /* Tied magic index forthcoming */ #define SX_UTF8STR C(23) /* UTF-8 string forthcoming (small) */ #define SX_LUTF8STR C(24) /* UTF-8 string forthcoming (large) */ #define SX_FLAG_HASH C(25) /* Hash with flags forthcoming (size, flags, key/flags/value triplet list) */ #define SX_CODE C(26) /* Code references as perl source code */ #define SX_WEAKREF C(27) /* Weak reference to object forthcoming */ #define SX_WEAKOVERLOAD C(28) /* Overloaded weak reference */ #define SX_ERROR C(29) /* Error */ /* * Those are only used to retrieve "old" pre-0.6 binary images. */ #define SX_ITEM 'i' /* An array item introducer */ #define SX_IT_UNDEF 'I' /* Undefined array item */ #define SX_KEY 'k' /* A hash key introducer */ #define SX_VALUE 'v' /* A hash value introducer */ #define SX_VL_UNDEF 'V' /* Undefined hash value */ /* * Those are only used to retrieve "old" pre-0.7 binary images */ #define SX_CLASS 'b' /* Object is blessed, class name length <255 */ #define SX_LG_CLASS 'B' /* Object is blessed, class name length >255 */ #define SX_STORED 'X' /* End of object */ /* * Limits between short/long length representation. */ #define LG_SCALAR 255 /* Large scalar length limit */ #define LG_BLESS 127 /* Large classname bless limit */ /* * Operation types */ #define ST_STORE 0x1 /* Store operation */ #define ST_RETRIEVE 0x2 /* Retrieval operation */ #define ST_CLONE 0x4 /* Deep cloning operation */ /* * The following structure is used for hash table key retrieval. Since, when * retrieving objects, we'll be facing blessed hash references, it's best * to pre-allocate that buffer once and resize it as the need arises, never * freeing it (keys will be saved away someplace else anyway, so even large * keys are not enough a motivation to reclaim that space). * * This structure is also used for memory store/retrieve operations which * happen in a fixed place before being malloc'ed elsewhere if persistency * is required. Hence the aptr pointer. */ struct extendable { char *arena; /* Will hold hash key strings, resized as needed */ STRLEN asiz; /* Size of aforementionned buffer */ char *aptr; /* Arena pointer, for in-place read/write ops */ char *aend; /* First invalid address */ }; /* * At store time: * A hash table records the objects which have already been stored. * Those are referred to as SX_OBJECT in the file, and their "tag" (i.e. * an arbitrary sequence number) is used to identify them. * * At retrieve time: * An array table records the objects which have already been retrieved, * as seen by the tag determind by counting the objects themselves. The * reference to that retrieved object is kept in the table, and is returned * when an SX_OBJECT is found bearing that same tag. * * The same processing is used to record "classname" for blessed objects: * indexing by a hash at store time, and via an array at retrieve time. */ typedef unsigned long stag_t; /* Used by pre-0.6 binary format */ /* * The following "thread-safe" related defines were contributed by * Murray Nesbitt and integrated by RAM, who * only renamed things a little bit to ensure consistency with surrounding * code. -- RAM, 14/09/1999 * * The original patch suffered from the fact that the stcxt_t structure * was global. Murray tried to minimize the impact on the code as much as * possible. * * Starting with 0.7, Storable can be re-entrant, via the STORABLE_xxx hooks * on objects. Therefore, the notion of context needs to be generalized, * threading or not. */ #define MY_VERSION "Storable(" XS_VERSION ")" /* * Conditional UTF8 support. * */ #ifdef SvUTF8_on #define STORE_UTF8STR(pv, len) STORE_PV_LEN(pv, len, SX_UTF8STR, SX_LUTF8STR) #define HAS_UTF8_SCALARS #ifdef HeKUTF8 #define HAS_UTF8_HASHES #define HAS_UTF8_ALL #else /* 5.6 perl has utf8 scalars but not hashes */ #endif #else #define SvUTF8(sv) 0 #define STORE_UTF8STR(pv, len) CROAK(("panic: storing UTF8 in non-UTF8 perl")) #endif #ifndef HAS_UTF8_ALL #define UTF8_CROAK() CROAK(("Cannot retrieve UTF8 data in non-UTF8 perl")) #endif #ifndef SvWEAKREF #define WEAKREF_CROAK() CROAK(("Cannot retrieve weak references in this perl")) #endif #ifdef HvPLACEHOLDERS #define HAS_RESTRICTED_HASHES #else #define HVhek_PLACEHOLD 0x200 #define RESTRICTED_HASH_CROAK() CROAK(("Cannot retrieve restricted hash")) #endif #ifdef HvHASKFLAGS #define HAS_HASH_KEY_FLAGS #endif #ifdef ptr_table_new #define USE_PTR_TABLE #endif /* * Fields s_tainted and s_dirty are prefixed with s_ because Perl's include * files remap tainted and dirty when threading is enabled. That's bad for * perl to remap such common words. -- RAM, 29/09/00 */ struct stcxt; typedef struct stcxt { int entry; /* flags recursion */ int optype; /* type of traversal operation */ /* which objects have been seen, store time. tags are numbers, which are cast to (SV *) and stored directly */ #ifdef USE_PTR_TABLE /* use pseen if we have ptr_tables. We have to store tag+1, because tag numbers start at 0, and we can't store (SV *) 0 in a ptr_table without it being confused for a fetch lookup failure. */ struct ptr_tbl *pseen; /* Still need hseen for the 0.6 file format code. */ #endif HV *hseen; AV *hook_seen; /* which SVs were returned by STORABLE_freeze() */ AV *aseen; /* which objects have been seen, retrieve time */ IV where_is_undef; /* index in aseen of PL_sv_undef */ HV *hclass; /* which classnames have been seen, store time */ AV *aclass; /* which classnames have been seen, retrieve time */ HV *hook; /* cache for hook methods per class name */ IV tagnum; /* incremented at store time for each seen object */ IV classnum; /* incremented at store time for each seen classname */ int netorder; /* true if network order used */ int s_tainted; /* true if input source is tainted, at retrieve time */ int forgive_me; /* whether to be forgiving... */ int deparse; /* whether to deparse code refs */ SV *eval; /* whether to eval source code */ int canonical; /* whether to store hashes sorted by key */ #ifndef HAS_RESTRICTED_HASHES int derestrict; /* whether to downgrade restrcted hashes */ #endif #ifndef HAS_UTF8_ALL int use_bytes; /* whether to bytes-ify utf8 */ #endif int accept_future_minor; /* croak immediately on future minor versions? */ int s_dirty; /* context is dirty due to CROAK() -- can be cleaned */ int membuf_ro; /* true means membuf is read-only and msaved is rw */ struct extendable keybuf; /* for hash key retrieval */ struct extendable membuf; /* for memory store/retrieve operations */ struct extendable msaved; /* where potentially valid mbuf is saved */ PerlIO *fio; /* where I/O are performed, NULL for memory */ int ver_major; /* major of version for retrieved object */ int ver_minor; /* minor of version for retrieved object */ SV *(**retrieve_vtbl)(pTHX_ struct stcxt *, const char *); /* retrieve dispatch table */ SV *prev; /* contexts chained backwards in real recursion */ SV *my_sv; /* the blessed scalar who's SvPVX() I am */ } stcxt_t; #define NEW_STORABLE_CXT_OBJ(cxt) \ STMT_START { \ SV *self = newSV(sizeof(stcxt_t) - 1); \ SV *my_sv = newRV_noinc(self); \ sv_bless(my_sv, gv_stashpv("Storable::Cxt", TRUE)); \ cxt = (stcxt_t *)SvPVX(self); \ Zero(cxt, 1, stcxt_t); \ cxt->my_sv = my_sv; \ } STMT_END #if defined(MULTIPLICITY) || defined(PERL_OBJECT) || defined(PERL_CAPI) #if (PATCHLEVEL <= 4) && (SUBVERSION < 68) #define dSTCXT_SV \ SV *perinterp_sv = perl_get_sv(MY_VERSION, FALSE) #else /* >= perl5.004_68 */ #define dSTCXT_SV \ SV *perinterp_sv = *hv_fetch(PL_modglobal, \ MY_VERSION, sizeof(MY_VERSION)-1, TRUE) #endif /* < perl5.004_68 */ #define dSTCXT_PTR(T,name) \ T name = ((perinterp_sv && SvIOK(perinterp_sv) && SvIVX(perinterp_sv) \ ? (T)SvPVX(SvRV(INT2PTR(SV*,SvIVX(perinterp_sv)))) : (T) 0)) #define dSTCXT \ dSTCXT_SV; \ dSTCXT_PTR(stcxt_t *, cxt) #define INIT_STCXT \ dSTCXT; \ NEW_STORABLE_CXT_OBJ(cxt); \ sv_setiv(perinterp_sv, PTR2IV(cxt->my_sv)) #define SET_STCXT(x) \ STMT_START { \ dSTCXT_SV; \ sv_setiv(perinterp_sv, PTR2IV(x->my_sv)); \ } STMT_END #else /* !MULTIPLICITY && !PERL_OBJECT && !PERL_CAPI */ static stcxt_t *Context_ptr = NULL; #define dSTCXT stcxt_t *cxt = Context_ptr #define SET_STCXT(x) Context_ptr = x #define INIT_STCXT \ dSTCXT; \ NEW_STORABLE_CXT_OBJ(cxt); \ SET_STCXT(cxt) #endif /* MULTIPLICITY || PERL_OBJECT || PERL_CAPI */ /* * KNOWN BUG: * Croaking implies a memory leak, since we don't use setjmp/longjmp * to catch the exit and free memory used during store or retrieve * operations. This is not too difficult to fix, but I need to understand * how Perl does it, and croaking is exceptional anyway, so I lack the * motivation to do it. * * The current workaround is to mark the context as dirty when croaking, * so that data structures can be freed whenever we renter Storable code * (but only *then*: it's a workaround, not a fix). * * This is also imperfect, because we don't really know how far they trapped * the croak(), and when we were recursing, we won't be able to clean anything * but the topmost context stacked. */ #define CROAK(x) STMT_START { cxt->s_dirty = 1; croak x; } STMT_END /* * End of "thread-safe" related definitions. */ /* * LOW_32BITS * * Keep only the low 32 bits of a pointer (used for tags, which are not * really pointers). */ #if PTRSIZE <= 4 #define LOW_32BITS(x) ((I32) (x)) #else #define LOW_32BITS(x) ((I32) ((unsigned long) (x) & 0xffffffffUL)) #endif /* * oI, oS, oC * * Hack for Crays, where sizeof(I32) == 8, and which are big-endians. * Used in the WLEN and RLEN macros. */ #if INTSIZE > 4 #define oI(x) ((I32 *) ((char *) (x) + 4)) #define oS(x) ((x) - 4) #define oC(x) (x = 0) #define CRAY_HACK #else #define oI(x) (x) #define oS(x) (x) #define oC(x) #endif /* * key buffer handling */ #define kbuf (cxt->keybuf).arena #define ksiz (cxt->keybuf).asiz #define KBUFINIT() \ STMT_START { \ if (!kbuf) { \ TRACEME(("** allocating kbuf of 128 bytes")); \ New(10003, kbuf, 128, char); \ ksiz = 128; \ } \ } STMT_END #define KBUFCHK(x) \ STMT_START { \ if (x >= ksiz) { \ TRACEME(("** extending kbuf to %d bytes (had %d)", x+1, ksiz)); \ Renew(kbuf, x+1, char); \ ksiz = x+1; \ } \ } STMT_END /* * memory buffer handling */ #define mbase (cxt->membuf).arena #define msiz (cxt->membuf).asiz #define mptr (cxt->membuf).aptr #define mend (cxt->membuf).aend #define MGROW (1 << 13) #define MMASK (MGROW - 1) #define round_mgrow(x) \ ((unsigned long) (((unsigned long) (x) + MMASK) & ~MMASK)) #define trunc_int(x) \ ((unsigned long) ((unsigned long) (x) & ~(sizeof(int)-1))) #define int_aligned(x) \ ((unsigned long) (x) == trunc_int(x)) #define MBUF_INIT(x) \ STMT_START { \ if (!mbase) { \ TRACEME(("** allocating mbase of %d bytes", MGROW)); \ New(10003, mbase, MGROW, char); \ msiz = (STRLEN)MGROW; \ } \ mptr = mbase; \ if (x) \ mend = mbase + x; \ else \ mend = mbase + msiz; \ } STMT_END #define MBUF_TRUNC(x) mptr = mbase + x #define MBUF_SIZE() (mptr - mbase) /* * MBUF_SAVE_AND_LOAD * MBUF_RESTORE * * Those macros are used in do_retrieve() to save the current memory * buffer into cxt->msaved, before MBUF_LOAD() can be used to retrieve * data from a string. */ #define MBUF_SAVE_AND_LOAD(in) \ STMT_START { \ ASSERT(!cxt->membuf_ro, ("mbase not already saved")); \ cxt->membuf_ro = 1; \ TRACEME(("saving mbuf")); \ StructCopy(&cxt->membuf, &cxt->msaved, struct extendable); \ MBUF_LOAD(in); \ } STMT_END #define MBUF_RESTORE() \ STMT_START { \ ASSERT(cxt->membuf_ro, ("mbase is read-only")); \ cxt->membuf_ro = 0; \ TRACEME(("restoring mbuf")); \ StructCopy(&cxt->msaved, &cxt->membuf, struct extendable); \ } STMT_END /* * Use SvPOKp(), because SvPOK() fails on tainted scalars. * See store_scalar() for other usage of this workaround. */ #define MBUF_LOAD(v) \ STMT_START { \ ASSERT(cxt->membuf_ro, ("mbase is read-only")); \ if (!SvPOKp(v)) \ CROAK(("Not a scalar string")); \ mptr = mbase = SvPV(v, msiz); \ mend = mbase + msiz; \ } STMT_END #define MBUF_XTEND(x) \ STMT_START { \ int nsz = (int) round_mgrow((x)+msiz); \ int offset = mptr - mbase; \ ASSERT(!cxt->membuf_ro, ("mbase is not read-only")); \ TRACEME(("** extending mbase from %d to %d bytes (wants %d new)", \ msiz, nsz, (x))); \ Renew(mbase, nsz, char); \ msiz = nsz; \ mptr = mbase + offset; \ mend = mbase + nsz; \ } STMT_END #define MBUF_CHK(x) \ STMT_START { \ if ((mptr + (x)) > mend) \ MBUF_XTEND(x); \ } STMT_END #define MBUF_GETC(x) \ STMT_START { \ if (mptr < mend) \ x = (int) (unsigned char) *mptr++; \ else \ return (SV *) 0; \ } STMT_END #ifdef CRAY_HACK #define MBUF_GETINT(x) \ STMT_START { \ oC(x); \ if ((mptr + 4) <= mend) { \ memcpy(oI(&x), mptr, 4); \ mptr += 4; \ } else \ return (SV *) 0; \ } STMT_END #else #define MBUF_GETINT(x) \ STMT_START { \ if ((mptr + sizeof(int)) <= mend) { \ if (int_aligned(mptr)) \ x = *(int *) mptr; \ else \ memcpy(&x, mptr, sizeof(int)); \ mptr += sizeof(int); \ } else \ return (SV *) 0; \ } STMT_END #endif #define MBUF_READ(x,s) \ STMT_START { \ if ((mptr + (s)) <= mend) { \ memcpy(x, mptr, s); \ mptr += s; \ } else \ return (SV *) 0; \ } STMT_END #define MBUF_SAFEREAD(x,s,z) \ STMT_START { \ if ((mptr + (s)) <= mend) { \ memcpy(x, mptr, s); \ mptr += s; \ } else { \ sv_free(z); \ return (SV *) 0; \ } \ } STMT_END #define MBUF_PUTC(c) \ STMT_START { \ if (mptr < mend) \ *mptr++ = (char) c; \ else { \ MBUF_XTEND(1); \ *mptr++ = (char) c; \ } \ } STMT_END #ifdef CRAY_HACK #define MBUF_PUTINT(i) \ STMT_START { \ MBUF_CHK(4); \ memcpy(mptr, oI(&i), 4); \ mptr += 4; \ } STMT_END #else #define MBUF_PUTINT(i) \ STMT_START { \ MBUF_CHK(sizeof(int)); \ if (int_aligned(mptr)) \ *(int *) mptr = i; \ else \ memcpy(mptr, &i, sizeof(int)); \ mptr += sizeof(int); \ } STMT_END #endif #define MBUF_WRITE(x,s) \ STMT_START { \ MBUF_CHK(s); \ memcpy(mptr, x, s); \ mptr += s; \ } STMT_END /* * Possible return values for sv_type(). */ #define svis_REF 0 #define svis_SCALAR 1 #define svis_ARRAY 2 #define svis_HASH 3 #define svis_TIED 4 #define svis_TIED_ITEM 5 #define svis_CODE 6 #define svis_OTHER 7 /* * Flags for SX_HOOK. */ #define SHF_TYPE_MASK 0x03 #define SHF_LARGE_CLASSLEN 0x04 #define SHF_LARGE_STRLEN 0x08 #define SHF_LARGE_LISTLEN 0x10 #define SHF_IDX_CLASSNAME 0x20 #define SHF_NEED_RECURSE 0x40 #define SHF_HAS_LIST 0x80 /* * Types for SX_HOOK (last 2 bits in flags). */ #define SHT_SCALAR 0 #define SHT_ARRAY 1 #define SHT_HASH 2 #define SHT_EXTRA 3 /* Read extra byte for type */ /* * The following are held in the "extra byte"... */ #define SHT_TSCALAR 4 /* 4 + 0 -- tied scalar */ #define SHT_TARRAY 5 /* 4 + 1 -- tied array */ #define SHT_THASH 6 /* 4 + 2 -- tied hash */ /* * per hash flags for flagged hashes */ #define SHV_RESTRICTED 0x01 /* * per key flags for flagged hashes */ #define SHV_K_UTF8 0x01 #define SHV_K_WASUTF8 0x02 #define SHV_K_LOCKED 0x04 #define SHV_K_ISSV 0x08 #define SHV_K_PLACEHOLDER 0x10 /* * Before 0.6, the magic string was "perl-store" (binary version number 0). * * Since 0.6 introduced many binary incompatibilities, the magic string has * been changed to "pst0" to allow an old image to be properly retrieved by * a newer Storable, but ensure a newer image cannot be retrieved with an * older version. * * At 0.7, objects are given the ability to serialize themselves, and the * set of markers is extended, backward compatibility is not jeopardized, * so the binary version number could have remained unchanged. To correctly * spot errors if a file making use of 0.7-specific extensions is given to * 0.6 for retrieval, the binary version was moved to "2". And I'm introducing * a "minor" version, to better track this kind of evolution from now on. * */ static const char old_magicstr[] = "perl-store"; /* Magic number before 0.6 */ static const char magicstr[] = "pst0"; /* Used as a magic number */ #define MAGICSTR_BYTES 'p','s','t','0' #define OLDMAGICSTR_BYTES 'p','e','r','l','-','s','t','o','r','e' /* 5.6.x introduced the ability to have IVs as long long. However, Configure still defined BYTEORDER based on the size of a long. Storable uses the BYTEORDER value as part of the header, but doesn't explicity store sizeof(IV) anywhere in the header. Hence on 5.6.x built with IV as long long on a platform that uses Configure (ie most things except VMS and Windows) headers are identical for the different IV sizes, despite the files containing some fields based on sizeof(IV) Erk. Broken-ness. 5.8 is consistent - the following redifinition kludge is only needed on 5.6.x, but the interwork is needed on 5.8 while data survives in files with the 5.6 header. */ #if defined (IVSIZE) && (IVSIZE == 8) && (LONGSIZE == 4) #ifndef NO_56_INTERWORK_KLUDGE #define USE_56_INTERWORK_KLUDGE #endif #if BYTEORDER == 0x1234 #undef BYTEORDER #define BYTEORDER 0x12345678 #else #if BYTEORDER == 0x4321 #undef BYTEORDER #define BYTEORDER 0x87654321 #endif #endif #endif #if BYTEORDER == 0x1234 #define BYTEORDER_BYTES '1','2','3','4' #else #if BYTEORDER == 0x12345678 #define BYTEORDER_BYTES '1','2','3','4','5','6','7','8' #ifdef USE_56_INTERWORK_KLUDGE #define BYTEORDER_BYTES_56 '1','2','3','4' #endif #else #if BYTEORDER == 0x87654321 #define BYTEORDER_BYTES '8','7','6','5','4','3','2','1' #ifdef USE_56_INTERWORK_KLUDGE #define BYTEORDER_BYTES_56 '4','3','2','1' #endif #else #if BYTEORDER == 0x4321 #define BYTEORDER_BYTES '4','3','2','1' #else #error Unknown byteorder. Please append your byteorder to Storable.xs #endif #endif #endif #endif static const char byteorderstr[] = {BYTEORDER_BYTES, 0}; #ifdef USE_56_INTERWORK_KLUDGE static const char byteorderstr_56[] = {BYTEORDER_BYTES_56, 0}; #endif #define STORABLE_BIN_MAJOR 2 /* Binary major "version" */ #define STORABLE_BIN_MINOR 7 /* Binary minor "version" */ #if (PATCHLEVEL <= 5) #define STORABLE_BIN_WRITE_MINOR 4 #else /* * Perl 5.6.0 onwards can do weak references. */ #define STORABLE_BIN_WRITE_MINOR 7 #endif /* (PATCHLEVEL <= 5) */ #if (PATCHLEVEL < 8 || (PATCHLEVEL == 8 && SUBVERSION < 1)) #define PL_sv_placeholder PL_sv_undef #endif /* * Useful store shortcuts... */ /* * Note that if you put more than one mark for storing a particular * type of thing, *and* in the retrieve_foo() function you mark both * the thingy's you get off with SEEN(), you *must* increase the * tagnum with cxt->tagnum++ along with this macro! * - samv 20Jan04 */ #define PUTMARK(x) \ STMT_START { \ if (!cxt->fio) \ MBUF_PUTC(x); \ else if (PerlIO_putc(cxt->fio, x) == EOF) \ return -1; \ } STMT_END #define WRITE_I32(x) \ STMT_START { \ ASSERT(sizeof(x) == sizeof(I32), ("writing an I32")); \ if (!cxt->fio) \ MBUF_PUTINT(x); \ else if (PerlIO_write(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \ return -1; \ } STMT_END #ifdef HAS_HTONL #define WLEN(x) \ STMT_START { \ if (cxt->netorder) { \ int y = (int) htonl(x); \ if (!cxt->fio) \ MBUF_PUTINT(y); \ else if (PerlIO_write(cxt->fio,oI(&y),oS(sizeof(y))) != oS(sizeof(y))) \ return -1; \ } else { \ if (!cxt->fio) \ MBUF_PUTINT(x); \ else if (PerlIO_write(cxt->fio,oI(&x),oS(sizeof(x))) != oS(sizeof(x))) \ return -1; \ } \ } STMT_END #else #define WLEN(x) WRITE_I32(x) #endif #define WRITE(x,y) \ STMT_START { \ if (!cxt->fio) \ MBUF_WRITE(x,y); \ else if (PerlIO_write(cxt->fio, x, y) != y) \ return -1; \ } STMT_END #define STORE_PV_LEN(pv, len, small, large) \ STMT_START { \ if (len <= LG_SCALAR) { \ unsigned char clen = (unsigned char) len; \ PUTMARK(small); \ PUTMARK(clen); \ if (len) \ WRITE(pv, len); \ } else { \ PUTMARK(large); \ WLEN(len); \ WRITE(pv, len); \ } \ } STMT_END #define STORE_SCALAR(pv, len) STORE_PV_LEN(pv, len, SX_SCALAR, SX_LSCALAR) /* * Store &PL_sv_undef in arrays without recursing through store(). */ #define STORE_SV_UNDEF() \ STMT_START { \ cxt->tagnum++; \ PUTMARK(SX_SV_UNDEF); \ } STMT_END /* * Useful retrieve shortcuts... */ #define GETCHAR() \ (cxt->fio ? PerlIO_getc(cxt->fio) : (mptr >= mend ? EOF : (int) *mptr++)) #define GETMARK(x) \ STMT_START { \ if (!cxt->fio) \ MBUF_GETC(x); \ else if ((int) (x = PerlIO_getc(cxt->fio)) == EOF) \ return (SV *) 0; \ } STMT_END #define READ_I32(x) \ STMT_START { \ ASSERT(sizeof(x) == sizeof(I32), ("reading an I32")); \ oC(x); \ if (!cxt->fio) \ MBUF_GETINT(x); \ else if (PerlIO_read(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \ return (SV *) 0; \ } STMT_END #ifdef HAS_NTOHL #define RLEN(x) \ STMT_START { \ oC(x); \ if (!cxt->fio) \ MBUF_GETINT(x); \ else if (PerlIO_read(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \ return (SV *) 0; \ if (cxt->netorder) \ x = (int) ntohl(x); \ } STMT_END #else #define RLEN(x) READ_I32(x) #endif #define READ(x,y) \ STMT_START { \ if (!cxt->fio) \ MBUF_READ(x, y); \ else if (PerlIO_read(cxt->fio, x, y) != y) \ return (SV *) 0; \ } STMT_END #define SAFEREAD(x,y,z) \ STMT_START { \ if (!cxt->fio) \ MBUF_SAFEREAD(x,y,z); \ else if (PerlIO_read(cxt->fio, x, y) != y) { \ sv_free(z); \ return (SV *) 0; \ } \ } STMT_END /* * This macro is used at retrieve time, to remember where object 'y', bearing a * given tag 'tagnum', has been retrieved. Next time we see an SX_OBJECT marker, * we'll therefore know where it has been retrieved and will be able to * share the same reference, as in the original stored memory image. * * We also need to bless objects ASAP for hooks (which may compute "ref $x" * on the objects given to STORABLE_thaw and expect that to be defined), and * also for overloaded objects (for which we might not find the stash if the * object is not blessed yet--this might occur for overloaded objects that * refer to themselves indirectly: if we blessed upon return from a sub * retrieve(), the SX_OBJECT marker we'd found could not have overloading * restored on it because the underlying object would not be blessed yet!). * * To achieve that, the class name of the last retrieved object is passed down * recursively, and the first SEEN() call for which the class name is not NULL * will bless the object. * * i should be true iff sv is immortal (ie PL_sv_yes, PL_sv_no or PL_sv_undef) */ #define SEEN(y,c,i) \ STMT_START { \ if (!y) \ return (SV *) 0; \ if (av_store(cxt->aseen, cxt->tagnum++, i ? (SV*)(y) : SvREFCNT_inc(y)) == 0) \ return (SV *) 0; \ TRACEME(("aseen(#%d) = 0x%"UVxf" (refcnt=%d)", cxt->tagnum-1, \ PTR2UV(y), SvREFCNT(y)-1)); \ if (c) \ BLESS((SV *) (y), c); \ } STMT_END /* * Bless `s' in `p', via a temporary reference, required by sv_bless(). */ #define BLESS(s,p) \ STMT_START { \ SV *ref; \ HV *stash; \ TRACEME(("blessing 0x%"UVxf" in %s", PTR2UV(s), (p))); \ stash = gv_stashpv((p), TRUE); \ ref = newRV_noinc(s); \ (void) sv_bless(ref, stash); \ SvRV_set(ref, NULL); \ SvREFCNT_dec(ref); \ } STMT_END /* * sort (used in store_hash) - conditionally use qsort when * sortsv is not available ( <= 5.6.1 ). */ #if (PATCHLEVEL <= 6) #if defined(USE_ITHREADS) #define STORE_HASH_SORT \ ENTER; { \ PerlInterpreter *orig_perl = PERL_GET_CONTEXT; \ SAVESPTR(orig_perl); \ PERL_SET_CONTEXT(aTHX); \ qsort((char *) AvARRAY(av), len, sizeof(SV *), sortcmp); \ } LEAVE; #else /* ! USE_ITHREADS */ #define STORE_HASH_SORT \ qsort((char *) AvARRAY(av), len, sizeof(SV *), sortcmp); #endif /* USE_ITHREADS */ #else /* PATCHLEVEL > 6 */ #define STORE_HASH_SORT \ sortsv(AvARRAY(av), len, Perl_sv_cmp); #endif /* PATCHLEVEL <= 6 */ static int store(pTHX_ stcxt_t *cxt, SV *sv); static SV *retrieve(pTHX_ stcxt_t *cxt, const char *cname); /* * Dynamic dispatching table for SV store. */ static int store_ref(pTHX_ stcxt_t *cxt, SV *sv); static int store_scalar(pTHX_ stcxt_t *cxt, SV *sv); static int store_array(pTHX_ stcxt_t *cxt, AV *av); static int store_hash(pTHX_ stcxt_t *cxt, HV *hv); static int store_tied(pTHX_ stcxt_t *cxt, SV *sv); static int store_tied_item(pTHX_ stcxt_t *cxt, SV *sv); static int store_code(pTHX_ stcxt_t *cxt, CV *cv); static int store_other(pTHX_ stcxt_t *cxt, SV *sv); static int store_blessed(pTHX_ stcxt_t *cxt, SV *sv, int type, HV *pkg); typedef int (*sv_store_t)(pTHX_ stcxt_t *cxt, SV *sv); static sv_store_t sv_store[] = { (sv_store_t)store_ref, /* svis_REF */ (sv_store_t)store_scalar, /* svis_SCALAR */ (sv_store_t)store_array, /* svis_ARRAY */ (sv_store_t)store_hash, /* svis_HASH */ (sv_store_t)store_tied, /* svis_TIED */ (sv_store_t)store_tied_item, /* svis_TIED_ITEM */ (sv_store_t)store_code, /* svis_CODE */ (sv_store_t)store_other, /* svis_OTHER */ }; #define SV_STORE(x) (*sv_store[x]) /* * Dynamic dispatching tables for SV retrieval. */ static SV *retrieve_lscalar(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_lutf8str(pTHX_ stcxt_t *cxt, const char *cname); static SV *old_retrieve_array(pTHX_ stcxt_t *cxt, const char *cname); static SV *old_retrieve_hash(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_ref(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_undef(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_integer(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_double(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_byte(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_netint(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_scalar(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_utf8str(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_tied_array(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_tied_hash(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_tied_scalar(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_other(pTHX_ stcxt_t *cxt, const char *cname); typedef SV* (*sv_retrieve_t)(pTHX_ stcxt_t *cxt, const char *name); static const sv_retrieve_t sv_old_retrieve[] = { 0, /* SX_OBJECT -- entry unused dynamically */ (sv_retrieve_t)retrieve_lscalar, /* SX_LSCALAR */ (sv_retrieve_t)old_retrieve_array, /* SX_ARRAY -- for pre-0.6 binaries */ (sv_retrieve_t)old_retrieve_hash, /* SX_HASH -- for pre-0.6 binaries */ (sv_retrieve_t)retrieve_ref, /* SX_REF */ (sv_retrieve_t)retrieve_undef, /* SX_UNDEF */ (sv_retrieve_t)retrieve_integer, /* SX_INTEGER */ (sv_retrieve_t)retrieve_double, /* SX_DOUBLE */ (sv_retrieve_t)retrieve_byte, /* SX_BYTE */ (sv_retrieve_t)retrieve_netint, /* SX_NETINT */ (sv_retrieve_t)retrieve_scalar, /* SX_SCALAR */ (sv_retrieve_t)retrieve_tied_array, /* SX_ARRAY */ (sv_retrieve_t)retrieve_tied_hash, /* SX_HASH */ (sv_retrieve_t)retrieve_tied_scalar, /* SX_SCALAR */ (sv_retrieve_t)retrieve_other, /* SX_SV_UNDEF not supported */ (sv_retrieve_t)retrieve_other, /* SX_SV_YES not supported */ (sv_retrieve_t)retrieve_other, /* SX_SV_NO not supported */ (sv_retrieve_t)retrieve_other, /* SX_BLESS not supported */ (sv_retrieve_t)retrieve_other, /* SX_IX_BLESS not supported */ (sv_retrieve_t)retrieve_other, /* SX_HOOK not supported */ (sv_retrieve_t)retrieve_other, /* SX_OVERLOADED not supported */ (sv_retrieve_t)retrieve_other, /* SX_TIED_KEY not supported */ (sv_retrieve_t)retrieve_other, /* SX_TIED_IDX not supported */ (sv_retrieve_t)retrieve_other, /* SX_UTF8STR not supported */ (sv_retrieve_t)retrieve_other, /* SX_LUTF8STR not supported */ (sv_retrieve_t)retrieve_other, /* SX_FLAG_HASH not supported */ (sv_retrieve_t)retrieve_other, /* SX_CODE not supported */ (sv_retrieve_t)retrieve_other, /* SX_WEAKREF not supported */ (sv_retrieve_t)retrieve_other, /* SX_WEAKOVERLOAD not supported */ (sv_retrieve_t)retrieve_other, /* SX_ERROR */ }; static SV *retrieve_array(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_hash(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_sv_undef(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_sv_yes(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_sv_no(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_blessed(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_idx_blessed(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_hook(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_overloaded(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_tied_key(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_tied_idx(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_flag_hash(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_code(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_weakref(pTHX_ stcxt_t *cxt, const char *cname); static SV *retrieve_weakoverloaded(pTHX_ stcxt_t *cxt, const char *cname); static const sv_retrieve_t sv_retrieve[] = { 0, /* SX_OBJECT -- entry unused dynamically */ (sv_retrieve_t)retrieve_lscalar, /* SX_LSCALAR */ (sv_retrieve_t)retrieve_array, /* SX_ARRAY */ (sv_retrieve_t)retrieve_hash, /* SX_HASH */ (sv_retrieve_t)retrieve_ref, /* SX_REF */ (sv_retrieve_t)retrieve_undef, /* SX_UNDEF */ (sv_retrieve_t)retrieve_integer, /* SX_INTEGER */ (sv_retrieve_t)retrieve_double, /* SX_DOUBLE */ (sv_retrieve_t)retrieve_byte, /* SX_BYTE */ (sv_retrieve_t)retrieve_netint, /* SX_NETINT */ (sv_retrieve_t)retrieve_scalar, /* SX_SCALAR */ (sv_retrieve_t)retrieve_tied_array, /* SX_ARRAY */ (sv_retrieve_t)retrieve_tied_hash, /* SX_HASH */ (sv_retrieve_t)retrieve_tied_scalar, /* SX_SCALAR */ (sv_retrieve_t)retrieve_sv_undef, /* SX_SV_UNDEF */ (sv_retrieve_t)retrieve_sv_yes, /* SX_SV_YES */ (sv_retrieve_t)retrieve_sv_no, /* SX_SV_NO */ (sv_retrieve_t)retrieve_blessed, /* SX_BLESS */ (sv_retrieve_t)retrieve_idx_blessed, /* SX_IX_BLESS */ (sv_retrieve_t)retrieve_hook, /* SX_HOOK */ (sv_retrieve_t)retrieve_overloaded, /* SX_OVERLOAD */ (sv_retrieve_t)retrieve_tied_key, /* SX_TIED_KEY */ (sv_retrieve_t)retrieve_tied_idx, /* SX_TIED_IDX */ (sv_retrieve_t)retrieve_utf8str, /* SX_UTF8STR */ (sv_retrieve_t)retrieve_lutf8str, /* SX_LUTF8STR */ (sv_retrieve_t)retrieve_flag_hash, /* SX_HASH */ (sv_retrieve_t)retrieve_code, /* SX_CODE */ (sv_retrieve_t)retrieve_weakref, /* SX_WEAKREF */ (sv_retrieve_t)retrieve_weakoverloaded, /* SX_WEAKOVERLOAD */ (sv_retrieve_t)retrieve_other, /* SX_ERROR */ }; #define RETRIEVE(c,x) (*(c)->retrieve_vtbl[(x) >= SX_ERROR ? SX_ERROR : (x)]) static SV *mbuf2sv(pTHX); /*** *** Context management. ***/ /* * init_perinterp * * Called once per "thread" (interpreter) to initialize some global context. */ static void init_perinterp(pTHX) 30 { 30 INIT_STCXT; 30 cxt->netorder = 0; /* true if network order used */ 30 cxt->forgive_me = -1; /* whether to be forgiving... */ 30 cxt->accept_future_minor = -1; /* would otherwise occur too late */ } /* * reset_context * * Called at the end of every context cleaning, to perform common reset * operations. */ static void reset_context(stcxt_t *cxt) 978 { 978 cxt->entry = 0; 978 cxt->s_dirty = 0; 978 cxt->optype &= ~(ST_STORE|ST_RETRIEVE); /* Leave ST_CLONE alone */ } /* * init_store_context * * Initialize a new store context for real recursion. */ static void init_store_context( pTHX_ stcxt_t *cxt, PerlIO *f, int optype, int network_order) 399 { TRACEME(("init_store_context")); 399 cxt->netorder = network_order; 399 cxt->forgive_me = -1; /* Fetched from perl if needed */ 399 cxt->deparse = -1; /* Idem */ 399 cxt->eval = NULL; /* Idem */ 399 cxt->canonical = -1; /* Idem */ 399 cxt->tagnum = -1; /* Reset tag numbers */ 399 cxt->classnum = -1; /* Reset class numbers */ 399 cxt->fio = f; /* Where I/O are performed */ 399 cxt->optype = optype; /* A store, or a deep clone */ 399 cxt->entry = 1; /* No recursion yet */ /* * The `hseen' table is used to keep track of each SV stored and their * associated tag numbers is special. It is "abused" because the * values stored are not real SV, just integers cast to (SV *), * which explains the freeing below. * * It is also one possible bottlneck to achieve good storing speed, * so the "shared keys" optimization is turned off (unlikely to be * of any use here), and the hash table is "pre-extended". Together, * those optimizations increase the throughput by 12%. */ #ifdef USE_PTR_TABLE cxt->pseen = ptr_table_new(); cxt->hseen = 0; #else 399 cxt->hseen = newHV(); /* Table where seen objects are stored */ 399 HvSHAREKEYS_off(cxt->hseen); #endif /* * The following does not work well with perl5.004_04, and causes * a core dump later on, in a completely unrelated spot, which * makes me think there is a memory corruption going on. * * Calling hv_ksplit(hseen, HBUCKETS) instead of manually hacking * it below does not make any difference. It seems to work fine * with perl5.004_68 but given the probable nature of the bug, * that does not prove anything. * * It's a shame because increasing the amount of buckets raises * store() throughput by 5%, but until I figure this out, I can't * allow for this to go into production. * * It is reported fixed in 5.005, hence the #if. */ #if PERL_VERSION >= 5 #define HBUCKETS 4096 /* Buckets for %hseen */ #ifndef USE_PTR_TABLE 399 HvMAX(cxt->hseen) = HBUCKETS - 1; /* keys %hseen = $HBUCKETS; */ #endif #endif /* * The `hclass' hash uses the same settings as `hseen' above, but it is * used to assign sequential tags (numbers) to class names for blessed * objects. * * We turn the shared key optimization on. */ 399 cxt->hclass = newHV(); /* Where seen classnames are stored */ #if PERL_VERSION >= 5 399 HvMAX(cxt->hclass) = HBUCKETS - 1; /* keys %hclass = $HBUCKETS; */ #endif /* * The `hook' hash table is used to keep track of the references on * the STORABLE_freeze hook routines, when found in some class name. * * It is assumed that the inheritance tree will not be changed during * storing, and that no new method will be dynamically created by the * hooks. */ 399 cxt->hook = newHV(); /* Table where hooks are cached */ /* * The `hook_seen' array keeps track of all the SVs returned by * STORABLE_freeze hooks for us to serialize, so that they are not * reclaimed until the end of the serialization process. Each SV is * only stored once, the first time it is seen. */ 399 cxt->hook_seen = newAV(); /* Lists SVs returned by STORABLE_freeze */ } /* * clean_store_context * * Clean store context by */ static void clean_store_context(pTHX_ stcxt_t *cxt) 399 { 399 HE *he; TRACEME(("clean_store_context")); ASSERT(cxt->optype & ST_STORE, ("was performing a store()")); /* * Insert real values into hashes where we stored faked pointers. */ #ifndef USE_PTR_TABLE 399 if (cxt->hseen) { 399 hv_iterinit(cxt->hseen); 32749 while ((he = hv_iternext(cxt->hseen))) /* Extra () for -Wall, grr.. */ 32350 HeVAL(he) = &PL_sv_undef; } #endif 399 if (cxt->hclass) { 399 hv_iterinit(cxt->hclass); 554 while ((he = hv_iternext(cxt->hclass))) /* Extra () for -Wall, grr.. */ 155 HeVAL(he) = &PL_sv_undef; } /* * And now dispose of them... * * The surrounding if() protection has been added because there might be * some cases where this routine is called more than once, during * exceptionnal events. This was reported by Marc Lehmann when Storable * is executed from mod_perl, and the fix was suggested by him. * -- RAM, 20/12/2000 */ #ifdef USE_PTR_TABLE if (cxt->pseen) { struct ptr_tbl *pseen = cxt->pseen; cxt->pseen = 0; ptr_table_free(pseen); } assert(!cxt->hseen); #else 399 if (cxt->hseen) { 399 HV *hseen = cxt->hseen; 399 cxt->hseen = 0; 399 hv_undef(hseen); 399 sv_free((SV *) hseen); } #endif 399 if (cxt->hclass) { 399 HV *hclass = cxt->hclass; 399 cxt->hclass = 0; 399 hv_undef(hclass); 399 sv_free((SV *) hclass); } 399 if (cxt->hook) { 399 HV *hook = cxt->hook; 399 cxt->hook = 0; 399 hv_undef(hook); 399 sv_free((SV *) hook); } 399 if (cxt->hook_seen) { 399 AV *hook_seen = cxt->hook_seen; 399 cxt->hook_seen = 0; 399 av_undef(hook_seen); 399 sv_free((SV *) hook_seen); } 399 cxt->forgive_me = -1; /* Fetched from perl if needed */ 399 cxt->deparse = -1; /* Idem */ 399 if (cxt->eval) { ###### SvREFCNT_dec(cxt->eval); } 399 cxt->eval = NULL; /* Idem */ 399 cxt->canonical = -1; /* Idem */ 399 reset_context(cxt); } /* * init_retrieve_context * * Initialize a new retrieve context for real recursion. */ static void init_retrieve_context(pTHX_ stcxt_t *cxt, int optype, int is_tainted) 515 { TRACEME(("init_retrieve_context")); /* * The hook hash table is used to keep track of the references on * the STORABLE_thaw hook routines, when found in some class name. * * It is assumed that the inheritance tree will not be changed during * storing, and that no new method will be dynamically created by the * hooks. */ 515 cxt->hook = newHV(); /* Caches STORABLE_thaw */ #ifdef USE_PTR_TABLE cxt->pseen = 0; #endif /* * If retrieving an old binary version, the cxt->retrieve_vtbl variable * was set to sv_old_retrieve. We'll need a hash table to keep track of * the correspondance between the tags and the tag number used by the * new retrieve routines. */ 515 cxt->hseen = (((void*)cxt->retrieve_vtbl == (void*)sv_old_retrieve) ? newHV() : 0); 515 cxt->aseen = newAV(); /* Where retrieved objects are kept */ 515 cxt->where_is_undef = -1; /* Special case for PL_sv_undef */ 515 cxt->aclass = newAV(); /* Where seen classnames are kept */ 515 cxt->tagnum = 0; /* Have to count objects... */ 515 cxt->classnum = 0; /* ...and class names as well */ 515 cxt->optype = optype; 515 cxt->s_tainted = is_tainted; 515 cxt->entry = 1; /* No recursion yet */ #ifndef HAS_RESTRICTED_HASHES cxt->derestrict = -1; /* Fetched from perl if needed */ #endif #ifndef HAS_UTF8_ALL cxt->use_bytes = -1; /* Fetched from perl if needed */ #endif 515 cxt->accept_future_minor = -1; /* Fetched from perl if needed */ } /* * clean_retrieve_context * * Clean retrieve context by */ static void clean_retrieve_context(pTHX_ stcxt_t *cxt) 514 { TRACEME(("clean_retrieve_context")); ASSERT(cxt->optype & ST_RETRIEVE, ("was performing a retrieve()")); 514 if (cxt->aseen) { 514 AV *aseen = cxt->aseen; 514 cxt->aseen = 0; 514 av_undef(aseen); 514 sv_free((SV *) aseen); } 514 cxt->where_is_undef = -1; 514 if (cxt->aclass) { 514 AV *aclass = cxt->aclass; 514 cxt->aclass = 0; 514 av_undef(aclass); 514 sv_free((SV *) aclass); } 514 if (cxt->hook) { 514 HV *hook = cxt->hook; 514 cxt->hook = 0; 514 hv_undef(hook); 514 sv_free((SV *) hook); } 514 if (cxt->hseen) { ###### HV *hseen = cxt->hseen; ###### cxt->hseen = 0; ###### hv_undef(hseen); ###### sv_free((SV *) hseen); /* optional HV, for backward compat. */ } #ifndef HAS_RESTRICTED_HASHES cxt->derestrict = -1; /* Fetched from perl if needed */ #endif #ifndef HAS_UTF8_ALL cxt->use_bytes = -1; /* Fetched from perl if needed */ #endif 514 cxt->accept_future_minor = -1; /* Fetched from perl if needed */ 514 reset_context(cxt); } /* * clean_context * * A workaround for the CROAK bug: cleanup the last context. */ static void clean_context(pTHX_ stcxt_t *cxt) 93 { TRACEME(("clean_context")); ASSERT(cxt->s_dirty, ("dirty context")); 93 if (cxt->membuf_ro) 46 MBUF_RESTORE(); ASSERT(!cxt->membuf_ro, ("mbase is not read-only")); 93 if (cxt->optype & ST_RETRIEVE) 23 clean_retrieve_context(aTHX_ cxt); 70 else if (cxt->optype & ST_STORE) 5 clean_store_context(aTHX_ cxt); else 65 reset_context(cxt); ASSERT(!cxt->s_dirty, ("context is clean")); ASSERT(cxt->entry == 0, ("context is reset")); } /* * allocate_context * * Allocate a new context and push it on top of the parent one. * This new context is made globally visible via SET_STCXT(). */ static stcxt_t *allocate_context(pTHX_ stcxt_t *parent_cxt) 79 { 79 stcxt_t *cxt; TRACEME(("allocate_context")); ASSERT(!parent_cxt->s_dirty, ("parent context clean")); 79 NEW_STORABLE_CXT_OBJ(cxt); 79 cxt->prev = parent_cxt->my_sv; 79 SET_STCXT(cxt); ASSERT(!cxt->s_dirty, ("clean context")); 79 return cxt; } /* * free_context * * Free current context, which cannot be the "root" one. * Make the context underneath globally visible via SET_STCXT(). */ static void free_context(pTHX_ stcxt_t *cxt) 79 { 79 stcxt_t *prev = (stcxt_t *)(cxt->prev ? SvPVX(SvRV(cxt->prev)) : 0); TRACEME(("free_context")); ASSERT(!cxt->s_dirty, ("clean context")); ASSERT(prev, ("not freeing root context")); 79 SvREFCNT_dec(cxt->my_sv); 79 SET_STCXT(prev); ASSERT(cxt, ("context not void")); } /*** *** Predicates. ***/ /* * is_storing * * Tells whether we're in the middle of a store operation. */ static int is_storing(pTHX) 2 { 2 dSTCXT; 2 return cxt->entry && (cxt->optype & ST_STORE); } /* * is_retrieving * * Tells whether we're in the middle of a retrieve operation. */ static int is_retrieving(pTHX) 2 { 2 dSTCXT; 2 return cxt->entry && (cxt->optype & ST_RETRIEVE); } /* * last_op_in_netorder * * Returns whether last operation was made using network order. * * This is typically out-of-band information that might prove useful * to people wishing to convert native to network order data when used. */ static int last_op_in_netorder(pTHX) 5 { 5 dSTCXT; 5 return cxt->netorder; } /*** *** Hook lookup and calling routines. ***/ /* * pkg_fetchmeth * * A wrapper on gv_fetchmethod_autoload() which caches results. * * Returns the routine reference as an SV*, or null if neither the package * nor its ancestors know about the method. */ static SV *pkg_fetchmeth( pTHX_ HV *cache, HV *pkg, char *method) 224 { 224 GV *gv; 224 SV *sv; 224 const char *hvname = HvNAME_get(pkg); /* * The following code is the same as the one performed by UNIVERSAL::can * in the Perl core. */ 224 gv = gv_fetchmethod_autoload(pkg, method, FALSE); 224 if (gv && isGV(gv)) { 145 sv = newRV((SV*) GvCV(gv)); TRACEME(("%s->%s: 0x%"UVxf, hvname, method, PTR2UV(sv))); } else { 79 sv = newSVsv(&PL_sv_undef); TRACEME(("%s->%s: not found", hvname, method)); } /* * Cache the result, ignoring failure: if we can't store the value, * it just won't be cached. */ 224 (void) hv_store(cache, hvname, strlen(hvname), sv, 0); 224 return SvOK(sv) ? sv : (SV *) 0; } /* * pkg_hide * * Force cached value to be undef: hook ignored even if present. */ static void pkg_hide( pTHX_ HV *cache, HV *pkg, char *method) 1 { 1 const char *hvname = HvNAME_get(pkg); 1 (void) hv_store(cache, hvname, strlen(hvname), newSVsv(&PL_sv_undef), 0); } /* * pkg_uncache * * Discard cached value: a whole fetch loop will be retried at next lookup. */ static void pkg_uncache( pTHX_ HV *cache, HV *pkg, char *method) 1 { 1 const char *hvname = HvNAME_get(pkg); 1 (void) hv_delete(cache, hvname, strlen(hvname), G_DISCARD); } /* * pkg_can * * Our own "UNIVERSAL::can", which caches results. * * Returns the routine reference as an SV*, or null if the object does not * know about the method. */ static SV *pkg_can( pTHX_ HV *cache, HV *pkg, char *method) 290 { 290 SV **svh; 290 SV *sv; 290 const char *hvname = HvNAME_get(pkg); TRACEME(("pkg_can for %s->%s", hvname, method)); /* * Look into the cache to see whether we already have determined * where the routine was, if any. * * NOTA BENE: we don't use `method' at all in our lookup, since we know * that only one hook (i.e. always the same) is cached in a given cache. */ 290 svh = hv_fetch(cache, hvname, strlen(hvname), FALSE); 290 if (svh) { 66 sv = *svh; 66 if (!SvOK(sv)) { TRACEME(("cached %s->%s: not found", hvname, method)); 28 return (SV *) 0; } else { TRACEME(("cached %s->%s: 0x%"UVxf, hvname, method, PTR2UV(sv))); 38 return sv; } } TRACEME(("not cached yet")); 224 return pkg_fetchmeth(aTHX_ cache, pkg, method); /* Fetch and cache */ } /* * scalar_call * * Call routine as obj->hook(av) in scalar context. * Propagates the single returned value if not called in void context. */ static SV *scalar_call( pTHX_ SV *obj, SV *hook, int cloning, AV *av, I32 flags) 96 { 96 dSP; 96 int count; 96 SV *sv = 0; TRACEME(("scalar_call (cloning=%d)", cloning)); 96 ENTER; 96 SAVETMPS; 96 PUSHMARK(sp); 96 XPUSHs(obj); 96 XPUSHs(sv_2mortal(newSViv(cloning))); /* Cloning flag */ 96 if (av) { 96 SV **ary = AvARRAY(av); 96 int cnt = AvFILLp(av) + 1; 96 int i; 96 XPUSHs(ary[0]); /* Frozen string */ 193 for (i = 1; i < cnt; i++) { TRACEME(("pushing arg #%d (0x%"UVxf")...", i, PTR2UV(ary[i]))); 97 XPUSHs(sv_2mortal(newRV(ary[i]))); } } 96 PUTBACK; TRACEME(("calling...")); 96 count = perl_call_sv(hook, flags); /* Go back to Perl code */ TRACEME(("count = %d", count)); 96 SPAGAIN; 96 if (count) { 10 sv = POPs; 10 SvREFCNT_inc(sv); /* We're returning it, must stay alive! */ } 96 PUTBACK; 96 FREETMPS; 96 LEAVE; 96 return sv; } /* * array_call * * Call routine obj->hook(cloning) in list context. * Returns the list of returned values in an array. */ static AV *array_call( pTHX_ SV *obj, SV *hook, int cloning) 97 { 97 dSP; 97 int count; 97 AV *av; 97 int i; TRACEME(("array_call (cloning=%d)", cloning)); 97 ENTER; 97 SAVETMPS; 97 PUSHMARK(sp); 97 XPUSHs(obj); /* Target object */ 97 XPUSHs(sv_2mortal(newSViv(cloning))); /* Cloning flag */ 97 PUTBACK; 97 count = perl_call_sv(hook, G_ARRAY); /* Go back to Perl code */ 97 SPAGAIN; 97 av = newAV(); 292 for (i = count - 1; i >= 0; i--) { 195 SV *sv = POPs; 195 av_store(av, i, SvREFCNT_inc(sv)); } 97 PUTBACK; 97 FREETMPS; 97 LEAVE; 97 return av; } /* * known_class * * Lookup the class name in the `hclass' table and either assign it a new ID * or return the existing one, by filling in `classnum'. * * Return true if the class was known, false if the ID was just generated. */ static int known_class( pTHX_ stcxt_t *cxt, char *name, /* Class name */ int len, /* Name length */ I32 *classnum) 201 { 201 SV **svh; 201 HV *hclass = cxt->hclass; TRACEME(("known_class (%s)", name)); /* * Recall that we don't store pointers in this hash table, but tags. * Therefore, we need LOW_32BITS() to extract the relevant parts. */ 201 svh = hv_fetch(hclass, name, len, FALSE); 201 if (svh) { 46 *classnum = LOW_32BITS(*svh); 46 return TRUE; } /* * Unknown classname, we need to record it. */ 155 cxt->classnum++; 155 if (!hv_store(hclass, name, len, INT2PTR(SV*, cxt->classnum), 0)) ###### CROAK(("Unable to record new classname")); 155 *classnum = cxt->classnum; 155 return FALSE; } /*** *** Sepcific store routines. ***/ /* * store_ref * * Store a reference. * Layout is SX_REF or SX_OVERLOAD . */ static int store_ref(pTHX_ stcxt_t *cxt, SV *sv) 3127 { 3127 int is_weak = 0; TRACEME(("store_ref (0x%"UVxf")", PTR2UV(sv))); /* * Follow reference, and check if target is overloaded. */ #ifdef SvWEAKREF 3127 if (SvWEAKREF(sv)) 16 is_weak = 1; TRACEME(("ref (0x%"UVxf") is%s weak", PTR2UV(sv), is_weak ? "" : "n't")); #endif 3127 sv = SvRV(sv); 3127 if (SvOBJECT(sv)) { 166 HV *stash = (HV *) SvSTASH(sv); 166 if (stash && Gv_AMG(stash)) { TRACEME(("ref (0x%"UVxf") is overloaded", PTR2UV(sv))); 32 PUTMARK(is_weak ? SX_WEAKOVERLOAD : SX_OVERLOAD); } else 134 PUTMARK(is_weak ? SX_WEAKREF : SX_REF); } else 2961 PUTMARK(is_weak ? SX_WEAKREF : SX_REF); 3127 return store(aTHX_ cxt, sv); } /* * store_scalar * * Store a scalar. * * Layout is SX_LSCALAR , SX_SCALAR or SX_UNDEF. * The section is omitted if is 0. * * If integer or double, the layout is SX_INTEGER or SX_DOUBLE . * Small integers (within [-127, +127]) are stored as SX_BYTE . */ static int store_scalar(pTHX_ stcxt_t *cxt, SV *sv) 26122 { 26122 IV iv; 26122 char *pv; 26122 STRLEN len; 26122 U32 flags = SvFLAGS(sv); /* "cc -O" may put it in register */ TRACEME(("store_scalar (0x%"UVxf")", PTR2UV(sv))); /* * For efficiency, break the SV encapsulation by peaking at the flags * directly without using the Perl macros to avoid dereferencing * sv->sv_flags each time we wish to check the flags. */ 26122 if (!(flags & SVf_OK)) { /* !SvOK(sv) */ 99 if (sv == &PL_sv_undef) { TRACEME(("immortal undef")); 75 PUTMARK(SX_SV_UNDEF); } else { TRACEME(("undef at 0x%"UVxf, PTR2UV(sv))); 24 PUTMARK(SX_UNDEF); } 99 return 0; } /* * Always store the string representation of a scalar if it exists. * Gisle Aas provided me with this test case, better than a long speach: * * perl -MDevel::Peek -le '$a="abc"; $a+0; Dump($a)' * SV = PVNV(0x80c8520) * REFCNT = 1 * FLAGS = (NOK,POK,pNOK,pPOK) * IV = 0 * NV = 0 * PV = 0x80c83d0 "abc"\0 * CUR = 3 * LEN = 4 * * Write SX_SCALAR, length, followed by the actual data. * * Otherwise, write an SX_BYTE, SX_INTEGER or an SX_DOUBLE as * appropriate, followed by the actual (binary) data. A double * is written as a string if network order, for portability. * * NOTE: instead of using SvNOK(sv), we test for SvNOKp(sv). * The reason is that when the scalar value is tainted, the SvNOK(sv) * value is false. * * The test for a read-only scalar with both POK and NOK set is meant * to quickly detect &PL_sv_yes and &PL_sv_no without having to pay the * address comparison for each scalar we store. */ #define SV_MAYBE_IMMORTAL (SVf_READONLY|SVf_POK|SVf_NOK) 26023 if ((flags & SV_MAYBE_IMMORTAL) == SV_MAYBE_IMMORTAL) { 6 if (sv == &PL_sv_yes) { TRACEME(("immortal yes")); 3 PUTMARK(SX_SV_YES); 3 } else if (sv == &PL_sv_no) { TRACEME(("immortal no")); 3 PUTMARK(SX_SV_NO); } else { ###### pv = SvPV(sv, len); /* We know it's SvPOK */ ###### goto string; /* Share code below */ } 26017 } else if (flags & SVf_POK) { /* public string - go direct to string read. */ 22740 goto string_readlen; 3277 } else if ( #if (PATCHLEVEL <= 6) /* For 5.6 and earlier NV flag trumps IV flag, so only use integer direct if NV flag is off. */ (flags & (SVf_NOK | SVf_IOK)) == SVf_IOK #else /* 5.7 rules are that if IV public flag is set, IV value is as good, if not better, than NV value. */ flags & SVf_IOK #endif ) { 3242 iv = SvIV(sv); /* * Will come here from below with iv set if double is an integer. */ integer: /* Sorry. This isn't in 5.005_56 (IIRC) or earlier. */ #ifdef SVf_IVisUV /* Need to do this out here, else 0xFFFFFFFF becomes iv of -1 * (for example) and that ends up in the optimised small integer * case. */ 3242 if ((flags & SVf_IVisUV) && SvUV(sv) > IV_MAX) { TRACEME(("large unsigned integer as string, value = %"UVuf, SvUV(sv))); 3197 goto string_readlen; } #endif /* * Optimize small integers into a single byte, otherwise store as * a real integer (converted into network order if they asked). */ 3197 if (iv >= -128 && iv <= 127) { 461 unsigned char siv = (unsigned char) (iv + 128); /* [0,255] */ 461 PUTMARK(SX_BYTE); 461 PUTMARK(siv); TRACEME(("small integer stored as %d", siv)); 2736 } else if (cxt->netorder) { #ifndef HAS_HTONL TRACEME(("no htonl, fall back to string for integer")); goto string_readlen; #else 35 I32 niv; #if IVSIZE > 4 if ( #ifdef SVf_IVisUV /* Sorry. This isn't in 5.005_56 (IIRC) or earlier. */ ((flags & SVf_IVisUV) && SvUV(sv) > 0x7FFFFFFF) || #endif (iv > 0x7FFFFFFF) || (iv < -0x80000000)) { /* Bigger than 32 bits. */ TRACEME(("large network order integer as string, value = %"IVdf, iv)); goto string_readlen; } #endif 35 niv = (I32) htonl((I32) iv); TRACEME(("using network order")); 35 PUTMARK(SX_NETINT); 35 WRITE_I32(niv); #endif } else { 2701 PUTMARK(SX_INTEGER); 2701 WRITE(&iv, sizeof(iv)); } TRACEME(("ok (integer 0x%"UVxf", value = %"IVdf")", PTR2UV(sv), iv)); 35 } else if (flags & SVf_NOK) { 35 NV nv; #if (PATCHLEVEL <= 6) nv = SvNV(sv); /* * Watch for number being an integer in disguise. */ if (nv == (NV) (iv = I_V(nv))) { TRACEME(("double %"NVff" is actually integer %"IVdf, nv, iv)); goto integer; /* Share code above */ } #else 35 SvIV_please(sv); 35 if (SvIOK_notUV(sv)) { ###### iv = SvIV(sv); ###### goto integer; /* Share code above */ } 35 nv = SvNV(sv); #endif 35 if (cxt->netorder) { TRACEME(("double %"NVff" stored as string", nv)); 9 goto string_readlen; /* Share code below */ } 26 PUTMARK(SX_DOUBLE); 26 WRITE(&nv, sizeof(nv)); TRACEME(("ok (double 0x%"UVxf", value = %"NVff")", PTR2UV(sv), nv)); ###### } else if (flags & (SVp_POK | SVp_NOK | SVp_IOK)) { 22794 I32 wlen; /* For 64-bit machines */ string_readlen: 22794 pv = SvPV(sv, len); /* * Will come here from above if it was readonly, POK and NOK but * neither &PL_sv_yes nor &PL_sv_no. */ string: 22794 wlen = (I32) len; /* WLEN via STORE_SCALAR expects I32 */ 22794 if (SvUTF8 (sv)) 12 STORE_UTF8STR(pv, wlen); else 22782 STORE_SCALAR(pv, wlen); TRACEME(("ok (scalar 0x%"UVxf" '%s', length = %"IVdf")", PTR2UV(sv), SvPVX(sv), (IV)len)); } else CROAK(("Can't determine type of %s(0x%"UVxf")", sv_reftype(sv, FALSE), ###### PTR2UV(sv))); 26023 return 0; /* Ok, no recursion on scalars */ } /* * store_array * * Store an array. * * Layout is SX_ARRAY followed by each item, in increading index order. * Each item is stored as . */ static int store_array(pTHX_ stcxt_t *cxt, AV *av) 1955 { 1955 SV **sav; 1955 I32 len = av_len(av) + 1; 1955 I32 i; 1955 int ret; TRACEME(("store_array (0x%"UVxf")", PTR2UV(av))); /* * Signal array by emitting SX_ARRAY, followed by the array length. */ 1955 PUTMARK(SX_ARRAY); 1955 WLEN(len); TRACEME(("size = %d", len)); /* * Now store each item recursively. */ 20306 for (i = 0; i < len; i++) { 18353 sav = av_fetch(av, i, 0); 18353 if (!sav) { TRACEME(("(#%d) undef item", i)); 3 STORE_SV_UNDEF(); 18350 continue; } TRACEME(("(#%d) item", i)); 18350 if ((ret = store(aTHX_ cxt, *sav))) /* Extra () for -Wall, grr... */ ###### return ret; } TRACEME(("ok (array)")); 1953 return 0; } #if (PATCHLEVEL <= 6) /* * sortcmp * * Sort two SVs * Borrowed from perl source file pp_ctl.c, where it is used by pp_sort. */ static int sortcmp(const void *a, const void *b) { #if defined(USE_ITHREADS) dTHX; #endif /* USE_ITHREADS */ return sv_cmp(*(SV * const *) a, *(SV * const *) b); } #endif /* PATCHLEVEL <= 6 */ /* * store_hash * * Store a hash table. * * For a "normal" hash (not restricted, no utf8 keys): * * Layout is SX_HASH followed by each key/value pair, in random order. * Values are stored as . * Keys are stored as , the section being omitted * if length is 0. * * For a "fancy" hash (restricted or utf8 keys): * * Layout is SX_FLAG_HASH followed by each key/value pair, * in random order. * Values are stored as . * Keys are stored as , the section being omitted * if length is 0. * Currently the only hash flag is "restriced" * Key flags are as for hv.h */ static int store_hash(pTHX_ stcxt_t *cxt, HV *hv) 1024 { dVAR; 1024 I32 len = #ifdef HAS_RESTRICTED_HASHES 1024 HvTOTALKEYS(hv); #else HvKEYS(hv); #endif 1024 I32 i; 1024 int ret = 0; 1024 I32 riter; 1024 HE *eiter; 1024 int flagged_hash = ((SvREADONLY(hv) #ifdef HAS_HASH_KEY_FLAGS || HvHASKFLAGS(hv) #endif 1024 ) ? 1 : 0); 1024 unsigned char hash_flags = (SvREADONLY(hv) ? SHV_RESTRICTED : 0); 1024 if (flagged_hash) { /* needs int cast for C++ compilers, doesn't it? */ TRACEME(("store_hash (0x%"UVxf") (flags %x)", PTR2UV(hv), (int) hash_flags)); } else { TRACEME(("store_hash (0x%"UVxf")", PTR2UV(hv))); } /* * Signal hash by emitting SX_HASH, followed by the table length. */ 1024 if (flagged_hash) { 31 PUTMARK(SX_FLAG_HASH); 31 PUTMARK(hash_flags); } else { 993 PUTMARK(SX_HASH); } 1024 WLEN(len); TRACEME(("size = %d", len)); /* * Save possible iteration state via each() on that table. */ 1024 riter = HvRITER_get(hv); 1024 eiter = HvEITER_get(hv); 1024 hv_iterinit(hv); /* * Now store each item recursively. * * If canonical is defined to some true value then store each * key/value pair in sorted order otherwise the order is random. * Canonical order is irrelevant when a deep clone operation is performed. * * Fetch the value from perl only once per store() operation, and only * when needed. */ 1024 if ( !(cxt->optype & ST_CLONE) && (cxt->canonical == 1 || (cxt->canonical < 0 && (cxt->canonical = 4 (SvTRUE(perl_get_sv("Storable::canonical", TRUE)) ? 1 : 0)))) ) { /* * Storing in order, sorted by key. * Run through the hash, building up an array of keys in a * mortal array, sort the array and then run through the * array. */ 404 AV *av = newAV(); /*av_extend (av, len);*/ TRACEME(("using canonical order")); 4802 for (i = 0; i < len; i++) { #ifdef HAS_RESTRICTED_HASHES 4398 HE *he = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS); #else HE *he = hv_iternext(hv); #endif 4398 SV *key = hv_iterkeysv(he); 4398 av_store(av, AvFILLp(av)+1, key); /* av_push(), really */ } 404 STORE_HASH_SORT; 4802 for (i = 0; i < len; i++) { #ifdef HAS_RESTRICTED_HASHES 4398 int placeholders = (int)HvPLACEHOLDERS_get(hv); #endif 4398 unsigned char flags = 0; 4398 char *keyval; 4398 STRLEN keylen_tmp; 4398 I32 keylen; 4398 SV *key = av_shift(av); /* This will fail if key is a placeholder. Track how many placeholders we have, and error if we "see" too many. */ 4398 HE *he = hv_fetch_ent(hv, key, 0, 0); 4398 SV *val; 4398 if (he) { 4380 if (!(val = HeVAL(he))) { /* Internal error, not I/O error */ ###### return 1; } } else { #ifdef HAS_RESTRICTED_HASHES /* Should be a placeholder. */ 18 if (placeholders-- < 0) { /* This should not happen - number of retrieves should be identical to number of placeholders. */ ###### return 1; } /* Value is never needed, and PL_sv_undef is more space efficient to store. */ 18 val = &PL_sv_undef; ASSERT (flags == 0, ("Flags not 0 but %d", flags)); 18 flags = SHV_K_PLACEHOLDER; #else return 1; #endif } /* * Store value first. */ TRACEME(("(#%d) value 0x%"UVxf, i, PTR2UV(val))); 4398 if ((ret = store(aTHX_ cxt, val))) /* Extra () for -Wall, grr... */ ###### goto out; /* * Write key string. * Keys are written after values to make sure retrieval * can be optimal in terms of memory usage, where keys are * read into a fixed unique buffer called kbuf. * See retrieve_hash() for details. */ /* Implementation of restricted hashes isn't nicely abstracted: */ 4398 if ((hash_flags & SHV_RESTRICTED) && SvREADONLY(val)) { 20 flags |= SHV_K_LOCKED; } 4398 keyval = SvPV(key, keylen_tmp); 4398 keylen = keylen_tmp; #ifdef HAS_UTF8_HASHES /* If you build without optimisation on pre 5.6 then nothing spots that SvUTF8(key) is always 0, so the block isn't optimised away, at which point the linker dislikes the reference to bytes_from_utf8. */ 4398 if (SvUTF8(key)) { 14 const char *keysave = keyval; 14 bool is_utf8 = TRUE; /* Just casting the &klen to (STRLEN) won't work well if STRLEN and I32 are of different widths. --jhi */ 14 keyval = (char*)bytes_from_utf8((U8*)keyval, &keylen_tmp, &is_utf8); /* If we were able to downgrade here, then than means that we have a key which only had chars 0-255, but was utf8 encoded. */ 14 if (keyval != keysave) { 5 keylen = keylen_tmp; 5 flags |= SHV_K_WASUTF8; } else { /* keylen_tmp can't have changed, so no need to assign back to keylen. */ 9 flags |= SHV_K_UTF8; } } #endif 4398 if (flagged_hash) { 47 PUTMARK(flags); TRACEME(("(#%d) key '%s' flags %x %u", i, keyval, flags, *keyval)); } else { /* This is a workaround for a bug in 5.8.0 that causes the HEK_WASUTF8 flag to be set on an HEK without the hash being marked as having key flags. We just cross our fingers and drop the flag. AMS 20030901 */ 4351 assert (flags == 0 || flags == SHV_K_WASUTF8); TRACEME(("(#%d) key '%s'", i, keyval)); } 4398 WLEN(keylen); 4398 if (keylen) 4398 WRITE(keyval, keylen); 4398 if (flags & SHV_K_WASUTF8) 5 Safefree (keyval); } /* * Free up the temporary array */ 404 av_undef(av); 404 sv_free((SV *) av); } else { /* * Storing in "random" order (in the order the keys are stored * within the hash). This is the default and will be faster! */ 6844 for (i = 0; i < len; i++) { 6225 char *key = 0; 6225 I32 len; 6225 unsigned char flags; #ifdef HV_ITERNEXT_WANTPLACEHOLDERS 6225 HE *he = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS); #else HE *he = hv_iternext(hv); #endif 6225 SV *val = (he ? hv_iterval(hv, he) : 0); 6225 SV *key_sv = NULL; 6225 HEK *hek; 6225 if (val == 0) ###### return 1; /* Internal error, not I/O error */ /* Implementation of restricted hashes isn't nicely abstracted: */ 6225 flags = (((hash_flags & SHV_RESTRICTED) && SvREADONLY(val)) ? SHV_K_LOCKED : 0); 6225 if (val == &PL_sv_placeholder) { 54 flags |= SHV_K_PLACEHOLDER; 54 val = &PL_sv_undef; } /* * Store value first. */ TRACEME(("(#%d) value 0x%"UVxf, i, PTR2UV(val))); 6225 if ((ret = store(aTHX_ cxt, val))) /* Extra () for -Wall, grr... */ ###### goto out; 6224 hek = HeKEY_hek(he); 6224 len = HEK_LEN(hek); 6224 if (len == HEf_SVKEY) { /* This is somewhat sick, but the internal APIs are * such that XS code could put one of these in in * a regular hash. * Maybe we should be capable of storing one if * found. */ ###### key_sv = HeKEY_sv(he); ###### flags |= SHV_K_ISSV; } else { /* Regular string key. */ #ifdef HAS_HASH_KEY_FLAGS 6224 if (HEK_UTF8(hek)) 9 flags |= SHV_K_UTF8; 6224 if (HEK_WASUTF8(hek)) 6 flags |= SHV_K_WASUTF8; #endif 6224 key = HEK_KEY(hek); } /* * Write key string. * Keys are written after values to make sure retrieval * can be optimal in terms of memory usage, where keys are * read into a fixed unique buffer called kbuf. * See retrieve_hash() for details. */ 6224 if (flagged_hash) { 94 PUTMARK(flags); TRACEME(("(#%d) key '%s' flags %x", i, key, flags)); } else { /* This is a workaround for a bug in 5.8.0 that causes the HEK_WASUTF8 flag to be set on an HEK without the hash being marked as having key flags. We just cross our fingers and drop the flag. AMS 20030901 */ 6130 assert (flags == 0 || flags == SHV_K_WASUTF8); TRACEME(("(#%d) key '%s'", i, key)); } 6224 if (flags & SHV_K_ISSV) { ###### store(aTHX_ cxt, key_sv); } else { 6224 WLEN(len); 6224 if (len) 6219 WRITE(key, len); } } } TRACEME(("ok (hash 0x%"UVxf")", PTR2UV(hv))); out: 1023 HvRITER_set(hv, riter); /* Restore hash iterator state */ 1023 HvEITER_set(hv, eiter); 1023 return ret; } /* * store_code * * Store a code reference. * * Layout is SX_CODE followed by a scalar containing the perl * source code of the code reference. */ static int store_code(pTHX_ stcxt_t *cxt, CV *cv) 69 { #if PERL_VERSION < 6 /* * retrieve_code does not work with perl 5.005 or less */ return store_other(aTHX_ cxt, (SV*)cv); #else 69 dSP; 69 I32 len; 69 int count, reallen; 69 SV *text, *bdeparse; TRACEME(("store_code (0x%"UVxf")", PTR2UV(cv))); 69 if ( cxt->deparse == 0 || (cxt->deparse < 0 && !(cxt->deparse = ###### SvTRUE(perl_get_sv("Storable::Deparse", TRUE)) ? 1 : 0)) ) { 3 return store_other(aTHX_ cxt, (SV*)cv); } /* * Require B::Deparse. At least B::Deparse 0.61 is needed for * blessed code references. */ /* Ownership of both SVs is passed to load_module, which frees them. */ 66 load_module(PERL_LOADMOD_NOIMPORT, newSVpvn("B::Deparse",10), newSVnv(0.61)); 66 ENTER; 66 SAVETMPS; /* * create the B::Deparse object */ 66 PUSHMARK(sp); 66 XPUSHs(sv_2mortal(newSVpvn("B::Deparse",10))); 66 PUTBACK; 66 count = call_method("new", G_SCALAR); 66 SPAGAIN; 66 if (count != 1) ###### CROAK(("Unexpected return value from B::Deparse::new\n")); 66 bdeparse = POPs; /* * call the coderef2text method */ 66 PUSHMARK(sp); 66 XPUSHs(bdeparse); /* XXX is this already mortal? */ 66 XPUSHs(sv_2mortal(newRV_inc((SV*)cv))); 66 PUTBACK; 66 count = call_method("coderef2text", G_SCALAR); 66 SPAGAIN; 66 if (count != 1) ###### CROAK(("Unexpected return value from B::Deparse::coderef2text\n")); 66 text = POPs; 66 len = SvCUR(text); 66 reallen = strlen(SvPV_nolen(text)); /* * Empty code references or XS functions are deparsed as * "(prototype) ;" or ";". */ 66 if (len == 0 || *(SvPV_nolen(text)+reallen-1) == ';') { 1 CROAK(("The result of B::Deparse::coderef2text was empty - maybe you're trying to serialize an XS function?\n")); } /* * Signal code by emitting SX_CODE. */ 65 PUTMARK(SX_CODE); 65 cxt->tagnum++; /* necessary, as SX_CODE is a SEEN() candidate */ TRACEME(("size = %d", len)); TRACEME(("code = %s", SvPV_nolen(text))); /* * Now store the source code. */ 65 STORE_SCALAR(SvPV_nolen(text), len); 65 FREETMPS; 65 LEAVE; TRACEME(("ok (code)")); 65 return 0; #endif } /* * store_tied * * When storing a tied object (be it a tied scalar, array or hash), we lay out * a special mark, followed by the underlying tied object. For instance, when * dealing with a tied hash, we store SX_TIED_HASH , where * stands for the serialization of the tied hash. */ static int store_tied(pTHX_ stcxt_t *cxt, SV *sv) 17 { 17 MAGIC *mg; 17 SV *obj = NULL; 17 int ret = 0; 17 int svt = SvTYPE(sv); 17 char mtype = 'P'; TRACEME(("store_tied (0x%"UVxf")", PTR2UV(sv))); /* * We have a small run-time penalty here because we chose to factorise * all tieds objects into the same routine, and not have a store_tied_hash, * a store_tied_array, etc... * * Don't use a switch() statement, as most compilers don't optimize that * well for 2/3 values. An if() else if() cascade is just fine. We put * tied hashes first, as they are the most likely beasts. */ 17 if (svt == SVt_PVHV) { TRACEME(("tied hash")); 7 PUTMARK(SX_TIED_HASH); /* Introduces tied hash */ 10 } else if (svt == SVt_PVAV) { TRACEME(("tied array")); 4 PUTMARK(SX_TIED_ARRAY); /* Introduces tied array */ } else { TRACEME(("tied scalar")); 6 PUTMARK(SX_TIED_SCALAR); /* Introduces tied scalar */ 6 mtype = 'q'; } 17 if (!(mg = mg_find(sv, mtype))) CROAK(("No magic '%c' found while storing tied %s", mtype, (svt == SVt_PVHV) ? "hash" : ###### (svt == SVt_PVAV) ? "array" : "scalar")); /* * The mg->mg_obj found by mg_find() above actually points to the * underlying tied Perl object implementation. For instance, if the * original SV was that of a tied array, then mg->mg_obj is an AV. * * Note that we store the Perl object as-is. We don't call its FETCH * method along the way. At retrieval time, we won't call its STORE * method either, but the tieing magic will be re-installed. In itself, * that ensures that the tieing semantics are preserved since futher * accesses on the retrieved object will indeed call the magic methods... */ /* [#17040] mg_obj is NULL for scalar self-ties. AMS 20030416 */ 17 obj = mg->mg_obj ? mg->mg_obj : newSV(0); 17 if ((ret = store(aTHX_ cxt, obj))) ###### return ret; TRACEME(("ok (tied)")); 17 return 0; } /* * store_tied_item * * Stores a reference to an item within a tied structure: * * . \$h{key}, stores both the (tied %h) object and 'key'. * . \$a[idx], stores both the (tied @a) object and 'idx'. * * Layout is therefore either: * SX_TIED_KEY * SX_TIED_IDX */ static int store_tied_item(pTHX_ stcxt_t *cxt, SV *sv) 2 { 2 MAGIC *mg; 2 int ret; TRACEME(("store_tied_item (0x%"UVxf")", PTR2UV(sv))); 2 if (!(mg = mg_find(sv, 'p'))) ###### CROAK(("No magic 'p' found while storing reference to tied item")); /* * We discriminate between \$h{key} and \$a[idx] via mg_ptr. */ 2 if (mg->mg_ptr) { TRACEME(("store_tied_item: storing a ref to a tied hash item")); 1 PUTMARK(SX_TIED_KEY); TRACEME(("store_tied_item: storing OBJ 0x%"UVxf, PTR2UV(mg->mg_obj))); 1 if ((ret = store(aTHX_ cxt, mg->mg_obj))) /* Extra () for -Wall, grr... */ ###### return ret; TRACEME(("store_tied_item: storing PTR 0x%"UVxf, PTR2UV(mg->mg_ptr))); 1 if ((ret = store(aTHX_ cxt, (SV *) mg->mg_ptr))) /* Idem, for -Wall */ ###### return ret; } else { 1 I32 idx = mg->mg_len; TRACEME(("store_tied_item: storing a ref to a tied array item ")); 1 PUTMARK(SX_TIED_IDX); TRACEME(("store_tied_item: storing OBJ 0x%"UVxf, PTR2UV(mg->mg_obj))); 1 if ((ret = store(aTHX_ cxt, mg->mg_obj))) /* Idem, for -Wall */ ###### return ret; TRACEME(("store_tied_item: storing IDX %d", idx)); 1 WLEN(idx); } TRACEME(("ok (tied item)")); 2 return 0; } /* * store_hook -- dispatched manually, not via sv_store[] * * The blessed SV is serialized by a hook. * * Simple Layout is: * * SX_HOOK [ ] * * where indicates how long , and are, whether * the trailing part [] is present, the type of object (scalar, array or hash). * There is also a bit which says how the classname is stored between: * * * * * and when the form is used (classname already seen), the "large * classname" bit in indicates how large the is. * * The serialized string returned by the hook is of length and comes * next. It is an opaque string for us. * * Those object IDs which are listed last represent the extra references * not directly serialized by the hook, but which are linked to the object. * * When recursion is mandated to resolve object-IDs not yet seen, we have * instead, with
being flags with bits set to indicate the object type * and that recursion was indeed needed: * * SX_HOOK
* * that same header being repeated between serialized objects obtained through * recursion, until we reach flags indicating no recursion, at which point * we know we've resynchronized with a single layout, after . * * When storing a blessed ref to a tied variable, the following format is * used: * * SX_HOOK ... [ ] * * The first indication carries an object of type SHT_EXTRA, and the * real object type is held in the flag. At the very end of the * serialization stream, the underlying magic object is serialized, just like * any other tied variable. */ static int store_hook( pTHX_ stcxt_t *cxt, SV *sv, int type, HV *pkg, SV *hook) 97 { 97 I32 len; 97 char *classname; 97 STRLEN len2; 97 SV *ref; 97 AV *av; 97 SV **ary; 97 int count; /* really len3 + 1 */ 97 unsigned char flags; 97 char *pv; 97 int i; 97 int recursed = 0; /* counts recursion */ 97 int obj_type; /* object type, on 2 bits */ 97 I32 classnum; 97 int ret; 97 int clone = cxt->optype & ST_CLONE; 97 char mtype = '\0'; /* for blessed ref to tied structures */ 97 unsigned char eflags = '\0'; /* used when object type is SHT_EXTRA */ TRACEME(("store_hook, classname \"%s\", tagged #%d", HvNAME_get(pkg), cxt->tagnum)); /* * Determine object type on 2 bits. */ 97 switch (type) { case svis_SCALAR: 2 obj_type = SHT_SCALAR; 2 break; case svis_ARRAY: 73 obj_type = SHT_ARRAY; 73 break; case svis_HASH: 21 obj_type = SHT_HASH; 21 break; case svis_TIED: /* * Produced by a blessed ref to a tied data structure, $o in the * following Perl code. * * my %h; * tie %h, 'FOO'; * my $o = bless \%h, 'BAR'; * * Signal the tie-ing magic by setting the object type as SHT_EXTRA * (since we have only 2 bits in to store the type), and an * byte flag will be emitted after the FIRST in the * stream, carrying what we put in `eflags'. */ 1 obj_type = SHT_EXTRA; 1 switch (SvTYPE(sv)) { case SVt_PVHV: 1 eflags = (unsigned char) SHT_THASH; 1 mtype = 'P'; 1 break; case SVt_PVAV: ###### eflags = (unsigned char) SHT_TARRAY; ###### mtype = 'P'; ###### break; default: ###### eflags = (unsigned char) SHT_TSCALAR; ###### mtype = 'q'; ###### break; } ###### break; default: ###### CROAK(("Unexpected object type (%d) in store_hook()", type)); } 97 flags = SHF_NEED_RECURSE | obj_type; 97 classname = HvNAME_get(pkg); 97 len = strlen(classname); /* * To call the hook, we need to fake a call like: * * $object->STORABLE_freeze($cloning); * * but we don't have the $object here. For instance, if $object is * a blessed array, what we have in `sv' is the array, and we can't * call a method on those. * * Therefore, we need to create a temporary reference to the object and * make the call on that reference. */ TRACEME(("about to call STORABLE_freeze on class %s", classname)); 97 ref = newRV_noinc(sv); /* Temporary reference */ 97 av = array_call(aTHX_ ref, hook, clone); /* @a = $object->STORABLE_freeze($c) */ 97 SvRV_set(ref, NULL); 97 SvREFCNT_dec(ref); /* Reclaim temporary reference */ 97 count = AvFILLp(av) + 1; TRACEME(("store_hook, array holds %d items", count)); /* * If they return an empty list, it means they wish to ignore the * hook for this class (and not just this instance -- that's for them * to handle if they so wish). * * Simply disable the cached entry for the hook (it won't be recomputed * since it's present in the cache) and recurse to store_blessed(). */ 97 if (!count) { /* * They must not change their mind in the middle of a serialization. */ 1 if (hv_fetch(cxt->hclass, classname, len, FALSE)) CROAK(("Too late to ignore hooks for %s class \"%s\"", ###### (cxt->optype & ST_CLONE) ? "cloning" : "storing", classname)); 1 pkg_hide(aTHX_ cxt->hook, pkg, "STORABLE_freeze"); ASSERT(!pkg_can(aTHX_ cxt->hook, pkg, "STORABLE_freeze"), ("hook invisible")); TRACEME(("ignoring STORABLE_freeze in class \"%s\"", classname)); 1 return store_blessed(aTHX_ cxt, sv, type, pkg); } /* * Get frozen string. */ 96 ary = AvARRAY(av); 96 pv = SvPV(ary[0], len2); /* We can't use pkg_can here because it only caches one method per * package */ { 96 GV* gv = gv_fetchmethod_autoload(pkg, "STORABLE_attach", FALSE); 96 if (gv && isGV(gv)) { 6 if (count > 1) 1 CROAK(("Freeze cannot return references if %s class is using STORABLE_attach", classname)); 90 goto check_done; } } /* * If they returned more than one item, we need to serialize some * extra references if not already done. * * Loop over the array, starting at position #1, and for each item, * ensure it is a reference, serialize it if not already done, and * replace the entry with the tag ID of the corresponding serialized * object. * * We CHEAT by not calling av_fetch() and read directly within the * array, for speed. */ 188 for (i = 1; i < count; i++) { #ifdef USE_PTR_TABLE char *fake_tag; #else 98 SV **svh; #endif 98 SV *rsv = ary[i]; 98 SV *xsv; 98 SV *tag; 98 AV *av_hook = cxt->hook_seen; 98 if (!SvROK(rsv)) CROAK(("Item #%d returned by STORABLE_freeze " ###### "for %s is not a reference", i, classname)); 98 xsv = SvRV(rsv); /* Follow ref to know what to look for */ /* * Look in hseen and see if we have a tag already. * Serialize entry if not done already, and get its tag. */ #ifdef USE_PTR_TABLE /* Fakery needed because ptr_table_fetch returns zero for a failure, whereas the existing code assumes that it can safely store a tag zero. So for ptr_tables we store tag+1 */ if ((fake_tag = ptr_table_fetch(cxt->pseen, xsv))) goto sv_seen; /* Avoid moving code too far to the right */ #else 98 if ((svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE))) 79 goto sv_seen; /* Avoid moving code too far to the right */ #endif TRACEME(("listed object %d at 0x%"UVxf" is unknown", i-1, PTR2UV(xsv))); /* * We need to recurse to store that object and get it to be known * so that we can resolve the list of object-IDs at retrieve time. * * The first time we do this, we need to emit the proper header * indicating that we recursed, and what the type of object is (the * object we're storing via a user-hook). Indeed, during retrieval, * we'll have to create the object before recursing to retrieve the * others, in case those would point back at that object. */ /* [SX_HOOK] [] */ 19 if (!recursed++) { 17 PUTMARK(SX_HOOK); 17 PUTMARK(flags); 17 if (obj_type == SHT_EXTRA) ###### PUTMARK(eflags); } else 2 PUTMARK(flags); 19 if ((ret = store(aTHX_ cxt, xsv))) /* Given by hook for us to store */ ###### return ret; #ifdef USE_PTR_TABLE fake_tag = ptr_table_fetch(cxt->pseen, xsv); if (!sv) CROAK(("Could not serialize item #%d from hook in %s", i, classname)); #else 19 svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE); 19 if (!svh) ###### CROAK(("Could not serialize item #%d from hook in %s", i, classname)); #endif /* * It was the first time we serialized `xsv'. * * Keep this SV alive until the end of the serialization: if we * disposed of it right now by decrementing its refcount, and it was * a temporary value, some next temporary value allocated during * another STORABLE_freeze might take its place, and we'd wrongly * assume that new SV was already serialized, based on its presence * in cxt->hseen. * * Therefore, push it away in cxt->hook_seen. */ 19 av_store(av_hook, AvFILLp(av_hook)+1, SvREFCNT_inc(xsv)); sv_seen: /* * Dispose of the REF they returned. If we saved the `xsv' away * in the array of returned SVs, that will not cause the underlying * referenced SV to be reclaimed. */ ASSERT(SvREFCNT(xsv) > 1, ("SV will survive disposal of its REF")); 98 SvREFCNT_dec(rsv); /* Dispose of reference */ /* * Replace entry with its tag (not a real SV, so no refcnt increment) */ #ifdef USE_PTR_TABLE tag = (SV *)--fake_tag; #else 98 tag = *svh; #endif 98 ary[i] = tag TRACEME(("listed object %d at 0x%"UVxf" is tag #%"UVuf, i-1, PTR2UV(xsv), PTR2UV(tag))); } /* * Allocate a class ID if not already done. * * This needs to be done after the recursion above, since at retrieval * time, we'll see the inner objects first. Many thanks to * Salvador Ortiz Garcia who spot that bug and * proposed the right fix. -- RAM, 15/09/2000 */ check_done: 95 if (!known_class(aTHX_ cxt, classname, len, &classnum)) { TRACEME(("first time we see class %s, ID = %d", classname, classnum)); 76 classnum = -1; /* Mark: we must store classname */ } else { TRACEME(("already seen class %s, ID = %d", classname, classnum)); } /* * Compute leading flags. */ 95 flags = obj_type; 95 if (((classnum == -1) ? len : classnum) > LG_SCALAR) ###### flags |= SHF_LARGE_CLASSLEN; 95 if (classnum != -1) 19 flags |= SHF_IDX_CLASSNAME; 95 if (len2 > LG_SCALAR) 22 flags |= SHF_LARGE_STRLEN; 95 if (count > 1) 80 flags |= SHF_HAS_LIST; 95 if (count > (LG_SCALAR + 1)) ###### flags |= SHF_LARGE_LISTLEN; /* * We're ready to emit either serialized form: * * SX_HOOK [ ] * SX_HOOK [ ] * * If we recursed, the SX_HOOK has already been emitted. */ TRACEME(("SX_HOOK (recursed=%d) flags=0x%x " "class=%"IVdf" len=%"IVdf" len2=%"IVdf" len3=%d", recursed, flags, (IV)classnum, (IV)len, (IV)len2, count-1)); /* SX_HOOK [] */ 95 if (!recursed) { 78 PUTMARK(SX_HOOK); 78 PUTMARK(flags); 78 if (obj_type == SHT_EXTRA) 1 PUTMARK(eflags); } else 17 PUTMARK(flags); /* or */ 95 if (flags & SHF_IDX_CLASSNAME) { 19 if (flags & SHF_LARGE_CLASSLEN) ###### WLEN(classnum); else { 19 unsigned char cnum = (unsigned char) classnum; 19 PUTMARK(cnum); } } else { 76 if (flags & SHF_LARGE_CLASSLEN) ###### WLEN(len); else { 76 unsigned char clen = (unsigned char) len; 76 PUTMARK(clen); } 76 WRITE(classname, len); /* Final \0 is omitted */ } /* */ 95 if (flags & SHF_LARGE_STRLEN) { 22 I32 wlen2 = len2; /* STRLEN might be 8 bytes */ 22 WLEN(wlen2); /* Must write an I32 for 64-bit machines */ } else { 73 unsigned char clen = (unsigned char) len2; 73 PUTMARK(clen); } 95 if (len2) 63 WRITE(pv, (SSize_t)len2); /* Final \0 is omitted */ /* [ ] */ 95 if (flags & SHF_HAS_LIST) { 80 int len3 = count - 1; 80 if (flags & SHF_LARGE_LISTLEN) ###### WLEN(len3); else { 80 unsigned char clen = (unsigned char) len3; 80 PUTMARK(clen); } /* * NOTA BENE, for 64-bit machines: the ary[i] below does not yield a * real pointer, rather a tag number, well under the 32-bit limit. */ 178 for (i = 1; i < count; i++) { 98 I32 tagval = htonl(LOW_32BITS(ary[i])); 98 WRITE_I32(tagval); TRACEME(("object %d, tag #%d", i-1, ntohl(tagval))); } } /* * Free the array. We need extra care for indices after 0, since they * don't hold real SVs but integers cast. */ 95 if (count > 1) 80 AvFILLp(av) = 0; /* Cheat, nothing after 0 interests us */ 95 av_undef(av); 95 sv_free((SV *) av); /* * If object was tied, need to insert serialization of the magic object. */ 95 if (obj_type == SHT_EXTRA) { 1 MAGIC *mg; 1 if (!(mg = mg_find(sv, mtype))) { ###### int svt = SvTYPE(sv); CROAK(("No magic '%c' found while storing ref to tied %s with hook", mtype, (svt == SVt_PVHV) ? "hash" : ###### (svt == SVt_PVAV) ? "array" : "scalar")); } TRACEME(("handling the magic object 0x%"UVxf" part of 0x%"UVxf, PTR2UV(mg->mg_obj), PTR2UV(sv))); /* * [] */ 1 if ((ret = store(aTHX_ cxt, mg->mg_obj))) /* Extra () for -Wall, grr... */ ###### return ret; } 95 return 0; } /* * store_blessed -- dispatched manually, not via sv_store[] * * Check whether there is a STORABLE_xxx hook defined in the class or in one * of its ancestors. If there is, then redispatch to store_hook(); * * Otherwise, the blessed SV is stored using the following layout: * * SX_BLESS * * where indicates whether is stored on 0 or 4 bytes, depending * on the high-order bit in flag: if 1, then length follows on 4 bytes. * Otherwise, the low order bits give the length, thereby giving a compact * representation for class names less than 127 chars long. * * Each seen is remembered and indexed, so that the next time * an object in the blessed in the same is stored, the following * will be emitted: * * SX_IX_BLESS * * where is the classname index, stored on 0 or 4 bytes depending * on the high-order bit in flag (same encoding as above for ). */ static int store_blessed( pTHX_ stcxt_t *cxt, SV *sv, int type, HV *pkg) 203 { 203 SV *hook; 203 I32 len; 203 char *classname; 203 I32 classnum; TRACEME(("store_blessed, type %d, class \"%s\"", type, HvNAME_get(pkg))); /* * Look for a hook for this blessed SV and redirect to store_hook() * if needed. */ 203 hook = pkg_can(aTHX_ cxt->hook, pkg, "STORABLE_freeze"); 203 if (hook) 97 return store_hook(aTHX_ cxt, sv, type, pkg, hook); /* * This is a blessed SV without any serialization hook. */ 106 classname = HvNAME_get(pkg); 106 len = strlen(classname); TRACEME(("blessed 0x%"UVxf" in %s, no hook: tagged #%d", PTR2UV(sv), classname, cxt->tagnum)); /* * Determine whether it is the first time we see that class name (in which * case it will be stored in the SX_BLESS form), or whether we already * saw that class name before (in which case the SX_IX_BLESS form will be * used). */ 106 if (known_class(aTHX_ cxt, classname, len, &classnum)) { TRACEME(("already seen class %s, ID = %d", classname, classnum)); 27 PUTMARK(SX_IX_BLESS); 27 if (classnum <= LG_BLESS) { 27 unsigned char cnum = (unsigned char) classnum; 27 PUTMARK(cnum); } else { ###### unsigned char flag = (unsigned char) 0x80; ###### PUTMARK(flag); ###### WLEN(classnum); } } else { TRACEME(("first time we see class %s, ID = %d", classname, classnum)); 79 PUTMARK(SX_BLESS); 79 if (len <= LG_BLESS) { 78 unsigned char clen = (unsigned char) len; 78 PUTMARK(clen); } else { 1 unsigned char flag = (unsigned char) 0x80; 1 PUTMARK(flag); 1 WLEN(len); /* Don't BER-encode, this should be rare */ } 79 WRITE(classname, len); /* Final \0 is omitted */ } /* * Now emit the part. */ 106 return SV_STORE(type)(aTHX_ cxt, sv); } /* * store_other * * We don't know how to store the item we reached, so return an error condition. * (it's probably a GLOB, some CODE reference, etc...) * * If they defined the `forgive_me' variable at the Perl level to some * true value, then don't croak, just warn, and store a placeholder string * instead. */ static int store_other(pTHX_ stcxt_t *cxt, SV *sv) 5 { 5 I32 len; 5 char buf[80]; TRACEME(("store_other")); /* * Fetch the value from perl only once per store() operation. */ 5 if ( cxt->forgive_me == 0 || (cxt->forgive_me < 0 && !(cxt->forgive_me = ###### SvTRUE(perl_get_sv("Storable::forgive_me", TRUE)) ? 1 : 0)) ) 3 CROAK(("Can't store %s items", sv_reftype(sv, FALSE))); 2 warn("Can't store item %s(0x%"UVxf")", sv_reftype(sv, FALSE), PTR2UV(sv)); /* * Store placeholder string as a scalar instead... */ 2 (void) sprintf(buf, "You lost %s(0x%"UVxf")%c", sv_reftype(sv, FALSE), PTR2UV(sv), (char) 0); 2 len = strlen(buf); 2 STORE_SCALAR(buf, len); TRACEME(("ok (dummy \"%s\", length = %"IVdf")", buf, (IV) len)); 2 return 0; } /*** *** Store driving routines ***/ /* * sv_type * * WARNING: partially duplicates Perl's sv_reftype for speed. * * Returns the type of the SV, identified by an integer. That integer * may then be used to index the dynamic routine dispatch table. */ static int sv_type(pTHX_ SV *sv) 32350 { 32350 switch (SvTYPE(sv)) { case SVt_NULL: case SVt_IV: case SVt_NV: /* * No need to check for ROK, that can't be set here since there * is no field capable of hodling the xrv_rv reference. */ 2955 return svis_SCALAR; case SVt_PV: case SVt_RV: case SVt_PVIV: case SVt_PVNV: /* * Starting from SVt_PV, it is possible to have the ROK flag * set, the pointer to the other SV being either stored in * the xrv_rv (in the case of a pure SVt_RV), or as the * xpv_pv field of an SVt_PV and its heirs. * * However, those SV cannot be magical or they would be an * SVt_PVMG at least. */ 24853 return SvROK(sv) ? svis_REF : svis_SCALAR; case SVt_PVMG: case SVt_PVLV: /* Workaround for perl5.004_04 "LVALUE" bug */ 1387 if (SvRMAGICAL(sv) && (mg_find(sv, 'p'))) 2 return svis_TIED_ITEM; /* FALL THROUGH */ case SVt_PVBM: 1385 if (SvRMAGICAL(sv) && (mg_find(sv, 'q'))) 6 return svis_TIED; 1379 return SvROK(sv) ? svis_REF : svis_SCALAR; case SVt_PVAV: 2032 if (SvRMAGICAL(sv) && (mg_find(sv, 'P'))) 4 return svis_TIED; 2028 return svis_ARRAY; case SVt_PVHV: 1052 if (SvRMAGICAL(sv) && (mg_find(sv, 'P'))) 8 return svis_TIED; 1044 return svis_HASH; case SVt_PVCV: 69 return svis_CODE; default: 2 break; } 2 return svis_OTHER; } /* * store * * Recursively store objects pointed to by the sv to the specified file. * * Layout is or SX_OBJECT if we reach an already stored * object (one for which storage has started -- it may not be over if we have * a self-referenced structure). This data set forms a stored . */ static int store(pTHX_ stcxt_t *cxt, SV *sv) 32539 { 32539 SV **svh; 32539 int ret; 32539 int type; #ifdef USE_PTR_TABLE struct ptr_tbl *pseen = cxt->pseen; #else 32539 HV *hseen = cxt->hseen; #endif TRACEME(("store (0x%"UVxf")", PTR2UV(sv))); /* * If object has already been stored, do not duplicate data. * Simply emit the SX_OBJECT marker followed by its tag data. * The tag is always written in network order. * * NOTA BENE, for 64-bit machines: the "*svh" below does not yield a * real pointer, rather a tag number (watch the insertion code below). * That means it probably safe to assume it is well under the 32-bit limit, * and makes the truncation safe. * -- RAM, 14/09/1999 */ #ifdef USE_PTR_TABLE svh = ptr_table_fetch(pseen, sv); #else 32539 svh = hv_fetch(hseen, (char *) &sv, sizeof(sv), FALSE); #endif 32539 if (svh) { 189 I32 tagval; 189 if (sv == &PL_sv_undef) { /* We have seen PL_sv_undef before, but fake it as if we have not. Not the simplest solution to making restricted hashes work on 5.8.0, but it does mean that repeated references to the one true undef will take up less space in the output file. */ /* Need to jump past the next hv_store, because on the second store of undef the old hash value will be SvREFCNT_dec()ed, and as Storable cheats horribly by storing non-SVs in the hash a SEGV will ensure. Need to increase the tag number so that the receiver has no idea what games we're up to. This special casing doesn't affect hooks that store undef, as the hook routine does its own lookup into hseen. Also this means that any references back to PL_sv_undef (from the pathological case of hooks storing references to it) will find the seen hash entry for the first time, as if we didn't have this hackery here. (That hseen lookup works even on 5.8.0 because it's a key of &PL_sv_undef and a value which is a tag number, not a value which is PL_sv_undef.) */ 64 cxt->tagnum++; 64 type = svis_SCALAR; 64 goto undef_special_case; } #ifdef USE_PTR_TABLE tagval = htonl(LOW_32BITS(((char *)svh)-1)); #else 125 tagval = htonl(LOW_32BITS(*svh)); #endif TRACEME(("object 0x%"UVxf" seen as #%d", PTR2UV(sv), ntohl(tagval))); 125 PUTMARK(SX_OBJECT); 125 WRITE_I32(tagval); 125 return 0; } /* * Allocate a new tag and associate it with the address of the sv being * stored, before recursing... * * In order to avoid creating new SvIVs to hold the tagnum we just * cast the tagnum to an SV pointer and store that in the hash. This * means that we must clean up the hash manually afterwards, but gives * us a 15% throughput increase. * */ 32350 cxt->tagnum++; #ifdef USE_PTR_TABLE ptr_table_store(pseen, sv, INT2PTR(SV*, 1 + cxt->tagnum)); #else 32350 if (!hv_store(hseen, (char *) &sv, sizeof(sv), INT2PTR(SV*, cxt->tagnum), 0)) ###### return -1; #endif /* * Store `sv' and everything beneath it, using appropriate routine. * Abort immediately if we get a non-zero status back. */ 32350 type = sv_type(aTHX_ sv); undef_special_case: TRACEME(("storing 0x%"UVxf" tag #%d, type %d...", PTR2UV(sv), cxt->tagnum, type)); 32414 if (SvOBJECT(sv)) { 202 HV *pkg = SvSTASH(sv); 202 ret = store_blessed(aTHX_ cxt, sv, type, pkg); } else 32212 ret = SV_STORE(type)(aTHX_ cxt, sv); TRACEME(("%s (stored 0x%"UVxf", refcnt=%d, %s)", ret ? "FAILED" : "ok", PTR2UV(sv), SvREFCNT(sv), sv_reftype(sv, FALSE))); 32403 return ret; } /* * magic_write * * Write magic number and system information into the file. * Layout is [ * ] where is the length of the byteorder hexa string. * All size and lenghts are written as single characters here. * * Note that no byte ordering info is emitted when is true, since * integers will be emitted in network order in that case. */ static int magic_write(pTHX_ stcxt_t *cxt) 399 { /* * Starting with 0.6, the "use_network_order" byte flag is also used to * indicate the version number of the binary image, encoded in the upper * bits. The bit 0 is always used to indicate network order. */ /* * Starting with 0.7, a full byte is dedicated to the minor version of * the binary format, which is incremented only when new markers are * introduced, for instance, but when backward compatibility is preserved. */ /* Make these at compile time. The WRITE() macro is sufficiently complex that it saves about 200 bytes doing it this way and only using it once. */ static const unsigned char network_file_header[] = { MAGICSTR_BYTES, (STORABLE_BIN_MAJOR << 1) | 1, STORABLE_BIN_WRITE_MINOR 399 }; static const unsigned char file_header[] = { MAGICSTR_BYTES, (STORABLE_BIN_MAJOR << 1) | 0, STORABLE_BIN_WRITE_MINOR, /* sizeof the array includes the 0 byte at the end: */ (char) sizeof (byteorderstr) - 1, BYTEORDER_BYTES, (unsigned char) sizeof(int), (unsigned char) sizeof(long), (unsigned char) sizeof(char *), (unsigned char) sizeof(NV) 399 }; #ifdef USE_56_INTERWORK_KLUDGE static const unsigned char file_header_56[] = { MAGICSTR_BYTES, (STORABLE_BIN_MAJOR << 1) | 0, STORABLE_BIN_WRITE_MINOR, /* sizeof the array includes the 0 byte at the end: */ (char) sizeof (byteorderstr_56) - 1, BYTEORDER_BYTES_56, (unsigned char) sizeof(int), (unsigned char) sizeof(long), (unsigned char) sizeof(char *), (unsigned char) sizeof(NV) }; #endif 399 const unsigned char *header; 399 SSize_t length; TRACEME(("magic_write on fd=%d", cxt->fio ? PerlIO_fileno(cxt->fio) : -1)); 399 if (cxt->netorder) { 91 header = network_file_header; 91 length = sizeof (network_file_header); } else { #ifdef USE_56_INTERWORK_KLUDGE if (SvTRUE(perl_get_sv("Storable::interwork_56_64bit", TRUE))) { header = file_header_56; length = sizeof (file_header_56); } else #endif { 308 header = file_header; 308 length = sizeof (file_header); } } 399 if (!cxt->fio) { /* sizeof the array includes the 0 byte at the end. */ 302 header += sizeof (magicstr) - 1; 302 length -= sizeof (magicstr) - 1; } 399 WRITE( (unsigned char*) header, length); 399 if (!cxt->netorder) { TRACEME(("ok (magic_write byteorder = 0x%lx [%d], I%d L%d P%d D%d)", (unsigned long) BYTEORDER, (int) sizeof (byteorderstr) - 1, (int) sizeof(int), (int) sizeof(long), (int) sizeof(char *), (int) sizeof(NV))); } 399 return 0; } /* * do_store * * Common code for store operations. * * When memory store is requested (f = NULL) and a non null SV* is given in * `res', it is filled with a new SV created out of the memory buffer. * * It is required to provide a non-null `res' when the operation type is not * dclone() and store() is performed to memory. */ static int do_store( pTHX_ PerlIO *f, SV *sv, int optype, int network_order, SV **res) 399 { 399 dSTCXT; 399 int status; ASSERT(!(f == 0 && !(optype & ST_CLONE)) || res, ("must supply result SV pointer for real recursion to memory")); TRACEME(("do_store (optype=%d, netorder=%d)", optype, network_order)); 399 optype |= ST_STORE; /* * Workaround for CROAK leak: if they enter with a "dirty" context, * free up memory for them now. */ 399 if (cxt->s_dirty) 15 clean_context(aTHX_ cxt); /* * Now that STORABLE_xxx hooks exist, it is possible that they try to * re-enter store() via the hooks. We need to stack contexts. */ 399 if (cxt->entry) 40 cxt = allocate_context(aTHX_ cxt); 399 cxt->entry++; ASSERT(cxt->entry == 1, ("starting new recursion")); ASSERT(!cxt->s_dirty, ("clean context")); /* * Ensure sv is actually a reference. From perl, we called something * like: * pstore(aTHX_ FILE, \@array); * so we must get the scalar value behing that reference. */ 399 if (!SvROK(sv)) ###### CROAK(("Not a reference")); 399 sv = SvRV(sv); /* So follow it to know what to store */ /* * If we're going to store to memory, reset the buffer. */ 399 if (!f) 302 MBUF_INIT(0); /* * Prepare context and emit headers. */ 399 init_store_context(aTHX_ cxt, f, optype, network_order); 399 if (-1 == magic_write(aTHX_ cxt)) /* Emit magic and ILP info */ ###### return 0; /* Error */ /* * Recursively store object... */ ASSERT(is_storing(aTHX), ("within store operation")); 399 status = store(aTHX_ cxt, sv); /* Just do it! */ /* * If they asked for a memory store and they provided an SV pointer, * make an SV string out of the buffer and fill their pointer. * * When asking for ST_REAL, it's MANDATORY for the caller to provide * an SV, since context cleanup might free the buffer if we did recurse. * (unless caller is dclone(), which is aware of that). */ 394 if (!cxt->fio && res) 237 *res = mbuf2sv(aTHX); /* * Final cleanup. * * The "root" context is never freed, since it is meant to be always * handy for the common case where no recursion occurs at all (i.e. * we enter store() outside of any Storable code and leave it, period). * We know it's the "root" context because there's nothing stacked * underneath it. * * OPTIMIZATION: * * When deep cloning, we don't free the context: doing so would force * us to copy the data in the memory buffer. Sicne we know we're * about to enter do_retrieve... */ 394 clean_store_context(aTHX_ cxt); 394 if (cxt->prev && !(cxt->optype & ST_CLONE)) 39 free_context(aTHX_ cxt); TRACEME(("do_store returns %d", status)); 394 return status == 0; } /* * pstore * * Store the transitive data closure of given object to disk. * Returns 0 on error, a true value otherwise. */ static int pstore(pTHX_ PerlIO *f, SV *sv) 51 { TRACEME(("pstore")); 51 return do_store(aTHX_ f, sv, 0, FALSE, (SV**) 0); } /* * net_pstore * * Same as pstore(), but network order is used for integers and doubles are * emitted as strings. */ static int net_pstore(pTHX_ PerlIO *f, SV *sv) 46 { TRACEME(("net_pstore")); 46 return do_store(aTHX_ f, sv, 0, TRUE, (SV**) 0); } /*** *** Memory stores. ***/ /* * mbuf2sv * * Build a new SV out of the content of the internal memory buffer. */ static SV *mbuf2sv(pTHX) 237 { 237 dSTCXT; 237 return newSVpv(mbase, MBUF_SIZE()); } /* * mstore * * Store the transitive data closure of given object to memory. * Returns undef on error, a scalar value containing the data otherwise. */ static SV *mstore(pTHX_ SV *sv) 196 { 196 SV *out; TRACEME(("mstore")); 196 if (!do_store(aTHX_ (PerlIO*) 0, sv, 0, FALSE, &out)) ###### return &PL_sv_undef; 192 return out; } /* * net_mstore * * Same as mstore(), but network order is used for integers and doubles are * emitted as strings. */ static SV *net_mstore(pTHX_ SV *sv) 45 { 45 SV *out; TRACEME(("net_mstore")); 45 if (!do_store(aTHX_ (PerlIO*) 0, sv, 0, TRUE, &out)) ###### return &PL_sv_undef; 45 return out; } /*** *** Specific retrieve callbacks. ***/ /* * retrieve_other * * Return an error via croak, since it is not possible that we get here * under normal conditions, when facing a file produced via pstore(). */ static SV *retrieve_other(pTHX_ stcxt_t *cxt, const char *cname) 8 { 8 if ( cxt->ver_major != STORABLE_BIN_MAJOR && cxt->ver_minor != STORABLE_BIN_MINOR ) { CROAK(("Corrupted storable %s (binary v%d.%d), current is v%d.%d", cxt->fio ? "file" : "string", cxt->ver_major, cxt->ver_minor, ###### STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR)); } else { CROAK(("Corrupted storable %s (binary v%d.%d)", cxt->fio ? "file" : "string", 8 cxt->ver_major, cxt->ver_minor)); } return (SV *) 0; /* Just in case */ } /* * retrieve_idx_blessed * * Layout is SX_IX_BLESS with SX_IX_BLESS already read. * can be coded on either 1 or 5 bytes. */ static SV *retrieve_idx_blessed(pTHX_ stcxt_t *cxt, const char *cname) 19 { 19 I32 idx; 19 const char *classname; 19 SV **sva; 19 SV *sv; TRACEME(("retrieve_idx_blessed (#%d)", cxt->tagnum)); ASSERT(!cname, ("no bless-into class given here, got %s", cname)); 19 GETMARK(idx); /* Index coded on a single char? */ 19 if (idx & 0x80) ###### RLEN(idx); /* * Fetch classname in `aclass' */ 19 sva = av_fetch(cxt->aclass, idx, FALSE); 19 if (!sva) ###### CROAK(("Class name #%"IVdf" should have been seen already", (IV) idx)); 19 classname = SvPVX(*sva); /* We know it's a PV, by construction */ TRACEME(("class ID %d => %s", idx, classname)); /* * Retrieve object and bless it. */ 19 sv = retrieve(aTHX_ cxt, classname); /* First SV which is SEEN will be blessed */ 19 return sv; } /* * retrieve_blessed * * Layout is SX_BLESS with SX_BLESS already read. * can be coded on either 1 or 5 bytes. */ static SV *retrieve_blessed(pTHX_ stcxt_t *cxt, const char *cname) 70 { 70 I32 len; 70 SV *sv; 70 char buf[LG_BLESS + 1]; /* Avoid malloc() if possible */ 70 char *classname = buf; TRACEME(("retrieve_blessed (#%d)", cxt->tagnum)); ASSERT(!cname, ("no bless-into class given here, got %s", cname)); /* * Decode class name length and read that name. * * Short classnames have two advantages: their length is stored on one * single byte, and the string can be read on the stack. */ 70 GETMARK(len); /* Length coded on a single char? */ 70 if (len & 0x80) { 1 RLEN(len); TRACEME(("** allocating %d bytes for class name", len+1)); 1 New(10003, classname, len+1, char); } 70 READ(classname, len); 70 classname[len] = '\0'; /* Mark string end */ /* * It's a new classname, otherwise it would have been an SX_IX_BLESS. */ TRACEME(("new class name \"%s\" will bear ID = %d", classname, cxt->classnum)); 70 if (!av_store(cxt->aclass, cxt->classnum++, newSVpvn(classname, len))) ###### return (SV *) 0; /* * Retrieve object and bless it. */ 70 sv = retrieve(aTHX_ cxt, classname); /* First SV which is SEEN will be blessed */ 70 if (classname != buf) 1 Safefree(classname); 70 return sv; } /* * retrieve_hook * * Layout: SX_HOOK [ ] * with leading mark already read, as usual. * * When recursion was involved during serialization of the object, there * is an unknown amount of serialized objects after the SX_HOOK mark. Until * we reach a marker w