prop_object_impl.h revision 1.17 1 /* $NetBSD: prop_object_impl.h,v 1.17 2007/08/16 21:44:08 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 #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 #define _PROP_EOF(c) ((c) == '\0')
115 #define _PROP_ISSPACE(c) \
116 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || \
117 _PROP_EOF(c))
118
119 #define _PROP_TAG_MATCH(ctx, t) \
120 _prop_object_internalize_match((ctx)->poic_tagname, \
121 (ctx)->poic_tagname_len, \
122 (t), strlen(t))
123
124 #define _PROP_TAGATTR_MATCH(ctx, a) \
125 _prop_object_internalize_match((ctx)->poic_tagattr, \
126 (ctx)->poic_tagattr_len, \
127 (a), strlen(a))
128
129 #define _PROP_TAGATTRVAL_MATCH(ctx, a) \
130 _prop_object_internalize_match((ctx)->poic_tagattrval, \
131 (ctx)->poic_tagattrval_len,\
132 (a), strlen(a))
133
134 bool _prop_object_internalize_find_tag(
135 struct _prop_object_internalize_context *,
136 const char *, _prop_tag_type_t);
137 bool _prop_object_internalize_match(const char *, size_t,
138 const char *, size_t);
139 prop_object_t _prop_object_internalize_by_tag(
140 struct _prop_object_internalize_context *);
141 bool _prop_object_internalize_decode_string(
142 struct _prop_object_internalize_context *,
143 char *, size_t, size_t *, const char **);
144 prop_object_t _prop_generic_internalize(const char *, const char *);
145
146 struct _prop_object_internalize_context *
147 _prop_object_internalize_context_alloc(const char *);
148 void _prop_object_internalize_context_free(
149 struct _prop_object_internalize_context *);
150
151 #if !defined(_KERNEL) && !defined(_STANDALONE)
152 bool _prop_object_externalize_write_file(const char *,
153 const char *, size_t);
154
155 struct _prop_object_internalize_mapped_file {
156 char * poimf_xml;
157 size_t poimf_mapsize;
158 };
159
160 struct _prop_object_internalize_mapped_file *
161 _prop_object_internalize_map_file(const char *);
162 void _prop_object_internalize_unmap_file(
163 struct _prop_object_internalize_mapped_file *);
164 #endif /* !_KERNEL && !_STANDALONE */
165
166 typedef bool (*prop_object_internalizer_t)(prop_stack_t, prop_object_t *,
167 struct _prop_object_internalize_context *);
168 typedef bool (*prop_object_internalizer_continue_t)(prop_stack_t, prop_object_t *,
169 struct _prop_object_internalize_context *, void *, prop_object_t);
170
171 /* These are here because they're required by shared code. */
172 bool _prop_array_internalize(prop_stack_t, prop_object_t *,
173 struct _prop_object_internalize_context *);
174 bool _prop_bool_internalize(prop_stack_t, prop_object_t *,
175 struct _prop_object_internalize_context *);
176 bool _prop_data_internalize(prop_stack_t, prop_object_t *,
177 struct _prop_object_internalize_context *);
178 bool _prop_dictionary_internalize(prop_stack_t, prop_object_t *,
179 struct _prop_object_internalize_context *);
180 bool _prop_number_internalize(prop_stack_t, prop_object_t *,
181 struct _prop_object_internalize_context *);
182 bool _prop_string_internalize(prop_stack_t, prop_object_t *,
183 struct _prop_object_internalize_context *);
184
185 struct _prop_object_type {
186 /* type indicator */
187 uint32_t pot_type;
188 /* func to free object */
189 int (*pot_free)(prop_stack_t, prop_object_t *);
190 /*
191 * func to free the child returned by pot_free with stack == NULL.
192 *
193 * Must be implemented if pot_free can return non-0.
194 */
195 void (*pot_emergency_free)(prop_object_t);
196 /* func to externalize object */
197 bool (*pot_extern)(struct _prop_object_externalize_context *,
198 void *);
199 /* func to test quality */
200 bool (*pot_equals)(void *, void *);
201 };
202
203 struct _prop_object {
204 const struct _prop_object_type *po_type;/* type descriptor */
205 uint32_t po_refcnt; /* reference count */
206 };
207
208 void _prop_object_init(struct _prop_object *,
209 const struct _prop_object_type *);
210 void _prop_object_fini(struct _prop_object *);
211
212 struct _prop_object_iterator {
213 prop_object_t (*pi_next_object)(void *);
214 void (*pi_reset)(void *);
215 prop_object_t pi_obj;
216 uint32_t pi_version;
217 };
218
219 #if defined(_KERNEL)
220
221 /*
222 * proplib in the kernel...
223 */
224
225 #include <sys/param.h>
226 #include <sys/malloc.h>
227 #include <sys/pool.h>
228 #include <sys/systm.h>
229 #include <sys/lock.h>
230 #include <sys/rwlock.h>
231
232 #define _PROP_ASSERT(x) KASSERT(x)
233
234 #define _PROP_MALLOC(s, t) malloc((s), (t), M_WAITOK)
235 #define _PROP_CALLOC(s, t) malloc((s), (t), M_WAITOK | M_ZERO)
236 #define _PROP_REALLOC(v, s, t) realloc((v), (s), (t), M_WAITOK)
237 #define _PROP_FREE(v, t) free((v), (t))
238
239 #define _PROP_POOL_GET(p) pool_get(&(p), PR_WAITOK)
240 #define _PROP_POOL_PUT(p, v) pool_put(&(p), (v))
241
242 #define _PROP_POOL_INIT(p, s, d) \
243 POOL_INIT(p, s, 0, 0, 0, d, &pool_allocator_nointr, IPL_NONE);
244
245 #define _PROP_MALLOC_DEFINE(t, s, l) \
246 MALLOC_DEFINE(t, s, l);
247
248 #define _PROP_MUTEX_DECL_STATIC(x) \
249 static struct simplelock x = SIMPLELOCK_INITIALIZER;
250 #define _PROP_MUTEX_LOCK(x) simple_lock(&(x))
251 #define _PROP_MUTEX_UNLOCK(x) simple_unlock(&(x))
252
253 #define _PROP_RWLOCK_DECL(x) krwlock_t x ;
254 #define _PROP_RWLOCK_INIT(x) rw_init(&(x))
255 #define _PROP_RWLOCK_RDLOCK(x) rw_enter(&(x), RW_READER)
256 #define _PROP_RWLOCK_WRLOCK(x) rw_enter(&(x), RW_WRITER)
257 #define _PROP_RWLOCK_UNLOCK(x) rw_exit(&(x))
258 #define _PROP_RWLOCK_DESTROY(x) rw_destroy(&(x))
259
260 #elif defined(_STANDALONE)
261
262 /*
263 * proplib in a standalone environment...
264 */
265
266 #include <lib/libsa/stand.h>
267
268 void * _prop_standalone_calloc(size_t);
269 void * _prop_standalone_realloc(void *, size_t);
270
271 #define _PROP_ASSERT(x) /* nothing */
272
273 #define _PROP_MALLOC(s, t) alloc((s))
274 #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s))
275 #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s))
276 #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */
277
278 #define _PROP_POOL_GET(p) alloc((p))
279 #define _PROP_POOL_PUT(p, v) dealloc((v), (p))
280
281 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
282
283 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
284
285 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
286 #define _PROP_MUTEX_LOCK(x) /* nothing */
287 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
288
289 #define _PROP_RWLOCK_DECL(x) /* nothing */
290 #define _PROP_RWLOCK_INIT(x) /* nothing */
291 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
292 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
293 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
294 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
295
296 #else
297
298 /*
299 * proplib in user space...
300 */
301
302 #include <assert.h>
303 #include <string.h>
304 #include <stdio.h>
305 #include <stdlib.h>
306 #include <stddef.h>
307
308 #define _PROP_ASSERT(x) /*LINTED*/assert(x)
309
310 #define _PROP_MALLOC(s, t) malloc((s))
311 #define _PROP_CALLOC(s, t) calloc(1, (s))
312 #define _PROP_REALLOC(v, s, t) realloc((v), (s))
313 #define _PROP_FREE(v, t) free((v))
314
315 #define _PROP_POOL_GET(p) malloc((p))
316 #define _PROP_POOL_PUT(p, v) free((v))
317
318 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
319
320 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
321
322 #if defined(__NetBSD__) && defined(_LIBPROP)
323 /*
324 * Use the same mechanism as libc; we get pthread mutexes for threaded
325 * programs and do-nothing stubs for non-threaded programs.
326 */
327 #include "reentrant.h"
328 #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x = MUTEX_INITIALIZER;
329 #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x))
330 #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x))
331
332 #define _PROP_RWLOCK_DECL(x) rwlock_t x ;
333 #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL)
334 #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x))
335 #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x))
336 #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x))
337 #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x))
338 #elif defined(HAVE_NBTOOL_CONFIG_H)
339 /*
340 * None of NetBSD's build tools are multi-threaded.
341 */
342 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
343 #define _PROP_MUTEX_LOCK(x) /* nothing */
344 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
345
346 #define _PROP_RWLOCK_DECL(x) /* nothing */
347 #define _PROP_RWLOCK_INIT(x) /* nothing */
348 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
349 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
350 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
351 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
352 #else
353 /*
354 * Use pthread mutexes everywhere else.
355 */
356 #include <pthread.h>
357 #define _PROP_MUTEX_DECL_STATIC(x) \
358 static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
359 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x))
360 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
361
362 #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ;
363 #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL)
364 #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x))
365 #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x))
366 #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x))
367 #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x))
368 #endif
369
370 #endif /* _KERNEL */
371
372 /*
373 * Language features.
374 */
375 #if defined(__NetBSD__)
376 #include <sys/cdefs.h>
377 #define _PROP_ARG_UNUSED __unused
378 #else
379 #define _PROP_ARG_UNUSED /* delete */
380 #endif /* __NetBSD__ */
381
382 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
383