]> gitweb.factorcode.org Git - factor.git/blob - vm/types.h
Use kqueue on Mac OS X instead of select
[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_clone(void);
120
121 F_ARRAY *reallot_array(F_ARRAY* array, CELL capacity);
122 F_BYTE_ARRAY *reallot_byte_array(F_BYTE_ARRAY *array, CELL capacity);
123 void primitive_resize_array(void);
124 void primitive_resize_byte_array(void);
125
126 F_STRING* allot_string_internal(CELL capacity);
127 F_STRING* allot_string(CELL capacity, CELL fill);
128 void primitive_string(void);
129 F_STRING *reallot_string(F_STRING *string, CELL capacity);
130 void primitive_resize_string(void);
131
132 F_STRING *memory_to_char_string(const char *string, CELL length);
133 F_STRING *from_char_string(const char *c_string);
134 DLLEXPORT void box_char_string(const char *c_string);
135
136 F_STRING *memory_to_u16_string(const u16 *string, CELL length);
137 F_STRING *from_u16_string(const u16 *c_string);
138 DLLEXPORT void box_u16_string(const u16 *c_string);
139
140 void char_string_to_memory(F_STRING *s, char *string);
141 F_BYTE_ARRAY *string_to_char_alien(F_STRING *s, bool check);
142 char* to_char_string(F_STRING *s, bool check);
143 DLLEXPORT char *unbox_char_string(void);
144
145 void u16_string_to_memory(F_STRING *s, u16 *string);
146 F_BYTE_ARRAY *string_to_u16_alien(F_STRING *s, bool check);
147 u16* to_u16_string(F_STRING *s, bool check);
148 DLLEXPORT u16 *unbox_u16_string(void);
149
150 /* String getters and setters */
151 CELL string_nth(F_STRING* string, CELL index);
152 void set_string_nth(F_STRING* string, CELL index, CELL value);
153
154 void primitive_string_nth(void);
155 void primitive_set_string_nth_slow(void);
156 void primitive_set_string_nth_fast(void);
157
158 F_WORD *allot_word(CELL vocab, CELL name);
159 void primitive_word(void);
160 void primitive_word_xt(void);
161
162 void primitive_wrapper(void);
163
164 /* Macros to simulate a vector in C */
165 #define GROWABLE_ARRAY(result) \
166         CELL result##_count = 0; \
167         CELL result = tag_object(allot_array(ARRAY_TYPE,100,F))
168
169 F_ARRAY *growable_array_add(F_ARRAY *result, CELL elt, CELL *result_count);
170
171 #define GROWABLE_ARRAY_ADD(result,elt) \
172         result = tag_object(growable_array_add(untag_object(result),elt,&result##_count))
173
174 F_ARRAY *growable_array_append(F_ARRAY *result, F_ARRAY *elts, CELL *result_count);
175
176 #define GROWABLE_ARRAY_APPEND(result,elts) \
177         result = tag_object(growable_array_append(untag_object(result),elts,&result##_count))
178
179 #define GROWABLE_ARRAY_TRIM(result) \
180         result = tag_object(reallot_array(untag_object(result),result##_count))
181
182 /* Macros to simulate a byte vector in C */
183 #define GROWABLE_BYTE_ARRAY(result) \
184         CELL result##_count = 0; \
185         CELL result = tag_object(allot_byte_array(100))
186
187 F_ARRAY *growable_byte_array_append(F_BYTE_ARRAY *result, void *elts, CELL len, CELL *result_count);
188
189 #define GROWABLE_BYTE_ARRAY_APPEND(result,elts,len) \
190         result = tag_object(growable_byte_array_append(untag_object(result),elts,len,&result##_count))
191
192 #define GROWABLE_BYTE_ARRAY_TRIM(result) \
193         result = tag_object(reallot_byte_array(untag_object(result),result##_count))