Revert "lists: Add list literal doc example."
[factor.git] / vm / byte_arrays.cpp
blob4c83a9de7464ee8feb9d52f8d975da9ee0d44902
1 #include "master.hpp"
3 namespace factor {
5 // Allocates memory
6 byte_array* factor_vm::allot_byte_array(cell size) {
7 byte_array* array = allot_uninitialized_array<byte_array>(size);
8 memset(array + 1, 0, size);
9 return array;
12 // Allocates memory
13 void factor_vm::primitive_byte_array() {
14 cell size = unbox_array_size();
15 ctx->push(tag<byte_array>(allot_byte_array(size)));
18 // Allocates memory
19 void factor_vm::primitive_uninitialized_byte_array() {
20 cell size = unbox_array_size();
21 ctx->push(tag<byte_array>(allot_uninitialized_array<byte_array>(size)));
24 // Allocates memory
25 void factor_vm::primitive_resize_byte_array() {
26 data_root<byte_array> array(ctx->pop(), this);
27 check_tagged(array);
28 cell capacity = unbox_array_size();
29 ctx->push(tag<byte_array>(reallot_array(array.untagged(), capacity)));
32 // Allocates memory
33 void growable_byte_array::reallot_array(cell count) {
34 byte_array *ba_old = elements.untagged();
35 byte_array *ba_new = elements.parent->reallot_array(ba_old, count);
36 elements.set_untagged(ba_new);
39 // Allocates memory
40 void growable_byte_array::grow_bytes(cell len) {
41 count += len;
42 if (count >= array_capacity(elements.untagged())) {
43 reallot_array(2 * count);
47 // Allocates memory
48 void growable_byte_array::append_bytes(void* elts, cell len) {
49 cell old_count = count;
50 grow_bytes(len);
51 memcpy(&elements->data<uint8_t>()[old_count], elts, len);
54 // Allocates memory
55 void growable_byte_array::append_byte_array(cell byte_array_) {
56 data_root<byte_array> byte_array(byte_array_, elements.parent);
58 cell len = array_capacity(byte_array.untagged());
59 cell new_size = count + len;
60 if (new_size >= array_capacity(elements.untagged())) {
61 reallot_array(2 * new_size);
64 memcpy(&elements->data<uint8_t>()[count], byte_array->data<uint8_t>(), len);
66 count += len;
69 // Allocates memory
70 void growable_byte_array::trim() {
71 reallot_array(count);