Index: src/pmc/resizableintegerarray.pmc =================================================================== --- src/pmc/resizableintegerarray.pmc (revision 41731) +++ src/pmc/resizableintegerarray.pmc (working copy) @@ -84,12 +84,14 @@ VTABLE void set_integer_native(INTVAL size) { INTVAL *int_array; + INTVAL cur_size; INTVAL resize_threshold; if (size < 0) Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, "ResizableIntegerArray: Can't resize!"); + GET_ATTR_size(INTERP, SELF, cur_size); GET_ATTR_int_array(INTERP, SELF, int_array); GET_ATTR_resize_threshold(INTERP, SELF, resize_threshold); if (!int_array) { @@ -103,15 +105,14 @@ SUPER(size); SET_ATTR_resize_threshold(INTERP, SELF, size); } + GET_ATTR_int_array(INTERP, SELF, int_array); } else if (size <= resize_threshold) { /* we could shrink here if necessary */ SET_ATTR_size(INTERP, SELF, size); - return; } else { INTVAL cur = resize_threshold; - INTVAL *int_array; if (cur < 8192) cur = size < 2 * cur ? 2 * cur : size; @@ -121,12 +122,19 @@ cur &= ~0xfff; } - GET_ATTR_int_array(INTERP, SELF, int_array); int_array = (INTVAL*) mem_sys_realloc((void*) int_array, cur * sizeof (INTVAL)); SET_ATTR_int_array(INTERP, SELF, int_array); SET_ATTR_size(INTERP, SELF, size); SET_ATTR_resize_threshold(INTERP, SELF, cur); } + + if ( size > cur_size ) { + if ( size == cur_size+1 ) + int_array[cur_size] = 0; // optimize for most common case (push) + else + mem_sys_memset(int_array + cur_size, 0, (size - cur_size) * sizeof (INTVAL)); + } + } /* Index: include/parrot/memory.h =================================================================== --- include/parrot/memory.h (revision 41731) +++ include/parrot/memory.h (working copy) @@ -33,6 +33,7 @@ #define mem_allocate_new_stack() NULL #define mem_sys_memcopy memcpy #define mem_sys_memmove memmove +#define mem_sys_memset memset #define mem_allocate_typed(type) (type *)mem_sys_allocate(sizeof (type)) #define mem_allocate_n_typed(n, type) (type *)mem_sys_allocate((n) * sizeof(type)) Index: t/pmc/resizableintegerarray.t =================================================================== --- t/pmc/resizableintegerarray.t (revision 41731) +++ t/pmc/resizableintegerarray.t (working copy) @@ -1,6 +1,6 @@ #! parrot # Copyright © 2001-2007, Parrot Foundation. -# $Id: resizableintegerarray.t 38718 2009-05-12 16:48:28Z kurahaupo $ +# $Id$ =head1 NAME @@ -38,11 +38,13 @@ * Doesn't change array size * Multiple concurrent iterators don't interfere + * Lengthening zeroes elements if previously allocated space + =cut .sub main :main .include 'test_more.pir' - plan(41) + plan(42) test_does_interfaces() # 4 tests @@ -54,6 +56,7 @@ test_cant_get_negative() # 1 test test_set_beyond_end() # 2 test test_get_beyond_end() # 3 tests + test_zero_after_resize() # 1 test test_conversion() # 6 tests test_conversion_overflow() # 1 test @@ -225,6 +228,16 @@ is( $I0, 1, '... and should not extend array' ) .end +.sub test_zero_after_resize + $P0 = new ['ResizableIntegerArray'] + $P0 = 10 + $P0[7] = 23 + $P0 = 5 + $P0[9] = 0 + $I0 = $P0[7] + is( $I0, 0, 'Latent elements are zeroed when array is shrunk and re-stretched' ) +.end + .sub test_conversion $P0 = new ['ResizableIntegerArray'] $P0 = 6