factor/vm/arrays.cpp

83 lines
2.1 KiB
C++
Raw Normal View History

2009-05-02 05:04:19 -04:00
#include "master.hpp"
/* make a new array with an initial element */
2009-05-02 10:19:09 -04:00
F_ARRAY *allot_array(CELL capacity, CELL fill_)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
gc_root<F_OBJECT> fill(fill_);
gc_root<F_ARRAY> array(allot_array_internal<F_ARRAY>(capacity));
if(fill.value() == tag_fixnum(0))
memset((void*)AREF(array.untagged(),0),'\0',capacity * CELLS);
2009-05-02 05:04:19 -04:00
else
{
/* No need for write barrier here. Either the object is in
the nursery, or it was allocated directly in tenured space
and the write barrier is already hit for us in that case. */
CELL i;
for(i = 0; i < capacity; i++)
2009-05-02 10:19:09 -04:00
put(AREF(array.untagged(),i),fill.value());
2009-05-02 05:04:19 -04:00
}
2009-05-02 10:19:09 -04:00
return array.untagged();
2009-05-02 05:04:19 -04:00
}
/* push a new array on the stack */
void primitive_array(void)
{
CELL initial = dpop();
CELL size = unbox_array_size();
2009-05-02 21:37:18 -04:00
dpush(tag<F_ARRAY>(allot_array(size,initial)));
2009-05-02 05:04:19 -04:00
}
2009-05-02 10:19:09 -04:00
CELL allot_array_1(CELL obj_)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
gc_root<F_OBJECT> obj(obj_);
gc_root<F_ARRAY> a(allot_array_internal<F_ARRAY>(1));
set_array_nth(a.untagged(),0,obj.value());
return a.value();
2009-05-02 05:04:19 -04:00
}
2009-05-02 10:19:09 -04:00
CELL allot_array_2(CELL v1_, CELL v2_)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
gc_root<F_OBJECT> v1(v1_);
gc_root<F_OBJECT> v2(v2_);
gc_root<F_ARRAY> a(allot_array_internal<F_ARRAY>(2));
set_array_nth(a.untagged(),0,v1.value());
set_array_nth(a.untagged(),1,v2.value());
return a.value();
2009-05-02 05:04:19 -04:00
}
2009-05-02 10:19:09 -04:00
CELL allot_array_4(CELL v1_, CELL v2_, CELL v3_, CELL v4_)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
gc_root<F_OBJECT> v1(v1_);
gc_root<F_OBJECT> v2(v2_);
gc_root<F_OBJECT> v3(v3_);
gc_root<F_OBJECT> v4(v4_);
gc_root<F_ARRAY> a(allot_array_internal<F_ARRAY>(4));
set_array_nth(a.untagged(),0,v1.value());
set_array_nth(a.untagged(),1,v2.value());
set_array_nth(a.untagged(),2,v3.value());
set_array_nth(a.untagged(),3,v4.value());
return a.value();
2009-05-02 05:04:19 -04:00
}
void primitive_resize_array(void)
{
2009-05-02 21:37:18 -04:00
F_ARRAY* array = untag_check<F_ARRAY>(dpop());
2009-05-02 05:04:19 -04:00
CELL capacity = unbox_array_size();
2009-05-02 21:37:18 -04:00
dpush(tag<F_ARRAY>(reallot_array(array,capacity)));
2009-05-02 05:04:19 -04:00
}
2009-05-02 10:19:09 -04:00
void growable_array::add(CELL elt_)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
gc_root<F_OBJECT> elt(elt_);
if(count == array_capacity(array.untagged()))
array = reallot_array(array.untagged(),count * 2);
2009-05-02 05:04:19 -04:00
2009-05-02 10:19:09 -04:00
set_array_nth(array.untagged(),count++,elt.value());
2009-05-02 05:04:19 -04:00
}
2009-05-02 10:19:09 -04:00
void growable_array::trim()
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
array = reallot_array(array.untagged(),count);
2009-05-02 05:04:19 -04:00
}