VM: Refactor byte_arrays to Factor style
parent
1e618e16ee
commit
22eed89484
|
@ -1,76 +1,67 @@
|
||||||
#include "master.hpp"
|
#include "master.hpp"
|
||||||
|
|
||||||
namespace factor
|
namespace factor {
|
||||||
{
|
|
||||||
|
|
||||||
/* Allocates memory */
|
/* 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);
|
||||||
byte_array *array = allot_uninitialized_array<byte_array>(size);
|
memset(array + 1, 0, size);
|
||||||
memset(array + 1,0,size);
|
return array;
|
||||||
return array;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
void factor_vm::primitive_byte_array()
|
void factor_vm::primitive_byte_array() {
|
||||||
{
|
cell size = unbox_array_size();
|
||||||
cell size = unbox_array_size();
|
ctx->push(tag<byte_array>(allot_byte_array(size)));
|
||||||
ctx->push(tag<byte_array>(allot_byte_array(size)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
void factor_vm::primitive_uninitialized_byte_array()
|
void factor_vm::primitive_uninitialized_byte_array() {
|
||||||
{
|
cell size = unbox_array_size();
|
||||||
cell size = unbox_array_size();
|
ctx->push(tag<byte_array>(allot_uninitialized_array<byte_array>(size)));
|
||||||
ctx->push(tag<byte_array>(allot_uninitialized_array<byte_array>(size)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
void factor_vm::primitive_resize_byte_array()
|
void factor_vm::primitive_resize_byte_array() {
|
||||||
{
|
data_root<byte_array> array(ctx->pop(), this);
|
||||||
data_root<byte_array> array(ctx->pop(),this);
|
array.untag_check(this);
|
||||||
array.untag_check(this);
|
cell capacity = unbox_array_size();
|
||||||
cell capacity = unbox_array_size();
|
ctx->push(tag<byte_array>(reallot_array(array.untagged(), capacity)));
|
||||||
ctx->push(tag<byte_array>(reallot_array(array.untagged(),capacity)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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);
|
||||||
elements = elements.parent->reallot_array(elements.untagged(),count * 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* 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;
|
||||||
cell old_count = count;
|
grow_bytes(len);
|
||||||
grow_bytes(len);
|
memcpy(&elements->data<u8>()[old_count], elts, len);
|
||||||
memcpy(&elements->data<u8>()[old_count],elts,len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* 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);
|
||||||
data_root<byte_array> byte_array(byte_array_,elements.parent);
|
|
||||||
|
|
||||||
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;
|
factor_vm* parent = elements.parent;
|
||||||
if(new_size >= array_capacity(elements.untagged()))
|
if (new_size >= array_capacity(elements.untagged()))
|
||||||
elements = parent->reallot_array(elements.untagged(),new_size * 2);
|
elements = parent->reallot_array(elements.untagged(), new_size * 2);
|
||||||
|
|
||||||
memcpy(&elements->data<u8>()[count],byte_array->data<u8>(),len);
|
memcpy(&elements->data<u8>()[count], byte_array->data<u8>(), len);
|
||||||
|
|
||||||
count += len;
|
count += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
void growable_byte_array::trim()
|
void growable_byte_array::trim() {
|
||||||
{
|
factor_vm* parent = elements.parent;
|
||||||
factor_vm *parent = elements.parent;
|
elements = parent->reallot_array(elements.untagged(), count);
|
||||||
elements = parent->reallot_array(elements.untagged(),count);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,25 @@
|
||||||
namespace factor
|
namespace factor {
|
||||||
{
|
|
||||||
|
|
||||||
struct growable_byte_array {
|
struct growable_byte_array {
|
||||||
cell count;
|
cell count;
|
||||||
data_root<byte_array> elements;
|
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 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);
|
||||||
|
|
||||||
void trim();
|
void trim();
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Allocates memory */
|
/* 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));
|
byte_array* data = allot_uninitialized_array<byte_array>(sizeof(Type));
|
||||||
memcpy(data->data<char>(),value,sizeof(Type));
|
memcpy(data->data<char>(), value, sizeof(Type));
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue