prop_object_impl.h revision 1.11.4.1.2.1 1 /* $NetBSD: prop_object_impl.h,v 1.11.4.1.2.1 2008/11/30 23:53:04 snj 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 #include "prop_stack.h"
49
50 struct _prop_object_externalize_context {
51 char * poec_buf; /* string buffer */
52 size_t poec_capacity; /* capacity of buffer */
53 size_t poec_len; /* current length of string */
54 unsigned int poec_depth; /* nesting depth */
55 };
56
57 bool _prop_object_externalize_start_tag(
58 struct _prop_object_externalize_context *,
59 const char *);
60 bool _prop_object_externalize_end_tag(
61 struct _prop_object_externalize_context *,
62 const char *);
63 bool _prop_object_externalize_empty_tag(
64 struct _prop_object_externalize_context *,
65 const char *);
66 bool _prop_object_externalize_append_cstring(
67 struct _prop_object_externalize_context *,
68 const char *);
69 bool _prop_object_externalize_append_encoded_cstring(
70 struct _prop_object_externalize_context *,
71 const char *);
72 bool _prop_object_externalize_append_char(
73 struct _prop_object_externalize_context *,
74 unsigned char);
75 bool _prop_object_externalize_header(
76 struct _prop_object_externalize_context *);
77 bool _prop_object_externalize_footer(
78 struct _prop_object_externalize_context *);
79
80 struct _prop_object_externalize_context *
81 _prop_object_externalize_context_alloc(void);
82 void _prop_object_externalize_context_free(
83 struct _prop_object_externalize_context *);
84
85 typedef enum {
86 _PROP_TAG_TYPE_START, /* e.g. <dict> */
87 _PROP_TAG_TYPE_END, /* e.g. </dict> */
88 _PROP_TAG_TYPE_EITHER
89 } _prop_tag_type_t;
90
91 struct _prop_object_internalize_context {
92 const char *poic_xml;
93 const char *poic_cp;
94
95 const char *poic_tag_start;
96
97 const char *poic_tagname;
98 size_t poic_tagname_len;
99 const char *poic_tagattr;
100 size_t poic_tagattr_len;
101 const char *poic_tagattrval;
102 size_t poic_tagattrval_len;
103
104 bool poic_is_empty_element;
105 _prop_tag_type_t poic_tag_type;
106 };
107
108 enum {
109 _PROP_OBJECT_FREE_DONE,
110 _PROP_OBJECT_FREE_RECURSE,
111 _PROP_OBJECT_FREE_FAILED
112 };
113
114 enum {
115 _PROP_OBJECT_EQUALS_FALSE,
116 _PROP_OBJECT_EQUALS_TRUE,
117 _PROP_OBJECT_EQUALS_RECURSE
118 };
119
120 #define _PROP_EOF(c) ((c) == '\0')
121 #define _PROP_ISSPACE(c) \
122 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || \
123 _PROP_EOF(c))
124
125 #define _PROP_TAG_MATCH(ctx, t) \
126 _prop_object_internalize_match((ctx)->poic_tagname, \
127 (ctx)->poic_tagname_len, \
128 (t), strlen(t))
129
130 #define _PROP_TAGATTR_MATCH(ctx, a) \
131 _prop_object_internalize_match((ctx)->poic_tagattr, \
132 (ctx)->poic_tagattr_len, \
133 (a), strlen(a))
134
135 #define _PROP_TAGATTRVAL_MATCH(ctx, a) \
136 _prop_object_internalize_match((ctx)->poic_tagattrval, \
137 (ctx)->poic_tagattrval_len,\
138 (a), strlen(a))
139
140 bool _prop_object_internalize_find_tag(
141 struct _prop_object_internalize_context *,
142 const char *, _prop_tag_type_t);
143 bool _prop_object_internalize_match(const char *, size_t,
144 const char *, size_t);
145 prop_object_t _prop_object_internalize_by_tag(
146 struct _prop_object_internalize_context *);
147 bool _prop_object_internalize_decode_string(
148 struct _prop_object_internalize_context *,
149 char *, size_t, size_t *, const char **);
150 prop_object_t _prop_generic_internalize(const char *, const char *);
151
152 struct _prop_object_internalize_context *
153 _prop_object_internalize_context_alloc(const char *);
154 void _prop_object_internalize_context_free(
155 struct _prop_object_internalize_context *);
156
157 #if !defined(_KERNEL) && !defined(_STANDALONE)
158 bool _prop_object_externalize_write_file(const char *,
159 const char *, size_t);
160
161 struct _prop_object_internalize_mapped_file {
162 char * poimf_xml;
163 size_t poimf_mapsize;
164 };
165
166 struct _prop_object_internalize_mapped_file *
167 _prop_object_internalize_map_file(const char *);
168 void _prop_object_internalize_unmap_file(
169 struct _prop_object_internalize_mapped_file *);
170 #endif /* !_KERNEL && !_STANDALONE */
171
172 typedef bool (*prop_object_internalizer_t)(prop_stack_t, prop_object_t *,
173 struct _prop_object_internalize_context *);
174 typedef bool (*prop_object_internalizer_continue_t)(prop_stack_t, prop_object_t *,
175 struct _prop_object_internalize_context *, void *, prop_object_t);
176
177 /* These are here because they're required by shared code. */
178 bool _prop_array_internalize(prop_stack_t, prop_object_t *,
179 struct _prop_object_internalize_context *);
180 bool _prop_bool_internalize(prop_stack_t, prop_object_t *,
181 struct _prop_object_internalize_context *);
182 bool _prop_data_internalize(prop_stack_t, prop_object_t *,
183 struct _prop_object_internalize_context *);
184 bool _prop_dictionary_internalize(prop_stack_t, prop_object_t *,
185 struct _prop_object_internalize_context *);
186 bool _prop_number_internalize(prop_stack_t, prop_object_t *,
187 struct _prop_object_internalize_context *);
188 bool _prop_string_internalize(prop_stack_t, prop_object_t *,
189 struct _prop_object_internalize_context *);
190
191 struct _prop_object_type {
192 /* type indicator */
193 uint32_t pot_type;
194 /* func to free object */
195 int (*pot_free)(prop_stack_t, prop_object_t *);
196 /*
197 * func to free the child returned by pot_free with stack == NULL.
198 *
199 * Must be implemented if pot_free can return anything other than
200 * _PROP_OBJECT_FREE_DONE.
201 */
202 void (*pot_emergency_free)(prop_object_t);
203 /* func to externalize object */
204 bool (*pot_extern)(struct _prop_object_externalize_context *,
205 void *);
206 /* func to test quality */
207 bool (*pot_equals)(prop_object_t, prop_object_t,
208 void **, void **,
209 prop_object_t *, prop_object_t *);
210 /*
211 * func to finish equality iteration.
212 *
213 * Must be implemented if pot_equals can return
214 * _PROP_OBJECT_EQUALS_RECURSE
215 */
216 void (*pot_equals_finish)(prop_object_t, prop_object_t);
217 void (*pot_lock)(void);
218 void (*pot_unlock)(void);
219 };
220
221 struct _prop_object {
222 const struct _prop_object_type *po_type;/* type descriptor */
223 uint32_t po_refcnt; /* reference count */
224 };
225
226 void _prop_object_init(struct _prop_object *,
227 const struct _prop_object_type *);
228 void _prop_object_fini(struct _prop_object *);
229
230 struct _prop_object_iterator {
231 prop_object_t (*pi_next_object)(void *);
232 void (*pi_reset)(void *);
233 prop_object_t pi_obj;
234 uint32_t pi_version;
235 };
236
237 #if defined(_KERNEL)
238
239 /*
240 * proplib in the kernel...
241 */
242
243 #include <sys/param.h>
244 #include <sys/malloc.h>
245 #include <sys/pool.h>
246 #include <sys/systm.h>
247 #include <sys/lock.h>
248
249 #define _PROP_ASSERT(x) KASSERT(x)
250
251 #define _PROP_MALLOC(s, t) malloc((s), (t), M_WAITOK)
252 #define _PROP_CALLOC(s, t) malloc((s), (t), M_WAITOK | M_ZERO)
253 #define _PROP_REALLOC(v, s, t) realloc((v), (s), (t), M_WAITOK)
254 #define _PROP_FREE(v, t) free((v), (t))
255
256 #define _PROP_POOL_GET(p) pool_get(&(p), PR_WAITOK)
257 #define _PROP_POOL_PUT(p, v) pool_put(&(p), (v))
258
259 #define _PROP_POOL_INIT(p, s, d) \
260 POOL_INIT(p, s, 0, 0, 0, d, &pool_allocator_nointr);
261
262 #define _PROP_MALLOC_DEFINE(t, s, l) \
263 MALLOC_DEFINE(t, s, l);
264
265 #define _PROP_MUTEX_DECL_STATIC(x) \
266 static struct simplelock x = SIMPLELOCK_INITIALIZER;
267 #define _PROP_MUTEX_LOCK(x) simple_lock(&(x))
268 #define _PROP_MUTEX_UNLOCK(x) simple_unlock(&(x))
269
270 #define _PROP_RWLOCK_DECL(x) struct lock x ;
271 #define _PROP_RWLOCK_INIT(x) lockinit(&(x), PZERO, "proprwlk", 0, 0)
272 #define _PROP_RWLOCK_RDLOCK(x) lockmgr(&(x), LK_SHARED, NULL)
273 #define _PROP_RWLOCK_WRLOCK(x) lockmgr(&(x), LK_EXCLUSIVE, NULL)
274 #define _PROP_RWLOCK_UNLOCK(x) lockmgr(&(x), LK_RELEASE, NULL)
275 #define _PROP_RWLOCK_DESTROY(x) lockmgr(&(x), LK_DRAIN, NULL)
276
277 #elif defined(_STANDALONE)
278
279 /*
280 * proplib in a standalone environment...
281 */
282
283 #include <lib/libsa/stand.h>
284
285 void * _prop_standalone_calloc(size_t);
286 void * _prop_standalone_realloc(void *, size_t);
287
288 #define _PROP_ASSERT(x) /* nothing */
289
290 #define _PROP_MALLOC(s, t) alloc((s))
291 #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s))
292 #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s))
293 #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */
294
295 #define _PROP_POOL_GET(p) alloc((p))
296 #define _PROP_POOL_PUT(p, v) dealloc((v), (p))
297
298 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
299
300 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
301
302 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
303 #define _PROP_MUTEX_LOCK(x) /* nothing */
304 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
305
306 #define _PROP_RWLOCK_DECL(x) /* nothing */
307 #define _PROP_RWLOCK_INIT(x) /* nothing */
308 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
309 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
310 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
311 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
312
313 #else
314
315 /*
316 * proplib in user space...
317 */
318
319 #include <assert.h>
320 #include <string.h>
321 #include <stdio.h>
322 #include <stdlib.h>
323 #include <stddef.h>
324
325 #define _PROP_ASSERT(x) /*LINTED*/assert(x)
326
327 #define _PROP_MALLOC(s, t) malloc((s))
328 #define _PROP_CALLOC(s, t) calloc(1, (s))
329 #define _PROP_REALLOC(v, s, t) realloc((v), (s))
330 #define _PROP_FREE(v, t) free((v))
331
332 #define _PROP_POOL_GET(p) malloc((p))
333 #define _PROP_POOL_PUT(p, v) free((v))
334
335 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
336
337 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
338
339 #if defined(__NetBSD__) && defined(_LIBPROP)
340 /*
341 * Use the same mechanism as libc; we get pthread mutexes for threaded
342 * programs and do-nothing stubs for non-threaded programs.
343 */
344 #include "reentrant.h"
345 #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x = MUTEX_INITIALIZER;
346 #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x))
347 #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x))
348
349 #define _PROP_RWLOCK_DECL(x) rwlock_t x ;
350 #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL)
351 #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x))
352 #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x))
353 #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x))
354 #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x))
355 #elif defined(HAVE_NBTOOL_CONFIG_H)
356 /*
357 * None of NetBSD's build tools are multi-threaded.
358 */
359 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
360 #define _PROP_MUTEX_LOCK(x) /* nothing */
361 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
362
363 #define _PROP_RWLOCK_DECL(x) /* nothing */
364 #define _PROP_RWLOCK_INIT(x) /* nothing */
365 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
366 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
367 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
368 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
369 #else
370 /*
371 * Use pthread mutexes everywhere else.
372 */
373 #include <pthread.h>
374 #define _PROP_MUTEX_DECL_STATIC(x) \
375 static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
376 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x))
377 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
378
379 #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ;
380 #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL)
381 #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x))
382 #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x))
383 #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x))
384 #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x))
385 #endif
386
387 #endif /* _KERNEL */
388
389 /*
390 * Language features.
391 */
392 #if defined(__NetBSD__)
393 #include <sys/cdefs.h>
394 #define _PROP_ARG_UNUSED __unused
395 #else
396 #define _PROP_ARG_UNUSED /* delete */
397 #endif /* __NetBSD__ */
398
399 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
400