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 void factor_vm::primitive_string()
54 cell initial = to_cell(ctx->pop());
55 cell length = unbox_array_size();
56 ctx->push(tag<string>(allot_string(length,initial)));
59 bool factor_vm::reallot_string_in_place_p(string *str, cell capacity)
61 return nursery.contains_p(str)
62 && (!to_boolean(str->aux) || nursery.contains_p(untag<byte_array>(str->aux)))
63 && capacity <= string_capacity(str);
66 string* factor_vm::reallot_string(string *str_, cell capacity)
68 data_root<string> str(str_,this);
70 if(reallot_string_in_place_p(str.untagged(),capacity))
72 str->length = tag_fixnum(capacity);
74 if(to_boolean(str->aux))
76 byte_array *aux = untag<byte_array>(str->aux);
77 aux->capacity = tag_fixnum(capacity * 2);
80 return str.untagged();
84 cell to_copy = string_capacity(str.untagged());
85 if(capacity < to_copy)
88 data_root<string> new_str(allot_string_internal(capacity),this);
90 memcpy(new_str->data(),str->data(),to_copy);
92 if(to_boolean(str->aux))
94 byte_array *new_aux = allot_uninitialized_array<byte_array>(capacity * 2);
95 new_str->aux = tag<byte_array>(new_aux);
96 write_barrier(&new_str->aux);
98 byte_array *aux = untag<byte_array>(str->aux);
99 memcpy(new_aux->data<u16>(),aux->data<u16>(),to_copy * sizeof(u16));
102 fill_string(new_str.untagged(),to_copy,capacity,'\0');
103 return new_str.untagged();
107 void factor_vm::primitive_resize_string()
109 data_root<string> str(ctx->pop(),this);
110 str.untag_check(this);
111 cell capacity = unbox_array_size();
112 ctx->push(tag<string>(reallot_string(str.untagged(),capacity)));
115 void factor_vm::primitive_set_string_nth_fast()
117 string *str = untag<string>(ctx->pop());
118 cell index = untag_fixnum(ctx->pop());
119 cell value = untag_fixnum(ctx->pop());
120 str->data()[index] = (u8)value;