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

cotto at svn.parrot.org cotto at svn.parrot.org
Fri Jan 30 02:04:50 UTC 2009


Author: cotto
Date: Fri Jan 30 02:04:48 2009
New Revision: 36165
URL: https://trac.parrot.org/parrot/log/branches?rev=36165

Log:
[pmc] update *StringArray to use ATTRs

Modified:
   trunk/src/pmc/fixedstringarray.pmc
   trunk/src/pmc/resizablestringarray.pmc

Modified: trunk/src/pmc/fixedstringarray.pmc
==============================================================================
--- trunk/src/pmc/fixedstringarray.pmc	Fri Jan 30 00:03:37 2009	(r36164)
+++ trunk/src/pmc/fixedstringarray.pmc	Fri Jan 30 02:04:48 2009	(r36165)
@@ -22,6 +22,8 @@
 #include "parrot/parrot.h"
 
 pmclass FixedStringArray need_ext provides array {
+    ATTR STRING **str_array; /* where the STRINGs are stored */
+    ATTR UINTVAL  size;      /* element count */
 
 /*
 
@@ -40,8 +42,13 @@
 */
 
     VTABLE void init() {
-        PMC_int_val(SELF) = 0;
-        PMC_data(SELF)    = NULL;
+
+        Parrot_FixedStringArray_attributes *attrs =
+            mem_allocate_zeroed_typed(Parrot_FixedStringArray_attributes);
+
+        PMC_data(SELF) = attrs;
+
+        PObj_custom_mark_destroy_SETALL(SELF);
     }
 
 /*
@@ -55,11 +62,15 @@
 */
 
     VTABLE void destroy() {
-        if (PMC_data(SELF))
-            mem_sys_free(PMC_data(SELF));
+        
+        STRING **str_array;
+        
+        GET_ATTR_str_array(INTERP, SELF, str_array);
+
+        if (str_array)
+            mem_sys_free(str_array);
 
-        PMC_data(SELF)    = NULL;
-        PMC_int_val(SELF) = 0;
+        mem_sys_free(PMC_data(SELF));
     }
 
 /*
@@ -73,15 +84,24 @@
 */
 
     VTABLE PMC *clone() {
-        PMC * const dest          = pmc_new(INTERP, SELF->vtable->base_type);
+        
+        STRING    **my_str_array, **dest_str_array;
+        PMC        *const dest = pmc_new(INTERP, SELF->vtable->base_type);
+
+        GET_ATTR_str_array(INTERP, SELF, my_str_array);
+
+        if (my_str_array) {
+            INTVAL size;
+            size_t mem_size;
+
+            GET_ATTR_size(INTERP, SELF, size);
+            mem_size = size * sizeof (STRING *);
+
+            dest_str_array = mem_allocate_n_zeroed_typed(size, STRING *);
+            mem_sys_memcopy(dest_str_array, my_str_array, mem_size);
+            SET_ATTR_str_array(INTERP, dest, dest_str_array);
+            SET_ATTR_size(INTERP, dest, size);
 
-        if (PMC_data(SELF)) {
-            const INTVAL size     = PMC_int_val(SELF);
-            const size_t mem_size = size * sizeof (STRING *);
-
-            PMC_int_val(dest)     = size;
-            PMC_data(dest)        = mem_sys_allocate(mem_size);
-            mem_sys_memcopy(PMC_data(dest), PMC_data(SELF), mem_size);
             PObj_custom_mark_destroy_SETALL(dest);
         }
 
@@ -99,14 +119,17 @@
 */
 
     VTABLE void mark() {
-        STRING * const * data = (STRING **) PMC_data(SELF);
-        if (data) {
-            const int end  = PMC_int_val(SELF);
-            int i;
-
-            for (i = 0; i < end; i++) {
-                if (data[i])
-                    pobject_lives(INTERP, (PObj *) data[i]);
+
+        STRING **str_array;
+        GET_ATTR_str_array(INTERP, SELF, str_array);
+        
+        if (str_array) {
+            UINTVAL size, i;
+            GET_ATTR_size(INTERP, SELF, size);
+
+            for (i = 0; i < size; i++) {
+                if (str_array[i])
+                    pobject_lives(INTERP, (PObj *) str_array[i]);
             }
         }
     }
@@ -138,9 +161,9 @@
 
 */
     VTABLE PMC *get_iter() {
-        STRING     *name     = CONST_STRING(interp, "set_key");
-        PMC * const iter     = pmc_new_init(INTERP, enum_class_Iterator, SELF);
-        PMC * const key      = pmc_new(INTERP, enum_class_Key);
+        STRING     *name = CONST_STRING(interp, "set_key");
+        PMC * const iter = pmc_new_init(INTERP, enum_class_Iterator, SELF);
+        PMC * const key  = pmc_new(INTERP, enum_class_Key);
 
         Parrot_PCCINVOKE(interp, iter, name, "P->", key);
         PObj_get_FLAGS(key) |= KEY_integer_FLAG;
@@ -161,7 +184,9 @@
 */
 
     VTABLE INTVAL elements() {
-        return PMC_int_val(SELF);
+        UINTVAL size;
+        GET_ATTR_size(INTERP, SELF, size);
+        return size;
     }
 
 /*
@@ -252,14 +277,17 @@
 */
 
     VTABLE STRING *get_string_keyed_int(INTVAL key) {
-        STRING **data;
 
-        if (key < 0 || key >= PMC_int_val(SELF))
+        STRING **str_array;
+        UINTVAL  size;
+
+        GET_ATTR_size(INTERP, SELF, size);
+        if (key < 0 || (UINTVAL)key >= size)
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                 "FixedStringArray: index out of bounds!");
 
-        data = (STRING **)PMC_data(SELF);
-        return data[key];
+        GET_ATTR_str_array(INTERP, SELF, str_array);
+        return str_array[key];
     }
 
 /*
@@ -323,13 +351,18 @@
 
 */
 
-    VTABLE void set_integer_native(INTVAL size) {
-        if (PMC_int_val(SELF) || size < 1)
+    VTABLE void set_integer_native(INTVAL new_size) {
+        
+        UINTVAL  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,
                 "FixedStringArray: Can't resize!");
 
-        PMC_int_val(SELF) = size;
-        PMC_data(SELF)    = mem_sys_allocate_zeroed(size * sizeof (STRING*));
+        SET_ATTR_size(INTERP, SELF, new_size);
+        SET_ATTR_str_array(INTERP, SELF, 
+                (STRING**)mem_sys_allocate_zeroed(new_size * sizeof (STRING*)));
 
         PObj_custom_mark_destroy_SETALL(SELF);
     }
@@ -345,7 +378,7 @@
 */
 
     VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
-        PMC    * const ret = pmc_new(INTERP, enum_class_String);
+        PMC    *const ret = pmc_new(INTERP, enum_class_String);
         STRING *val;
 
         VTABLE_set_integer_native(INTERP, ret, value);
@@ -380,7 +413,7 @@
 */
 
     VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
-        PMC    * const ret = pmc_new(INTERP, enum_class_String);
+        PMC    *const ret = pmc_new(INTERP, enum_class_String);
         STRING *val;
 
         VTABLE_set_number_native(INTERP, ret, value);
@@ -415,15 +448,15 @@
 */
 
     VTABLE void set_string_keyed_int(INTVAL key, STRING *value) {
-        STRING **data;
+        STRING **str_array;
 
-        if (key < 0 || key >= PMC_int_val(SELF))
+        if (key < 0 || key >= SELF.elements())
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                 "FixedStringArray: index out of bounds!");
 
-        data      = (STRING **)PMC_data(SELF);
-        GC_WRITE_BARRIER(INTERP, SELF, data[key], value);
-        data[key] = value;
+        GET_ATTR_str_array(INTERP, SELF, str_array);
+        GC_WRITE_BARRIER(INTERP, SELF, str_array[key], value);
+        str_array[key] = value;
     }
 
 /*
@@ -484,9 +517,10 @@
 */
 
     VTABLE STRING *get_repr() {
-        STRING *res    = CONST_STRING(INTERP, "[ ");
-        const INTVAL n = SELF.get_integer();
-        INTVAL  j;
+
+        STRING       *res = CONST_STRING(INTERP, "[ ");
+        const INTVAL  n   = SELF.get_integer();
+        INTVAL        j;
 
         for (j = 0; j < n; ++j) {
             PMC * const val = SELF.get_pmc_keyed_int(j);
@@ -520,15 +554,16 @@
 
 */
     VTABLE void freeze(visit_info *info) {
-        IMAGE_IO *  const io   = info->image_io;
-        STRING   ** const data = (STRING**)PMC_data(SELF);
-        const INTVAL n         = PMC_int_val(SELF);
-        INTVAL       i;
-
-        VTABLE_push_integer(INTERP, io, n);
+        IMAGE_IO * const   io  = info->image_io;
+        STRING           **str_array;
+        UINTVAL            size, i;
+
+        GET_ATTR_size(INTERP, SELF, size);
+        GET_ATTR_str_array(INTERP, SELF, str_array);
+        VTABLE_push_integer(INTERP, io, size);
 
-        for (i = 0; i < n; ++i)
-            VTABLE_push_string(INTERP, io, data[i]);
+        for (i = 0; i < size; ++i)
+            VTABLE_push_string(INTERP, io, str_array[i]);
     }
 
 /*
@@ -545,17 +580,17 @@
         SUPER(info);
 
         if (info->extra_flags == EXTRA_IS_NULL) {
-            INTVAL i, n;
-            STRING **data;
+            UINTVAL  i, size;
+            STRING **str_array;
 
             SELF.init();
 
-            n    = VTABLE_shift_integer(INTERP, io);
-            SELF.set_integer_native(n);
-            data = PMC_data_typed(SELF, STRING **);
+            size   = VTABLE_shift_integer(INTERP, io);
+            SELF.set_integer_native((INTVAL)size);
+            GET_ATTR_str_array(INTERP, SELF, str_array);
 
-            for (i = 0; i < n; ++i)
-                data[i] = VTABLE_shift_string(INTERP, io);
+            for (i = 0; i < size; ++i)
+                str_array[i] = VTABLE_shift_string(INTERP, io);
         }
     }
 }

Modified: trunk/src/pmc/resizablestringarray.pmc
==============================================================================
--- trunk/src/pmc/resizablestringarray.pmc	Fri Jan 30 00:03:37 2009	(r36164)
+++ trunk/src/pmc/resizablestringarray.pmc	Fri Jan 30 02:04:48 2009	(r36165)
@@ -14,17 +14,40 @@
 to elements of the array will be stringified by having their C<get_string>
 method called.
 
+=cut
+
+*/
+
+#include "parrot/parrot.h"
+
+pmclass ResizableStringArray extends FixedStringArray need_ext provides array {
+    ATTR UINTVAL resize_threshold; /*max capacity before resizing */
+
+/*
+
 =head2 Functions
 
 =over 4
 
+=over 4
+
+=item C<void init()>
+
+Initializes the array.
+
 =cut
 
 */
 
-#include "parrot/parrot.h"
+    VTABLE void init() {
 
-pmclass ResizableStringArray extends FixedStringArray need_ext provides array {
+        Parrot_ResizableStringArray_attributes *attrs =
+            mem_allocate_zeroed_typed(Parrot_ResizableStringArray_attributes);
+
+        PMC_data(SELF)   = attrs;
+
+        PObj_custom_mark_destroy_SETALL(SELF);
+    }
 
 /*
 
@@ -37,10 +60,11 @@
 */
 
     VTABLE STRING *get_string_keyed_int(INTVAL key) {
-        STRING **data;
+
+        STRING **str_array;
+        INTVAL size = SELF.elements();
 
         if (key < 0) {
-            INTVAL size = SELF.elements();
             if (key < -size)
                 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                     "ResizableStringArray: index out of bounds!");
@@ -48,15 +72,15 @@
                 key += size;
         }
 
-        if (key >= PMC_int_val(SELF))
+        if (key >= size)
             return string_from_literal(INTERP, "");
 
-        data = (STRING**)PMC_data(SELF);
+        GET_ATTR_str_array(INTERP, SELF, str_array);
 
-        if (!data[key])
-            data[key] = string_from_cstring(interp, NULL, 0);
+        if (!str_array[key])
+            str_array[key] = string_from_cstring(interp, NULL, 0);
 
-        return data[key];
+        return str_array[key];
     }
 
 /*
@@ -70,10 +94,11 @@
 */
 
     VTABLE void set_string_keyed_int(INTVAL key, STRING *value) {
-        STRING **data;
+        
+        STRING **str_array;
+        INTVAL size = SELF.elements();
 
         if (key < 0) {
-            INTVAL size = SELF.elements();
             if (key < -size)
                 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                     "ResizableStringArray: index out of bounds!");
@@ -81,12 +106,12 @@
                 key += size;
         }
 
-        if (key >= PMC_int_val(SELF))
+        if (key >= size)
             SELF.set_integer_native(key+1);
 
-        data      = (STRING **)PMC_data(SELF);
-        GC_WRITE_BARRIER(INTERP, SELF, data[key], value);
-        data[key] = value;
+        GET_ATTR_str_array(INTERP, SELF, str_array);
+        GC_WRITE_BARRIER(INTERP, SELF, str_array[key], value);
+        str_array[key] = value;
     }
 
 /*
@@ -101,8 +126,8 @@
 */
 
     VTABLE void push_string(STRING *value) {
-        INTVAL nextix = SELF.elements();
-        SELF.set_string_keyed_int(nextix, value);
+        INTVAL next_idx = SELF.elements();
+        SELF.set_string_keyed_int(next_idx, value);
     }
 
 /*
@@ -116,13 +141,12 @@
 */
 
     VTABLE STRING *pop_string() {
-        INTVAL  size = PMC_int_val(SELF);
+        INTVAL  size = SELF.elements();
         STRING *value;
 
-        if (size == 0) {
+        if (size == 0)
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                     "ResizableStringArray: Can't pop from an empty array!");
-        }
 
         value = SELF.get_string_keyed_int(size - 1);
         SELF.set_integer_native(size - 1);
@@ -188,57 +212,64 @@
 
 */
 
-    VTABLE void set_integer_native(INTVAL size) {
-        if (size < 0)
+    VTABLE void set_integer_native(INTVAL new_size) {
+
+        STRING **str_array;
+        INTVAL   resize_threshold;
+
+        if (new_size < 0)
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
                     "ResizableStringArray: Can't resize!");
 
-        if (!PMC_data(SELF)) {
+        GET_ATTR_str_array(INTERP, SELF, str_array);
+        GET_ATTR_resize_threshold(INTERP, SELF, resize_threshold);
+        if (!str_array) {
             /* empty - used fixed routine */
-            if (size < 8) {
+            if (new_size < 8) {
                 SUPER(8);
-                PMC_int_val(SELF)  = size;
-                PMC_int_val2(SELF) = 8;
+                SET_ATTR_size(INTERP, SELF, new_size);
+                SET_ATTR_resize_threshold(INTERP, SELF, 8);
             }
             else {
-                SUPER(size);
-                PMC_int_val2(SELF) = size;
+                SUPER(new_size);
+                SET_ATTR_resize_threshold(INTERP, SELF, new_size);
             }
         }
-        else if (size <= PMC_int_val2(SELF)) {
+        else if (new_size <= resize_threshold) {
             /* zero out anything that was previously allocated
              * if we're growing the array */
-            INTVAL oldsize = PMC_int_val(SELF);
-            if (size > oldsize) {
-                STRING **data   = (STRING**)PMC_data(SELF);
+            INTVAL old_size = SELF.elements();
+            if (new_size > old_size) {
                 INTVAL i;
-                for (i=oldsize; i<size; i++)
-                    data[i] = NULL;
+                for (i = old_size; i < new_size; i++)
+                    str_array[i] = NULL;
             }
 
-            PMC_int_val(SELF) = size;
+            SET_ATTR_size(INTERP, SELF, new_size);
             /* we could shrink here if necessary */
             return;
         }
         else {
-            INTVAL i   = PMC_int_val2(SELF);
+            INTVAL i   = resize_threshold;
             INTVAL cur = i;
 
             if (cur < 8192)
-                cur = size < 2 * cur ? 2 * cur : size;
+                cur = new_size < 2 * cur ? 2 * cur : new_size;
             else {
-                cur = size + 4096;
+                cur = new_size + 4096;
                 cur &= ~0xfff;
             }
 
-            PMC_data(SELF) = mem_sys_realloc(PMC_data(SELF),
-                    cur * sizeof (STRING*));
+            SET_ATTR_str_array(INTERP, SELF, 
+                    (STRING **)mem_sys_realloc(
+                        str_array, cur * sizeof (STRING*)));
+            GET_ATTR_str_array(INTERP, SELF, str_array);
 
             for (; i < cur; i++)
-                ((STRING **)PMC_data(SELF))[i] = NULL;
+                str_array[i] = NULL;
 
-            PMC_int_val2(SELF) = cur;
-            PMC_int_val(SELF)  = size;
+            SET_ATTR_size(INTERP, SELF, new_size);
+            SET_ATTR_resize_threshold(INTERP, SELF, cur);
         }
     }
 
@@ -255,7 +286,7 @@
     VTABLE PMC *clone() {
         PMC *copy = SUPER();
         /* copy trimmed extra space */
-        PMC_int_val2(copy) = PMC_int_val(SELF);
+        SET_ATTR_resize_threshold(INTERP, copy, SELF.elements());
         return copy;
     }
 
@@ -270,7 +301,7 @@
 */
 
     VTABLE STRING *shift_string() {
-        INTVAL  size = PMC_int_val(SELF);
+        INTVAL  size = SELF.elements();
         STRING *value;
 
         if (size == 0)
@@ -399,9 +430,11 @@
 */
 
     VTABLE PMC *shift_pmc() {
-        INTVAL  size = PMC_int_val(SELF);
-        PMC    *ret;
-        STRING *value;
+        UINTVAL  size;
+        PMC     *ret;
+        STRING  *value;
+
+        GET_ATTR_size(INTERP, SELF, size);
 
         if (size == 0)
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
@@ -428,17 +461,15 @@
 */
 
     VTABLE void unshift_string(STRING *value) {
-        STRING **data = PMC_data_typed(SELF, STRING **);
-        INTVAL   size = PMC_int_val(SELF);
-        INTVAL   i;
-
+        STRING  **str_array;
+        UINTVAL   size, i;
 
+        GET_ATTR_str_array(INTERP, SELF, str_array);
+        GET_ATTR_size(INTERP, SELF, size);
         SELF.set_integer_native(size + 1);
 
-        data = PMC_data_typed(SELF, STRING **);
-
         for (i = size; i; --i)
-            data[i] = data[i - 1];
+            str_array[i] = str_array[i - 1];
 
         SELF.set_string_keyed_int(0, value);
     }
@@ -511,12 +542,14 @@
 */
 
     VTABLE void delete_keyed_int(INTVAL key) {
-        STRING **data = PMC_data_typed(SELF, STRING **);
-        INTVAL   size = PMC_int_val(SELF);
-        INTVAL   i;
+        STRING  **str_array;
+        UINTVAL   size, i;
+
+        GET_ATTR_str_array(INTERP, SELF, str_array);
+        GET_ATTR_size(INTERP, SELF, size);
 
         for (i = key; i < size - 1; ++i)
-            data[i] = data[i + 1];
+            str_array[i] = str_array[i + 1];
 
         SELF.set_integer_native(size - 1);
     }
@@ -532,13 +565,15 @@
 */
 
     VTABLE void delete_keyed(PMC *key) {
-        INTVAL   idx  = key_integer(INTERP, key);
-        STRING **data = PMC_data_typed(SELF, STRING **);
-        INTVAL   size = PMC_int_val(SELF);
-        INTVAL   i;
+        INTVAL    idx  = key_integer(INTERP, key);
+        STRING  **str_array;
+        UINTVAL   size, i;
+
+        GET_ATTR_str_array(INTERP, SELF, str_array);
+        GET_ATTR_size(INTERP, SELF, size);
 
         for (i = idx; i < size - 1; ++i)
-            data[i] = data[i + 1];
+            str_array[i] = str_array[i + 1];
 
         SELF.set_integer_native(size - 1);
     }
@@ -560,6 +595,7 @@
 */
 
     void splice(PMC *value, INTVAL offset, INTVAL count) {
+
         INTVAL length, elems, shift, i;
 
         if (value->vtable->base_type != SELF->vtable->base_type &&
@@ -588,7 +624,6 @@
 
             SELF.set_integer_native(length + shift);
         }
-
         /* grow the array */
         else if (shift > 0) {
             SELF.set_integer_native(length + shift);
@@ -620,12 +655,12 @@
 */
 
     METHOD PMC* shift() {
-        PMC *value = VTABLE_shift_pmc(INTERP, SELF);
+        PMC *value = SELF.shift_pmc();
         RETURN(PMC *value);
     }
 
     METHOD PMC* pop() {
-        PMC *value = VTABLE_pop_pmc(INTERP, SELF);
+        PMC *value = SELF.pop_pmc();
         RETURN(PMC *value);
     }
 
@@ -642,11 +677,11 @@
 */
 
     METHOD unshift(PMC* value) {
-        VTABLE_unshift_pmc(INTERP, SELF, value);
+        SELF.unshift_pmc(value);
     }
 
     METHOD push(PMC* value) {
-        VTABLE_push_pmc(INTERP, SELF, value);
+        SELF.push_pmc(value);
     }
 
 }


More information about the parrot-commits mailing list