kernel - Enable busmaster on bridges (BIOS might not)
[dragonfly.git] / sys / libprop / prop_object_impl.h
blobaeae44ea0797a8cfe9257f118063ba8bfce796df
1 /* $NetBSD: prop_object_impl.h,v 1.30 2009/09/13 18:45:10 pooka Exp $ */
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
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 <inttypes.h>
37 #endif
39 #include "prop_stack.h"
41 struct _prop_object_externalize_context {
42 char * poec_buf; /* string buffer */
43 size_t poec_capacity; /* capacity of buffer */
44 size_t poec_len; /* current length of string */
45 unsigned int poec_depth; /* nesting depth */
48 bool _prop_object_externalize_start_tag(
49 struct _prop_object_externalize_context *,
50 const char *);
51 bool _prop_object_externalize_end_tag(
52 struct _prop_object_externalize_context *,
53 const char *);
54 bool _prop_object_externalize_empty_tag(
55 struct _prop_object_externalize_context *,
56 const char *);
57 bool _prop_object_externalize_append_cstring(
58 struct _prop_object_externalize_context *,
59 const char *);
60 bool _prop_object_externalize_append_encoded_cstring(
61 struct _prop_object_externalize_context *,
62 const char *);
63 bool _prop_object_externalize_append_char(
64 struct _prop_object_externalize_context *,
65 unsigned char);
66 bool _prop_object_externalize_header(
67 struct _prop_object_externalize_context *);
68 bool _prop_object_externalize_footer(
69 struct _prop_object_externalize_context *);
71 struct _prop_object_externalize_context *
72 _prop_object_externalize_context_alloc(void);
73 void _prop_object_externalize_context_free(
74 struct _prop_object_externalize_context *);
76 typedef enum {
77 _PROP_TAG_TYPE_START, /* e.g. <dict> */
78 _PROP_TAG_TYPE_END, /* e.g. </dict> */
79 _PROP_TAG_TYPE_EITHER
80 } _prop_tag_type_t;
82 struct _prop_object_internalize_context {
83 const char *poic_xml;
84 const char *poic_cp;
86 const char *poic_tag_start;
88 const char *poic_tagname;
89 size_t poic_tagname_len;
90 const char *poic_tagattr;
91 size_t poic_tagattr_len;
92 const char *poic_tagattrval;
93 size_t poic_tagattrval_len;
95 bool poic_is_empty_element;
96 _prop_tag_type_t poic_tag_type;
99 typedef enum {
100 _PROP_OBJECT_FREE_DONE,
101 _PROP_OBJECT_FREE_RECURSE,
102 _PROP_OBJECT_FREE_FAILED
103 } _prop_object_free_rv_t;
105 typedef enum {
106 _PROP_OBJECT_EQUALS_FALSE,
107 _PROP_OBJECT_EQUALS_TRUE,
108 _PROP_OBJECT_EQUALS_RECURSE
109 } _prop_object_equals_rv_t;
111 #define _PROP_EOF(c) ((c) == '\0')
112 #define _PROP_ISSPACE(c) \
113 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || \
114 _PROP_EOF(c))
116 #define _PROP_TAG_MATCH(ctx, t) \
117 _prop_object_internalize_match((ctx)->poic_tagname, \
118 (ctx)->poic_tagname_len, \
119 (t), strlen(t))
121 #define _PROP_TAGATTR_MATCH(ctx, a) \
122 _prop_object_internalize_match((ctx)->poic_tagattr, \
123 (ctx)->poic_tagattr_len, \
124 (a), strlen(a))
126 #define _PROP_TAGATTRVAL_MATCH(ctx, a) \
127 _prop_object_internalize_match((ctx)->poic_tagattrval, \
128 (ctx)->poic_tagattrval_len,\
129 (a), strlen(a))
131 bool _prop_object_internalize_find_tag(
132 struct _prop_object_internalize_context *,
133 const char *, _prop_tag_type_t);
134 bool _prop_object_internalize_match(const char *, size_t,
135 const char *, size_t);
136 prop_object_t _prop_object_internalize_by_tag(
137 struct _prop_object_internalize_context *);
138 bool _prop_object_internalize_decode_string(
139 struct _prop_object_internalize_context *,
140 char *, size_t, size_t *, const char **);
141 prop_object_t _prop_generic_internalize(const char *, const char *);
143 struct _prop_object_internalize_context *
144 _prop_object_internalize_context_alloc(const char *);
145 void _prop_object_internalize_context_free(
146 struct _prop_object_internalize_context *);
148 #if !defined(_KERNEL) && !defined(_STANDALONE)
149 bool _prop_object_externalize_write_file(const char *,
150 const char *, size_t);
152 struct _prop_object_internalize_mapped_file {
153 char * poimf_xml;
154 size_t poimf_mapsize;
157 struct _prop_object_internalize_mapped_file *
158 _prop_object_internalize_map_file(const char *);
159 void _prop_object_internalize_unmap_file(
160 struct _prop_object_internalize_mapped_file *);
161 #endif /* !_KERNEL && !_STANDALONE */
163 typedef bool (*prop_object_internalizer_t)(prop_stack_t, prop_object_t *,
164 struct _prop_object_internalize_context *);
165 typedef bool (*prop_object_internalizer_continue_t)(prop_stack_t,
166 prop_object_t *,
167 struct _prop_object_internalize_context *,
168 void *, prop_object_t);
170 /* These are here because they're required by shared code. */
171 bool _prop_array_internalize(prop_stack_t, prop_object_t *,
172 struct _prop_object_internalize_context *);
173 bool _prop_bool_internalize(prop_stack_t, prop_object_t *,
174 struct _prop_object_internalize_context *);
175 bool _prop_data_internalize(prop_stack_t, prop_object_t *,
176 struct _prop_object_internalize_context *);
177 bool _prop_dictionary_internalize(prop_stack_t, prop_object_t *,
178 struct _prop_object_internalize_context *);
179 bool _prop_number_internalize(prop_stack_t, prop_object_t *,
180 struct _prop_object_internalize_context *);
181 bool _prop_string_internalize(prop_stack_t, prop_object_t *,
182 struct _prop_object_internalize_context *);
184 struct _prop_object_type {
185 /* type indicator */
186 uint32_t pot_type;
187 /* func to free object */
188 _prop_object_free_rv_t
189 (*pot_free)(prop_stack_t, prop_object_t *);
191 * func to free the child returned by pot_free with stack == NULL.
193 * Must be implemented if pot_free can return anything other than
194 * _PROP_OBJECT_FREE_DONE.
196 void (*pot_emergency_free)(prop_object_t);
197 /* func to externalize object */
198 bool (*pot_extern)(struct _prop_object_externalize_context *,
199 void *);
200 /* func to test quality */
201 _prop_object_equals_rv_t
202 (*pot_equals)(prop_object_t, prop_object_t,
203 void **, void **,
204 prop_object_t *, prop_object_t *);
206 * func to finish equality iteration.
208 * Must be implemented if pot_equals can return
209 * _PROP_OBJECT_EQUALS_RECURSE
211 void (*pot_equals_finish)(prop_object_t, prop_object_t);
212 void (*pot_lock)(void);
213 void (*pot_unlock)(void);
216 struct _prop_object {
217 const struct _prop_object_type *po_type;/* type descriptor */
218 uint32_t po_refcnt; /* reference count */
221 void _prop_object_init(struct _prop_object *,
222 const struct _prop_object_type *);
223 void _prop_object_fini(struct _prop_object *);
225 struct _prop_object_iterator {
226 prop_object_t (*pi_next_object)(void *);
227 void (*pi_reset)(void *);
228 prop_object_t pi_obj;
229 uint32_t pi_version;
232 #define _PROP_NOTHREAD_ONCE_DECL(x) static bool x = false;
233 #define _PROP_NOTHREAD_ONCE_RUN(x,f) \
234 do { \
235 if ((x) == false) { \
236 f(); \
237 x = true; \
239 } while (/*CONSTCOND*/0)
241 #if defined(_KERNEL)
244 * proplib in the kernel...
247 #include <sys/kernel.h>
248 #include <sys/types.h>
249 #include <sys/param.h>
250 #include <machine/inttypes.h>
251 #include <sys/malloc.h>
252 #include <sys/objcache.h>
253 #include <sys/systm.h>
254 #include <sys/globaldata.h>
255 #include <sys/mutex2.h>
256 #include <sys/lock.h>
258 #define _PROP_ASSERT(x) KKASSERT(x)
260 #define _PROP_MALLOC(s, t) kmalloc((s), (t), M_WAITOK)
261 #define _PROP_CALLOC(s, t) kmalloc((s), (t), M_WAITOK | M_ZERO)
262 #define _PROP_REALLOC(v, s, t) krealloc((v), (s), (t), M_WAITOK)
263 #define _PROP_FREE(v, t) kfree((v), (t))
265 #define _PROP_POOL_GET(p) objcache_get((p), M_WAITOK)
266 #define _PROP_POOL_PUT(p, v) objcache_put((p), (v))
268 struct prop_pool_init {
269 struct pool *pp;
270 size_t size;
271 const char *wchan;
273 #define _PROP_POOL_INIT(pp, size, wchan) \
274 MALLOC_DEFINE(M_##pp, wchan, wchan); \
275 struct objcache *pp; \
276 static void \
277 pp##_init(void) \
279 pp = objcache_create_simple(M_##pp, size); \
281 SYSINIT(pp##_init, SI_SUB_PROP, SI_ORDER_ANY, pp##_init, NULL)
283 #define _PROP_MALLOC_DEFINE(t, s, l) \
284 MALLOC_DEFINE(t, s, l);
287 * NOTE: These locks might be held through a sleep so no spinlocks
288 * can be used.
290 #define _PROP_MUTEX_DECL_STATIC(x) static struct lock x;
291 #define _PROP_MUTEX_INIT(x) lockinit(&(x),"proplib",0,LK_CANRECURSE)
292 #define _PROP_MUTEX_LOCK(x) lockmgr(&(x), LK_EXCLUSIVE)
293 #define _PROP_MUTEX_UNLOCK(x) lockmgr(&(x), LK_RELEASE)
295 #define _PROP_RWLOCK_DECL(x) struct mtx x;
296 #define _PROP_RWLOCK_INIT(x) mtx_init(&(x), "prop")
297 #define _PROP_RWLOCK_RDLOCK(x) mtx_lock(&(x))
298 #define _PROP_RWLOCK_WRLOCK(x) mtx_lock(&(x))
299 #define _PROP_RWLOCK_UNLOCK(x) mtx_unlock(&(x))
300 #define _PROP_RWLOCK_DESTROY(x) mtx_uninit(&(x))
302 #define _PROP_ONCE_DECL(x) static int x = 0;
303 #define _PROP_ONCE_RUN(x,f) if (atomic_cmpset_int(&(x), 0, 1)) f()
305 #elif defined(_STANDALONE)
308 * proplib in a standalone environment...
311 #include <lib/libsa/stand.h>
313 void * _prop_standalone_calloc(size_t);
314 void * _prop_standalone_realloc(void *, size_t);
316 #define _PROP_ASSERT(x) /* nothing */
318 #define _PROP_MALLOC(s, t) alloc((s))
319 #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s))
320 #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s))
321 #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */
323 #define _PROP_POOL_GET(p) alloc((p))
324 #define _PROP_POOL_PUT(p, v) dealloc((v), (p))
326 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
328 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
330 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
331 #define _PROP_MUTEX_INIT(x) /* nothing */
332 #define _PROP_MUTEX_LOCK(x) /* nothing */
333 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
335 #define _PROP_RWLOCK_DECL(x) /* nothing */
336 #define _PROP_RWLOCK_INIT(x) /* nothing */
337 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
338 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
339 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
340 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
342 #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x)
343 #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f)
345 #else
348 * proplib in user space...
351 #include <assert.h>
352 #include <string.h>
353 #include <stdio.h>
354 #include <stdlib.h>
355 #include <stddef.h>
357 #define _PROP_ASSERT(x) /*LINTED*/assert(x)
359 #define _PROP_MALLOC(s, t) malloc((s))
360 #define _PROP_CALLOC(s, t) calloc(1, (s))
361 #define _PROP_REALLOC(v, s, t) realloc((v), (s))
362 #define _PROP_FREE(v, t) free((v))
364 #define _PROP_POOL_GET(p) malloc((p))
365 #define _PROP_POOL_PUT(p, v) free((v))
367 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
369 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
371 #if defined(__NetBSD__) && defined(_LIBPROP)
373 * Use the same mechanism as libc; we get pthread mutexes for threaded
374 * programs and do-nothing stubs for non-threaded programs.
376 #include "reentrant.h"
377 #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x;
378 #define _PROP_MUTEX_INIT(x) mutex_init(&(x), NULL)
379 #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x))
380 #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x))
382 #define _PROP_RWLOCK_DECL(x) rwlock_t x ;
383 #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL)
384 #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x))
385 #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x))
386 #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x))
387 #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x))
389 #define _PROP_ONCE_DECL(x) \
390 static pthread_once_t x = PTHREAD_ONCE_INIT;
391 #define _PROP_ONCE_RUN(x,f) thr_once(&(x), (void(*)(void))f);
393 #elif defined(HAVE_NBTOOL_CONFIG_H)
395 * None of NetBSD's build tools are multi-threaded.
397 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
398 #define _PROP_MUTEX_INIT(x) /* nothing */
399 #define _PROP_MUTEX_LOCK(x) /* nothing */
400 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
402 #define _PROP_RWLOCK_DECL(x) /* nothing */
403 #define _PROP_RWLOCK_INIT(x) /* nothing */
404 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
405 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
406 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
407 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
409 #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x)
410 #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f)
411 #else
413 * Use pthread mutexes everywhere else.
415 #include <pthread.h>
416 #define _PROP_MUTEX_DECL_STATIC(x) static pthread_mutex_t x;
417 #define _PROP_MUTEX_INIT(x) pthread_mutex_init(&(x), NULL)
418 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x))
419 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
421 #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ;
422 #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL)
423 #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x))
424 #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x))
425 #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x))
426 #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x))
428 #define _PROP_ONCE_DECL(x) \
429 static pthread_once_t x = PTHREAD_ONCE_INIT;
430 #define _PROP_ONCE_RUN(x,f) pthread_once(&(x),(void(*)(void))f)
431 #endif
433 #endif /* _KERNEL */
436 * Language features.
438 #include <sys/cdefs.h>
439 #define _PROP_ARG_UNUSED __unused
441 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */