[svn:parrot] r38026 - in branches/packfile_revamp: src/pmc t/native_pbc t/pmc

bacek at svn.parrot.org bacek at svn.parrot.org
Fri Apr 10 15:37:59 UTC 2009


Author: bacek
Date: Fri Apr 10 15:37:59 2009
New Revision: 38026
URL: https://trac.parrot.org/parrot/changeset/38026

Log:
Initial implementation of writable Packfile PMCs.

Create PackFile on fly, pack it, destroy it.
Every Packfile*.pmc responsible for creating "native" PackFile segment
in VTABLE get_pointer. Those pointers gathered by PackfileDirectory and
added into PackFile_Directory for serialization and memory management.

Modified:
   branches/packfile_revamp/src/pmc/packfile.pmc
   branches/packfile_revamp/src/pmc/packfileconstanttable.pmc
   branches/packfile_revamp/src/pmc/packfiledirectory.pmc
   branches/packfile_revamp/src/pmc/packfilefixuptable.pmc
   branches/packfile_revamp/src/pmc/packfilerawsegment.pmc
   branches/packfile_revamp/t/native_pbc/integer_1.pbc
   branches/packfile_revamp/t/native_pbc/number_1.pbc
   branches/packfile_revamp/t/native_pbc/string_1.pbc
   branches/packfile_revamp/t/pmc/packfile.t

Modified: branches/packfile_revamp/src/pmc/packfile.pmc
==============================================================================
--- branches/packfile_revamp/src/pmc/packfile.pmc	Fri Apr 10 15:37:25 2009	(r38025)
+++ branches/packfile_revamp/src/pmc/packfile.pmc	Fri Apr 10 15:37:59 2009	(r38026)
@@ -99,18 +99,30 @@
 
 =cut
 
+Implementation note: all hard stuff done by PackfileDirectory.
+
 */
     VTABLE STRING *get_string() {
-    /*
         Parrot_Packfile_attributes * attrs = PARROT_PACKFILE(SELF);
-        opcode_t length = PackFile_pack_size(interp, attrs->pf) * sizeof (opcode_t);
-        opcode_t *ptr = (opcode_t*)mem_sys_allocate(length);
-        PackFile_pack(interp, attrs->pf, ptr);
+        PackFile                   * pf =
+                (PackFile*)VTABLE_get_pointer(interp, attrs->directory);
+
+        opcode_t    length;
+        opcode_t    *ptr;
+        STRING      *str;
+
+        /* Calculate required memory */
+        length  = PackFile_pack_size(interp, pf) * sizeof(opcode_t);
+        ptr     = (opcode_t*)mem_sys_allocate(length);
+
+        /* And pack it! */
+        PackFile_pack(interp, pf, ptr);
+
         str = Parrot_str_new_init(interp, (const char*)ptr, length,
                 PARROT_FIXED_8_ENCODING, PARROT_BINARY_CHARSET, 0);
         mem_sys_free(ptr);
-        */
-        STRING *str = Parrot_str_new_noinit(INTERP, enum_stringrep_one, 0);
+        
+        PackFile_destroy(interp, pf);
         return str;
     }
 

