VM: replacing the copy assignment operators of the smart pointers

Little more to write, but much easier to see what is going on with
methods rather than assignment operator overloading.
char-rename
Björn Lindqvist 2016-11-23 03:25:07 +01:00
parent 3b3cc151e9
commit 7661ed3b57
8 changed files with 42 additions and 38 deletions

View File

@ -57,13 +57,20 @@ cell factor_vm::std_vector_to_array(std::vector<cell>& elements) {
return objects.value(); return objects.value();
} }
// Allocates memory
void growable_array::reallot_array(cell count) {
array *a_old = elements.untagged();
array *a_new = elements.parent->reallot_array(a_old, count);
elements.set_untagged(a_new);
}
// Allocates memory // Allocates memory
void growable_array::add(cell elt_) { void growable_array::add(cell elt_) {
factor_vm* parent = elements.parent; factor_vm* parent = elements.parent;
data_root<object> elt(elt_, parent); data_root<object> elt(elt_, parent);
if (count == array_capacity(elements.untagged())) if (count == array_capacity(elements.untagged())) {
elements = parent->reallot_array(elements.untagged(), count * 2); reallot_array(2 * count);
}
parent->set_array_nth(elements.untagged(), count++, elt.value()); parent->set_array_nth(elements.untagged(), count++, elt.value());
} }
@ -73,8 +80,7 @@ void growable_array::append(array* elts_) {
data_root<array> elts(elts_, parent); data_root<array> elts(elts_, parent);
cell capacity = array_capacity(elts.untagged()); cell capacity = array_capacity(elts.untagged());
if (count + capacity > array_capacity(elements.untagged())) { if (count + capacity > array_capacity(elements.untagged())) {
elements = reallot_array(2 * (count + capacity));
parent->reallot_array(elements.untagged(), (count + capacity) * 2);
} }
for (cell index = 0; index < capacity; index++) for (cell index = 0; index < capacity; index++)
@ -84,8 +90,7 @@ void growable_array::append(array* elts_) {
// Allocates memory // Allocates memory
void growable_array::trim() { void growable_array::trim() {
factor_vm* parent = elements.parent; reallot_array(count);
elements = parent->reallot_array(elements.untagged(), count);
} }
} }

View File

