xref: /minix3/crypto/external/bsd/heimdal/dist/base/heimbase.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: heimbase.h,v 1.1.1.2 2014/04/24 12:45:26 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 2010 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * 3. Neither the name of the Institute nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #ifndef HEIM_BASE_H
39 #define HEIM_BASE_H 1
40 
41 #include <sys/types.h>
42 #include <krb5/krb5-types.h>
43 #include <stdarg.h>
44 #include <stdbool.h>
45 
46 typedef void * heim_object_t;
47 typedef unsigned int heim_tid_t;
48 typedef heim_object_t heim_bool_t;
49 typedef heim_object_t heim_null_t;
50 #define HEIM_BASE_ONCE_INIT 0
51 typedef long heim_base_once_t; /* XXX arch dependant */
52 
53 #if !defined(__has_extension)
54 #define __has_extension(x) 0
55 #endif
56 
57 #define HEIM_REQUIRE_GNUC(m,n,p) \
58     (((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__) >= \
59      (((m) * 10000) + ((n) * 100) + (p)))
60 
61 
62 #if __has_extension(__builtin_expect) || HEIM_REQUIRE_GNUC(3,0,0)
63 #define heim_builtin_expect(_op,_res) __builtin_expect(_op,_res)
64 #else
65 #define heim_builtin_expect(_op,_res) (_op)
66 #endif
67 
68 
69 void *	heim_retain(heim_object_t);
70 void	heim_release(heim_object_t);
71 
72 typedef void (*heim_type_dealloc)(void *);
73 
74 void *
75 heim_alloc(size_t size, const char *name, heim_type_dealloc dealloc);
76 
77 heim_tid_t
78 heim_get_tid(heim_object_t object);
79 
80 int
81 heim_cmp(heim_object_t a, heim_object_t b);
82 
83 unsigned long
84 heim_get_hash(heim_object_t ptr);
85 
86 void
87 heim_base_once_f(heim_base_once_t *, void *, void (*)(void *));
88 
89 void
90 heim_abort(const char *fmt, ...)
91     HEIMDAL_NORETURN_ATTRIBUTE
92     HEIMDAL_PRINTF_ATTRIBUTE((printf, 1, 2));
93 
94 void
95 heim_abortv(const char *fmt, va_list ap)
96     HEIMDAL_NORETURN_ATTRIBUTE
97     HEIMDAL_PRINTF_ATTRIBUTE((printf, 1, 0));
98 
99 #define heim_assert(e,t) \
100     (heim_builtin_expect(!(e), 0) ? heim_abort(t ":" #e) : (void)0)
101 
102 /*
103  *
104  */
105 
106 heim_null_t
107 heim_null_create(void);
108 
109 heim_bool_t
110 heim_bool_create(int);
111 
112 int
113 heim_bool_val(heim_bool_t);
114 
115 /*
116  * Array
117  */
118 
119 typedef struct heim_array_data *heim_array_t;
120 
121 heim_array_t heim_array_create(void);
122 heim_tid_t heim_array_get_type_id(void);
123 
124 typedef void (*heim_array_iterator_f_t)(heim_object_t, void *);
125 
126 int	heim_array_append_value(heim_array_t, heim_object_t);
127 void	heim_array_iterate_f(heim_array_t, heim_array_iterator_f_t, void *);
128 #ifdef __BLOCKS__
129 void	heim_array_iterate(heim_array_t, void (^)(heim_object_t));
130 #endif
131 size_t	heim_array_get_length(heim_array_t);
132 heim_object_t
133 	heim_array_copy_value(heim_array_t, size_t);
134 void	heim_array_delete_value(heim_array_t, size_t);
135 #ifdef __BLOCKS__
136 void	heim_array_filter(heim_array_t, int (^)(heim_object_t));
137 #endif
138 
139 /*
140  * Dict
141  */
142 
143 typedef struct heim_dict_data *heim_dict_t;
144 
145 heim_dict_t heim_dict_create(size_t size);
146 heim_tid_t heim_dict_get_type_id(void);
147 
148 typedef void (*heim_dict_iterator_f_t)(heim_object_t, heim_object_t, void *);
149 
150 int	heim_dict_add_value(heim_dict_t, heim_object_t, heim_object_t);
151 void	heim_dict_iterate_f(heim_dict_t, heim_dict_iterator_f_t, void *);
152 #ifdef __BLOCKS__
153 void	heim_dict_iterate(heim_dict_t, void (^)(heim_object_t, heim_object_t));
154 #endif
155 
156 heim_object_t
157 	heim_dict_copy_value(heim_dict_t, heim_object_t);
158 void	heim_dict_delete_key(heim_dict_t, heim_object_t);
159 
160 /*
161  * String
162  */
163 
164 typedef struct heim_string_data *heim_string_t;
165 
166 heim_string_t heim_string_create(const char *);
167 heim_tid_t heim_string_get_type_id(void);
168 const char * heim_string_get_utf8(heim_string_t);
169 
170 /*
171  * Number
172  */
173 
174 typedef struct heim_number_data *heim_number_t;
175 
176 heim_number_t heim_number_create(int);
177 heim_tid_t heim_number_get_type_id(void);
178 int heim_number_get_int(heim_number_t);
179 
180 /*
181  *
182  */
183 
184 typedef struct heim_auto_release * heim_auto_release_t;
185 
186 heim_auto_release_t heim_auto_release_create(void);
187 void heim_auto_release_drain(heim_auto_release_t);
188 void heim_auto_release(heim_object_t);
189 
190 #endif /* HEIM_BASE_H */
191