factor/native/array.c

116 lines
2.5 KiB
C
Raw Normal View History

2004-07-16 02:26:21 -04:00
#include "factor.h"
/* the array is full of undefined data, and must be correctly filled before the
next GC. */
2005-11-04 22:50:26 -05:00
F_ARRAY* allot_array(CELL type, F_FIXNUM capacity)
2004-07-16 02:26:21 -04:00
{
F_ARRAY *array;
if(capacity < 0)
2006-02-07 19:09:46 -05:00
general_error(ERROR_NEGATIVE_ARRAY_SIZE,tag_integer(capacity),true);
2005-06-16 18:50:49 -04:00
array = allot_object(type,array_size(capacity));
2005-01-27 20:06:10 -05:00
array->capacity = tag_fixnum(capacity);
2004-07-16 02:26:21 -04:00
return array;
}
/* make a new array with an initial element */
2005-11-04 22:50:26 -05:00
F_ARRAY* array(CELL type, F_FIXNUM capacity, CELL fill)
2004-07-16 02:26:21 -04:00
{
2005-12-24 18:29:31 -05:00
int i;
F_ARRAY* array = allot_array(type, capacity);
2004-07-16 02:26:21 -04:00
for(i = 0; i < capacity; i++)
put(AREF(array,i),fill);
return array;
}
/* push a new array on the stack */
2005-01-27 20:06:10 -05:00
void primitive_array(void)
{
2005-12-24 23:01:49 -05:00
CELL initial;
F_FIXNUM size;
maybe_gc(0);
initial = dpop();
size = to_fixnum(dpop());
2005-12-24 18:29:31 -05:00
dpush(tag_object(array(ARRAY_TYPE,size,initial)));
}
/* push a new tuple on the stack */
void primitive_tuple(void)
{
2005-11-04 22:50:26 -05:00
F_FIXNUM size = to_fixnum(dpop());
2005-06-16 18:50:49 -04:00
maybe_gc(array_size(size));
dpush(tag_object(array(TUPLE_TYPE,size,F)));
2005-01-27 20:06:10 -05:00
}
/* push a new byte on the stack */
2005-04-09 18:30:46 -04:00
void primitive_byte_array(void)
{
2005-11-04 22:50:26 -05:00
F_FIXNUM size = to_fixnum(dpop());
2005-06-16 18:50:49 -04:00
maybe_gc(array_size(size));
2005-12-25 17:46:21 -05:00
F_FIXNUM byte_size = (size + sizeof(CELL) - 1) / sizeof(CELL);
dpush(tag_object(array(BYTE_ARRAY_TYPE,byte_size,0)));
2005-04-09 18:30:46 -04:00
}
2005-11-04 22:50:26 -05:00
F_ARRAY* resize_array(F_ARRAY* array, F_FIXNUM capacity, CELL fill)
2004-07-16 02:26:21 -04:00
{
2005-06-10 16:08:00 -04:00
int i;
F_ARRAY* new_array;
CELL to_copy = array_capacity(array);
if(capacity < to_copy)
to_copy = capacity;
2004-12-24 02:52:02 -05:00
new_array = allot_array(untag_header(array->header),capacity);
2005-06-10 16:08:00 -04:00
memcpy(new_array + 1,array + 1,to_copy * CELLS);
for(i = to_copy; i < capacity; i++)
2004-07-16 02:26:21 -04:00
put(AREF(new_array,i),fill);
2005-06-10 16:08:00 -04:00
2004-07-16 02:26:21 -04:00
return new_array;
}
2005-06-10 16:08:00 -04:00
void primitive_resize_array(void)
2004-12-24 02:52:02 -05:00
{
F_ARRAY* array;
2005-11-04 22:50:26 -05:00
F_FIXNUM capacity = to_fixnum(dpeek2());
maybe_gc(array_size(capacity));
array = untag_array(dpop());
drepl(tag_object(resize_array(array,capacity,F)));
2004-08-24 23:46:55 -04:00
}
void primitive_array_to_tuple(void)
{
CELL array = dpeek();
type_check(ARRAY_TYPE,array);
array = clone(array);
put(SLOT(UNTAG(array),0),tag_header(TUPLE_TYPE));
drepl(array);
}
void primitive_tuple_to_array(void)
{
CELL tuple = dpeek();
type_check(TUPLE_TYPE,tuple);
tuple = clone(tuple);
put(SLOT(UNTAG(tuple),0),tag_header(ARRAY_TYPE));
drepl(tuple);
}
/* image loading */
void fixup_array(F_ARRAY* array)
2004-07-16 02:26:21 -04:00
{
2005-03-03 20:43:55 -05:00
int i = 0; CELL capacity = array_capacity(array);
2005-01-27 20:06:10 -05:00
for(i = 0; i < capacity; i++)
2004-12-25 02:55:03 -05:00
data_fixup((void*)AREF(array,i));
2004-07-16 02:26:21 -04:00
}
/* GC */
void collect_array(F_ARRAY* array)
2004-07-16 02:26:21 -04:00
{
2005-03-03 20:43:55 -05:00
int i = 0; CELL capacity = array_capacity(array);
2005-01-27 20:06:10 -05:00
for(i = 0; i < capacity; i++)
2005-02-19 23:25:21 -05:00
copy_handle((void*)AREF(array,i));
2004-07-16 02:26:21 -04:00
}