Uploading it here
Index: classes/resizablepmcarray.pmc
===================================================================
--- classes/resizablepmcarray.pmc	(revision 8687)
+++ classes/resizablepmcarray.pmc	(working copy)
@@ -506,7 +506,7 @@
 
     PMC* clone () {
         PMC *copy = SUPER();
-
+        /* copy trimmed extra space */
         PMC_int_val2(copy) = PMC_int_val(SELF);
         return copy;
     }
Index: classes/resizableintegerarray.pmc
===================================================================
--- classes/resizableintegerarray.pmc	(revision 8687)
+++ classes/resizableintegerarray.pmc	(working copy)
@@ -21,11 +21,6 @@
 
 #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 @@
 */
 
     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 @@
 */
 
     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 @@
 */
 
     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 @@
     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 @@
 */
 
     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 @@
 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
 
Index: classes/resizablefloatarray.pmc
===================================================================
--- classes/resizablefloatarray.pmc	(revision 8687)
+++ classes/resizablefloatarray.pmc	(working copy)
@@ -22,12 +22,6 @@
 
 #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 @@
 */
 
     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 @@
 */
 
     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 @@
 */
 
     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 @@
 */
 
     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 @@
 */
 
     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 @@
     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 @@
 
 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
 
Index: classes/resizablestringarray.pmc
===================================================================
--- classes/resizablestringarray.pmc	(revision 8687)
+++ classes/resizablestringarray.pmc	(working copy)
@@ -24,12 +24,6 @@
 
 #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 @@
 */
 
     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 @@
 */
 
     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 @@
     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 @@
 */
 
     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,46 +173,12 @@
 */
 
     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;
+        PMC *copy = SUPER();
+        /* copy trimmed extra space */
+        PMC_int_val2(copy) = PMC_int_val(SELF);
+        return copy;
     }
 
-/*
-
-=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]);
-            }
-        }
-    }
-
 }
 
 /*
@@ -226,6 +194,7 @@
 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
 
Index: classes/resizablebooleanarray.pmc
===================================================================
--- classes/resizablebooleanarray.pmc	(revision 8687)
+++ classes/resizablebooleanarray.pmc	(working copy)
@@ -8,10 +8,10 @@
 
 =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 @@
 */
 
     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 @@
 */
 
     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);
+        }
+        else if (size <= PMC_int_val2(SELF)) {
             PMC_int_val(SELF) = size;
-        } else {
-            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 @@
 
 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
 

Reply via email to