Properly activate the pthread support on GNU/Hurd.
[heimdal.git] / base / heimbase.h
blob7e99bd087d99e037d9e274a8156e4db5dfe4b745
1 /*
2 * Copyright (c) 2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #ifndef HEIM_BASE_H
37 #define HEIM_BASE_H 1
39 #include <sys/types.h>
40 #include <krb5-types.h>
41 #include <stdarg.h>
42 #include <stdbool.h>
44 typedef void * heim_object_t;
45 typedef unsigned int heim_tid_t;
46 typedef heim_object_t heim_bool_t;
47 typedef heim_object_t heim_null_t;
48 #define HEIM_BASE_ONCE_INIT 0
49 typedef long heim_base_once_t; /* XXX arch dependant */
51 #if !defined(__has_extension)
52 #define __has_extension(x) 0
53 #endif
55 #define HEIM_REQUIRE_GNUC(m,n,p) \
56 (((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__) >= \
57 (((m) * 10000) + ((n) * 100) + (p)))
60 #if __has_extension(__builtin_expect) || HEIM_REQUIRE_GNUC(3,0,0)
61 #define heim_builtin_expect(_op,_res) __builtin_expect(_op,_res)
62 #else
63 #define heim_builtin_expect(_op,_res) (_op)
64 #endif
67 void * heim_retain(heim_object_t);
68 void heim_release(heim_object_t);
70 void heim_show(heim_object_t);
72 typedef void (*heim_type_dealloc)(void *);
74 void *
75 heim_alloc(size_t size, const char *name, heim_type_dealloc dealloc);
77 heim_tid_t
78 heim_get_tid(heim_object_t object);
80 int
81 heim_cmp(heim_object_t a, heim_object_t b);
83 unsigned long
84 heim_get_hash(heim_object_t ptr);
86 void
87 heim_base_once_f(heim_base_once_t *, void *, void (*)(void *));
89 void
90 heim_abort(const char *fmt, ...)
91 HEIMDAL_NORETURN_ATTRIBUTE
92 HEIMDAL_PRINTF_ATTRIBUTE((printf, 1, 2));
94 void
95 heim_abortv(const char *fmt, va_list ap)
96 HEIMDAL_NORETURN_ATTRIBUTE
97 HEIMDAL_PRINTF_ATTRIBUTE((printf, 1, 0));
99 #define heim_assert(e,t) \
100 (heim_builtin_expect(!(e), 0) ? heim_abort(t ":" #e) : (void)0)
106 heim_null_t
107 heim_null_create(void);
109 heim_bool_t
110 heim_bool_create(int);
113 heim_bool_val(heim_bool_t);
116 * Array
119 typedef struct heim_array_data *heim_array_t;
121 heim_array_t heim_array_create(void);
122 heim_tid_t heim_array_get_type_id(void);
124 typedef void (*heim_array_iterator_f_t)(heim_object_t, void *);
126 int heim_array_append_value(heim_array_t, heim_object_t);
127 int heim_array_insert_value(heim_array_t, size_t idx, heim_object_t);
128 void heim_array_iterate_f(heim_array_t, void *, heim_array_iterator_f_t);
129 void heim_array_iterate_reverse_f(heim_array_t, void *, heim_array_iterator_f_t);
130 #ifdef __BLOCKS__
131 void heim_array_iterate(heim_array_t, void (^)(heim_object_t));
132 void heim_array_iterate_reverse(heim_array_t, void (^)(heim_object_t));
133 #endif
134 size_t heim_array_get_length(heim_array_t);
135 heim_object_t
136 heim_array_get_value(heim_array_t, size_t);
137 heim_object_t
138 heim_array_copy_value(heim_array_t, size_t);
139 void heim_array_set_value(heim_array_t, size_t, heim_object_t);
140 void heim_array_delete_value(heim_array_t, size_t);
141 #ifdef __BLOCKS__
142 void heim_array_filter(heim_array_t, int (^)(heim_object_t));
143 #endif
146 * Dict
149 typedef struct heim_dict_data *heim_dict_t;
151 heim_dict_t heim_dict_create(size_t size);
152 heim_tid_t heim_dict_get_type_id(void);
154 typedef void (*heim_dict_iterator_f_t)(heim_object_t, heim_object_t, void *);
156 int heim_dict_set_value(heim_dict_t, heim_object_t, heim_object_t);
157 void heim_dict_iterate_f(heim_dict_t, void *, heim_dict_iterator_f_t);
158 #ifdef __BLOCKS__
159 void heim_dict_iterate(heim_dict_t, void (^)(heim_object_t, heim_object_t));
160 #endif
162 heim_object_t
163 heim_dict_get_value(heim_dict_t, heim_object_t);
164 heim_object_t
165 heim_dict_copy_value(heim_dict_t, heim_object_t);
166 void heim_dict_delete_key(heim_dict_t, heim_object_t);
169 * String
172 typedef struct heim_string_data *heim_string_t;
173 typedef void (*heim_string_free_f_t)(void *);
175 heim_string_t heim_string_create(const char *);
176 heim_string_t heim_string_ref_create(const char *, heim_string_free_f_t);
177 heim_string_t heim_string_create_with_bytes(const void *, size_t);
178 heim_string_t heim_string_ref_create_with_bytes(const void *, size_t,
179 heim_string_free_f_t);
180 heim_string_t heim_string_create_with_format(const char *, ...);
181 heim_tid_t heim_string_get_type_id(void);
182 const char * heim_string_get_utf8(heim_string_t);
184 #define HSTR(_str) (__heim_string_constant("" _str ""))
185 heim_string_t __heim_string_constant(const char *);
188 * Errors
191 typedef struct heim_error * heim_error_t;
192 heim_error_t heim_error_enomem(void);
194 heim_error_t heim_error_create(int, const char *, ...)
195 HEIMDAL_PRINTF_ATTRIBUTE((printf, 2, 3));
197 heim_error_t heim_error_createv(int, const char *, va_list)
198 HEIMDAL_PRINTF_ATTRIBUTE((printf, 2, 0));
200 heim_string_t heim_error_copy_string(heim_error_t);
201 int heim_error_get_code(heim_error_t);
203 heim_error_t heim_error_append(heim_error_t, heim_error_t);
206 * Path
209 heim_object_t heim_path_get(heim_object_t ptr, heim_error_t *error, ...);
210 heim_object_t heim_path_copy(heim_object_t ptr, heim_error_t *error, ...);
211 heim_object_t heim_path_vget(heim_object_t ptr, heim_error_t *error,
212 va_list ap);
213 heim_object_t heim_path_vcopy(heim_object_t ptr, heim_error_t *error,
214 va_list ap);
216 int heim_path_vcreate(heim_object_t ptr, size_t size, heim_object_t leaf,
217 heim_error_t *error, va_list ap);
218 int heim_path_create(heim_object_t ptr, size_t size, heim_object_t leaf,
219 heim_error_t *error, ...);
221 void heim_path_vdelete(heim_object_t ptr, heim_error_t *error, va_list ap);
222 void heim_path_delete(heim_object_t ptr, heim_error_t *error, ...);
225 * Data (octet strings)
228 #ifndef __HEIM_OCTET_STRING__
229 #define __HEIM_OCTET_STRING__
230 typedef struct heim_octet_string {
231 size_t length;
232 void *data;
233 } heim_octet_string;
234 #endif
236 typedef struct heim_data * heim_data_t;
237 typedef void (*heim_data_free_f_t)(void *);
239 heim_data_t heim_data_create(const void *, size_t);
240 heim_data_t heim_data_ref_create(const void *, size_t, heim_data_free_f_t);
241 heim_tid_t heim_data_get_type_id(void);
242 const heim_octet_string *
243 heim_data_get_data(heim_data_t);
244 const void * heim_data_get_ptr(heim_data_t);
245 size_t heim_data_get_length(heim_data_t);
248 * DB
251 typedef struct heim_db_data *heim_db_t;
253 typedef void (*heim_db_iterator_f_t)(heim_data_t, heim_data_t, void *);
255 typedef int (*heim_db_plug_open_f_t)(void *, const char *, const char *,
256 heim_dict_t, void **, heim_error_t *);
257 typedef int (*heim_db_plug_clone_f_t)(void *, void **, heim_error_t *);
258 typedef int (*heim_db_plug_close_f_t)(void *, heim_error_t *);
259 typedef int (*heim_db_plug_lock_f_t)(void *, int, heim_error_t *);
260 typedef int (*heim_db_plug_unlock_f_t)(void *, heim_error_t *);
261 typedef int (*heim_db_plug_sync_f_t)(void *, heim_error_t *);
262 typedef int (*heim_db_plug_begin_f_t)(void *, int, heim_error_t *);
263 typedef int (*heim_db_plug_commit_f_t)(void *, heim_error_t *);
264 typedef int (*heim_db_plug_rollback_f_t)(void *, heim_error_t *);
265 typedef heim_data_t (*heim_db_plug_copy_value_f_t)(void *, heim_string_t,
266 heim_data_t,
267 heim_error_t *);
268 typedef int (*heim_db_plug_set_value_f_t)(void *, heim_string_t, heim_data_t,
269 heim_data_t, heim_error_t *);
270 typedef int (*heim_db_plug_del_key_f_t)(void *, heim_string_t, heim_data_t,
271 heim_error_t *);
272 typedef void (*heim_db_plug_iter_f_t)(void *, heim_string_t, void *,
273 heim_db_iterator_f_t, heim_error_t *);
275 struct heim_db_type {
276 int version;
277 heim_db_plug_open_f_t openf;
278 heim_db_plug_clone_f_t clonef;
279 heim_db_plug_close_f_t closef;
280 heim_db_plug_lock_f_t lockf;
281 heim_db_plug_unlock_f_t unlockf;
282 heim_db_plug_sync_f_t syncf;
283 heim_db_plug_begin_f_t beginf;
284 heim_db_plug_commit_f_t commitf;
285 heim_db_plug_rollback_f_t rollbackf;
286 heim_db_plug_copy_value_f_t copyf;
287 heim_db_plug_set_value_f_t setf;
288 heim_db_plug_del_key_f_t delf;
289 heim_db_plug_iter_f_t iterf;
292 extern struct heim_db_type heim_sorted_text_file_dbtype;
294 #define HEIM_DB_TYPE_VERSION_01 1
296 int heim_db_register(const char *dbtype,
297 void *data,
298 struct heim_db_type *plugin);
300 heim_db_t heim_db_create(const char *dbtype, const char *dbname,
301 heim_dict_t options, heim_error_t *error);
302 heim_db_t heim_db_clone(heim_db_t, heim_error_t *);
303 int heim_db_begin(heim_db_t, int, heim_error_t *);
304 int heim_db_commit(heim_db_t, heim_error_t *);
305 int heim_db_rollback(heim_db_t, heim_error_t *);
306 heim_tid_t heim_db_get_type_id(void);
308 int heim_db_set_value(heim_db_t, heim_string_t, heim_data_t, heim_data_t,
309 heim_error_t *);
310 heim_data_t heim_db_copy_value(heim_db_t, heim_string_t, heim_data_t,
311 heim_error_t *);
312 int heim_db_delete_key(heim_db_t, heim_string_t, heim_data_t,
313 heim_error_t *);
314 void heim_db_iterate_f(heim_db_t, heim_string_t, void *,
315 heim_db_iterator_f_t, heim_error_t *);
316 #ifdef __BLOCKS__
317 void heim_db_iterate(heim_db_t, heim_string_t,
318 void (^)(heim_data_t, heim_data_t), heim_error_t *);
319 #endif
323 * Number
326 typedef struct heim_number_data *heim_number_t;
328 heim_number_t heim_number_create(int);
329 heim_tid_t heim_number_get_type_id(void);
330 int heim_number_get_int(heim_number_t);
336 typedef struct heim_auto_release * heim_auto_release_t;
338 heim_auto_release_t heim_auto_release_create(void);
339 void heim_auto_release_drain(heim_auto_release_t);
340 heim_object_t heim_auto_release(heim_object_t);
343 * JSON
345 typedef enum heim_json_flags {
346 HEIM_JSON_F_NO_C_NULL = 1,
347 HEIM_JSON_F_STRICT_STRINGS = 2,
348 HEIM_JSON_F_NO_DATA = 4,
349 HEIM_JSON_F_NO_DATA_DICT = 8,
350 HEIM_JSON_F_STRICT_DICT = 16,
351 HEIM_JSON_F_STRICT = 31,
352 HEIM_JSON_F_CNULL2JSNULL = 32,
353 HEIM_JSON_F_TRY_DECODE_DATA = 64,
354 HEIM_JSON_F_ONE_LINE = 128
355 } heim_json_flags_t;
357 heim_object_t heim_json_create(const char *, size_t, heim_json_flags_t,
358 heim_error_t *);
359 heim_object_t heim_json_create_with_bytes(const void *, size_t, size_t,
360 heim_json_flags_t,
361 heim_error_t *);
362 heim_string_t heim_serialize(heim_object_t, heim_json_flags_t flags,
363 heim_error_t *);
367 * Debug
370 heim_string_t
371 heim_description(heim_object_t ptr);
374 * Binary search.
376 * Note: these are private until integrated into the heimbase object system.
378 typedef struct bsearch_file_handle *bsearch_file_handle;
379 int _bsearch_text(const char *buf, size_t buf_sz, const char *key,
380 char **value, size_t *location, size_t *loops);
381 int _bsearch_file_open(const char *fname, size_t max_sz, size_t page_sz,
382 bsearch_file_handle *bfh, size_t *reads);
383 int _bsearch_file(bsearch_file_handle bfh, const char *key, char **value,
384 size_t *location, size_t *loops, size_t *reads);
385 void _bsearch_file_info(bsearch_file_handle bfh, size_t *page_sz,
386 size_t *max_sz, int *blockwise);
387 void _bsearch_file_close(bsearch_file_handle *bfh);
389 #endif /* HEIM_BASE_H */