--- Compress-LZF/LZF.xs 2002/02/27 20:51:21 1.8 +++ 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)) @@ -230,12 +232,16 @@ XPUSHs (sv_2mortal (newSVpvn ("\02", 1))); /* 02 == MAGIC_undef */ else if (SvTYPE(sv) != SVt_IV && SvTYPE(sv) != SVt_NV - && SvTYPE(sv) != SVt_PV - && SvTYPE(sv) != SVt_PVMG) /* mstore */ + && 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; @@ -251,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 */ @@ -268,6 +279,8 @@ PROTOTYPE: $ PPCODE: + int deref = 0; + SvGETMAGIC (sv); if (SvPOK (sv) && IN_RANGE (SvPV_nolen (sv)[0], MAGIC_LO, MAGIC_HI)) { @@ -285,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 (); @@ -300,7 +321,18 @@ SPAGAIN; - /*XPUSHs (POPs); this is a nop, hopefully */ + 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;