dcbdbddd2c77ed92e75e17002ddc5832cf09fa08
[chise/concord.git] / cos-i.h
1 /* Internal definitions of CONCORD Object System
2
3    Copyright (C) 2013 MORIOKA Tomohiko
4    This file is part of the CONCORD Library.
5
6    The CONCORD Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The CONCORD Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the CONCORD Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #ifndef _COS_I_H
22 #define _COS_I_H
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 #if 0
28 }
29 #endif
30
31 #include "sysdep.h"
32 #include "concord.h"
33 #include "cos.h"
34 #include "cos-hash.h"
35
36 #ifndef SIZEOF_COS_INT
37 # define SIZEOF_COS_INT SIZEOF_VOID_P
38 #endif
39
40 #ifndef SIZEOF_INT
41 # define SIZEOF_INT (sizeof (int))
42 #endif
43
44 #ifndef SIZEOF_LONG
45 # define SIZEOF_LONG (sizeof (long))
46 #endif
47
48 #ifndef SIZEOF_LONG_LONG
49 # define SIZEOF_LONG_LONG (sizeof (long long))
50 #endif
51
52 #ifndef COS_INT
53 # if SIZEOF_COS_INT == SIZEOF_LONG
54 #  define COS_INT long
55 # elif SIZEOF_COS_INT == SIZEOF_INT
56 #  define COS_INT int
57 # elif SIZEOF_COS_INT == SIZEOF_LONG_LONG
58 #  define COS_INT long long
59 # else
60 #  error Unable to determine suitable type for COS_INT
61 # endif
62 #endif
63
64 #ifndef COS_UINT
65 # define COS_UINT unsigned COS_INT
66 #endif
67
68 #define BITS_PER_COS_INT (SIZEOF_COS_INT * BITS_PER_CHAR)
69
70
71 enum COS_Type {
72   COS_Type_Entity,
73   COS_Type_Int_Even,
74   COS_Type_Char,
75   COS_Type_Int_Odd
76 };
77
78 #define COS_POINTER_TYPE_P(type) ((type) == COS_Type_Entity)
79
80 #define COS_TYPE_BITS  2
81 #define COS_INT_TYPE_BITS  1
82
83 #define COS_INT_VAL_BITS (BITS_PER_COS_INT - COS_INT_TYPE_BITS)
84 #define COS_VAL_BITS (BITS_PER_COS_INT - COS_TYPE_BITS)
85 #define COS_INT_MAX ((COS_INT) ((1UL << (COS_INT_VAL_BITS - 1)) -1UL))
86 #define COS_INT_MIN (-(COS_INT_MAX) - 1)
87
88
89 typedef struct COS_Object_Header
90 {
91   unsigned char prefix;
92   unsigned char type;
93   COS_INT       reference_count;
94 } COS_Object_Header;
95
96 #define COS_OBJECT_PREFIX_OBJECT          0
97
98 enum COS_Object_Type {
99   COS_Object_Type_NULL,
100   COS_Object_Type_C_String,
101   COS_Object_Type_int,
102   COS_Object_Type_char,
103   COS_Object_Type_String,
104   COS_Object_Type_Symbol,
105   COS_Object_Type_Cons,
106   COS_Object_Type_Container,
107   COS_Object_Type_Sexp,
108   COS_Object_Type_Binary,
109   COS_Object_Type_DS,
110   COS_Object_Type_Genre,
111   COS_Object_Type_Feature,
112   COS_Object_Type_Feature_INDEX,
113   COS_Object_Type_DB_Object
114 };
115
116 #define COS_FAT_OBJECT_TYPE_MIN COS_Object_Type_String
117 #define COS_OBJECT_TYPE_MAX COS_Object_Type_DB_Object
118
119 extern int (*COS_Object_retain_function_table[]) (COS_Object);
120 extern int (*COS_Object_release_function_table[]) (COS_Object);
121
122 struct COS_Object_ent
123 {
124   COS_Object_Header header;
125 };
126
127 COS_Object cos_allocate_object_0 (enum COS_Object_Type type,
128                                   size_t size);
129
130 #define COS_ALLOCATE_OBJECT(type) \
131   ((COS_##type)cos_allocate_object_0(COS_Object_Type_##type, \
132                                      sizeof(COS_##type##_ent)))
133
134 #define COS_OBJECT_INT_P(obj) \
135   ((COS_UINT)(obj) & 1)
136
137 #define COS_OBJECT_CHAR_P(obj) \
138   ((COS_UINT)(obj) & 2)
139
140 #define COS_OBJECT_C_STRING_P(obj) \
141   ((obj != NULL) \
142    && (((unsigned char *)obj)[0] != COS_OBJECT_PREFIX_OBJECT))
143
144 #define COS_OBJECT_P(obj) \
145   ((obj != NULL) \
146    && (!COS_OBJECT_INT_P (obj)) \
147    && (!COS_OBJECT_CHAR_P (obj)) \
148    && (((unsigned char *)obj)[0] == COS_OBJECT_PREFIX_OBJECT))
149
150 #define COS_OBJECT_TYPE_P(obj, TYPE) \
151   (COS_OBJECT_P (obj) \
152    && (((COS_Object)obj)->header.type == COS_Object_Type_##TYPE))
153
154 #define COS_OBJECT_STRING_P(obj) \
155   COS_OBJECT_TYPE_P (obj, String)
156
157 #define COS_OBJECT_SYMBOL_P(obj) \
158   COS_OBJECT_TYPE_P (obj, Symbol)
159
160 #define COS_OBJECT_CONS_P(obj) \
161   COS_OBJECT_TYPE_P (obj, Cons)
162
163 #define COS_OBJECT_DS_P(obj) \
164   COS_OBJECT_TYPE_P (obj, DS)
165
166 #define COS_OBJECT_GENRE_P(obj) \
167   COS_OBJECT_TYPE_P (obj, Genre)
168
169 #define COS_OBJECT_FEATURE_P(obj) \
170   COS_OBJECT_TYPE_P (obj, Feature)
171
172 #define COS_OBJECT_FEATURE_INDEX_P(obj) \
173   COS_OBJECT_TYPE_P (obj, Feature_INDEX)
174
175
176 struct COS_String_ent
177 {
178   COS_Object_Header header;
179
180   size_t size;
181   unsigned char* data;
182 };
183
184 int cos_retain_string (COS_Object obj);
185 int cos_release_string (COS_Object obj);
186
187
188 struct COS_Symbol_ent
189 {
190   COS_Object_Header header;
191
192   COS_String name;
193   COS_object value;
194 };
195
196 COS_Symbol cos_make_symbol (COS_String string);
197
198 int cos_retain_symbol (COS_Object obj);
199 int cos_release_symbol (COS_Object obj);
200
201
202 typedef struct COS_Symbol_Table_ent COS_Symbol_Table_ent;
203 typedef COS_Symbol_Table_ent* COS_Symbol_Table;
204
205 struct COS_Symbol_Table_ent
206 {
207   size_t size;
208   COS_Symbol* data;
209 };
210
211 COS_Symbol_Table cos_make_symbol_table (void);
212
213 int cos_symbol_table_grow (COS_Symbol_Table table);
214
215 void cos_destroy_symbol_table (COS_Symbol_Table table);
216
217 int cos_symbol_table_set (COS_Symbol_Table table, COS_Symbol symbol);
218
219 COS_Symbol
220 cos_symbol_table_intern (COS_Symbol_Table table, COS_object name);
221
222
223 struct COS_Cons_ent
224 {
225   COS_Object_Header header;
226
227   COS_object car;
228   COS_object cdr;
229 };
230
231 #define COS_CAR(obj) \
232   (((COS_Cons)(obj))->car)
233 #define COS_CDR(obj) \
234   (((COS_Cons)(obj))->cdr)
235
236 int cos_retain_cons (COS_Object obj);
237 int cos_release_cons (COS_Object obj);
238
239
240 struct COS_Container_ent
241 {
242   COS_Object_Header header;
243
244   COS_Object type;
245   size_t size;
246   size_t len;
247   size_t offset;
248   COS_Object* data;
249 };
250
251 int cos_retain_container (COS_Object obj);
252 int cos_release_container (COS_Object obj);
253
254
255 struct COS_Sexp_ent
256 {
257   COS_Object_Header header;
258
259   size_t len;
260   char* data;
261 };
262
263 int cos_retain_sexp (COS_Object obj);
264 int cos_release_sexp (COS_Object obj);
265
266
267 struct COS_Binary_ent
268 {
269   COS_Object_Header header;
270
271   size_t len;
272   unsigned char* data;
273 };
274
275 int cos_retain_binary (COS_Object obj);
276 int cos_release_binary (COS_Object obj);
277
278
279 int cos_retain_ds (COS_Object obj);
280 int cos_release_ds (COS_Object obj);
281
282
283 int cos_retain_genre (COS_Object obj);
284 int cos_release_genre (COS_Object obj);
285
286 int concord_close_genre (COS_Genre genre);
287
288
289 struct COS_Feature_ent
290 {
291   COS_Object_Header header;
292   CONCORD_Genre genre;
293   char* name;
294   DB* db;
295   u_int32_t access;
296   COS_Hash_Table value_table;
297   COS_Feature next;
298 };
299
300 int cos_retain_feature (COS_Object obj);
301 int cos_release_feature (COS_Object obj);
302
303 int concord_close_feature (COS_Feature feature);
304
305
306 struct COS_Feature_INDEX_ent
307 {
308   COS_Object_Header header;
309   CONCORD_Genre genre;
310   char *name;
311   DB* db;
312   u_int32_t access;
313   COS_Hash_Table decoding_table;
314   COS_Feature_INDEX next;
315 };
316
317 int cos_retain_index (COS_Object obj);
318 int cos_release_index (COS_Object obj);
319
320 int concord_close_index (COS_Feature_INDEX table);
321
322
323 struct COS_DB_Object_ent
324 {
325   COS_Object_Header header;
326
327   COS_Feature feature;
328   COS_Object id;
329 };
330
331 int cos_retain_db_object (COS_Object obj);
332 int cos_release_db_object (COS_Object obj);
333
334 #if 0
335 {
336 #endif
337 #ifdef __cplusplus
338 }
339 #endif
340
341 #endif /* !_COS_I_H */