Skip to content

Commit

Permalink
[pmc] get rid of UnionVal in *FloatArray
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.parrot.org/parrot/trunk@35991 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
Christoph Otto committed Jan 25, 2009
1 parent dd60341 commit a638c12
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 58 deletions.
67 changes: 42 additions & 25 deletions src/pmc/fixedfloatarray.pmc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ stored FLOATVALs. It uses Float PMCs to do all necessary conversions.
#include "parrot/parrot.h" #include "parrot/parrot.h"


pmclass FixedFloatArray need_ext provides array { pmclass FixedFloatArray need_ext provides array {
ATTR INTVAL size;
ATTR FLOATVAL *float_array;


/* /*


Expand All @@ -40,8 +42,9 @@ Initializes the array.
*/ */


VTABLE void init() { VTABLE void init() {
PMC_int_val(SELF) = 0; Parrot_FixedFloatArray_attributes* attrs =
PMC_data(SELF) = NULL; mem_allocate_zeroed_typed(Parrot_FixedFloatArray_attributes);
PMC_data(SELF) = attrs;
} }


/* /*
Expand All @@ -55,11 +58,12 @@ Destroys the array.
*/ */


VTABLE void destroy() { VTABLE void destroy() {
if (PMC_data(SELF)) FLOATVAL *float_array;
mem_sys_free(PMC_data(SELF)); GET_ATTR_float_array(INTERP, SELF, float_array);
if (float_array)
mem_sys_free(float_array);


PMC_data(SELF) = NULL; mem_sys_free(PMC_data(SELF));
PMC_int_val(SELF) = 0;
} }


/* /*
Expand All @@ -73,19 +77,22 @@ Creates and returns a copy of the array.
*/ */


VTABLE PMC *clone() { VTABLE PMC *clone() {
INTVAL size; INTVAL size;
FLOATVAL *self_float_array, *dest_float_array;
size_t mem_size; size_t mem_size;
PMC * const dest = pmc_new(INTERP, SELF->vtable->base_type); 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; return dest;


size = PMC_int_val(SELF); GET_ATTR_size(INTERP, SELF, size);
PMC_int_val(dest) = size; SET_ATTR_size(INTERP, dest, size);
mem_size = size * sizeof (FLOATVAL); mem_size = size * sizeof (FLOATVAL);


PMC_data(dest) = mem_sys_allocate(mem_size); dest_float_array = (FLOATVAL*)mem_sys_allocate(mem_size);
mem_sys_memcopy(PMC_data(dest), PMC_data(SELF), 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); PObj_active_destroy_SET(dest);


return dest; return dest;
Expand Down Expand Up @@ -115,7 +122,9 @@ fixed sized array).
*/ */


VTABLE INTVAL elements() { VTABLE INTVAL elements() {
return PMC_int_val(SELF); INTVAL size;
GET_ATTR_size(INTERP, SELF, size);
return size;
} }


/* /*
Expand Down Expand Up @@ -176,14 +185,16 @@ Returns the floating-point value of the element at index C<key>.
*/ */


VTABLE FLOATVAL get_number_keyed_int(INTVAL 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, Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedFloatArray: index out of bounds!"); "FixedFloatArray: index out of bounds!");


data = (FLOATVAL *)PMC_data(SELF); GET_ATTR_float_array(INTERP, SELF, float_array);
return data[key]; return float_array[key];
} }


/* /*
Expand Down Expand Up @@ -275,13 +286,16 @@ Resizes the array to C<size> elements.


*/ */


VTABLE void set_integer_native(INTVAL size) { VTABLE void set_integer_native(INTVAL new_size) {
if (PMC_int_val(SELF) || size < 1) 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, Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedFloatArray: Can't resize!"); "FixedFloatArray: Can't resize!");


PMC_int_val(SELF) = size; SET_ATTR_size(INTERP, SELF, new_size);
PMC_data(SELF) = mem_sys_allocate(size * sizeof (FLOATVAL)); SET_ATTR_float_array(INTERP, SELF,
mem_allocate_n_typed(new_size, FLOATVAL));
PObj_active_destroy_SET(SELF); PObj_active_destroy_SET(SELF);
} }


Expand Down Expand Up @@ -326,13 +340,16 @@ C<value>.
*/ */


VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) { VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
FLOATVAL *data; FLOATVAL *float_array;
if (key < 0 || key >= PMC_int_val(SELF)) 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, Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"FixedFloatArray: index out of bounds!"); "FixedFloatArray: index out of bounds!");


data = (FLOATVAL *)PMC_data(SELF); GET_ATTR_float_array(INTERP, SELF, float_array);
data[key] = value; float_array[key] = value;
} }


/* /*
Expand Down
99 changes: 66 additions & 33 deletions src/pmc/resizablefloatarray.pmc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,8 +23,25 @@ which stores FLOATVALs. It uses Float PMCs to do all necessary conversions.
#include "parrot/parrot.h" #include "parrot/parrot.h"


pmclass ResizableFloatArray extends FixedFloatArray need_ext provides array { 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)> =item C<FLOATVAL get_number_keyed_int(INTVAL key)>
Expand All @@ -36,17 +53,19 @@ Returns the floating-point value of the element at index C<key>.
*/ */


