prop_object_impl.h revision 1.16 1 /* $NetBSD: prop_object_impl.h,v 1.16 2007/08/16 16:28:18 thorpej 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 bool _prop_object_externalize_start_tag(
56 struct _prop_object_externalize_context *,
57 const char *);
58 bool _prop_object_externalize_end_tag(
59 struct _prop_object_externalize_context *,
60 const char *);
61 bool _prop_object_externalize_empty_tag(
62 struct _prop_object_externalize_context *,
63 const char *);
64 bool _prop_object_externalize_append_cstring(
65 struct _prop_object_externalize_context *,
66 const char *);
67 bool _prop_object_externalize_append_encoded_cstring(
68 struct _prop_object_externalize_context *,
69 const char *);
70 bool _prop_object_externalize_append_char(
71 struct _prop_object_externalize_context *,
72 unsigned char);
73 bool _prop_object_externalize_header(
74 struct _prop_object_externalize_context *);
75 bool _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 bool 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 bool _prop_object_internalize_find_tag(
127 struct _prop_object_internalize_context *,
128 const char *, _prop_tag_type_t);
129 bool _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 bool _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 bool _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 bool (*pot_extern) /* func to externalize object */
176 (struct _prop_object_externalize_context *,
177 void *);
178 bool (*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 #include <sys/rwlock.h>
210
211 #define _PROP_ASSERT(x) KASSERT(x)
212
213 #define _PROP_MALLOC(s, t) malloc((s), (t), M_WAITOK)
214 #define _PROP_CALLOC(s, t) malloc((s), (t), M_WAITOK | M_ZERO)
215 #define _PROP_REALLOC(v, s, t) realloc((v), (s), (t), M_WAITOK)
216 #define _PROP_FREE(v, t) free((v), (t))
217
218 #define _PROP_POOL_GET(p) pool_get(&(p), PR_WAITOK)
219 #define _PROP_POOL_PUT(p, v) pool_put(&(p), (v))
220
221 #define _PROP_POOL_INIT(p, s, d) \
222 POOL_INIT(p, s, 0, 0, 0, d, &pool_allocator_nointr, IPL_NONE);
223
224 #define _PROP_MALLOC_DEFINE(t, s, l) \
225 MALLOC_DEFINE(t, s, l);
226
227 #define _PROP_MUTEX_DECL_STATIC(x) \
228 static struct simplelock x = SIMPLELOCK_INITIALIZER;
229 #define _PROP_MUTEX_LOCK(x) simple_lock(&(x))
230 #define _PROP_MUTEX_UNLOCK(x) simple_unlock(&(x))
231
232 #define _PROP_RWLOCK_DECL(x) krwlock_t x ;
233 #define _PROP_RWLOCK_INIT(x) rw_init(&(x))
234 #define _PROP_RWLOCK_RDLOCK(x) rw_enter(&(x), RW_READER)
235 #define _PROP_RWLOCK_WRLOCK(x) rw_enter(&(x), RW_WRITER)
236 #define _PROP_RWLOCK_UNLOCK(x) rw_exit(&(x))
237 #define _PROP_RWLOCK_DESTROY(x) rw_destroy(&(x))
238
239 #elif defined(_STANDALONE)
240
241 /*
242 * proplib in a standalone environment...
243 */
244
245 #include <lib/libsa/stand.h>
246
247 void * _prop_standalone_calloc(size_t);
248 void * _prop_standalone_realloc(void *, size_t);
249
250 #define _PROP_ASSERT(x) /* nothing */
251
252 #define _PROP_MALLOC(s, t) alloc((s))
253 #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s))
254 #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s))
255 #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */
256
257 #define _PROP_POOL_GET(p) alloc((p))
258 #define _PROP_POOL_PUT(p, v) dealloc((v), (p))
259
260 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
261
262 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
263
264 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
265 #define _PROP_MUTEX_LOCK(x) /* nothing */
266 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
267
268 #define _PROP_RWLOCK_DECL(x) /* nothing */
269 #define _PROP_RWLOCK_INIT(x) /* nothing */
270 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
271 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
272 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
273 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
274
275 #else
276
277 /*
278 * proplib in user space...
279 */
280
281 #include <assert.h>
282 #include <string.h>
283 #include <stdio.h>
284 #include <stdlib.h>
285 #include <stddef.h>
286
287 #define _PROP_ASSERT(x) /*LINTED*/assert(x)
288
289 #define _PROP_MALLOC(s, t) malloc((s))
290 #define _PROP_CALLOC(s, t) calloc(1, (s))
291 #define _PROP_REALLOC(v, s, t) realloc((v), (s))
292 #define _PROP_FREE(v, t) free((v))
293
294 #define _PROP_POOL_GET(p) malloc((p))
295 #define _PROP_POOL_PUT(p, v) free((v))
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 #if defined(__NetBSD__) && defined(_LIBPROP)
302 /*
303 * Use the same mechanism as libc; we get pthread mutexes for threaded
304 * programs and do-nothing stubs for non-threaded programs.
305 */
306 #include "reentrant.h"
307 #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x = MUTEX_INITIALIZER;
308 #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x))
309 #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x))
310
311 #define _PROP_RWLOCK_DECL(x) rwlock_t x ;
312 #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL)
313 #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x))
314 #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x))
315 #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x))
316 #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x))
317 #elif defined(HAVE_NBTOOL_CONFIG_H)
318 /*
319 * None of NetBSD's build tools are multi-threaded.
320 */
321 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
322 #define _PROP_MUTEX_LOCK(x) /* nothing */
323 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
324
325 #define _PROP_RWLOCK_DECL(x) /* nothing */
326 #define _PROP_RWLOCK_INIT(x) /* nothing */
327 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
328 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
329 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
330 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
331 #else
332 /*
333 * Use pthread mutexes everywhere else.
334 */
335 #include <pthread.h>
336 #define _PROP_MUTEX_DECL_STATIC(x) \
337 static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
338 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x))
339 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
340
341 #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ;
342 #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL)
343 #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x))
344 #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x))
345 #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x))
346 #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x))
347 #endif
348
349 #endif /* _KERNEL */
350
351 /*
352 * Language features.
353 */
354 #if defined(__NetBSD__)
355 #include <sys/cdefs.h>
356 #define _PROP_ARG_UNUSED __unused
357 #else
358 #define _PROP_ARG_UNUSED /* delete */
359 #endif /* __NetBSD__ */
360
361 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
362