develooper Front page | perl.cvs.parrot | Postings from January 2009

[svn:parrot] r35991 - trunk/src/pmc

From:
cotto
Date:
January 25, 2009 03:36
Subject:
[svn:parrot] r35991 - trunk/src/pmc
Message ID:
20090125113643.C4799CB9AE@x12.develooper.com
Author: cotto
Date: Sun Jan 25 03:36:43 2009
New Revision: 35991

Modified:
   trunk/src/pmc/fixedfloatarray.pmc
   trunk/src/pmc/resizablefloatarray.pmc

Log:
[pmc] get rid of UnionVal in *FloatArray 


Modified: trunk/src/pmc/fixedfloatarray.pmc
==============================================================================
--- trunk/src/pmc/fixedfloatarray.pmc	(original)
+++ trunk/src/pmc/fixedfloatarray.pmc	Sun Jan 25 03:36:43 2009
@@ -22,6 +22,8 @@
 #include "parrot/parrot.h"
 
 pmclass FixedFloatArray need_ext provides array {
+    ATTR INTVAL    size;
+    ATTR FLOATVAL *float_array;
 
 /*
 
@@ -40,8 +42,9 @@
 */
 
     VTABLE void init() {
-        PMC_int_val(SELF) = 0;
-        PMC_data(SELF)    = NULL;
+        Parrot_FixedFloatArray_attributes* attrs =
+            mem_allocate_zeroed_typed(Parrot_FixedFloatArray_attributes);
+        PMC_data(SELF) = attrs;
     }
 
 /*
@@ -55,11 +58,12 @@
 */
 
     VTABLE void destroy() {
-        if (PMC_data(SELF))
-            mem_sys_free(PMC_data(SELF));
+        FLOATVAL *float_array;
+        GET_ATTR_float_array(INTERP, SELF, float_array);
+        if (float_array)
+            mem_sys_free(float_array);
 
-        PMC_data(SELF)    = NULL;
-        PMC_int_val(SELF) = 0;
+        mem_sys_free(PMC_data(SELF));
     }
 
 /*
@@ -73,19 +77,22 @@
 */
 
     VTABLE PMC *clone() {
-        INTVAL size;
+        INTVAL    size;
+        FLOATVAL *self_float_array, *dest_float_array;
         size_t mem_size;
         PMC   * const dest = pmc_new(INTERP, SELF->vtable->base_type);
+        GET_ATTR_float_array(INTERP, SELF, self_float_array);
 
-        if (!PMC_data(SELF))
+        if (!self_float_array)
             return dest;
 
-        size              = PMC_int_val(SELF);
-        PMC_int_val(dest) = size;
+        GET_ATTR_size(INTERP, SELF, size);
+        SET_ATTR_size(INTERP, dest, size);
         mem_size          = size * sizeof (FLOATVAL);
 
-        PMC_data(dest)    = mem_sys_allocate(mem_size);
-        mem_sys_memcopy(PMC_data(dest), PMC_data(SELF), mem_size);
+        dest_float_array  = (FLOATVAL*)mem_sys_allocate(mem_size);
+        mem_sys_memcopy(dest_float_array, self_float_array, mem_size);
+        SET_ATTR_float_array(INTERP, dest, dest_float_array);
         PObj_active_destroy_SET(dest);
 
         return dest;
@@ -115,7 +122,9 @@
 */
 
     VTABLE INTVAL elements() {
-        return PMC_int_val(SELF);
+        INTVAL size;
+        GET_ATTR_size(INTERP, SELF, size);
+        return size;
     }
 
 /*
@@ -176,14 +185,16 @@
 */
 
     VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
-        FLOATVAL *data;
+        FLOATVAL *float_array;
+        INTVAL    size;
 
-        if (key < 0 || key >= PMC_int_val(SELF))
+        GET_ATTR_size(INTERP, SELF, size);
+        if (key < 0 || key >= size)
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                 "FixedFloatArray: index out of bounds!");
 
-        data = (FLOATVAL *)PMC_data(SELF);
-        return data[key];
+        GET_ATTR_float_array(INTERP, SELF, float_array);
+        return float_array[key];
     }
 
 /*
@@ -275,13 +286,16 @@
 
 */
 
