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