VTABLE FLOATVAL get_number_keyed_int(INTVAL key) { VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
FLOATVAL *data; FLOATVAL *float_array;
INTVAL size;


if (key < 0) if (key < 0)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS, Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableFloatArray: index 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; return 0.0;


data = (FLOATVAL *)PMC_data(SELF); GET_ATTR_float_array(INTERP, SELF, float_array);
return data[key]; return float_array[key];
} }


/* /*
Expand All @@ -61,17 +80,19 @@ C<value>.
*/ */


VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) { VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
FLOATVAL *data; FLOATVAL *float_array;
INTVAL size;


if (key < 0) if (key < 0)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS, Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableFloatArray: index 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); SELF.set_integer_native(key+1);


data = (FLOATVAL *)PMC_data(SELF); GET_ATTR_float_array(INTERP, SELF, float_array);
data[key] = value; float_array[key] = value;
} }


/* /*
Expand All @@ -92,40 +113,46 @@ equal to three quarters of the old size).
*/ */


VTABLE void set_integer_native(INTVAL size) { VTABLE void set_integer_native(INTVAL size) {
FLOATVAL *float_array;
INTVAL resize_threshold;

if (size < 0) if (size < 0)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS, Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableFloatArray: Can't resize to negative value!"); "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 */ /* empty - used fixed routine */
if (size < 8) { if (size < 8) {
SUPER(8); SUPER(8);
PMC_int_val(SELF) = size; SET_ATTR_size(INTERP, SELF, size);
PMC_int_val2(SELF) = 8; SET_ATTR_resize_threshold(INTERP, SELF, 8);
} }
else { else {
SUPER(size); 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 */ /* we could shrink here if necessary */
PMC_int_val(SELF) = size; SET_ATTR_size(INTERP, SELF, size);
return; return;
} }
else { else {
INTVAL cur = PMC_int_val2(SELF); INTVAL cur = resize_threshold;
if (cur < 8192) if (cur < 8192)
cur = size < 2 * cur ? 2 * cur : size; cur = size < 2 * cur ? 2 * cur : size;
else { else {
INTVAL needed = size - cur; INTVAL needed = size - cur;
cur += needed + 4096; cur += needed + 4096;
cur &= ~0xfff; cur &= ~0xfff;
} }
PMC_data(SELF) = mem_sys_realloc(PMC_data(SELF), SET_ATTR_float_array(INTERP, SELF,
cur * sizeof (FLOATVAL)); (FLOATVAL*)mem_sys_realloc(float_array,
PMC_int_val2(SELF) = cur; cur * sizeof (FLOATVAL)));
PMC_int_val(SELF) = size; SET_ATTR_size(INTERP, SELF, size);
SET_ATTR_resize_threshold(INTERP, SELF, cur);
} }
} }


Expand All @@ -140,10 +167,12 @@ Creates and returns a copy of the array.
*/ */


VTABLE PMC *clone() { VTABLE PMC *clone() {
PMC *copy = SUPER(); INTVAL size;
PMC *copy = SUPER();


/* copy trimmed extra space */ /* 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; return copy;
} }
Expand Down Expand Up @@ -175,7 +204,8 @@ Removes and returns the last element in the array.


VTABLE FLOATVAL pop_float() { VTABLE FLOATVAL pop_float() {
FLOATVAL value; FLOATVAL value;
INTVAL size = PMC_int_val(SELF); INTVAL size;
GET_ATTR_size(INTERP, SELF, size);


if (size == 0) if (size == 0)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS, Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
Expand All @@ -196,18 +226,20 @@ Removes and returns an item from the start of the array.
*/ */


VTABLE FLOATVAL shift_float() { VTABLE FLOATVAL shift_float() {
FLOATVAL value, *data; FLOATVAL value, *float_array;
INTVAL size = PMC_int_val(SELF); INTVAL size;

GET_ATTR_size(INTERP, SELF, size);


if (size == 0) if (size == 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableFloatArray: Can't shift from an empty array!"); "ResizableFloatArray: Can't shift from an empty array!");


data = (FLOATVAL *)PMC_data(SELF); GET_ATTR_float_array(INTERP, SELF, float_array);
value = data[0]; value = float_array[0];
PMC_int_val(SELF) = --size; 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; return value;
} }


Expand All @@ -222,13 +254,14 @@ Add and integer to the start of the array.
*/ */


VTABLE void unshift_float(FLOATVAL value) { VTABLE void unshift_float(FLOATVAL value) {
INTVAL size = PMC_int_val(SELF); INTVAL size;
FLOATVAL *data; FLOATVAL *float_array;


GET_ATTR_size(INTERP, SELF, size);
SELF.set_integer_native(size + 1); SELF.set_integer_native(size + 1);
data = (FLOATVAL *)PMC_data(SELF); GET_ATTR_float_array(INTERP, SELF, float_array);
mem_sys_memmove(data + 1, data, size * sizeof (FLOATVAL)); mem_sys_memmove(float_array + 1, float_array, size * sizeof (FLOATVAL));
data[0] = value; float_array[0] = value;
} }


} }
Expand Down

0 comments on commit a638c12

Please sign in to comment.