Skip to content

Commit

Permalink
indexing was broken, and was creating a memory corruption segfault. F…
Browse files Browse the repository at this point in the history
…ixed indexing, cleaned up some code, etc. Should all work now
  • Loading branch information
Whiteknight committed Oct 30, 2009
1 parent 6bbc362 commit 0a67263
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
24 changes: 16 additions & 8 deletions src/pmc/nummatrix2d.pmc
Expand Up @@ -12,30 +12,32 @@ static void
resize_matrix(PARROT_INTERP, PMC * self, INTVAL x, INTVAL y)
{
Parrot_NumMatrix2D_attributes * const attrs = PARROT_NUMMATRIX2D(self);
/* Store the old values */
const INTVAL old_x = attrs->x;
const INTVAL old_y = attrs->y;
FLOATVAL * old_s = attrs->storage;

/* x and y are indices, not sizes. Resize the matrix to accomodate this
new point without shrinking by taking the max. */
const INTVAL new_x = INDEX_MAX(old_x, x + 1);
const INTVAL new_y = INDEX_MAX(old_y, y + 1);
const INTVAL newsize = new_x * new_y;
FLOATVAL * new_s = (FLOATVAL *)mem_sys_allocate_zeroed(newsize * sizeof (FLOATVAL));
FLOATVAL * old_s = attrs->storage;
const INTVAL min_x = INDEX_MIN(old_x, new_x);
const INTVAL min_y = INDEX_MIN(old_y, new_y);

INTVAL i, j;
for (i = 0; i < min_x; i++) {
for (j = 0; j < min_y; j++) {
for (i = 0; i < old_x; i++) {
for (j = 0; j < old_y; j++) {
ITEM_XY_ROWMAJOR(new_s, new_x, new_y, i, j) =
ITEM_XY_ROWMAJOR(old_s, old_x, old_y, i, j);
}
}
attrs->storage = new_s;
attrs->x = new_x;
attrs->y = new_y;
mem_sys_free(old_s);
if (old_s)
mem_sys_free(old_s);
}



pmclass NumMatrix2D dynpmc auto_attrs {
ATTR FLOATVAL * storage;
ATTR INTVAL x;
Expand All @@ -55,6 +57,12 @@ pmclass NumMatrix2D dynpmc auto_attrs {
*/

VTABLE void init() {
Parrot_NumMatrix2D_attributes * attr =
(Parrot_NumMatrix2D_attributes *) PARROT_NUMMATRIX2D(SELF);
attr->storage = NULL;
attr->x = 0;
attr->y = 0;
attr->flags = 0;
PObj_custom_destroy_SET(SELF);
}

Expand Down
8 changes: 4 additions & 4 deletions src/pmc/pla_matrix_types.h
Expand Up @@ -11,16 +11,16 @@ do { \
} while(0);

#define INDEX_XY_ROWMAJOR(x_max, y_max, x, y) \
(((x_max) * (x)) + (y))
(((y_max) * (x)) + (y))

#define INDEX_XY_COLMAJOR(x_max, y_max, x, y) \
(((y_max) * (y)) + (x))
(((x_max) * (y)) + (x))

#define ITEM_XY_ROWMAJOR(s, x_max, y_max, x, y) \
(s)[((x_max) * (x)) + (y)]
(s)[((y_max) * (x)) + (y)]

#define ITEM_XY_COLMAJOR(s, x_max, y_max, x, y) \
(s)[((y_max) * (y)) + (x)]
(s)[((x_max) * (y)) + (x)]

#define INDEX_MIN(a, b) (((a) <= (b))?(a):(b))
#define INDEX_MAX(a, b) (((a) >= (b))?(a):(b))
Expand Down
5 changes: 2 additions & 3 deletions t/10-nummatrix.t
Expand Up @@ -23,7 +23,7 @@ sub MAIN () {
create_nummatrix2d();
vtable_set_number_keyed();
vtable_get_number_keyed();
vtable_get_attr_keyed_str();
vtable_get_attr_str();
vtable_get_integer_keyed();
vtable_set_integer_keyed();
vtable_get_string();
Expand All @@ -46,7 +46,6 @@ sub MAIN () {
method_mem_transpose();
method_iterate_function_inplace();
}

sub create_nummatrix2d() {
Q:PIR {
$P0 = new 'NumMatrix2D'
Expand Down Expand Up @@ -107,7 +106,7 @@ sub vtable_get_number_keyed() {
}
}
sub vtable_get_attr_keyed_str() {
sub vtable_get_attr_str() {
Q:PIR {
$P0 = new 'NumMatrix2D'
$P0[2;5] = 1.0
Expand Down

0 comments on commit 0a67263

Please sign in to comment.