]> gitweb.factorcode.org Git - factor.git/blob - vm/types.h
O(1) <sbuf> and new-sequence on byte-arrays (work in progress)
[factor.git] / vm / types.h
1 /* Inline functions */
2 INLINE CELL array_size(CELL size)
3 {
4         return sizeof(F_ARRAY) + size * CELLS;
5 }
6
7 INLINE CELL string_capacity(F_STRING* str)
8 {
9         return untag_fixnum_fast(str->length);
10 }
11
12 INLINE CELL string_size(CELL size)
13 {
14         return sizeof(F_STRING) + size;
15 }
16
17 DEFINE_UNTAG(F_BYTE_ARRAY,BYTE_ARRAY_TYPE,byte_array)
18
19 INLINE CELL byte_array_capacity(F_BYTE_ARRAY *array)
20 {
21         return untag_fixnum_fast(array->capacity);
22 }
23
24 INLINE CELL byte_array_size(CELL size)
25 {
26         return sizeof(F_BYTE_ARRAY) + size;
27 }
28
29 INLINE CELL callstack_size(CELL size)
30 {
31         return sizeof(F_CALLSTACK) + size;
32 }
33
34 DEFINE_UNTAG(F_CALLSTACK,CALLSTACK_TYPE,callstack)
35
36 INLINE CELL tag_boolean(CELL untagged)
37 {
38         return (untagged == false ? F : T);
39 }
40
41 DEFINE_UNTAG(F_ARRAY,ARRAY_TYPE,array)
42
43 #define AREF(array,index) ((CELL)(array) + sizeof(F_ARRAY) + (index) * CELLS)
44 #define UNAREF(array,ptr) (((CELL)(ptr)-(CELL)(array)-sizeof(F_ARRAY)) / CELLS)
45
46 INLINE CELL array_nth(F_ARRAY *array, CELL slot)
47 {
48         return get(AREF(array,slot));
49 }
50
51 INLINE void set_array_nth(F_ARRAY *array, CELL slot, CELL value)
52 {
53         put(AREF(array,slot),value);
54         write_barrier((CELL)array);
55 }
56
57 INLINE CELL array_capacity(F_ARRAY* array)
58 {
59         return array->capacity >> TAG_BITS;
60 }
61
62 #define BREF(byte_array,index) ((CELL)byte_array + sizeof(F_BYTE_ARRAY) + (index))
63 #define SREF(string,index) ((CELL)string + sizeof(F_STRING) + (index))
64
65 INLINE F_STRING* untag_string(CELL tagged)
66 {
67         type_check(STRING_TYPE,tagged);
68         return untag_object(tagged);
69 }
70
71 DEFINE_UNTAG(F_QUOTATION,QUOTATION_TYPE,quotation)
72
73 DEFINE_UNTAG(F_WORD,WORD_TYPE,word)
74
75 INLINE CELL tag_tuple(F_TUPLE *tuple)
76 {
77         return RETAG(tuple,TUPLE_TYPE);
78 }
79
80 INLINE F_TUPLE *untag_tuple(CELL object)
81 {
82         type_check(TUPLE_TYPE,object);
83         return untag_object(object);
84 }
85
86 INLINE CELL tuple_size(F_TUPLE_LAYOUT *layout)
87 {
88         CELL size = untag_fixnum_fast(layout->size);
89         return sizeof(F_TUPLE) + size * CELLS;
90 }
91
92 INLINE CELL tuple_nth(F_TUPLE *tuple, CELL slot)
93 {
94         return get(AREF(tuple,slot));
95 }
96
97 INLINE void set_tuple_nth(F_TUPLE *tuple, CELL slot, CELL value)
98 {
99         put(AREF(tuple,slot),value);
100         write_barrier((CELL)tuple);
101 }
102
103 /* Prototypes */
104 DLLEXPORT void box_boolean(bool value);
105 DLLEXPORT bool to_boolean(CELL value);
106
107 F_ARRAY *allot_array_internal(CELL type, CELL capacity);
108 F_ARRAY *allot_array(CELL type, CELL capacity, CELL fill);
109 F_BYTE_ARRAY *allot_byte_array(CELL size);
110
111 CELL allot_array_1(CELL obj);
112 CELL allot_array_4(CELL v1, CELL v2, CELL v3, CELL v4);
113
114 void primitive_array(void);
115 void primitive_tuple(void);
116 void primitive_tuple_boa(void);
117 void primitive_tuple_layout(void);
118 void primitive_byte_array(void);
119 void primitive_uninitialized_byte_array(void);
120 void primitive_clone(void);
121
122 F_ARRAY *reallot_array(F_ARRAY* array, CELL capacity, CELL fill);
123 F_BYTE_ARRAY *reallot_byte_array(F_BYTE_ARRAY *array, CELL capacity);
124 void primitive_resize_array(void);
125 void primitive_resize_byte_array(void);
126
127 F_STRING* allot_string_internal(CELL capacity);
128 F_STRING* allot_string(CELL capacity, CELL fill);
129 void primitive_uninitialized_string(void);
130 void primitive_string(void);
131 F_STRING *reallot_string(F_STRING *string, CELL capacity, CELL fill);
132 void primitive_resize_string(void);
133
134 F_STRING *memory_to_char_string(const char *string, CELL length);
135 F_STRING *from_char_string(const char *c_string);
136 DLLEXPORT void box_char_string(const char *c_string);
137
138 F_STRING *memory_to_u16_string(const u16 *string, CELL length);
139 F_STRING *from_u16_string(const u16 *c_string);
140 DLLEXPORT void box_u16_string(const u16 *c_string);
141
142 void char_string_to_memory(F_STRING *s, char *string);
143 F_BYTE_ARRAY *string_to_char_alien(F_STRING *s, bool check);
144 char* to_char_string(F_STRING *s, bool check);
145 DLLEXPORT char *unbox_char_string(void);
146
147 void u16_string_to_memory(F_STRING *s, u16 *string);
148 F_BYTE_ARRAY *string_to_u16_alien(F_STRING *s, bool check);
149 u16* to_u16_string(F_STRING *s, bool check);
150 DLLEXPORT u16 *unbox_u16_string(void);
151
152 /* String getters and setters */
153 CELL string_nth(F_STRING* string, CELL index);
154 void set_string_nth(F_STRING* string, CELL index, CELL value);
155
156 void primitive_string_nth(void);
157 void primitive_set_string_nth_slow(void);
158 void primitive_set_string_nth_fast(void);
159
160 F_WORD *allot_word(CELL vocab, CELL name);
161 void primitive_word(void);
162 void primitive_word_xt(void);
163
164 void primitive_wrapper(void);
165
166 /* Macros to simulate a vector in C */
167 #define GROWABLE_ARRAY(result) \
168         CELL result##_count = 0; \
169         CELL result = tag_object(allot_array(ARRAY_TYPE,100,F))
170
171 F_ARRAY *growable_array_add(F_ARRAY *result, CELL elt, CELL *result_count);
172
173 #define GROWABLE_ARRAY_ADD(result,elt) \
174         result = tag_object(growable_array_add(untag_object(result),elt,&result##_count))
175
176 F_ARRAY *growable_array_append(F_ARRAY *result, F_ARRAY *elts, CELL *result_count);
177
178 #define GROWABLE_ARRAY_APPEND(result,elts) \
179         result = tag_object(growable_array_append(untag_object(result),elts,&result##_count))
180
181 #define GROWABLE_ARRAY_TRIM(result) \
182         result = tag_object(reallot_array(untag_object(result),result##_count,F))
183
184 /* Macros to simulate a byte vector in C */
185 #define GROWABLE_BYTE_ARRAY(result) \
186         CELL result##_count = 0; \
187         CELL result = tag_object(allot_byte_array(100))
188
189 F_ARRAY *growable_byte_array_append(F_BYTE_ARRAY *result, void *elts, CELL len, CELL *result_count);
190
191 #define GROWABLE_BYTE_ARRAY_APPEND(result,elts,len) \
192         result = tag_object(growable_byte_array_append(untag_object(result),elts,len,&result##_count))
193
194 #define GROWABLE_BYTE_ARRAY_TRIM(result) \
195         result = tag_object(reallot_byte_array(untag_object(result),result##_count))