Modified: branches/packfile_revamp/src/pmc/packfileconstanttable.pmc
==============================================================================
--- branches/packfile_revamp/src/pmc/packfileconstanttable.pmc	Fri Apr 10 15:37:25 2009	(r38025)
+++ branches/packfile_revamp/src/pmc/packfileconstanttable.pmc	Fri Apr 10 15:37:59 2009	(r38026)
@@ -136,6 +136,25 @@
 
 /*
 
+=item C<void *get_pointer()>
+=cut
+
+*/
+    VTABLE void *get_pointer() {
+        Parrot_PackfileConstantTable_attributes * attrs =
+                PARROT_PACKFILECONSTANTTABLE(SELF);
+        PackFile_ConstTable * pftable = 
+                mem_allocate_zeroed_typed(PackFile_ConstTable);
+
+        pftable->base.type = PF_CONST_SEG;
+        
+        /* TODO Copy all constanst with respect of type */
+
+        return pftable;
+    }
+
+/*
+
 =item C<INTVAL elements()>
 
 Get the number of elements in the array.

Modified: branches/packfile_revamp/src/pmc/packfiledirectory.pmc
==============================================================================
--- branches/packfile_revamp/src/pmc/packfiledirectory.pmc	Fri Apr 10 15:37:25 2009	(r38025)
+++ branches/packfile_revamp/src/pmc/packfiledirectory.pmc	Fri Apr 10 15:37:59 2009	(r38026)
@@ -143,6 +143,41 @@
 
 /*
 
+=item C<void *get_pointer()>
+
+Creates PackFile for given directory.
+
+=cut
+
+*/
+
+    VTABLE void *get_pointer() {
+        Parrot_PackfileDirectory_attributes * attrs =
+                PARROT_PACKFILEDIRECTORY(SELF);
+        PackFile           * pf = PackFile_new(interp, 0); /* dummy PackFile... */
+        PackFile_Directory * pfdir = &pf->directory;
+        PackFile_Segment   * pfseg;
+        PMC                * seg;
+        PMC                * iter;
+        STRING             * name;
+        STRING             * res;
+
+        /* Create Segments. Add to Directory with transfering ownership */
+        iter = VTABLE_get_iter(interp, attrs->hash);
+        while (VTABLE_get_bool(interp, iter)) {
+            name  = VTABLE_shift_string(interp, iter);
+            seg   = VTABLE_get_pmc_keyed_str(interp, attrs->hash, name);
+            pfseg = (PackFile_Segment*)VTABLE_get_pointer(interp, seg);
+            pfseg->pf   = pf;
+            pfseg->name = strdup(Parrot_string_cstring(interp, name));
+            PackFile_add_segment(interp, pfdir, pfseg);
+        }
+        
+        return pf;
+    }
+
+/*
+
 =item C<INTVAL elements()>
 
 Get the number of elements in the array.

Modified: branches/packfile_revamp/src/pmc/packfilefixuptable.pmc
==============================================================================
--- branches/packfile_revamp/src/pmc/packfilefixuptable.pmc	Fri Apr 10 15:37:25 2009	(r38025)
+++ branches/packfile_revamp/src/pmc/packfilefixuptable.pmc	Fri Apr 10 15:37:59 2009	(r38026)
@@ -27,6 +27,98 @@
 #include "parrot/parrot.h"
 
 pmclass PackfileFixupTable extends PackfileSegment {
+    /* RPA of entries */
+    ATTR PMC *entries;
+
+/*
+
+=item C<init>
+
+Create empty PackfileFixupTable.
+
+=cut
+
+*/
+
+    VTABLE void init() {
+        Parrot_PackfileFixupTable_attributes * attrs =
+                mem_allocate_zeroed_typed(Parrot_PackfileFixupTable_attributes);
+
+        attrs->entries = pmc_new(interp, enum_class_ResizablePMCArray);
+
+        PObj_custom_mark_destroy_SETALL(SELF);
+        PMC_data(SELF) = attrs;
+    }
+
+/*
+
+=item C<void mark()>
+
+Marks the object as live. 
+
+=cut
+
+*/
+
+    VTABLE void mark() {
+        Parrot_PackfileFixupTable_attributes * attrs =
+                PARROT_PACKFILEFIXUPTABLE(SELF);
+
+        if (attrs->entries)
+            pobject_lives(interp, (PObj *)attrs->entries);
+    }
+
+/*
+
+=item C<void destroy()>
+
+Destroys the PMC and frees all allocated memory.
+
+=cut
+
+*/
+
+    VTABLE void destroy() {
+        Parrot_PackfileFixupTable_attributes * attrs =
+                PARROT_PACKFILEFIXUPTABLE(SELF);
+
+        if (attrs) {
+            mem_sys_free(attrs);
+            PMC_data(SELF) = NULL;
+        }
+    }
+
+
+
+/*
+
+=item C<set_pointer>
+
+=cut
+
+*/
+
+    VTABLE void set_pointer(void * pointer) {
+    }
+
+/*
+
+=item C<void *get_pointer()>
+=cut
+
+*/
+    VTABLE void *get_pointer() {
+        Parrot_PackfileFixupTable_attributes * attrs =
+                PARROT_PACKFILEFIXUPTABLE(SELF);
+        PackFile_FixupTable * pftable = 
+                mem_allocate_zeroed_typed(PackFile_FixupTable);
+
+        pftable->base.type = PF_FIXUP_SEG;
+
+        /* TODO: Copy all entries */
+
+        return pftable;
+    }
 
 
 /*

Modified: branches/packfile_revamp/src/pmc/packfilerawsegment.pmc
==============================================================================
--- branches/packfile_revamp/src/pmc/packfilerawsegment.pmc	Fri Apr 10 15:37:25 2009	(r38025)
+++ branches/packfile_revamp/src/pmc/packfilerawsegment.pmc	Fri Apr 10 15:37:59 2009	(r38026)
@@ -28,7 +28,7 @@
 
 pmclass PackfileRawSegment extends PackfileSegment {
     /* ResizableIntegerArray of opcodes */
-    ATTR PMC *opcodes;
+    ATTR PMC    *opcodes;
 
 /*
 
@@ -106,16 +106,45 @@
         PMC * opcodes = PARROT_PACKFILERAWSEGMENT(SELF)->opcodes;
         size_t i;
 
-        /* copy data to own array */
-        VTABLE_set_integer_native(interp, opcodes, pfseg->op_count);
-        /* Not very efficient... */
-        for (i = 0; i < pfseg->op_count; ++i) {
-            VTABLE_set_integer_keyed_int(interp, opcodes, i, pfseg->data[i]);
+        if (pfseg->size) {
+            /* copy data to own array */
+            VTABLE_set_integer_native(interp, opcodes, pfseg->size);
+            /* Not very efficient... */
+            for (i = 0; i < pfseg->op_count; ++i) {
+                VTABLE_set_integer_keyed_int(interp, opcodes, i, pfseg->data[i]);
+            }
         }
     }
 
 /*
 
+=item C<void *get_pointer()>
+
+=cut
+
+*/
+
+    VTABLE void *get_pointer() {
+        PackFile_Segment * pfseg =
+                (PackFile_Segment*)mem_allocate_zeroed_typed(PackFile_ByteCode);
+        Parrot_PackfileRawSegment_attributes * attrs =
+                PARROT_PACKFILERAWSEGMENT(SELF);
+        PMC * opcodes = attrs->opcodes;
+        size_t i;
+
+        pfseg->type     = PF_BYTEC_SEG;
+        pfseg->size     = VTABLE_get_integer(interp, opcodes);
+        pfseg->data     = mem_allocate_n_typed(pfseg->size, opcode_t);
+
+        /* Not very efficient... */
+        for (i = 0; i < pfseg->size; ++i) {
+            pfseg->data[i] = VTABLE_get_integer_keyed_int(interp, opcodes, i);
+        }
+
+        return pfseg;
+    }
+/*
+
 =item C<INTVAL elements()>
 
 Get the number of elements in the array.

Modified: branches/packfile_revamp/t/native_pbc/integer_1.pbc
==============================================================================
Binary file (source and/or target). No diff available.

Modified: branches/packfile_revamp/t/native_pbc/number_1.pbc
==============================================================================
Binary file (source and/or target). No diff available.

Modified: branches/packfile_revamp/t/native_pbc/string_1.pbc
==============================================================================
Binary file (source and/or target). No diff available.

Modified: branches/packfile_revamp/t/pmc/packfile.t
==============================================================================
--- branches/packfile_revamp/t/pmc/packfile.t	Fri Apr 10 15:37:25 2009	(r38025)
+++ branches/packfile_revamp/t/pmc/packfile.t	Fri Apr 10 15:37:59 2009	(r38026)
@@ -22,7 +22,7 @@
 .sub main :main
 .include 'test_more.pir'
 
-    plan(15)
+    plan(17)
     'test_new'()
     'test_get_string'()
     'test_set_string'()
@@ -30,6 +30,7 @@
     'test_set_integer'()
     'test_get_directory'()
     'test_load'()
+    'test_pack_fresh_packfile'()
     'test_pack'()
     # This test will crash on many platforms. See TT#545.
     #'test_synonyms'()
@@ -167,6 +168,34 @@
     ok($I0, "bytecode_major set")
 .end
 
+
+# Create very simple Packfile and pack it
+.sub 'test_pack_fresh_packfile'
+    .local pmc pf, pfdir
+    pf = new 'Packfile'
+    pfdir = pf.'get_directory'()
+    #$P0 = new 'PackfileConstantTable'
+    #$P0[0] = 42.0
+    $P0 = new 'PackfileFixupTable'
+    pfdir["FIXUP_t/pmc/packfile.t"] = $P0
+
+    $P1 = new 'PackfileRawSegment'
+    pfdir["BYTECODE_t/pmc/packfile.t"] = $P1
+
+    # Pack it
+    $S0 = pf
+
+    ok(1, "PackFile packed")
+
+    pf = new 'Packfile'
+    pf = $S0
+    ok(1, "PackFile unpacked after pack")
+
+    #$P1 = open "/tmp/1.pbc", "w"
+    #$P1.'puts'($S0)
+    #close $P1
+.end
+
 # Packfile.pack.
 # Check that unpack-pack produce correct result.
 .sub 'test_pack'


More information about the parrot-commits mailing list