--- Compress-LZF/LZF.xs 2001/09/27 20:05:38 1.4 +++ Compress-LZF/LZF.xs 2002/03/03 04:45:18 1.9 @@ -24,6 +24,8 @@ #define MAGIC_undef 2 /* the special value undef */ #define MAGIC_CR 3 /* storable (reference, freeze), compressed */ #define MAGIC_R 4 /* storable (reference, freeze) */ +#define MAGIC_CR_deref 5 /* storable (NO reference, freeze), compressed */ +#define MAGIC_R_deref 6 /* storable NO (reference, freeze) */ #define MAGIC_HI 7 /* room for one higher storable major */ #define IN_RANGE(v,l,h) ((unsigned int)((unsigned)(v) - (unsigned)(l)) <= (unsigned)(h) - (unsigned)(l)) @@ -232,9 +234,14 @@ && SvTYPE(sv) != SVt_NV && SvTYPE(sv) != SVt_PV) /* mstore */ { + int deref = !SvROK (sv); + if (!storable_mstore) need_storable (); + if (deref) + sv = newRV_noinc (sv); + PUSHMARK (SP); XPUSHs (sv); PUTBACK; @@ -250,9 +257,14 @@ croak ("Storable format changed, need newer version of Compress::LZF"); if (ix) /* compress */ - XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_CR, 0))); + XPUSHs (sv_2mortal (compress_sv (sv, deref ? MAGIC_CR_deref : MAGIC_CR, 0))); else - XPUSHs (sv); + { + if (deref) + SvPVX (sv)[0] = MAGIC_R_deref; + + XPUSHs (sv); + } } else if (sv && IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI)) XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 1))); /* need to prefix only */ @@ -267,7 +279,10 @@ PROTOTYPE: $ PPCODE: - if (IN_RANGE (SvPV_nolen (sv)[0], MAGIC_LO, MAGIC_HI)) + int deref = 0; + + SvGETMAGIC (sv); + if (SvPOK (sv) && IN_RANGE (SvPV_nolen (sv)[0], MAGIC_LO, MAGIC_HI)) { switch (SvPVX (sv)[0]) { @@ -283,8 +298,16 @@ XPUSHs (sv_2mortal (decompress_sv (sv, 1))); break; + case MAGIC_CR_deref: + deref = 1; case MAGIC_CR: sv = sv_2mortal (decompress_sv (sv, 1)); /* mortal could be optimized */ + case MAGIC_R_deref: + if (SvPVX (sv)[0] == MAGIC_R_deref) + { + deref = 1; + SvPVX (sv)[0] = MAGIC_R; + } case MAGIC_R: if (!storable_mstore) need_storable (); @@ -298,7 +321,18 @@ SPAGAIN; - XPUSHs (POPs); /* this is a nop, hope the compiler also knows this */ + if (deref) + { + SV *ref = SvREFCNT_inc (SvRV (TOPs)); + + SvREFCNT_dec (TOPs); /* destroy superfluous ref */ + SETs (ref); + + if (SvPVX (sv)[0] == MAGIC_R) + SvPVX (sv)[0] = MAGIC_R; + } + + XPUSHs (POPs); /* this is a nop, hopefully */ break;