@ -23,6 +23,7 @@ struct growable_array {
: count(0), : count(0),
elements(parent->allot_array(capacity, false_object), parent) {} elements(parent->allot_array(capacity, false_object), parent) {}
void reallot_array(cell count);
void add(cell elt); void add(cell elt);
void append(array* elts); void append(array* elts);
void trim(); void trim();

View File

@ -797,7 +797,7 @@ void factor_vm::bignum_divide_unsigned_large_denominator(
bignum_destructive_unnormalization(u.untagged(), shift); bignum_destructive_unnormalization(u.untagged(), shift);
} }
q = bignum_trim(q.untagged()); q.set_untagged(bignum_trim(q.untagged()));
*quotient = q.untagged(); *quotient = q.untagged();
} else { } else {
@ -821,7 +821,7 @@ void factor_vm::bignum_divide_unsigned_large_denominator(
} }
} }
u = bignum_trim(u.untagged()); u.set_untagged(bignum_trim(u.untagged()));
if (remainder != NULL) if (remainder != NULL)
*remainder = u.untagged(); *remainder = u.untagged();
} }
@ -996,7 +996,7 @@ void factor_vm::bignum_divide_unsigned_medium_denominator(
(*scan) = qj; (*scan) = qj;
} }
q = bignum_trim(q.untagged()); q.set_untagged(bignum_trim(q.untagged()));
if (remainder != ((bignum**)0)) { if (remainder != ((bignum**)0)) {
if (shift != 0) if (shift != 0)
@ -1179,7 +1179,7 @@ void factor_vm::bignum_divide_unsigned_small_denominator(
bignum_digit_type r = bignum_destructive_scale_down(q.untagged(), denominator); bignum_digit_type r = bignum_destructive_scale_down(q.untagged(), denominator);
q = bignum_trim(q.untagged()); q.set_untagged(bignum_trim(q.untagged()));
if (remainder != ((bignum**)0)) if (remainder != ((bignum**)0))
(*remainder) = bignum_digit_to_bignum(r, r_negative_p); (*remainder) = bignum_digit_to_bignum(r, r_negative_p);
@ -1797,7 +1797,7 @@ bignum* factor_vm::bignum_gcd(bignum* a_, bignum* b_) {
} }
data_root<bignum> e(bignum_trim(a.untagged()), this); data_root<bignum> e(bignum_trim(a.untagged()), this);
data_root<bignum> f(bignum_trim(b.untagged()), this); data_root<bignum> f(bignum_trim(b.untagged()), this);
c = bignum_remainder(e.untagged(), f.untagged()); c.set_untagged(bignum_remainder(e.untagged(), f.untagged()));
if (c.untagged() == BIGNUM_OUT_OF_BAND) { if (c.untagged() == BIGNUM_OUT_OF_BAND) {
return c.untagged(); return c.untagged();
} }

View File

@ -29,11 +29,19 @@ void factor_vm::primitive_resize_byte_array() {
ctx->push(tag<byte_array>(reallot_array(array.untagged(), capacity))); ctx->push(tag<byte_array>(reallot_array(array.untagged(), capacity)));
} }
// Allocates memory
void growable_byte_array::reallot_array(cell count) {
byte_array *ba_old = elements.untagged();
byte_array *ba_new = elements.parent->reallot_array(ba_old, count);
elements.set_untagged(ba_new);
}
// Allocates memory // Allocates memory
void growable_byte_array::grow_bytes(cell len) { void growable_byte_array::grow_bytes(cell len) {
count += len; count += len;
if (count >= array_capacity(elements.untagged())) if (count >= array_capacity(elements.untagged())) {
elements = elements.parent->reallot_array(elements.untagged(), count * 2); reallot_array(2 * count);
}
} }
// Allocates memory // Allocates memory
@ -49,9 +57,9 @@ void growable_byte_array::append_byte_array(cell byte_array_) {
cell len = array_capacity(byte_array.untagged()); cell len = array_capacity(byte_array.untagged());
cell new_size = count + len; cell new_size = count + len;
factor_vm* parent = elements.parent; if (new_size >= array_capacity(elements.untagged())) {
if (new_size >= array_capacity(elements.untagged())) reallot_array(2 * new_size);
elements = parent->reallot_array(elements.untagged(), new_size * 2); }
memcpy(&elements->data<uint8_t>()[count], byte_array->data<uint8_t>(), len); memcpy(&elements->data<uint8_t>()[count], byte_array->data<uint8_t>(), len);
@ -60,8 +68,7 @@ void growable_byte_array::append_byte_array(cell byte_array_) {
// Allocates memory // Allocates memory
void growable_byte_array::trim() { void growable_byte_array::trim() {
factor_vm* parent = elements.parent; reallot_array(count);
elements = parent->reallot_array(elements.untagged(), count);
} }
} }

View File

@ -8,6 +8,7 @@ struct growable_byte_array {
growable_byte_array(factor_vm* parent, cell capacity = 40) growable_byte_array(factor_vm* parent, cell capacity = 40)
: count(0), elements(parent->allot_byte_array(capacity), parent) {} : count(0), elements(parent->allot_byte_array(capacity), parent) {}
void reallot_array(cell count);
void grow_bytes(cell len); void grow_bytes(cell len);
void append_bytes(void* elts, cell len); void append_bytes(void* elts, cell len);
void append_byte_array(cell elts); void append_byte_array(cell elts);

View File

@ -18,15 +18,6 @@ template <typename Type> struct data_root : public tagged<Type> {
push(); push();
} }
const data_root<Type>& operator=(const Type* x) {
tagged<Type>::operator=(x);
return *this;
}
const data_root<Type>& operator=(const cell& x) {
tagged<Type>::operator=(x);
return *this;
}
~data_root() { ~data_root() {
parent->data_roots.pop_back(); parent->data_roots.pop_back();
} }

View File

@ -43,7 +43,7 @@ inline cell quotation_jit::nth(cell index) {
} }
void quotation_jit::init_quotation(cell quot) { void quotation_jit::init_quotation(cell quot) {
elements = untag<quotation>(quot)->array; elements.set_value(untag<quotation>(quot)->array);
} }
bool quotation_jit::fast_if_p(cell i, cell length) { bool quotation_jit::fast_if_p(cell i, cell length) {

View File

@ -32,18 +32,17 @@ template <typename Type> struct tagged {
explicit tagged(cell tagged) : value_(tagged) {} explicit tagged(cell tagged) : value_(tagged) {}
explicit tagged(Type* untagged) : value_(factor::tag(untagged)) {} explicit tagged(Type* untagged) : value_(factor::tag(untagged)) {}
void set_value(const cell ptr) {
value_ = ptr;
}
void set_untagged(const Type *untagged) {
set_value(tag(untagged));
}
Type* operator->() const { return untagged(); } Type* operator->() const { return untagged(); }
cell* operator&() const { return &value_; } cell* operator&() const { return &value_; }
const tagged<Type>& operator=(const Type* x) {
value_ = tag(x);
return *this;
}
const tagged<Type>& operator=(const cell& x) {
value_ = x;
return *this;
}
bool operator==(const tagged<Type>& x) { return value_ == x.value_; } bool operator==(const tagged<Type>& x) { return value_ == x.value_; }
bool operator!=(const tagged<Type>& x) { return value_ != x.value_; } bool operator!=(const tagged<Type>& x) { return value_ != x.value_; }