VM: Refactor byte_arrays to Factor style

db4
Erik Charlebois 2013-05-11 21:46:08 -04:00
parent 1e618e16ee
commit 22eed89484
2 changed files with 50 additions and 59 deletions

View File

@ -1,33 +1,28 @@
#include "master.hpp"
namespace factor
{
namespace factor {
/* Allocates memory */
byte_array *factor_vm::allot_byte_array(cell size)
{
byte_array* factor_vm::allot_byte_array(cell size) {
byte_array* array = allot_uninitialized_array<byte_array>(size);
memset(array + 1, 0, size);
return array;
}
/* Allocates memory */
void factor_vm::primitive_byte_array()
{
void factor_vm::primitive_byte_array() {
cell size = unbox_array_size();
ctx->push(tag<byte_array>(allot_byte_array(size)));
}
/* Allocates memory */
void factor_vm::primitive_uninitialized_byte_array()
{
void factor_vm::primitive_uninitialized_byte_array() {
cell size = unbox_array_size();
ctx->push(tag<byte_array>(allot_uninitialized_array<byte_array>(size)));
}
/* Allocates memory */
void factor_vm::primitive_resize_byte_array()
{
void factor_vm::primitive_resize_byte_array() {
data_root<byte_array> array(ctx->pop(), this);
array.untag_check(this);
cell capacity = unbox_array_size();
@ -35,24 +30,21 @@ void factor_vm::primitive_resize_byte_array()
}
/* Allocates memory */
void growable_byte_array::grow_bytes(cell len)
{
void growable_byte_array::grow_bytes(cell len) {
count += len;
if (count >= array_capacity(elements.untagged()))
elements = elements.parent->reallot_array(elements.untagged(), count * 2);
}
/* Allocates memory */
void growable_byte_array::append_bytes(void *elts, cell len)
{
void growable_byte_array::append_bytes(void* elts, cell len) {
cell old_count = count;
grow_bytes(len);
memcpy(&elements->data<u8>()[old_count], elts, len);
}
/* Allocates memory */
void growable_byte_array::append_byte_array(cell byte_array_)
{
void growable_byte_array::append_byte_array(cell byte_array_) {
data_root<byte_array> byte_array(byte_array_, elements.parent);
cell len = array_capacity(byte_array.untagged());
@ -67,8 +59,7 @@ void growable_byte_array::append_byte_array(cell byte_array_)
}
/* Allocates memory */
void growable_byte_array::trim()
{
void growable_byte_array::trim() {
factor_vm* parent = elements.parent;
elements = parent->reallot_array(elements.untagged(), count);
}

View File

@ -1,11 +1,11 @@
namespace factor
{
namespace factor {
struct growable_byte_array {
cell count;
data_root<byte_array> elements;
explicit growable_byte_array(factor_vm *parent,cell capacity = 40) : count(0), elements(parent->allot_byte_array(capacity),parent) { }
explicit growable_byte_array(factor_vm* parent, cell capacity = 40)
: count(0), elements(parent->allot_byte_array(capacity), parent) {}
void grow_bytes(cell len);
void append_bytes(void* elts, cell len);
@ -15,8 +15,8 @@ struct growable_byte_array {
};
/* Allocates memory */
template<typename Type> byte_array *factor_vm::byte_array_from_value(Type *value)
{
template <typename Type>
byte_array* factor_vm::byte_array_from_value(Type* value) {
byte_array* data = allot_uninitialized_array<byte_array>(sizeof(Type));
memcpy(data->data<char>(), value, sizeof(Type));
return data;