-    VTABLE void set_integer_native(INTVAL size) {
-        if (PMC_int_val(SELF) || size < 1)
+    VTABLE void set_integer_native(INTVAL new_size) {
+        INTVAL old_size;
+        GET_ATTR_size(INTERP, SELF, old_size);
+        if (old_size || new_size < 1)
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                     "FixedFloatArray: Can't resize!");
 
-        PMC_int_val(SELF) = size;
-        PMC_data(SELF)    = mem_sys_allocate(size * sizeof (FLOATVAL));
+        SET_ATTR_size(INTERP, SELF, new_size);
+        SET_ATTR_float_array(INTERP, SELF, 
+                mem_allocate_n_typed(new_size, FLOATVAL));
         PObj_active_destroy_SET(SELF);
     }
 
@@ -326,13 +340,16 @@
 */
 
     VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
-        FLOATVAL *data;
-        if (key < 0 || key >= PMC_int_val(SELF))
+        FLOATVAL *float_array;
+        INTVAL size;
+
+        GET_ATTR_size(INTERP, SELF, size);
+        if (key < 0 || key >= size)
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                 "FixedFloatArray: index out of bounds!");
 
-        data      = (FLOATVAL *)PMC_data(SELF);
-        data[key] = value;
+        GET_ATTR_float_array(INTERP, SELF, float_array);
+        float_array[key] = value;
     }
 
 /*

Modified: trunk/src/pmc/resizablefloatarray.pmc
==============================================================================
--- trunk/src/pmc/resizablefloatarray.pmc	(original)
+++ trunk/src/pmc/resizablefloatarray.pmc	Sun Jan 25 03:36:43 2009
@@ -23,8 +23,25 @@
 #include "parrot/parrot.h"
 
 pmclass ResizableFloatArray extends FixedFloatArray need_ext provides array {
+    ATTR INTVAL resize_threshold; /* max size before array needs resizing */
 
 
+
+/*
+
+=item C<void init()>
+
+Initializes this array.
+
+=cut
+
+*/
+    VTABLE void init() {
+        Parrot_ResizableFloatArray_attributes* attrs =
+            mem_allocate_zeroed_typed(Parrot_ResizableFloatArray_attributes);
+        PMC_data(SELF) = attrs;
+    }
+
 /*
 
 =item C<FLOATVAL get_number_keyed_int(INTVAL key)>
@@ -36,17 +53,19 @@
 */
 
     VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
-        FLOATVAL *data;
+        FLOATVAL *float_array;
+        INTVAL    size;
 
         if (key < 0)
             Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
                     "ResizableFloatArray: index out of bounds!");
 
-        if (key >= PMC_int_val(SELF))
+        GET_ATTR_size(INTERP, SELF, size);
+        if (key >= size)
             return 0.0;
 
-        data = (FLOATVAL *)PMC_data(SELF);
-        return data[key];
+        GET_ATTR_float_array(INTERP, SELF, float_array);
+        return float_array[key];
     }
 
 /*
@@ -61,17 +80,19 @@
 */
 
     VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
-        FLOATVAL *data;
+        FLOATVAL *float_array;
+        INTVAL    size;
 
         if (key < 0)
             Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
                     "ResizableFloatArray: index out of bounds!");
 
-        if (key >= PMC_int_val(SELF))
+        GET_ATTR_size(INTERP, SELF, size);
+        if (key >= size)
             SELF.set_integer_native(key+1);
 
-        data      = (FLOATVAL *)PMC_data(SELF);
-        data[key] = value;
+        GET_ATTR_float_array(INTERP, SELF, float_array);
+        float_array[key] = value;
     }
 
 /*
@@ -92,29 +113,34 @@
 */
 
     VTABLE void set_integer_native(INTVAL size) {
+        FLOATVAL *float_array;
+        INTVAL    resize_threshold;
+
         if (size < 0)
             Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
                     "ResizableFloatArray: Can't resize to negative value!");
 
