Author: bernhard
Date: Mon Jul 25 11:11:34 2005
New Revision: 8699
Modified:
trunk/classes/resizablebooleanarray.pmc
trunk/classes/resizablefloatarray.pmc
trunk/classes/resizableintegerarray.pmc
trunk/classes/resizablepmcarray.pmc
trunk/classes/resizablestringarray.pmc
Log:
Cleanup of the Resizable*Array PMCs.
They all use now the same allocation strategy as ResizablePMCArray.
Thanks to Matt Fowles, [perl #36644]
Modified: trunk/classes/resizablebooleanarray.pmc
==============================================================================
--- trunk/classes/resizablebooleanarray.pmc (original)
+++ trunk/classes/resizablebooleanarray.pmc Mon Jul 25 11:11:34 2005
@@ -8,10 +8,10 @@ classes/resizablebooleanarray.pmc - resi
=head1 DESCRIPTION
-The C<ResizableBooleanArray PMC> implements an array of resizable size,
-which stores booleans.
+The C<ResizableBooleanArray PMC> implements an array of resizable
+size, which stores booleans.
It uses the C<Boolean PMC> for all conversions.
-The C<ResizableBooleanArray PMC> extends the C<FixedBooleanArray PMC>.
+C<ResizableBooleanArray PMC> extends C<FixedBooleanArray PMC>
=head2 Functions
@@ -49,7 +49,6 @@ Returns the integer value of the element
*/
INTVAL get_integer_keyed_int (INTVAL key) {
- Parrot_UInt1 *sd;
if (key < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableBooleanArray: index out of bounds!");
@@ -90,23 +89,35 @@ Resizes the array to C<size> elements.
*/
void set_integer_native (INTVAL size) {
- Parrot_UInt1 *sd;
if (size < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableBooleanArray: Can't resize!");
if ( ! PMC_data(SELF) ) {
SUPER(size);
- } else if ( size > PMC_int_val2(SELF) ) {
- INTVAL old_size = PMC_int_val2(SELF);
- PMC_int_val2(SELF) = size < 2*old_size ? 2*old_size :
(size/BITS_PER_CHAR+1)*BITS_PER_CHAR;
- sd = PMC_data(SELF);
- PMC_data(SELF) = mem_sys_realloc(sd,
PMC_int_val2(SELF)/BITS_PER_CHAR);
- PMC_int_val(SELF) = size;
- } else {
+ }
+ else if (size <= PMC_int_val2(SELF)) {
PMC_int_val(SELF) = size;
+ /* we could shrink here if necessary */
return;
}
+ else {
+ INTVAL cur, needed;
+ cur = PMC_int_val2(SELF);
+ if (cur < 8192) {
+ cur = size < 2 * cur ? 2 * cur
+ : (size/BITS_PER_CHAR+1)*BITS_PER_CHAR;
+ }
+ else {
+ needed = size - cur;
+ cur += needed + 4096;
+ cur &= ~0xfff;
+ }
+ PMC_data(SELF) = mem_sys_realloc(PMC_data(SELF),
+ cur/BITS_PER_CHAR);
+ PMC_int_val(SELF) = size;
+ PMC_int_val2(SELF) = cur;
+ }
}
}
@@ -123,7 +134,6 @@ F<docs/pdds/pdd17_basic_types.pod>.
Initial version - Matt Fowles 2004-06-11
Changed allocator to double size - Matt Fowles 2004-06-15
-Added push_integer - Bernhard Schmalhofer 2004-10-17
=cut
Modified: trunk/classes/resizablefloatarray.pmc
==============================================================================
--- trunk/classes/resizablefloatarray.pmc (original)
+++ trunk/classes/resizablefloatarray.pmc Mon Jul 25 11:11:34 2005
@@ -22,12 +22,6 @@ which stores FLOATVALs. It uses Float PM
#include "parrot/parrot.h"
-typedef struct _SizeFloatData {
- INTVAL size;
- FLOATVAL data[1];
-} SizeFloatData;
-#define NEEDED_SIZE(n) ((n-1)*sizeof(FLOATVAL) + sizeof(SizeFloatData))
-
pmclass ResizableFloatArray extends FixedFloatArray need_ext does array {
@@ -42,15 +36,15 @@ Returns the floating-point value of the
*/
FLOATVAL get_number_keyed_int (INTVAL key) {
- SizeFloatData *sd;
+ FLOATVAL *data;
if (key < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableFloatArray: index out of bounds!");
if (key >= PMC_int_val(SELF))
DYNSELF.set_integer_native(key+1);
- sd = (SizeFloatData *)PMC_data(SELF);
- return sd->data[key];
+ data = (FLOATVAL *)PMC_data(SELF);
+ return data[key];
}
/*
@@ -65,15 +59,15 @@ C<value>.
*/
void set_number_keyed_int (INTVAL key, FLOATVAL value) {
- SizeFloatData *sd;
+ FLOATVAL *data;
if (key < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableFloatArray: index out of bounds!");
if(key >= PMC_int_val(SELF))
DYNSELF.set_integer_native(key+1);
- sd = (SizeFloatData *)PMC_data(SELF);
- sd->data[key] = value;
+ data = (FLOATVAL *)PMC_data(SELF);
+ data[key] = value;
}
/*
@@ -94,28 +88,42 @@ equal to three quarters of the old size)
*/
void set_integer_native (INTVAL size) {
- SizeFloatData *sd;
if (size < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableFloatArray: Can't resize to negative value!");
- sd = PMC_data(SELF);
- PMC_int_val(SELF) = size;
- if (sd == NULL) {
- sd = mem_sys_allocate(NEEDED_SIZE(size));
- sd->size = size;
- } else if (size >= sd->size) {
- sd->size = size < 2 * sd->size ? 2 * sd->size : size;
- sd = mem_sys_realloc(sd, NEEDED_SIZE(sd->size));
- } else if (size < sd->size / 2) {
- sd->size = size * 3 / 2;
- sd = mem_sys_realloc(sd, NEEDED_SIZE(sd->size));
- } else {
+ if(!PMC_data(SELF)) {
+ /* empty - used fixed routine */
+ if (size < 8) {
+ SUPER(8);
+ PMC_int_val(SELF) = size;
+ PMC_int_val2(SELF) = 8;
+ }
+ else {
+ SUPER(size);
+ PMC_int_val2(SELF) = size;
+ }
+ }
+ else if (size <= PMC_int_val2(SELF)) {
+ PMC_int_val(SELF) = size;
+ /* we could shrink here if necessary */
return;
}
-
- PMC_data(SELF) = sd;
- PObj_active_destroy_SET(SELF);
+ else {
+ INTVAL cur, needed;
+ cur = PMC_int_val2(SELF);
+ if (cur < 8192)
+ cur = size < 2 * cur ? 2 * cur : size;
+ else {
+ 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;
+ }
}
/*
@@ -129,18 +137,10 @@ Creates and returns a copy of the array.
*/
PMC* clone () {
- SizeFloatData *sd;
- PMC * dest = pmc_new(INTERP, SELF->vtable->base_type);
-
- if (!PMC_data(SELF))
- return dest;
- PMC_int_val(dest) = PMC_int_val(SELF);
- sd = PMC_data(SELF);
-
- PMC_data(dest) = mem_sys_allocate(NEEDED_SIZE(sd->size));
- mem_sys_memcopy(PMC_data(dest), PMC_data(SELF), NEEDED_SIZE(sd->size));
- PObj_active_destroy_SET(dest);
- return dest;
+ PMC *copy = SUPER();
+ /* copy trimmed extra space */
+ PMC_int_val2(copy) = PMC_int_val(SELF);
+ return copy;
}
/*
@@ -154,13 +154,8 @@ Adds C<value> to the end of the array.
*/
void push_float (FLOATVAL value) {
- INTVAL size;
- SizeFloatData *sd;
-
- size = PMC_int_val(SELF);
- DYNSELF.set_integer_native(size+1);
- sd = (SizeFloatData *)PMC_data(SELF);
- sd->data[size] = value;
+ INTVAL nextix = DYNSELF.elements();
+ DYNSELF.set_number_keyed_int(nextix, value);
}
/*
@@ -176,19 +171,16 @@ Removes and returns the last element in
FLOATVAL pop_float() {
INTVAL size;
FLOATVAL value;
- SizeFloatData *sd;
size = PMC_int_val(SELF);
- sd = (SizeFloatData *)PMC_data(SELF);
- if (sd == NULL || size == 0) {
+ if (size == 0) {
internal_exception(OUT_OF_BOUNDS,
"ResizableFloatArray: Can't pop from an empty array!");
}
- value = sd->data[size - 1];
+ value = DYNSELF.get_number_keyed_int(size-1);
DYNSELF.set_integer_native(size - 1);
-
return value;
}
@@ -206,6 +198,7 @@ F<docs/pdds/pdd17_basic_types.pod>.
Initial version - Matt Fowles 2004-06-11
Changed allocator to double size - Matt Fowles 2004-06-15
+moved available size to int_val2 - Matt Fowles 2005-07-22
=cut
Modified: trunk/classes/resizableintegerarray.pmc
==============================================================================
--- trunk/classes/resizableintegerarray.pmc (original)
+++ trunk/classes/resizableintegerarray.pmc Mon Jul 25 11:11:34 2005
@@ -21,11 +21,6 @@ It uses Integer PMCs for all of the conv
#include "parrot/parrot.h"
-typedef struct _SizeIntData {
- INTVAL size;
- INTVAL data[1];
-} SizeIntData;
-#define NEEDED_SIZE(n) ((n-1)*sizeof(INTVAL) + sizeof(SizeIntData))
pmclass ResizableIntegerArray extends FixedIntegerArray need_ext does array {
@@ -40,15 +35,15 @@ Returns the integer value of the element
*/
INTVAL get_integer_keyed_int (INTVAL key) {
- SizeIntData *sd;
+ INTVAL *data;
if (key < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableIntegerArray: index out of bounds!");
if(key >= PMC_int_val(SELF))
DYNSELF.set_integer_native(key+1);
- sd = (SizeIntData *)PMC_data(SELF);
- return sd->data[key];
+ data = (INTVAL *)PMC_data(SELF);
+ return data[key];
}
/*
@@ -62,15 +57,15 @@ Sets the integer value of the element at
*/
void set_integer_keyed_int (INTVAL key, INTVAL value) {
- SizeIntData *sd;
+ INTVAL *data;
if (key < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableIntegerArray: index out of bounds!");
if(key >= PMC_int_val(SELF))
DYNSELF.set_integer_native(key+1);
- sd = (SizeIntData *)PMC_data(SELF);
- sd->data[key] = value;
+ data = (INTVAL *)PMC_data(SELF);
+ data[key] = value;
}
/*
@@ -84,35 +79,50 @@ Resizes the array to C<size> elements.
*/
void set_integer_native (INTVAL size) {
- SizeIntData *sd;
if (size < 0)
internal_exception(OUT_OF_BOUNDS,
- "ResizableIntegerArray: Can't resize!");
+ "ResizableStringArray: Can't resize!");
- sd = PMC_data(SELF);
- PMC_int_val(SELF) = size;
- if(sd == NULL) {
- sd = mem_sys_allocate_zeroed(NEEDED_SIZE(size));
- sd->size = size;
- } else if(size >= sd->size) {
- INTVAL old = sd->size;
- sd->size = size < 2*sd->size ? sd->size*2 : size;
- sd = mem_sys_realloc(sd, NEEDED_SIZE(sd->size));
- memset(sd->data + old, 0, (sd->size - old) * sizeof(INTVAL));
- } else {
+ if(!PMC_data(SELF)) {
+ /* empty - used fixed routine */
+ if (size < 8) {
+ SUPER(8);
+ PMC_int_val(SELF) = size;
+ PMC_int_val2(SELF) = 8;
+ }
+ else {
+ SUPER(size);
+ PMC_int_val2(SELF) = size;
+ }
+ }
+ else if (size <= PMC_int_val2(SELF)) {
+ PMC_int_val(SELF) = size;
+ /* we could shrink here if necessary */
return;
}
-
- PMC_data(SELF) = sd;
- PObj_active_destroy_SET(SELF);
+ else {
+ INTVAL cur, needed;
+ cur = PMC_int_val2(SELF);
+ if (cur < 8192)
+ cur = size < 2 * cur ? 2 * cur : size;
+ else {
+ needed = size - cur;
+ cur += needed + 4096;
+ cur &= ~0xfff;
+ }
+ PMC_data(SELF) = mem_sys_realloc(PMC_data(SELF),
+ cur * sizeof(INTVAL));
+ PMC_int_val2(SELF) = cur;
+ PMC_int_val(SELF) = size;
+ }
}
/*
=item C<void push_integer (INTVAL value)>
-Extends the array by adding an element of value C<value> to the end of
-the array.
+Extends the array by adding an element of value C<value> to the end
+of the array.
=cut
@@ -136,19 +146,14 @@ Removes and returns the last element in
INTVAL pop_integer() {
INTVAL size;
INTVAL value;
- SizeIntData *sd;
size = PMC_int_val(SELF);
- sd = (SizeIntData *)PMC_data(SELF);
-
- if (sd == NULL || size == 0) {
+ if (size == 0) {
internal_exception(OUT_OF_BOUNDS,
"ResizableIntegerArray: Can't pop from an empty array!");
}
-
- value = sd->data[size - 1];
+ value = DYNSELF.get_integer_keyed_int(size-1);
DYNSELF.set_integer_native(size - 1);
-
return value;
}
@@ -163,18 +168,10 @@ Creates and returns a copy of the array.
*/
PMC* clone () {
- SizeIntData *sd;
- PMC * dest = pmc_new(INTERP, SELF->vtable->base_type);
-
- if (!PMC_data(SELF))
- return dest;
- PMC_int_val(dest) = PMC_int_val(SELF);
- sd = PMC_data(SELF);
-
- PMC_data(dest) = mem_sys_allocate(NEEDED_SIZE(sd->size));
- mem_sys_memcopy(PMC_data(dest), PMC_data(SELF), NEEDED_SIZE(sd->size));
- PObj_active_destroy_SET(dest);
- return dest;
+ PMC *copy = SUPER();
+ /* copy trimmed extra space */
+ PMC_int_val2(copy) = PMC_int_val(SELF);
+ return copy;
}
}
@@ -192,6 +189,7 @@ F<docs/pdds/pdd17_basic_types.pod>.
Initial version - Matt Fowles 2004-06-11
Changed allocator to double size - Matt Fowles 2004-06-15
Added push_integer - Bernhard Schmalhofer 2004-10-17
+moved available size to int_val2 - Matt Fowles 2005-07-22
=cut
Modified: trunk/classes/resizablepmcarray.pmc
==============================================================================
--- trunk/classes/resizablepmcarray.pmc (original)
+++ trunk/classes/resizablepmcarray.pmc Mon Jul 25 11:11:34 2005
@@ -506,7 +506,7 @@ Creates and returns a copy of the array.
PMC* clone () {
PMC *copy = SUPER();
-
+ /* copy trimmed extra space */
PMC_int_val2(copy) = PMC_int_val(SELF);
return copy;
}
Modified: trunk/classes/resizablestringarray.pmc
==============================================================================
--- trunk/classes/resizablestringarray.pmc (original)
+++ trunk/classes/resizablestringarray.pmc Mon Jul 25 11:11:34 2005
@@ -24,12 +24,6 @@ their C<get_string> method called.
#include "parrot/parrot.h"
-typedef struct _SizeStringData {
- INTVAL size;
- STRING* data[1];
-} SizeStringData;
-#define NEEDED_SIZE(n) ((n-1)*sizeof(STRING*) + sizeof(SizeStringData))
-
pmclass ResizableStringArray extends FixedStringArray need_ext does array {
/*
@@ -43,15 +37,15 @@ Returns the Parrot string value of the e
*/
STRING* get_string_keyed_int (INTVAL key) {
- SizeStringData *sd;
+ STRING **data;
if (key < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableStringArray: index out of bounds!");
if(key >= PMC_int_val(SELF))
DYNSELF.set_integer_native(key+1);
- sd = (SizeStringData *)PMC_data(SELF);
- return sd->data[key];
+ data = (STRING**)PMC_data(SELF);
+ return data[key];
}
/*
@@ -65,16 +59,16 @@ Sets the Parrot string value of the elem
*/
void set_string_keyed_int (INTVAL key, STRING* value) {
- SizeStringData *sd;
+ STRING **data;
if (key < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableStringArray: index out of bounds!");
if(key >= PMC_int_val(SELF))
DYNSELF.set_integer_native(key+1);
- sd = (SizeStringData *)PMC_data(SELF);
- DOD_WRITE_BARRIER(INTERP, SELF, sd->data[key], value);
- sd->data[key] = value;
+ data = (STRING**)PMC_data(SELF);
+ DOD_WRITE_BARRIER(INTERP, SELF, data[key], value);
+ data[key] = value;
}
/*
@@ -106,19 +100,14 @@ Removes and returns the last element in
STRING* pop_string() {
INTVAL size;
STRING* value;
- SizeStringData *sd;
size = PMC_int_val(SELF);
- sd = (SizeStringData *)PMC_data(SELF);
-
- if (sd == NULL || size == 0) {
+ if (size == 0) {
internal_exception(OUT_OF_BOUNDS,
"ResizableStringArray: Can't pop from an empty array!");
}
-
- value = sd->data[size - 1];
+ value = DYNSELF.get_string_keyed_int(size-1);
DYNSELF.set_integer_native(size - 1);
-
return value;
}
@@ -133,31 +122,44 @@ Resizes the array to C<size> elements.
*/
void set_integer_native (INTVAL size) {
- SizeStringData *sd;
-
if (size < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableStringArray: Can't resize!");
- sd = (SizeStringData *)PMC_data(SELF);
- PMC_int_val(SELF) = size;
- if(sd == NULL) {
- /* The parent class makes the assumption that NULL pointers are all
- bits zero, so we might as well too. */
- sd = mem_sys_allocate_zeroed(NEEDED_SIZE(size));
- sd->size = size;
- } else if(size >= sd->size) {
- int i = sd->size;
- sd->size = size < 2*sd->size ? sd->size*2 : size;
- sd = mem_sys_realloc(sd, NEEDED_SIZE(sd->size));
- for(; i < sd->size; i++)
- sd->data[i] = NULL;
- } else {
+ if(!PMC_data(SELF)) {
+ /* empty - used fixed routine */
+ if (size < 8) {
+ SUPER(8);
+ PMC_int_val(SELF) = size;
+ PMC_int_val2(SELF) = 8;
+ }
+ else {
+ SUPER(size);
+ PMC_int_val2(SELF) = size;
+ }
+ }
+ else if (size <= PMC_int_val2(SELF)) {
+ PMC_int_val(SELF) = size;
+ /* we could shrink here if necessary */
return;
}
-
- PMC_data(SELF) = sd;
- PObj_custom_mark_destroy_SETALL(SELF);
+ else {
+ INTVAL i, cur, needed;
+ i = cur = PMC_int_val2(SELF);
+ if (cur < 8192)
+ cur = size < 2 * cur ? 2 * cur : size;
+ else {
+ needed = size - cur;
+ cur += needed + 4096;
+ cur &= ~0xfff;
+ }
+ PMC_data(SELF) = mem_sys_realloc(PMC_data(SELF),
+ cur * sizeof(STRING*));
+ for (; i < cur; i++)
+ ((STRING**)PMC_data(SELF))[i] = NULL;
+ PMC_int_val2(SELF) = cur;
+ PMC_int_val(SELF) = size;
+ }
}
/*
@@ -171,44 +173,10 @@ Creates and returns a copy of the array.
*/
PMC* clone () {
- SizeStringData *sd;
- PMC * dest = pmc_new(INTERP, SELF->vtable->base_type);
-
- if (!PMC_data(SELF))
- return dest;
- PMC_int_val(dest) = PMC_int_val(SELF);
- sd = (SizeStringData *)PMC_data(SELF);
-
- PMC_data(dest) = mem_sys_allocate(NEEDED_SIZE(sd->size));
- mem_sys_memcopy(PMC_data(dest), PMC_data(SELF), NEEDED_SIZE(sd->size));
- PObj_custom_mark_destroy_SETALL(SELF);
- return dest;
- }
-
-/*
-
-=item C<void mark()>
-
-Marks the array as live.
-
-=cut
-
-*/
-
- void mark () {
- SizeStringData *sd;
- int i, end;
- STRING **data;
- if (!PMC_data(SELF))
- return;
- sd = (SizeStringData *)PMC_data(SELF);
- data = sd->data;
- end = PMC_int_val(SELF);
- for(i = 0; i < end; i++) {
- if(data[i]) {
- pobject_lives(INTERP, (PObj *) data[i]);
- }
- }
+ PMC *copy = SUPER();
+ /* copy trimmed extra space */
+ PMC_int_val2(copy) = PMC_int_val(SELF);
+ return copy;
}
}
@@ -226,6 +194,7 @@ F<docs/pdds/pdd17_basic_types.pod>.
Initial version - Matt Fowles 2004-06-11
Changed allocator to double size - Matt Fowles 2004-06-15
Added push_string - Bernhard Schmalhofer 2004-10-17
+moved available size to int_val2 - Matt Fowles 2005-07-22
=cut