Address code review comments (k5login/foo in EXTRA_DIST)
[heimdal.git] / base / heimbase.h
blob6c7bb2b5c23f4acc332f03f09654bac9984b8f9d
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 typedef void (*heim_type_dealloc)(void *);
72 void *
73 heim_alloc(size_t size, const char *name, heim_type_dealloc dealloc);
75 heim_tid_t
76 heim_get_tid(heim_object_t object);
78 int
79 heim_cmp(heim_object_t a, heim_object_t b);
81 unsigned long
82 heim_get_hash(heim_object_t ptr);
84 void
85 heim_base_once_f(heim_base_once_t *, void *, void (*)(void *));
87 void
88 heim_abort(const char *fmt, ...)
89 HEIMDAL_NORETURN_ATTRIBUTE
90 HEIMDAL_PRINTF_ATTRIBUTE((printf, 1, 2));
92 void
93 heim_abortv(const char *fmt, va_list ap)
94 HEIMDAL_NORETURN_ATTRIBUTE
95 HEIMDAL_PRINTF_ATTRIBUTE((printf, 1, 0));
97 #define heim_assert(e,t) \
98 (heim_builtin_expect(!(e), 0) ? heim_abort(t ":" #e) : (void)0)
104 heim_null_t
105 heim_null_create(void);
107 heim_bool_t
108 heim_bool_create(int);
111 heim_bool_val(heim_bool_t);
114 * Array
117 typedef struct heim_array_data *heim_array_t;
119 heim_array_t heim_array_create(void);
120 heim_tid_t heim_array_get_type_id(void);
122 typedef void (*heim_array_iterator_f_t)(heim_object_t, void *);
124 int heim_array_append_value(heim_array_t, heim_object_t);
125 void heim_array_iterate_f(heim_array_t, void *, heim_array_iterator_f_t);
126 #ifdef __BLOCKS__
127 void heim_array_iterate(heim_array_t, void (^)(heim_object_t));
128 #endif
129 size_t heim_array_get_length(heim_array_t);
130 heim_object_t
131 heim_array_get_value(heim_array_t, size_t);
132 void heim_array_delete_value(heim_array_t, size_t);
133 #ifdef __BLOCKS__
134 void heim_array_filter(heim_array_t, int (^)(heim_object_t));
135 #endif
138 * Dict
141 typedef struct heim_dict_data *heim_dict_t;
143 heim_dict_t heim_dict_create(size_t size);
144 heim_tid_t heim_dict_get_type_id(void);
146 typedef void (*heim_dict_iterator_f_t)(heim_object_t, heim_object_t, void *);
148 int heim_dict_set_value(heim_dict_t, heim_object_t, heim_object_t);
149 void heim_dict_iterate_f(heim_dict_t, heim_dict_iterator_f_t, void *);
150 #ifdef __BLOCKS__
151 void heim_dict_iterate(heim_dict_t, void (^)(heim_object_t, heim_object_t));
152 #endif
154 heim_object_t
155 heim_dict_get_value(heim_dict_t, heim_object_t);
156 void heim_dict_delete_key(heim_dict_t, heim_object_t);
159 * String
162 typedef struct heim_string_data *heim_string_t;
164 heim_string_t heim_string_create(const char *);
165 heim_string_t heim_string_create_with_bytes(const void *, size_t);
166 heim_tid_t heim_string_get_type_id(void);
167 const char * heim_string_get_utf8(heim_string_t);
169 #define HSTR(_str) (__heim_string_constant("" _str ""))
170 heim_string_t __heim_string_constant(const char *);
173 * Number
176 typedef struct heim_number_data *heim_number_t;
178 heim_number_t heim_number_create(int);
179 heim_tid_t heim_number_get_type_id(void);
180 int heim_number_get_int(heim_number_t);
186 typedef struct heim_auto_release * heim_auto_release_t;
188 heim_auto_release_t heim_auto_release_create(void);
189 void heim_auto_release_drain(heim_auto_release_t);
190 void heim_auto_release(heim_object_t);
197 typedef struct heim_error * heim_error_t;
199 heim_error_t heim_error_create(int, const char *, ...)
200 HEIMDAL_PRINTF_ATTRIBUTE((printf, 2, 3));
202 heim_error_t heim_error_createv(int, const char *, va_list)
203 HEIMDAL_PRINTF_ATTRIBUTE((printf, 2, 0));
205 heim_string_t heim_error_copy_string(heim_error_t);
206 int heim_error_get_code(heim_error_t);
208 heim_error_t heim_error_append(heim_error_t, heim_error_t);
211 * JSON
213 heim_object_t heim_json_create(const char *, heim_error_t *);
214 heim_object_t heim_json_create_with_bytes(const void *, size_t, heim_error_t *);
220 #ifndef __HEIM_OCTET_STRING__
221 #define __HEIM_OCTET_STRING__
222 typedef struct heim_octet_string {
223 size_t length;
224 void *data;
225 } heim_octet_string;
226 #endif
228 typedef struct heim_data * heim_data_t;
230 heim_data_t heim_data_create(const void *, size_t);
231 heim_tid_t heim_data_get_type_id(void);
232 const heim_octet_string *
233 heim_data_get_data(heim_data_t);
234 const void * heim_data_get_ptr(heim_data_t);
235 size_t heim_data_get_length(heim_data_t);
239 * Binary search.
241 * Note: these are private until integrated into the heimbase object system.
243 typedef struct bsearch_file_handle *bsearch_file_handle;
244 int __bsearch_text(const char *buf, size_t buf_sz, const char *key,
245 char **value, size_t *location, size_t *loops);
246 int __bsearch_file_open(const char *fname, size_t max_sz, size_t page_sz,
247 bsearch_file_handle *bfh, size_t *reads);
248 int __bsearch_file(bsearch_file_handle bfh, const char *key, char **value,
249 size_t *location, size_t *loops, size_t *reads);
250 void __bsearch_file_info(bsearch_file_handle bfh, size_t *page_sz,
251 size_t *max_sz, int *blockwise);
252 void __bsearch_file_close(bsearch_file_handle *bfh);
254 #endif /* HEIM_BASE_H */