prop_object_impl.h revision 1.14 1 /* $NetBSD: prop_object_impl.h,v 1.14 2007/07/17 20:36:38 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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 * 1. Redistributions of source code must retain the above copyright
14 * 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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_
40 #define _PROPLIB_PROP_OBJECT_IMPL_H_
41
42 #if defined(_KERNEL) || defined(_STANDALONE)
43 #include <lib/libkern/libkern.h>
44 #else
45 #include <inttypes.h>
46 #endif
47
48 struct _prop_object_externalize_context {
49 char * poec_buf; /* string buffer */
50 size_t poec_capacity; /* capacity of buffer */
51 size_t poec_len; /* current length of string */
52 unsigned int poec_depth; /* nesting depth */
53 };
54
55 boolean_t _prop_object_externalize_start_tag(
56 struct _prop_object_externalize_context *,
57 const char *);
58 boolean_t _prop_object_externalize_end_tag(
59 struct _prop_object_externalize_context *,
60 const char *);
61 boolean_t _prop_object_externalize_empty_tag(
62 struct _prop_object_externalize_context *,
63 const char *);
64 boolean_t _prop_object_externalize_append_cstring(
65 struct _prop_object_externalize_context *,
66 const char *);
67 boolean_t _prop_object_externalize_append_encoded_cstring(
68 struct _prop_object_externalize_context *,
69 const char *);
70 boolean_t _prop_object_externalize_append_char(
71 struct _prop_object_externalize_context *,
72 unsigned char);
73 boolean_t _prop_object_externalize_header(
74 struct _prop_object_externalize_context *);
75 boolean_t _prop_object_externalize_footer(
76 struct _prop_object_externalize_context *);
77
78 struct _prop_object_externalize_context *
79 _prop_object_externalize_context_alloc(void);
80 void _prop_object_externalize_context_free(
81 struct _prop_object_externalize_context *);
82
83 typedef enum {
84 _PROP_TAG_TYPE_START, /* e.g. <dict> */
85 _PROP_TAG_TYPE_END, /* e.g. </dict> */
86 _PROP_TAG_TYPE_EITHER
87 } _prop_tag_type_t;
88
89 struct _prop_object_internalize_context {
90 const char *poic_xml;
91 const char *poic_cp;
92
93 const char *poic_tag_start;
94
95 const char *poic_tagname;
96 size_t poic_tagname_len;
97 const char *poic_tagattr;
98 size_t poic_tagattr_len;
99 const char *poic_tagattrval;
100 size_t poic_tagattrval_len;
101
102 boolean_t poic_is_empty_element;
103 _prop_tag_type_t poic_tag_type;
104 };
105
106 #define _PROP_EOF(c) ((c) == '\0')
107 #define _PROP_ISSPACE(c) \
108 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || \
109 _PROP_EOF(c))
110
111 #define _PROP_TAG_MATCH(ctx, t) \
112 _prop_object_internalize_match((ctx)->poic_tagname, \
113 (ctx)->poic_tagname_len, \
114 (t), strlen(t))
115
116 #define _PROP_TAGATTR_MATCH(ctx, a) \
117 _prop_object_internalize_match((ctx)->poic_tagattr, \
118 (ctx)->poic_tagattr_len, \
119 (a), strlen(a))
120
121 #define _PROP_TAGATTRVAL_MATCH(ctx, a) \
122 _prop_object_internalize_match((ctx)->poic_tagattrval, \
123 (ctx)->poic_tagattrval_len,\
124 (a), strlen(a))
125
126 boolean_t _prop_object_internalize_find_tag(
127 struct _prop_object_internalize_context *,
128 const char *, _prop_tag_type_t);
129 boolean_t _prop_object_internalize_match(const char *, size_t,
130 const char *, size_t);
131 prop_object_t _prop_object_internalize_by_tag(
132 struct _prop_object_internalize_context *);
133 boolean_t _prop_object_internalize_decode_string(
134 struct _prop_object_internalize_context *,
135 char *, size_t, size_t *, const char **);
136 prop_object_t _prop_generic_internalize(const char *, const char *);
137
138 struct _prop_object_internalize_context *
139 _prop_object_internalize_context_alloc(const char *);
140 void _prop_object_internalize_context_free(
141 struct _prop_object_internalize_context *);
142
143 #if !defined(_KERNEL) && !defined(_STANDALONE)
144 boolean_t _prop_object_externalize_write_file(const char *,
145 const char *, size_t);
146
147 struct _prop_object_internalize_mapped_file {
148 char * poimf_xml;
149 size_t poimf_mapsize;
150 };
151
152 struct _prop_object_internalize_mapped_file *
153 _prop_object_internalize_map_file(const char *);
154 void _prop_object_internalize_unmap_file(
155 struct _prop_object_internalize_mapped_file *);
156 #endif /* !_KERNEL && !_STANDALONE */
157
158 /* These are here because they're required by shared code. */
159 prop_object_t _prop_array_internalize(
160 struct _prop_object_internalize_context *);
161 prop_object_t _prop_bool_internalize(
162 struct _prop_object_internalize_context *);
163 prop_object_t _prop_data_internalize(
164 struct _prop_object_internalize_context *);
165 prop_object_t _prop_dictionary_internalize(
166 struct _prop_object_internalize_context *);
167 prop_object_t _prop_number_internalize(
168 struct _prop_object_internalize_context *);
169 prop_object_t _prop_string_internalize(
170 struct _prop_object_internalize_context *);
171
172 struct _prop_object_type {
173 uint32_t pot_type; /* type indicator */
174 void (*pot_free)(void *); /* func to free object */
175 boolean_t (*pot_extern) /* func to externalize object */
176 (struct _prop_object_externalize_context *,
177 void *);
178 boolean_t (*pot_equals) /* func to test quality */
179 (void *, void *);
180 };
181
182 struct _prop_object {
183 const struct _prop_object_type *po_type;/* type descriptor */
184 uint32_t po_refcnt; /* reference count */
185 };
186
187 void _prop_object_init(struct _prop_object *,
188 const struct _prop_object_type *);
189 void _prop_object_fini(struct _prop_object *);
190
191 struct _prop_object_iterator {
192 prop_object_t (*pi_next_object)(void *);
193 void (*pi_reset)(void *);
194 prop_object_t pi_obj;
195 uint32_t pi_version;
196 };
197
198 #if defined(_KERNEL)
199
200 /*
201 * proplib in the kernel...
202 */
203
204 #include <sys/param.h>
205 #include <sys/malloc.h>
206 #include <sys/pool.h>
207 #include <sys/systm.h>
208 #include <sys/lock.h>
209
210 #define _PROP_ASSERT(x) KASSERT(x)
211
212 #define _PROP_MALLOC(s, t) malloc((s), (t), M_WAITOK)
213 #define _PROP_CALLOC(s, t) malloc((s), (t), M_WAITOK | M_ZERO)
214 #define _PROP_REALLOC(v, s, t) realloc((v), (s), (t), M_WAITOK)
215 #define _PROP_FREE(v, t) free((v), (t))
216
217 #define _PROP_POOL_GET(p) pool_get(&(p), PR_WAITOK)
218 #define _PROP_POOL_PUT(p, v) pool_put(&(p), (v))
219
220 #define _PROP_POOL_INIT(p, s, d) \
221 POOL_INIT(p, s, 0, 0, 0, d, &pool_allocator_nointr, IPL_NONE);
222
223 #define _PROP_MALLOC_DEFINE(t, s, l) \
224 MALLOC_DEFINE(t, s, l);
225
226 #define _PROP_MUTEX_DECL_STATIC(x) \
227 static struct simplelock x = SIMPLELOCK_INITIALIZER;
228 #define _PROP_MUTEX_LOCK(x) simple_lock(&(x))
229 #define _PROP_MUTEX_UNLOCK(x) simple_unlock(&(x))
230
231 #define _PROP_RWLOCK_DECL(x) struct lock x ;
232 #define _PROP_RWLOCK_INIT(x) lockinit(&(x), PZERO, "proprwlk", 0, 0)
233 #define _PROP_RWLOCK_RDLOCK(x) lockmgr(&(x), LK_SHARED, NULL)
234 #define _PROP_RWLOCK_WRLOCK(x) lockmgr(&(x), LK_EXCLUSIVE, NULL)
235 #define _PROP_RWLOCK_UNLOCK(x) lockmgr(&(x), LK_RELEASE, NULL)
236 #define _PROP_RWLOCK_DESTROY(x) lockmgr(&(x), LK_DRAIN, NULL)
237
238 #elif defined(_STANDALONE)
239
240 /*
241 * proplib in a standalone environment...
242 */
243
244 #include <lib/libsa/stand.h>
245
246 void * _prop_standalone_calloc(size_t);
247 void * _prop_standalone_realloc(void *, size_t);
248
249 #define _PROP_ASSERT(x) /* nothing */
250
251 #define _PROP_MALLOC(s, t) alloc((s))
252 #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s))
253 #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s))
254 #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */
255
256 #define _PROP_POOL_GET(p) alloc((p))
257 #define _PROP_POOL_PUT(p, v) dealloc((v), (p))
258
259 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
260
261 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
262
263 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
264 #define _PROP_MUTEX_LOCK(x) /* nothing */
265 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
266
267 #define _PROP_RWLOCK_DECL(x) /* nothing */
268 #define _PROP_RWLOCK_INIT(x) /* nothing */
269 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
270 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
271 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
272 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
273
274 #else
275
276 /*
277 * proplib in user space...
278 */
279
280 #include <assert.h>
281 #include <string.h>
282 #include <stdio.h>
283 #include <stdlib.h>
284 #include <stddef.h>
285
286 #define _PROP_ASSERT(x) /*LINTED*/assert(x)
287
288 #define _PROP_MALLOC(s, t) malloc((s))
289 #define _PROP_CALLOC(s, t) calloc(1, (s))
290 #define _PROP_REALLOC(v, s, t) realloc((v), (s))
291 #define _PROP_FREE(v, t) free((v))
292
293 #define _PROP_POOL_GET(p) malloc((p))
294 #define _PROP_POOL_PUT(p, v) free((v))
295
296 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
297
298 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
299
300 #if defined(__NetBSD__) && defined(_LIBPROP)
301 /*
302 * Use the same mechanism as libc; we get pthread mutexes for threaded
303 * programs and do-nothing stubs for non-threaded programs.
304 */
305 #include "reentrant.h"
306 #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x = MUTEX_INITIALIZER;
307 #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x))
308 #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x))
309
310 #define _PROP_RWLOCK_DECL(x) rwlock_t x ;
311 #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL)
312 #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x))
313 #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x))
314 #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x))
315 #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x))
316 #elif defined(HAVE_NBTOOL_CONFIG_H)
317 /*
318 * None of NetBSD's build tools are multi-threaded.
319 */
320 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
321 #define _PROP_MUTEX_LOCK(x) /* nothing */
322 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
323
324 #define _PROP_RWLOCK_DECL(x) /* nothing */
325 #define _PROP_RWLOCK_INIT(x) /* nothing */
326 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
327 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
328 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
329 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
330 #else
331 /*
332 * Use pthread mutexes everywhere else.
333 */
334 #include <pthread.h>
335 #define _PROP_MUTEX_DECL_STATIC(x) \
336 static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
337 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x))
338 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
339
340 #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ;
341 #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL)
342 #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x))
343 #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x))
344 #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x))
345 #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x))
346 #endif
347
348 #endif /* _KERNEL */
349
350 /*
351 * Language features.
352 */
353 #if defined(__NetBSD__)
354 #include <sys/cdefs.h>
355 #define _PROP_ARG_UNUSED __unused
356 #else
357 #define _PROP_ARG_UNUSED /* delete */
358 #endif /* __NetBSD__ */
359
360 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
361