const and static for the const static private table.
[p5sagit/p5-mst-13.2.git] / sv.c
diff --git a/sv.c b/sv.c
index 148dcec..e7355b7 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -1231,10 +1231,10 @@ struct body_details {
     bool zero_nv;      /* zero the NV when upgrading from this */
 };
 
-struct body_details bodies_by_type[] = {
+static const struct body_details bodies_by_type[] = {
     {0, 0, 0, FALSE, TRUE},
     /* IVs are in the head, so the allocation size is 0  */
-    {0, sizeof(IV), STRUCT_OFFSET(XPVIV, xiv_iv), FALSE, TRUE},
+    {0, sizeof(IV), -STRUCT_OFFSET(XPVIV, xiv_iv), FALSE, TRUE},
     /* 8 bytes on most ILP32 with IEEE doubles */
     {sizeof(NV), sizeof(NV), 0, FALSE, FALSE},
     /* RVs are in the head now */
@@ -1392,11 +1392,8 @@ You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
 */
 
 void
-Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
+Perl_sv_upgrade(pTHX_ register SV *sv, U32 new_type)
 {
-    void**     old_body_arena;
-    size_t     old_body_offset;
-    size_t     old_body_length;        /* Well, the length to copy.  */
     void*      old_body;
     void*      new_body;
     size_t     new_body_length;
@@ -1407,22 +1404,19 @@ Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
     const struct body_details *const old_type_details
        = bodies_by_type + old_type;
 
-    if (mt != SVt_PV && SvIsCOW(sv)) {
+    if (new_type != SVt_PV && SvIsCOW(sv)) {
        sv_force_normal_flags(sv, 0);
     }
 
-    if (old_type == mt)
+    if (old_type == new_type)
        return;
 
-    if (old_type > mt)
+    if (old_type > new_type)
        Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
-               (int)old_type, (int)mt);
+               (int)old_type, (int)new_type);
 
 
     old_body = SvANY(sv);
-    old_body_arena = 0;
-    old_body_offset = 0;
-    old_body_length = 0;
     new_body_offset = 0;
     new_body_length = ~0;
 
@@ -1466,38 +1460,25 @@ Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
     case SVt_NULL:
        break;
     case SVt_IV:
-       if (mt == SVt_NV)
-           mt = SVt_PVNV;
-       else if (mt < SVt_PVIV)
-           mt = SVt_PVIV;
-       old_body_offset = old_type_details->offset;
-       old_body_length = old_type_details->copy;
+       if (new_type == SVt_NV)
+           new_type = SVt_PVNV;
+       else if (new_type < SVt_PVIV)
+           new_type = SVt_PVIV;
        break;
     case SVt_NV:
-       old_body_arena = &PL_body_roots[old_type];
-       old_body_length = old_type_details->copy;
-       if (mt < SVt_PVNV)
-           mt = SVt_PVNV;
+       if (new_type < SVt_PVNV)
+           new_type = SVt_PVNV;
        break;
     case SVt_RV:
        break;
     case SVt_PV:
-       old_body_arena = &PL_body_roots[SVt_PV];
-       old_body_offset = - bodies_by_type[SVt_PV].offset;
-       old_body_length = bodies_by_type[SVt_PV].copy;
-       if (mt <= SVt_IV)
-           mt = SVt_PVIV;
-       else if (mt == SVt_NV)
-           mt = SVt_PVNV;
+       assert(new_type > SVt_PV);
+       assert(SVt_IV < SVt_PV);
+       assert(SVt_NV < SVt_PV);
        break;
     case SVt_PVIV:
-       old_body_arena = &PL_body_roots[SVt_PVIV];
-       old_body_offset = - bodies_by_type[SVt_PVIV].offset;
-       old_body_length = bodies_by_type[SVt_PVIV].copy;
        break;
     case SVt_PVNV:
-       old_body_arena = &PL_body_roots[SVt_PVNV];
-       old_body_length = bodies_by_type[SVt_PVNV].copy;
        break;
     case SVt_PVMG:
        /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
@@ -1508,8 +1489,6 @@ Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
           Given that it only has meaning inside the pad, it shouldn't be set
           on anything that can get upgraded.  */
        assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
-       old_body_arena = &PL_body_roots[SVt_PVMG];
-       old_body_length = bodies_by_type[SVt_PVMG].copy;
        break;
     default:
        if (old_type_details->cant_upgrade)
@@ -1517,9 +1496,9 @@ Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
     }
 
     SvFLAGS(sv) &= ~SVTYPEMASK;
-    SvFLAGS(sv) |= mt;
+    SvFLAGS(sv) |= new_type;
 
-    switch (mt) {
+    switch (new_type) {
     case SVt_NULL:
        Perl_croak(aTHX_ "Can't upgrade to undef");
     case SVt_IV:
@@ -1589,9 +1568,9 @@ Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
     case SVt_PVLV:
     case SVt_PVMG:
     case SVt_PVNV:
-       new_body_length = bodies_by_type[mt].size;
-       new_body_arena = &PL_body_roots[mt];
-       new_body_arenaroot = &PL_body_arenaroots[mt];
+       new_body_length = bodies_by_type[new_type].size;
+       new_body_arena = &PL_body_roots[new_type];
+       new_body_arenaroot = &PL_body_arenaroots[new_type];
        goto new_body;
 
     case SVt_PVIV:
@@ -1617,7 +1596,7 @@ Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
        assert(new_body_length);
 #ifndef PURIFY
        /* This points to the start of the allocated area.  */
-       new_body_inline(new_body, new_body_arena, new_body_length, mt);
+       new_body_inline(new_body, new_body_arena, new_body_length, new_type);
 #else
        /* We always allocated the full length item with PURIFY */
        new_body_length += new_body_offset;
@@ -1630,10 +1609,10 @@ Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
        new_body = ((char *)new_body) - new_body_offset;
        SvANY(sv) = new_body;
 
-       if (old_body_length) {
-           Copy((char *)old_body + old_body_offset,
-                (char *)new_body + old_body_offset,
-                old_body_length, char);
+       if (old_type_details->copy) {
+           Copy((char *)old_body - old_type_details->offset,
+                (char *)new_body - old_type_details->offset,
+                old_type_details->copy, char);
        }
 
 #ifndef NV_ZERO_IS_ALLBITS_ZERO
@@ -1643,22 +1622,22 @@ Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
            SvNV_set(sv, 0);
 #endif
 
-       if (mt == SVt_PVIO)
+       if (new_type == SVt_PVIO)
            IoPAGE_LEN(sv)      = 60;
        if (old_type < SVt_RV)
            SvPV_set(sv, 0);
        break;
     default:
-       Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu", mt);
+       Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu", new_type);
     }
 
-
-    if (old_body_arena) {
+    if (old_type_details->size) {
+       /* If the old body had an allocated size, then we need to free it.  */
 #ifdef PURIFY
        my_safefree(old_body);
 #else
-       del_body((void*)((char*)old_body + old_body_offset),
-                old_body_arena);
+       del_body((void*)((char*)old_body - old_type_details->offset),
+                &PL_body_roots[old_type]);
 #endif
     }
 }