7 string *factor_vm::allot_string_internal(cell capacity)
9 string *str = allot<string>(string_size(capacity));
11 str->length = tag_fixnum(capacity);
12 str->hashcode = false_object;
13 str->aux = false_object;
18 /* Allocates memory */
19 void factor_vm::fill_string(string *str_, cell start, cell capacity, cell fill)
21 data_root<string> str(str_,this);
24 memset(&str->data()[start],(u8)fill,capacity - start);
28 if(to_boolean(str->aux))
29 aux = untag<byte_array>(str->aux);
32 aux = allot_uninitialized_array<byte_array>(untag_fixnum(str->length) * 2);
33 str->aux = tag<byte_array>(aux);
34 write_barrier(&str->aux);
37 u8 lo_fill = (u8)((fill & 0x7f) | 0x80);
38 u16 hi_fill = (u16)((fill >> 7) ^ 0x1);
39 memset(&str->data()[start],lo_fill,capacity - start);
40 memset_2(&aux->data<u16>()[start],hi_fill,(capacity - start) * sizeof(u16));
44 /* Allocates memory */
45 string *factor_vm::allot_string(cell capacity, cell fill)
47 data_root<string> str(allot_string_internal(capacity),this);
48 fill_string(str.untagged(),0,capacity,fill);
49 return str.untagged();
52 /* Allocates memory */
53 void factor_vm::primitive_string()
55 cell initial = to_cell(ctx->pop());
56 cell length = unbox_array_size();
57 ctx->push(tag<string>(allot_string(length,initial)));
60 bool factor_vm::reallot_string_in_place_p(string *str, cell capacity)
62 return nursery.contains_p(str)
63 && (!to_boolean(str->aux) || nursery.contains_p(untag<byte_array>(str->aux)))
64 && capacity <= string_capacity(str);
67 /* Allocates memory */
68 string* factor_vm::reallot_string(string *str_, cell capacity)
70 data_root<string> str(str_,this);
72 if(reallot_string_in_place_p(str.untagged(),capacity))
74 str->length = tag_fixnum(capacity);
76 if(to_boolean(str->aux))
78 byte_array *aux = untag<byte_array>(str->aux);
79 aux->capacity = tag_fixnum(capacity * 2);
82 return str.untagged();
86 cell to_copy = string_capacity(str.untagged());
87 if(capacity < to_copy)
90 data_root<string> new_str(allot_string_internal(capacity),this);
92 memcpy(new_str->data(),str->data(),to_copy);
94 if(to_boolean(str->aux))
96 byte_array *new_aux = allot_uninitialized_array<byte_array>(capacity * 2);
97 new_str->aux = tag<byte_array>(new_aux);
98 write_barrier(&new_str->aux);
100 byte_array *aux = untag<byte_array>(str->aux);
101 memcpy(new_aux->data<u16>(),aux->data<u16>(),to_copy * sizeof(u16));
104 fill_string(new_str.untagged(),to_copy,capacity,'\0');
105 return new_str.untagged();
109 /* Allocates memory */
110 void factor_vm::primitive_resize_string()
112 data_root<string> str(ctx->pop(),this);
113 str.untag_check(this);
114 cell capacity = unbox_array_size();
115 ctx->push(tag<string>(reallot_string(str.untagged(),capacity)));
118 void factor_vm::primitive_set_string_nth_fast()
120 string *str = untag<string>(ctx->pop());
121 cell index = untag_fixnum(ctx->pop());
122 cell value = untag_fixnum(ctx->pop());
123 str->data()[index] = (u8)value;