prop_object_impl.h revision 1.19 1 /* $NetBSD: prop_object_impl.h,v 1.19 2008/01/05 01:15:03 ad 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 };
218
219 struct _prop_object {
220 const struct _prop_object_type *po_type;/* type descriptor */
221 uint32_t po_refcnt; /* reference count */
222 };
223
224 void _prop_object_init(struct _prop_object *,
225 const struct _prop_object_type *);
226 void _prop_object_fini(struct _prop_object *);
227
228 struct _prop_object_iterator {
229 prop_object_t (*pi_next_object)(void *);
230 void (*pi_reset)(void *);
231 prop_object_t pi_obj;
232 uint32_t pi_version;
233 };
234
235 #if defined(_KERNEL)
236
237 /*
238 * proplib in the kernel...
239 */
240
241 #include <sys/param.h>
242 #include <sys/malloc.h>
243 #include <sys/pool.h>
244 #include <sys/systm.h>
245 #include <sys/simplelock.h>
246 #include <sys/rwlock.h>
247
248 #define _PROP_ASSERT(x) KASSERT(x)
249
250 #define _PROP_MALLOC(s, t) malloc((s), (t), M_WAITOK)
251 #define _PROP_CALLOC(s, t) malloc((s), (t), M_WAITOK | M_ZERO)
252 #define _PROP_REALLOC(v, s, t) realloc((v), (s), (t), M_WAITOK)
253 #define _PROP_FREE(v, t) free((v), (t))
254
255 #define _PROP_POOL_GET(p) pool_get(&(p), PR_WAITOK)
256 #define _PROP_POOL_PUT(p, v) pool_put(&(p), (v))
257
258 #define _PROP_POOL_INIT(p, s, d) \
259 POOL_INIT(p, s, 0, 0, 0, d, &pool_allocator_nointr, IPL_NONE);
260
261 #define _PROP_MALLOC_DEFINE(t, s, l) \
262 MALLOC_DEFINE(t, s, l);
263
264 #define _PROP_MUTEX_DECL_STATIC(x) \
265 static struct simplelock x = SIMPLELOCK_INITIALIZER;
266 #define _PROP_MUTEX_LOCK(x) simple_lock(&(x))
267 #define _PROP_MUTEX_UNLOCK(x) simple_unlock(&(x))
268
269 #define _PROP_RWLOCK_DECL(x) krwlock_t x ;
270 #define _PROP_RWLOCK_INIT(x) rw_init(&(x))
271 #define _PROP_RWLOCK_RDLOCK(x) rw_enter(&(x), RW_READER)
272 #define _PROP_RWLOCK_WRLOCK(x) rw_enter(&(x), RW_WRITER)
273 #define _PROP_RWLOCK_UNLOCK(x) rw_exit(&(x))
274 #define _PROP_RWLOCK_DESTROY(x) rw_destroy(&(x))
275
276 #elif defined(_STANDALONE)
277
278 /*
279 * proplib in a standalone environment...
280 */
281
282 #include <lib/libsa/stand.h>
283
284 void * _prop_standalone_calloc(size_t);
285 void * _prop_standalone_realloc(void *, size_t);
286
287 #define _PROP_ASSERT(x) /* nothing */
288
289 #define _PROP_MALLOC(s, t) alloc((s))
290 #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s))
291 #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s))
292 #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */
293
294 #define _PROP_POOL_GET(p) alloc((p))
295 #define _PROP_POOL_PUT(p, v) dealloc((v), (p))
296
297 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
298
299 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
300
301 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
302 #define _PROP_MUTEX_LOCK(x) /* nothing */
303 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
304
305 #define _PROP_RWLOCK_DECL(x) /* nothing */
306 #define _PROP_RWLOCK_INIT(x) /* nothing */
307 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
308 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
309 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
310 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
311
312 #else
313
314 /*
315 * proplib in user space...
316 */
317
318 #include <assert.h>
319 #include <string.h>
320 #include <stdio.h>
321 #include <stdlib.h>
322 #include <stddef.h>
323
324 #define _PROP_ASSERT(x) /*LINTED*/assert(x)
325
326 #define _PROP_MALLOC(s, t) malloc((s))
327 #define _PROP_CALLOC(s, t) calloc(1, (s))
328 #define _PROP_REALLOC(v, s, t) realloc((v), (s))
329 #define _PROP_FREE(v, t) free((v))
330
331 #define _PROP_POOL_GET(p) malloc((p))
332 #define _PROP_POOL_PUT(p, v) free((v))
333
334 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
335
336 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
337
338 #if defined(__NetBSD__) && defined(_LIBPROP)
339 /*
340 * Use the same mechanism as libc; we get pthread mutexes for threaded
341 * programs and do-nothing stubs for non-threaded programs.
342 */
343 #include "reentrant.h"
344 #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x = MUTEX_INITIALIZER;
345 #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x))
346 #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x))
347
348 #define _PROP_RWLOCK_DECL(x) rwlock_t x ;
349 #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL)
350 #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x))
351 #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x))
352 #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x))
353 #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x))
354 #elif defined(HAVE_NBTOOL_CONFIG_H)
355 /*
356 * None of NetBSD's build tools are multi-threaded.
357 */
358 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
359 #define _PROP_MUTEX_LOCK(x) /* nothing */
360 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
361
362 #define _PROP_RWLOCK_DECL(x) /* nothing */
363 #define _PROP_RWLOCK_INIT(x) /* nothing */
364 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
365 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
366 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
367 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
368 #else
369 /*
370 * Use pthread mutexes everywhere else.
371 */
372 #include <pthread.h>
373 #define _PROP_MUTEX_DECL_STATIC(x) \
374 static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
375 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x))
376 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
377
378 #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ;
379 #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL)
380 #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x))
381 #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x))
382 #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x))
383 #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x))
384 #endif
385
386 #endif /* _KERNEL */
387
388 /*
389 * Language features.
390 */
391 #if defined(__NetBSD__)
392 #include <sys/cdefs.h>
393 #define _PROP_ARG_UNUSED __unused
394 #else
395 #define _PROP_ARG_UNUSED /* delete */
396 #endif /* __NetBSD__ */
397
398 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
399