1/*
2 * Copyright (c) 2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
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.
18 *
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.
22 *
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.
34 */
35
36#ifndef HEIM_BASE_H
37#define HEIM_BASE_H 1
38
39#include <sys/types.h>
40#include <krb5-types.h>
41#include <stdarg.h>
42#include <stdbool.h>
43
44typedef void * heim_object_t;
45typedef unsigned int heim_tid_t;
46typedef heim_object_t heim_bool_t;
47typedef heim_object_t heim_null_t;
48#define HEIM_BASE_ONCE_INIT 0
49typedef long heim_base_once_t; /* XXX arch dependant */
50
51#if !defined(__has_extension)
52#define __has_extension(x) 0
53#endif
54
55#define HEIM_REQUIRE_GNUC(m,n,p) \
56    (((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__) >= \
57     (((m) * 10000) + ((n) * 100) + (p)))
58
59
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
65
66void *	heim_retain(heim_object_t);
67void	heim_release(heim_object_t);
68
69/*
70 *
71 */
72
73typedef void (*heim_type_dealloc)(void *);
74
75heim_object_t
76heim_uniq_alloc(size_t size, const char *name, heim_type_dealloc dealloc);
77
78/*
79 *
80 */
81
82heim_tid_t
83heim_get_tid(heim_object_t object);
84
85int
86heim_cmp(heim_object_t a, heim_object_t b);
87
88unsigned long
89heim_get_hash(heim_object_t ptr);
90
91void
92heim_base_once_f(heim_base_once_t *, void *, void (*)(void *));
93
94void
95heim_abort(const char *fmt, ...)
96    HEIMDAL_NORETURN_ATTRIBUTE
97    HEIMDAL_PRINTF_ATTRIBUTE((printf, 1, 2));
98
99void
100heim_abortv(const char *fmt, va_list ap)
101    HEIMDAL_NORETURN_ATTRIBUTE
102    HEIMDAL_PRINTF_ATTRIBUTE((printf, 1, 0));
103
104#define heim_assert(e,t) \
105    (__builtin_expect(!(e), 0) ? heim_abort(t ":" #e) : (void)0)
106
107void
108heim_warn_blocking(const char *apiname, heim_base_once_t *once);
109
110#define HEIM_WARN_BLOCKING(name, var) \
111{ static heim_base_once_t var = HEIM_BASE_ONCE_INIT; heim_warn_blocking(name, &var); }
112
113/*
114 *
115 */
116
117heim_null_t
118heim_null_create(void);
119
120heim_bool_t
121heim_bool_create(int);
122
123int
124heim_bool_val(heim_bool_t);
125
126/*
127 * Data
128 */
129
130typedef struct heim_data_data *heim_data_t;
131
132heim_data_t heim_data_create(void *, size_t);
133heim_tid_t heim_data_get_type_id(void);
134const void *
135	heim_data_get_bytes(heim_data_t);
136size_t	heim_data_get_length(heim_data_t);
137
138/*
139 * Array
140 */
141
142typedef struct heim_array_data *heim_array_t;
143
144heim_array_t heim_array_create(void);
145heim_tid_t heim_array_get_type_id(void);
146
147typedef void (*heim_array_iterator_f_t)(heim_object_t, int *, void *);
148
149int	heim_array_append_value(heim_array_t, heim_object_t);
150void	heim_array_iterate_f(heim_array_t, void *, heim_array_iterator_f_t);
151#ifdef __BLOCKS__
152typedef void (^heim_array_iterator_t)(heim_object_t, int *);
153void	heim_array_iterate(heim_array_t, heim_array_iterator_t);
154#endif
155size_t	heim_array_get_length(heim_array_t);
156heim_object_t
157	heim_array_copy_value(heim_array_t, size_t);
158void	heim_array_delete_value(heim_array_t, size_t);
159#ifdef __BLOCKS__
160void	heim_array_filter(heim_array_t, int (^)(heim_object_t));
161#endif
162
163int	heim_array_contains_value(heim_array_t array, heim_object_t value);
164
165/*
166 * Dict
167 */
168
169typedef struct heim_dict_data *heim_dict_t;
170
171heim_dict_t heim_dict_create(size_t size);
172heim_tid_t heim_dict_get_type_id(void);
173
174typedef void (*heim_dict_iterator_f_t)(heim_dict_t, heim_object_t, heim_object_t, void *);
175
176int	heim_dict_add_value(heim_dict_t, heim_object_t, heim_object_t);
177void	heim_dict_iterate_f(heim_dict_t, heim_dict_iterator_f_t, void *);
178#ifdef __BLOCKS__
179void	heim_dict_iterate(heim_dict_t, void (^)(heim_object_t, heim_object_t));
180#endif
181
182heim_object_t
183	heim_dict_copy_value(heim_dict_t, heim_object_t);
184void	heim_dict_delete_key(heim_dict_t, heim_object_t);
185
186/*
187 * String
188 */
189
190typedef struct heim_string_data *heim_string_t;
191
192heim_string_t heim_string_create(const char *);
193heim_tid_t heim_string_get_type_id(void);
194char * heim_string_copy_utf8(heim_string_t);
195
196/*
197 * Number
198 */
199
200typedef struct heim_number_data *heim_number_t;
201
202heim_number_t heim_number_create(int);
203heim_tid_t heim_number_get_type_id(void);
204int heim_number_get_int(heim_number_t);
205
206/*
207 *
208 */
209
210typedef struct heim_auto_release * heim_auto_release_t;
211
212heim_auto_release_t heim_auto_release_create(void);
213void heim_auto_release_drain(heim_auto_release_t);
214void heim_auto_release(heim_object_t);
215
216/*
217 *
218 */
219
220typedef struct heim_error * heim_error_t;
221
222heim_error_t	heim_error_create(int, const char *, ...)
223    HEIMDAL_PRINTF_ATTRIBUTE((printf, 2, 3));
224
225heim_error_t	heim_error_createv(int, const char *, va_list)
226    HEIMDAL_PRINTF_ATTRIBUTE((printf, 2, 0));
227
228heim_string_t heim_error_copy_string(heim_error_t);
229int heim_error_get_code(heim_error_t);
230
231heim_error_t
232heim_error_append(heim_error_t, heim_error_t);
233
234/*
235 *
236 */
237
238typedef struct heim_queue *heim_queue_t;
239typedef struct heim_queue_attr *heim_queue_attr_t;
240
241heim_queue_t	heim_queue_create(const char *, heim_queue_attr_t);
242void		heim_async_f(heim_queue_t, void *, void (*)(void *));
243
244typedef struct heim_sema_t *heim_sema_t;
245
246heim_sema_t	heim_sema_create(long count);
247void		heim_sema_signal(heim_sema_t);
248void		heim_sema_wait(heim_sema_t, time_t t);
249
250
251#endif /* HEIM_BASE_H */
252