-        if (!PMC_data(SELF)) {
+        GET_ATTR_float_array(INTERP, SELF, float_array);
+        GET_ATTR_resize_threshold(INTERP, SELF, resize_threshold);
+        if (!float_array) {
             /* empty - used fixed routine */
             if (size < 8) {
                 SUPER(8);
-                PMC_int_val(SELF)  = size;
-                PMC_int_val2(SELF) = 8;
+                SET_ATTR_size(INTERP, SELF, size);
+                SET_ATTR_resize_threshold(INTERP, SELF, 8);
             }
             else {
                 SUPER(size);
-                PMC_int_val2(SELF) = size;
+                SET_ATTR_resize_threshold(INTERP, SELF, size);
             }
         }
-        else if (size <= PMC_int_val2(SELF)) {
+        else if (size <= resize_threshold){
             /* we could shrink here if necessary */
-            PMC_int_val(SELF) = size;
+            SET_ATTR_size(INTERP, SELF, size);
             return;
         }
         else {
-            INTVAL cur = PMC_int_val2(SELF);
+            INTVAL cur = resize_threshold;
             if (cur < 8192)
                 cur = size < 2 * cur ? 2 * cur : size;
             else {
@@ -122,10 +148,11 @@
                 cur          += needed + 4096;
                 cur          &= ~0xfff;
             }
-            PMC_data(SELF) = mem_sys_realloc(PMC_data(SELF),
-                    cur * sizeof (FLOATVAL));
-            PMC_int_val2(SELF) = cur;
-            PMC_int_val(SELF)  = size;
+            SET_ATTR_float_array(INTERP, SELF, 
+                    (FLOATVAL*)mem_sys_realloc(float_array, 
+                        cur * sizeof (FLOATVAL)));
+            SET_ATTR_size(INTERP, SELF, size);
+            SET_ATTR_resize_threshold(INTERP, SELF, cur);
         }
     }
 
@@ -140,10 +167,12 @@
 */
 
     VTABLE PMC *clone() {
-        PMC *copy          = SUPER();
+        INTVAL size;
+        PMC   *copy          = SUPER();
 
         /* copy trimmed extra space */
-        PMC_int_val2(copy) = PMC_int_val(SELF);
+        GET_ATTR_size(INTERP, SELF, size);
+        SET_ATTR_resize_threshold(INTERP, SELF, size);
 
         return copy;
     }
@@ -175,7 +204,8 @@
 
     VTABLE FLOATVAL pop_float() {
         FLOATVAL value;
-        INTVAL size = PMC_int_val(SELF);
+        INTVAL size;
+        GET_ATTR_size(INTERP, SELF, size);
 
         if (size == 0)
             Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
@@ -196,18 +226,20 @@
 */
 
     VTABLE FLOATVAL shift_float() {
-        FLOATVAL value, *data;
-        INTVAL size = PMC_int_val(SELF);
+        FLOATVAL value, *float_array;
+        INTVAL size;
+
+        GET_ATTR_size(INTERP, SELF, size);
 
         if (size == 0)
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                     "ResizableFloatArray: Can't shift from an empty array!");
 
-        data              = (FLOATVAL *)PMC_data(SELF);
-        value             = data[0];
-        PMC_int_val(SELF) = --size;
+        GET_ATTR_float_array(INTERP, SELF, float_array);
+        value             = float_array[0];
+        SET_ATTR_size(INTERP, SELF, --size);
 
-        mem_sys_memmove(data, data + 1, size * sizeof (FLOATVAL));
+        mem_sys_memmove(float_array, float_array + 1, size * sizeof (FLOATVAL));
         return value;
     }
 
@@ -222,13 +254,14 @@
 */
 
     VTABLE void unshift_float(FLOATVAL value) {
-        INTVAL    size = PMC_int_val(SELF);
-        FLOATVAL *data;
+        INTVAL    size;
+        FLOATVAL *float_array;
 
+        GET_ATTR_size(INTERP, SELF, size);
         SELF.set_integer_native(size + 1);
-        data    = (FLOATVAL *)PMC_data(SELF);
-        mem_sys_memmove(data + 1, data, size * sizeof (FLOATVAL));
-        data[0] = value;
+        GET_ATTR_float_array(INTERP, SELF, float_array);
+        mem_sys_memmove(float_array + 1, float_array, size * sizeof (FLOATVAL));
+        float_array[0] = value;
     }
 
 }



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About