1 /* $NetBSD: prop_object_impl.h,v 1.30 2009/09/13 18:45:10 pooka Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_
33 #define _PROPLIB_PROP_OBJECT_IMPL_H_
35 #if defined(_KERNEL) || defined(_STANDALONE)
36 #include <sys/libkern.h>
41 #include "prop_stack.h"
43 struct _prop_object_externalize_context {
44 char * poec_buf; /* string buffer */
45 size_t poec_capacity; /* capacity of buffer */
46 size_t poec_len; /* current length of string */
47 unsigned int poec_depth; /* nesting depth */
50 bool _prop_object_externalize_start_tag(
51 struct _prop_object_externalize_context *,
53 bool _prop_object_externalize_end_tag(
54 struct _prop_object_externalize_context *,
56 bool _prop_object_externalize_empty_tag(
57 struct _prop_object_externalize_context *,
59 bool _prop_object_externalize_append_cstring(
60 struct _prop_object_externalize_context *,
62 bool _prop_object_externalize_append_encoded_cstring(
63 struct _prop_object_externalize_context *,
65 bool _prop_object_externalize_append_char(
66 struct _prop_object_externalize_context *,
68 bool _prop_object_externalize_header(
69 struct _prop_object_externalize_context *);
70 bool _prop_object_externalize_footer(
71 struct _prop_object_externalize_context *);
73 struct _prop_object_externalize_context *
74 _prop_object_externalize_context_alloc(void);
75 void _prop_object_externalize_context_free(
76 struct _prop_object_externalize_context *);
79 _PROP_TAG_TYPE_START, /* e.g. <dict> */
80 _PROP_TAG_TYPE_END, /* e.g. </dict> */
84 struct _prop_object_internalize_context {
88 const char *poic_tag_start;
90 const char *poic_tagname;
91 size_t poic_tagname_len;
92 const char *poic_tagattr;
93 size_t poic_tagattr_len;
94 const char *poic_tagattrval;
95 size_t poic_tagattrval_len;
97 bool poic_is_empty_element;
98 _prop_tag_type_t poic_tag_type;
102 _PROP_OBJECT_FREE_DONE,
103 _PROP_OBJECT_FREE_RECURSE,
104 _PROP_OBJECT_FREE_FAILED
105 } _prop_object_free_rv_t;
108 _PROP_OBJECT_EQUALS_FALSE,
109 _PROP_OBJECT_EQUALS_TRUE,
110 _PROP_OBJECT_EQUALS_RECURSE
111 } _prop_object_equals_rv_t;
113 #define _PROP_EOF(c) ((c) == '\0')
114 #define _PROP_ISSPACE(c) \
115 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || \
118 #define _PROP_TAG_MATCH(ctx, t) \
119 _prop_object_internalize_match((ctx)->poic_tagname, \
120 (ctx)->poic_tagname_len, \
123 #define _PROP_TAGATTR_MATCH(ctx, a) \
124 _prop_object_internalize_match((ctx)->poic_tagattr, \
125 (ctx)->poic_tagattr_len, \
128 #define _PROP_TAGATTRVAL_MATCH(ctx, a) \
129 _prop_object_internalize_match((ctx)->poic_tagattrval, \
130 (ctx)->poic_tagattrval_len,\
133 bool _prop_object_internalize_find_tag(
134 struct _prop_object_internalize_context *,
135 const char *, _prop_tag_type_t);
136 bool _prop_object_internalize_match(const char *, size_t,
137 const char *, size_t);
138 prop_object_t _prop_object_internalize_by_tag(
139 struct _prop_object_internalize_context *);
140 bool _prop_object_internalize_decode_string(
141 struct _prop_object_internalize_context *,
142 char *, size_t, size_t *, const char **);
143 prop_object_t _prop_generic_internalize(const char *, const char *);
145 struct _prop_object_internalize_context *
146 _prop_object_internalize_context_alloc(const char *);
147 void _prop_object_internalize_context_free(
148 struct _prop_object_internalize_context *);
150 #if !defined(_KERNEL) && !defined(_STANDALONE)
151 bool _prop_object_externalize_write_file(const char *,
152 const char *, size_t);
154 struct _prop_object_internalize_mapped_file {
156 size_t poimf_mapsize;
159 struct _prop_object_internalize_mapped_file *
160 _prop_object_internalize_map_file(const char *);
161 void _prop_object_internalize_unmap_file(
162 struct _prop_object_internalize_mapped_file *);
163 #endif /* !_KERNEL && !_STANDALONE */
165 typedef bool (*prop_object_internalizer_t)(prop_stack_t, prop_object_t *,
166 struct _prop_object_internalize_context *);
167 typedef bool (*prop_object_internalizer_continue_t)(prop_stack_t,
169 struct _prop_object_internalize_context *,
170 void *, prop_object_t);
172 /* These are here because they're required by shared code. */
173 bool _prop_array_internalize(prop_stack_t, prop_object_t *,
174 struct _prop_object_internalize_context *);
175 bool _prop_bool_internalize(prop_stack_t, prop_object_t *,
176 struct _prop_object_internalize_context *);
177 bool _prop_data_internalize(prop_stack_t, prop_object_t *,
178 struct _prop_object_internalize_context *);
179 bool _prop_dictionary_internalize(prop_stack_t, prop_object_t *,
180 struct _prop_object_internalize_context *);
181 bool _prop_number_internalize(prop_stack_t, prop_object_t *,
182 struct _prop_object_internalize_context *);
183 bool _prop_string_internalize(prop_stack_t, prop_object_t *,
184 struct _prop_object_internalize_context *);
186 struct _prop_object_type {
189 /* func to free object */
190 _prop_object_free_rv_t
191 (*pot_free)(prop_stack_t, prop_object_t *);
193 * func to free the child returned by pot_free with stack == NULL.
195 * Must be implemented if pot_free can return anything other than
196 * _PROP_OBJECT_FREE_DONE.
198 void (*pot_emergency_free)(prop_object_t);
199 /* func to externalize object */
200 bool (*pot_extern)(struct _prop_object_externalize_context *,
202 /* func to test quality */
203 _prop_object_equals_rv_t
204 (*pot_equals)(prop_object_t, prop_object_t,
206 prop_object_t *, prop_object_t *);
208 * func to finish equality iteration.
210 * Must be implemented if pot_equals can return
211 * _PROP_OBJECT_EQUALS_RECURSE
213 void (*pot_equals_finish)(prop_object_t, prop_object_t);
214 void (*pot_lock)(void);
215 void (*pot_unlock)(void);
218 struct _prop_object {
219 const struct _prop_object_type *po_type;/* type descriptor */
220 uint32_t po_refcnt; /* reference count */
223 void _prop_object_init(struct _prop_object *,
224 const struct _prop_object_type *);
225 void _prop_object_fini(struct _prop_object *);
227 struct _prop_object_iterator {
228 prop_object_t (*pi_next_object)(void *);
229 void (*pi_reset)(void *);
230 prop_object_t pi_obj;
234 #define _PROP_NOTHREAD_ONCE_DECL(x) static bool x = false;
235 #define _PROP_NOTHREAD_ONCE_RUN(x,f) \
237 if ((x) == false) { \
241 } while (/*CONSTCOND*/0)
246 * proplib in the kernel...
249 #include <sys/kernel.h>
250 #include <sys/types.h>
251 #include <sys/param.h>
252 #include <machine/inttypes.h>
253 #include <sys/malloc.h>
254 #include <sys/objcache.h>
255 #include <sys/systm.h>
256 #include <sys/spinlock.h>
257 #include <sys/spinlock2.h>
258 #include <sys/lock.h>
260 #define _PROP_ASSERT(x) KKASSERT(x)
262 #define _PROP_MALLOC(s, t) kmalloc((s), (t), M_WAITOK)
263 #define _PROP_CALLOC(s, t) kmalloc((s), (t), M_WAITOK | M_ZERO)
264 #define _PROP_REALLOC(v, s, t) krealloc((v), (s), (t), M_WAITOK)
265 #define _PROP_FREE(v, t) kfree((v), (t))
267 #define _PROP_POOL_GET(p) objcache_get((p), M_WAITOK)
268 #define _PROP_POOL_PUT(p, v) objcache_put((p), (v))
270 struct prop_pool_init {
275 #define _PROP_POOL_INIT(pp, size, wchan) \
276 MALLOC_DEFINE(M_##pp, wchan, wchan); \
277 struct objcache *pp; \
281 pp = objcache_create_simple(M_##pp, size); \
283 SYSINIT(pp##_init, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, pp##_init, NULL)
285 #define _PROP_MALLOC_DEFINE(t, s, l) \
286 MALLOC_DEFINE(t, s, l);
288 #define _PROP_MUTEX_DECL_STATIC(x) static struct lock x;
289 #define _PROP_MUTEX_INIT(x) lockinit(&(x),"proplib",0,LK_CANRECURSE)
290 #define _PROP_MUTEX_LOCK(x) lockmgr(&(x), LK_EXCLUSIVE)
291 #define _PROP_MUTEX_UNLOCK(x) lockmgr(&(x), LK_RELEASE)
293 #define _PROP_RWLOCK_DECL(x) struct spinlock x;
294 #define _PROP_RWLOCK_INIT(x) spin_init(&(x))
295 #define _PROP_RWLOCK_RDLOCK(x) spin_lock_wr(&(x))
296 #define _PROP_RWLOCK_WRLOCK(x) spin_lock_wr(&(x))
297 #define _PROP_RWLOCK_UNLOCK(x) spin_unlock_wr(&(x))
298 #define _PROP_RWLOCK_DESTROY(x) spin_uninit(&(x))
300 #define _PROP_ONCE_DECL(x) static int x = 0;
301 #define _PROP_ONCE_RUN(x,f) if (atomic_cmpset_int(&(x), 0, 1)) f()
303 #elif defined(_STANDALONE)
306 * proplib in a standalone environment...
309 #include <lib/libsa/stand.h>
311 void * _prop_standalone_calloc(size_t);
312 void * _prop_standalone_realloc(void *, size_t);
314 #define _PROP_ASSERT(x) /* nothing */
316 #define _PROP_MALLOC(s, t) alloc((s))
317 #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s))
318 #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s))
319 #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */
321 #define _PROP_POOL_GET(p) alloc((p))
322 #define _PROP_POOL_PUT(p, v) dealloc((v), (p))
324 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
326 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
328 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
329 #define _PROP_MUTEX_INIT(x) /* nothing */
330 #define _PROP_MUTEX_LOCK(x) /* nothing */
331 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
333 #define _PROP_RWLOCK_DECL(x) /* nothing */
334 #define _PROP_RWLOCK_INIT(x) /* nothing */
335 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
336 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
337 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
338 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
340 #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x)
341 #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f)
346 * proplib in user space...
355 #define _PROP_ASSERT(x) /*LINTED*/assert(x)
357 #define _PROP_MALLOC(s, t) malloc((s))
358 #define _PROP_CALLOC(s, t) calloc(1, (s))
359 #define _PROP_REALLOC(v, s, t) realloc((v), (s))
360 #define _PROP_FREE(v, t) free((v))
362 #define _PROP_POOL_GET(p) malloc((p))
363 #define _PROP_POOL_PUT(p, v) free((v))
365 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
367 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
369 #if defined(__NetBSD__) && defined(_LIBPROP)
371 * Use the same mechanism as libc; we get pthread mutexes for threaded
372 * programs and do-nothing stubs for non-threaded programs.
374 #include "reentrant.h"
375 #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x;
376 #define _PROP_MUTEX_INIT(x) mutex_init(&(x), NULL)
377 #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x))
378 #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x))
380 #define _PROP_RWLOCK_DECL(x) rwlock_t x ;
381 #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL)
382 #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x))
383 #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x))
384 #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x))
385 #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x))
387 #define _PROP_ONCE_DECL(x) \
388 static pthread_once_t x = PTHREAD_ONCE_INIT;
389 #define _PROP_ONCE_RUN(x,f) thr_once(&(x), (void(*)(void))f);
391 #elif defined(HAVE_NBTOOL_CONFIG_H)
393 * None of NetBSD's build tools are multi-threaded.
395 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
396 #define _PROP_MUTEX_INIT(x) /* nothing */
397 #define _PROP_MUTEX_LOCK(x) /* nothing */
398 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
400 #define _PROP_RWLOCK_DECL(x) /* nothing */
401 #define _PROP_RWLOCK_INIT(x) /* nothing */
402 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
403 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
404 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
405 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
407 #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x)
408 #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f)
411 * Use pthread mutexes everywhere else.
414 #define _PROP_MUTEX_DECL_STATIC(x) static pthread_mutex_t x;
415 #define _PROP_MUTEX_INIT(x) pthread_mutex_init(&(x), NULL)
416 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x))
417 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
419 #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ;
420 #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL)
421 #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x))
422 #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x))
423 #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x))
424 #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x))
426 #define _PROP_ONCE_DECL(x) \
427 static pthread_once_t x = PTHREAD_ONCE_INIT;
428 #define _PROP_ONCE_RUN(x,f) pthread_once(&(x),(void(*)(void))f)
436 #include <sys/cdefs.h>
437 #define _PROP_ARG_UNUSED __unused
439 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */