RPA Toolkit
rharray_t uses rcarray_t for storing the members
authorMartin Stoilov <martin@rpasearch.com>
Thu, 20 Jan 2011 05:15:57 +0000 (21:15 -0800)
committerMartin Stoilov <martin@rpasearch.com>
Thu, 20 Jan 2011 05:15:57 +0000 (21:15 -0800)
rlib/rcarray.c
rlib/rcarray.h
rlib/rharray.c
rlib/rharray.h
rvm/rvmcpu.c
rvm/rvmreg.c
tests/rharray-test.c

index c4b30df..bf6b6b8 100644 (file)
@@ -1,12 +1,6 @@
 #include "rcarray.h"
 #include "rmem.h"
 
-#define MIN_CARRAY_LEN 2048
-
-
-
-static void r_carray_checkexpand(rcarray_t *carray, ruint index);
-
 
 static rpointer r_carray_allocate_chunk(ruint elt_size)
 {
@@ -105,6 +99,7 @@ rint r_carray_replace(rcarray_t *carray, ruint index, rconstpointer data)
 rint r_carray_add(rcarray_t *carray, rconstpointer data)
 {
        ruint index = r_carray_length(carray);
+       r_carray_inclength(carray);
        return r_carray_replace(carray, index, data);
 }
 
@@ -130,7 +125,7 @@ void r_carray_declength(rcarray_t *carray)
 }
 
 
-static void r_carray_checkexpand(rcarray_t *carray, ruint size)
+void r_carray_checkexpand(rcarray_t *carray, ruint size)
 {
        ruint chunks;
 
@@ -141,7 +136,7 @@ static void r_carray_checkexpand(rcarray_t *carray, ruint size)
 }
 
 
-void *r_carray_slot_expand(rcarray_t *carray, ruint index)
+rpointer r_carray_slot_expand(rcarray_t *carray, ruint index)
 {
        r_carray_checkexpand(carray, index+1);
        return (void*) r_carray_slot(carray, index);
index 847cb87..7293599 100644 (file)
@@ -42,7 +42,8 @@ rint r_carray_add(rcarray_t *carray, rconstpointer data);
 void r_carray_setlength(rcarray_t *carray, ruint len);
 void r_carray_inclength(rcarray_t *carray);
 void r_carray_inclength(rcarray_t *carray);
-void *r_carray_slot_expand(rcarray_t *carray, ruint index);
+void r_carray_checkexpand(rcarray_t *carray, ruint size);
+rpointer r_carray_slot_expand(rcarray_t *carray, ruint index);
 
 /*
  * Virtual methods implementation
index df317f6..ab7dbfd 100644 (file)
@@ -67,7 +67,7 @@ robject_t *r_harray_init(robject_t *obj, ruint32 type, r_object_cleanupfun clean
        rharray_t *harray = (rharray_t*)obj;
        r_object_init(obj, type, cleanup, copy);
        harray->hash = r_hash_create(5, r_hash_rstrequal, r_hash_rstrhash);
-       harray->members = r_array_create(elt_size);
+       harray->members = r_carray_create(elt_size);
        harray->names = r_array_create(sizeof(rstr_t*));
        harray->names->oncleanup = r_array_oncleanup_rstr;
        harray->names->oncopy = r_array_oncopy_rstr;
@@ -94,9 +94,9 @@ robject_t *r_harray_copy(const robject_t *obj)
        harray = (rharray_t*)r_object_create(sizeof(*harray));
        r_object_init(&harray->obj, R_OBJECT_HARRAY, r_harray_cleanup, r_harray_copy);
        harray->names = (rarray_t*)r_array_copy((robject_t*)src->names);
-       harray->members = (rarray_t*)r_array_copy((robject_t*)src->members);
+       harray->members = (rcarray_t*)r_carray_copy((robject_t*)src->members);
        harray->hash = r_hash_create(5, r_hash_rstrequal, r_hash_rstrhash);
-       for (i = 0; i < r_array_length(src->members); i++) {
+       for (i = 0; i < r_carray_length(src->members); i++) {
                n = r_array_index(harray->names, i, rstr_t*);
                r_hash_insert_indexval(harray->hash, (rconstpointer)n, i);
        }
@@ -120,7 +120,7 @@ rint r_harray_add(rharray_t *harray, const rchar *name, ruint namesize, rconstpo
        rint index;
 
        membrName = r_rstrdup(name, namesize);
-       index = r_array_add(harray->members, pval);
+       index = r_carray_add(harray->members, pval);
        r_array_add(harray->names, &membrName);
        r_hash_insert_indexval(harray->hash, (rconstpointer)membrName, index);
        return index;
@@ -168,16 +168,16 @@ rint r_harray_set(rharray_t *harray, rlong index, rconstpointer pval)
 {
        if (index < 0)
                return -1;
-       r_array_replace(harray->members, index, pval);
+       r_carray_replace(harray->members, index, pval);
        return 0;
 }
 
 
 rpointer r_harray_get(rharray_t *harray, rulong index)
 {
-       if (index >= r_array_length(harray->members))
+       if (index >= r_carray_length(harray->members))
                return NULL;
-       return r_array_slot(harray->members, index);
+       return r_carray_slot(harray->members, index);
 }
 
 
index f04f236..a12c01e 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "rtypes.h"
 #include "rarray.h"
+#include "rcarray.h"
 #include "rhash.h"
 #include "rstring.h"
 #include "robject.h"
@@ -15,7 +16,7 @@ extern "C" {
 
 typedef struct rharray_s {
        robject_t obj;
-       rarray_t *members;
+       rcarray_t *members;
        rarray_t *names;
        rhash_t *hash;
 } rharray_t;
index 0261396..ee60713 100644 (file)
@@ -1395,6 +1395,21 @@ static void rvm_op_allocarr(rvmcpu_t *cpu, rvm_asmins_t *ins)
 }
 
 
+static void rvm_op_allocharr(rvmcpu_t *cpu, rvm_asmins_t *ins)
+{
+       rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
+       ruint size = RVM_CPUREG_GETU(cpu, ins->op2);
+       rharray_t *a = r_harray_create_rvmreg();
+       if (!a) {
+               RVM_ABORT(cpu, RVM_E_ILLEGAL);
+       }
+       if (size)
+               r_carray_checkexpand(a->members, size);
+       rvm_gc_add(cpu->gc, (robject_t*)a);
+       rvm_reg_setarray(arg1, (robject_t*)a);
+}
+
+
 static void rvm_op_addra(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
        rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
index 133da91..51555c0 100644 (file)
@@ -70,12 +70,8 @@ void rvm_reg_array_unref_gcdata(robject_t *obj)
        int i;
        rvmreg_t *r;
 
-       if (obj->type == R_OBJECT_ARRAY || obj->type == R_OBJECT_HARRAY) {
-               rarray_t *array;
-               if (obj->type == R_OBJECT_ARRAY)
-                       array = (rarray_t*)obj;
-               else
-                       array = ((rharray_t*)obj)->members;
+       if (obj->type == R_OBJECT_ARRAY) {
+               rarray_t *array = (rarray_t*)obj;
                if ((size = r_array_length(array)) > 0) {
                        /*
                         * set the size to 0, to prevent circular references to come back here
@@ -102,8 +98,10 @@ void rvm_reg_array_unref_gcdata(robject_t *obj)
                         */
                        r_array_setlength(array, size);
                }
-       } else if (obj->type == R_OBJECT_CARRAY) {
+       } else if (obj->type == R_OBJECT_CARRAY || obj->type == R_OBJECT_HARRAY) {
                rcarray_t *array = (rcarray_t*)obj;
+               if (obj->type == R_OBJECT_HARRAY)
+                       array = ((rharray_t*)obj)->members;
                if ((size = r_carray_length(array)) > 0) {
                        /*
                         * set the size to 0, to prevent circular references to come back here
@@ -161,8 +159,8 @@ rharray_t *r_harray_create_rvmreg()
 {
        rharray_t *harray = r_harray_create(sizeof(rvmreg_t));
        if (harray) {
-               harray->members->oncopy = rvm_reg_array_oncopy;
-               harray->members->oncleanup = rvm_reg_array_oncleanup;
+               harray->members->oncopy = rvm_reg_carray_oncopy;
+               harray->members->oncleanup = rvm_reg_carray_oncleanup;
        }
        return harray;
 }
index fc76d13..4e00bbe 100644 (file)
@@ -45,6 +45,7 @@ int main(int argc, char *argv[])
        rhash_node_t *node;
        rvmcpu_t *cpu;
 
+       r_memset(&rh_copy, 0, sizeof(rh_copy));
        cpu = rvm_cpu_create_default();
        rvm_cpu_addswitable(cpu, switable);
        cg = rvm_codegen_create();