1129198Scognet/*
2129198Scognet * Copyright (c) 2010 Kungliga Tekniska H��gskolan
3129198Scognet * (Royal Institute of Technology, Stockholm, Sweden).
4129198Scognet * All rights reserved.
5129198Scognet *
6129198Scognet * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
7129198Scognet *
8129198Scognet * Redistribution and use in source and binary forms, with or without
9129198Scognet * modification, are permitted provided that the following conditions
10129198Scognet * are met:
11129198Scognet *
12129198Scognet * 1. Redistributions of source code must retain the above copyright
13129198Scognet *    notice, this list of conditions and the following disclaimer.
14129198Scognet *
15129198Scognet * 2. Redistributions in binary form must reproduce the above copyright
16129198Scognet *    notice, this list of conditions and the following disclaimer in the
17129198Scognet *    documentation and/or other materials provided with the distribution.
18129198Scognet *
19129198Scognet * 3. Neither the name of the Institute nor the names of its contributors
20129198Scognet *    may be used to endorse or promote products derived from this software
21129198Scognet *    without specific prior written permission.
22129198Scognet *
23129198Scognet * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24129198Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25129198Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26129198Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27129198Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28129198Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29129198Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33129198Scognet * SUCH DAMAGE.
34129198Scognet */
35129198Scognet
36129198Scognet#ifndef HEIM_BASE_H
37129198Scognet#define HEIM_BASE_H 1
38129198Scognet
39129198Scognet#include <sys/types.h>
40129198Scognet#include <krb5-types.h>
41129198Scognet#include <stdarg.h>
42129198Scognet#include <stdbool.h>
43129198Scognet
44129198Scognettypedef void * heim_object_t;
45129198Scognettypedef unsigned int heim_tid_t;
46129198Scognettypedef heim_object_t heim_bool_t;
47129198Scognettypedef heim_object_t heim_null_t;
48129198Scognet#define HEIM_BASE_ONCE_INIT 0
49129198Scognettypedef long heim_base_once_t; /* XXX arch dependant */
50129198Scognet
51129198Scognet#if !defined(__has_extension)
52129198Scognet#define __has_extension(x) 0
53129198Scognet#endif
54129198Scognet
55129198Scognet#define HEIM_REQUIRE_GNUC(m,n,p) \
56129198Scognet    (((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__) >= \
57129198Scognet     (((m) * 10000) + ((n) * 100) + (p)))
58129198Scognet
59129198Scognet
60129198Scognet#if __has_extension(__builtin_expect) || HEIM_REQUIRE_GNUC(3,0,0)
61129198Scognet#define heim_builtin_expect(_op,_res) __builtin_expect(_op,_res)
62129198Scognet#else
63129198Scognet#define heim_builtin_expect(_op,_res) (_op)
64129198Scognet#endif
65129198Scognet
66129198Scognet
67129198Scognetvoid *	heim_retain(heim_object_t);
68129198Scognetvoid	heim_release(heim_object_t);
69129198Scognet
70129198Scognettypedef void (*heim_type_dealloc)(void *);
71129198Scognet
72129198Scognetvoid *
73129198Scognetheim_alloc(size_t size, const char *name, heim_type_dealloc dealloc);
74129198Scognet
75129198Scognetheim_tid_t
76129198Scognetheim_get_tid(heim_object_t object);
77129198Scognet
78129198Scognetint
79129198Scognetheim_cmp(heim_object_t a, heim_object_t b);
80129198Scognet
81129198Scognetunsigned long
82129198Scognetheim_get_hash(heim_object_t ptr);
83129198Scognet
84129198Scognetvoid
85129198Scognetheim_base_once_f(heim_base_once_t *, void *, void (*)(void *));
86129198Scognet
87129198Scognetvoid
88129198Scognetheim_abort(const char *fmt, ...)
89129198Scognet    HEIMDAL_NORETURN_ATTRIBUTE
90129198Scognet    HEIMDAL_PRINTF_ATTRIBUTE((printf, 1, 2));
91129198Scognet
92129198Scognetvoid
93129198Scognetheim_abortv(const char *fmt, va_list ap)
94129198Scognet    HEIMDAL_NORETURN_ATTRIBUTE
95129198Scognet    HEIMDAL_PRINTF_ATTRIBUTE((printf, 1, 0));
96129198Scognet
97129198Scognet#define heim_assert(e,t) \
98129198Scognet    (heim_builtin_expect(!(e), 0) ? heim_abort(t ":" #e) : (void)0)
99129198Scognet
100129198Scognet/*
101129198Scognet *
102129198Scognet */
103129198Scognet
104129198Scognetheim_null_t
105129198Scognetheim_null_create(void);
106129198Scognet
107129198Scognetheim_bool_t
108129198Scognetheim_bool_create(int);
109129198Scognet
110129198Scognetint
111129198Scognetheim_bool_val(heim_bool_t);
112129198Scognet
113129198Scognet/*
114129198Scognet * Array
115129198Scognet */
116129198Scognet
117129198Scognettypedef struct heim_array_data *heim_array_t;
118129198Scognet
119129198Scognetheim_array_t heim_array_create(void);
120129198Scognetheim_tid_t heim_array_get_type_id(void);
121129198Scognet
122129198Scognettypedef void (*heim_array_iterator_f_t)(heim_object_t, void *);
123129198Scognet
124129198Scognetint	heim_array_append_value(heim_array_t, heim_object_t);
125129198Scognetvoid	heim_array_iterate_f(heim_array_t, heim_array_iterator_f_t, void *);
126129198Scognet#ifdef __BLOCKS__
127129198Scognetvoid	heim_array_iterate(heim_array_t, void (^)(heim_object_t));
128129198Scognet#endif
129129198Scognetsize_t	heim_array_get_length(heim_array_t);
130129198Scognetheim_object_t
131129198Scognet	heim_array_copy_value(heim_array_t, size_t);
132129198Scognetvoid	heim_array_delete_value(heim_array_t, size_t);
133129198Scognet#ifdef __BLOCKS__
134129198Scognetvoid	heim_array_filter(heim_array_t, int (^)(heim_object_t));
135129198Scognet#endif
136129198Scognet
137129198Scognet/*
138129198Scognet * Dict
139129198Scognet */
140129198Scognet
141129198Scognettypedef struct heim_dict_data *heim_dict_t;
142129198Scognet
143129198Scognetheim_dict_t heim_dict_create(size_t size);
144129198Scognetheim_tid_t heim_dict_get_type_id(void);
145129198Scognet
146129198Scognettypedef void (*heim_dict_iterator_f_t)(heim_object_t, heim_object_t, void *);
147129198Scognet
148129198Scognetint	heim_dict_add_value(heim_dict_t, heim_object_t, heim_object_t);
149129198Scognetvoid	heim_dict_iterate_f(heim_dict_t, heim_dict_iterator_f_t, void *);
150129198Scognet#ifdef __BLOCKS__
151129198Scognetvoid	heim_dict_iterate(heim_dict_t, void (^)(heim_object_t, heim_object_t));
152129198Scognet#endif
153129198Scognet
154129198Scognetheim_object_t
155129198Scognet	heim_dict_copy_value(heim_dict_t, heim_object_t);
156129198Scognetvoid	heim_dict_delete_key(heim_dict_t, heim_object_t);
157129198Scognet
158129198Scognet/*
159129198Scognet * String
160129198Scognet */
161129198Scognet
162129198Scognettypedef struct heim_string_data *heim_string_t;
163129198Scognet
164129198Scognetheim_string_t heim_string_create(const char *);
165129198Scognetheim_tid_t heim_string_get_type_id(void);
166129198Scognetconst char * heim_string_get_utf8(heim_string_t);
167129198Scognet
168129198Scognet/*
169129198Scognet * Number
170129198Scognet */
171129198Scognet
172129198Scognettypedef struct heim_number_data *heim_number_t;
173129198Scognet
174129198Scognetheim_number_t heim_number_create(int);
175129198Scognetheim_tid_t heim_number_get_type_id(void);
176129198Scognetint heim_number_get_int(heim_number_t);
177129198Scognet
178129198Scognet/*
179129198Scognet *
180129198Scognet */
181129198Scognet
182129198Scognettypedef struct heim_auto_release * heim_auto_release_t;
183129198Scognet
184129198Scognetheim_auto_release_t heim_auto_release_create(void);
185129198Scognetvoid heim_auto_release_drain(heim_auto_release_t);
186129198Scognetvoid heim_auto_release(heim_object_t);
187129198Scognet
188129198Scognet#endif /* HEIM_BASE_H */
189129198Scognet