]> gitweb.factorcode.org Git - factor.git/blob - vm/strings.cpp
vm: Add messages about things that can allocate. Fix a gc bug in the primitive for...
[factor.git] / vm / strings.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 /* Allocates memory */
7 string *factor_vm::allot_string_internal(cell capacity)
8 {
9         string *str = allot<string>(string_size(capacity));
10
11         str->length = tag_fixnum(capacity);
12         str->hashcode = false_object;
13         str->aux = false_object;
14
15         return str;
16 }
17
18 /* Allocates memory */
19 void factor_vm::fill_string(string *str_, cell start, cell capacity, cell fill)
20 {
21         data_root<string> str(str_,this);
22
23         if(fill <= 0x7f)
24                 memset(&str->data()[start],(u8)fill,capacity - start);
25         else
26         {
27                 byte_array *aux;
28                 if(to_boolean(str->aux))
29                         aux = untag<byte_array>(str->aux);
30                 else
31                 {
32                         aux = allot_uninitialized_array<byte_array>(untag_fixnum(str->length) * 2);
33                         str->aux = tag<byte_array>(aux);
34                         write_barrier(&str->aux);
35                 }
36
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));
41         }
42 }
43
44 /* Allocates memory */
45 string *factor_vm::allot_string(cell capacity, cell fill)
46 {
47         data_root<string> str(allot_string_internal(capacity),this);
48         fill_string(str.untagged(),0,capacity,fill);
49         return str.untagged();
50 }
51
52 /* Allocates memory */
53 void factor_vm::primitive_string()
54 {
55         cell initial = to_cell(ctx->pop());
56         cell length = unbox_array_size();
57         ctx->push(tag<string>(allot_string(length,initial)));
58 }
59
60 bool factor_vm::reallot_string_in_place_p(string *str, cell capacity)
61 {
62         return nursery.contains_p(str)
63                 && (!to_boolean(str->aux) || nursery.contains_p(untag<byte_array>(str->aux)))
64                 && capacity <= string_capacity(str);
65 }
66
67 /* Allocates memory */
68 string* factor_vm::reallot_string(string *str_, cell capacity)
69 {
70         data_root<string> str(str_,this);
71
72         if(reallot_string_in_place_p(str.untagged(),capacity))
73         {
74                 str->length = tag_fixnum(capacity);
75
76                 if(to_boolean(str->aux))
77                 {
78                         byte_array *aux = untag<byte_array>(str->aux);
79                         aux->capacity = tag_fixnum(capacity * 2);
80                 }
81
82                 return str.untagged();
83         }
84         else
85         {
86                 cell to_copy = string_capacity(str.untagged());
87                 if(capacity < to_copy)
88                         to_copy = capacity;
89
90                 data_root<string> new_str(allot_string_internal(capacity),this);
91
92                 memcpy(new_str->data(),str->data(),to_copy);
93
94                 if(to_boolean(str->aux))
95                 {
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);
99
100                         byte_array *aux = untag<byte_array>(str->aux);
101                         memcpy(new_aux->data<u16>(),aux->data<u16>(),to_copy * sizeof(u16));
102                 }
103
104                 fill_string(new_str.untagged(),to_copy,capacity,'\0');
105                 return new_str.untagged();
106         }
107 }
108
109 /* Allocates memory */
110 void factor_vm::primitive_resize_string()
111 {
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)));
116 }
117
118 void factor_vm::primitive_set_string_nth_fast()
119 {
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;
124 }
125
126 }