Permalink
Browse files

[pmc] get rid of UnionVal in *FloatArray

git-svn-id: https://svn.parrot.org/parrot/trunk@35991 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information...
1 parent dd60341 commit a638c12e463d4c87b0304bcc9c727f5133927d44 @cotto cotto committed Jan 25, 2009
Showing with 108 additions and 58 deletions.
  1. +42 −25 src/pmc/fixedfloatarray.pmc
  2. +66 −33 src/pmc/resizablefloatarray.pmc
@@ -22,6 +22,8 @@ stored FLOATVALs. It uses Float PMCs to do all necessary conversions.
#include "parrot/parrot.h"
pmclass FixedFloatArray need_ext provides array {
+ ATTR INTVAL size;
+ ATTR FLOATVAL *float_array;
/*
@@ -40,8 +42,9 @@ Initializes the array.
*/
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 @@ Destroys the array.
*/
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 @@ Creates and returns a copy of the array.
*/
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 @@ fixed sized array).
*/
VTABLE INTVAL elements() {
- return PMC_int_val(SELF);
+ INTVAL size;
+ GET_ATTR_size(INTERP, SELF, size);
+ return size;
}
/*
@@ -176,14 +185,16 @@ Returns the floating-point value of the element at index C<key>.
*/
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 @@ Resizes the array to C<size> elements.
*/
- 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 @@ C<value>.
*/
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;
}
/*
@@ -23,8 +23,25 @@ which stores FLOATVALs. It uses Float PMCs to do all necessary conversions.
#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 @@ Returns the floating-point value of the element at index C<key>.
*/
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 @@ C<value>.
*/
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,40 +113,46 @@ equal to three quarters of the old size).
*/
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 {
INTVAL needed = size - cur;
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 @@ Creates and returns a copy of the array.
*/
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 @@ Removes and returns the last element in the array.
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 @@ Removes and returns an item from the start of the array.
*/
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 @@ Add and integer to the start of the array.
*/
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;
}
}

0 comments on commit a638c12

Please sign in to comment.