Home | History | Annotate | Line # | Download | only in libprop
prop_object_impl.h revision 1.22
      1 /*	$NetBSD: prop_object_impl.h,v 1.22 2008/05/06 22:57:26 xtraeme 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  *
     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.
     30  */
     31 
     32 #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_
     33 #define	_PROPLIB_PROP_OBJECT_IMPL_H_
     34 
     35 #if defined(_KERNEL) || defined(_STANDALONE)
     36 #include <lib/libkern/libkern.h>
     37 #else
     38 #include <inttypes.h>
     39 #endif
     40 
     41 #include "prop_stack.h"
     42 
     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 */
     48 };
     49 
     50 bool	_prop_object_externalize_start_tag(
     51 				struct _prop_object_externalize_context *,
     52 				const char *);
     53 bool	_prop_object_externalize_end_tag(
     54 				struct _prop_object_externalize_context *,
     55 				const char *);
     56 bool	_prop_object_externalize_empty_tag(
     57 				struct _prop_object_externalize_context *,
     58 				const char *);
     59 bool	_prop_object_externalize_append_cstring(
     60 				struct _prop_object_externalize_context *,
     61 				const char *);
     62 bool	_prop_object_externalize_append_encoded_cstring(
     63 				struct _prop_object_externalize_context *,
     64 				const char *);
     65 bool	_prop_object_externalize_append_char(
     66 				struct _prop_object_externalize_context *,
     67 				unsigned char);
     68 bool	_prop_object_externalize_header(
     69 				struct _prop_object_externalize_context *);
     70 bool	_prop_object_externalize_footer(
     71 				struct _prop_object_externalize_context *);
     72 
     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 *);
     77 
     78 typedef enum {
     79 	_PROP_TAG_TYPE_START,			/* e.g. <dict> */
     80 	_PROP_TAG_TYPE_END,			/* e.g. </dict> */
     81 	_PROP_TAG_TYPE_EITHER
     82 } _prop_tag_type_t;
     83 
     84 struct _prop_object_internalize_context {
     85 	const char *poic_xml;
     86 	const char *poic_cp;
     87 
     88 	const char *poic_tag_start;
     89 
     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;
     96 
     97 	bool   poic_is_empty_element;
     98 	_prop_tag_type_t poic_tag_type;
     99 };
    100 
    101 enum {
    102 	_PROP_OBJECT_FREE_DONE,
    103 	_PROP_OBJECT_FREE_RECURSE,
    104 	_PROP_OBJECT_FREE_FAILED
    105 };
    106 
    107 enum {
    108 	_PROP_OBJECT_EQUALS_FALSE,
    109 	_PROP_OBJECT_EQUALS_TRUE,
    110 	_PROP_OBJECT_EQUALS_RECURSE
    111 };
    112 
    113 #define	_PROP_EOF(c)		((c) == '\0')
    114 #define	_PROP_ISSPACE(c)	\
    115 	((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || \
    116 	 _PROP_EOF(c))
    117 
    118 #define	_PROP_TAG_MATCH(ctx, t)					\
    119 	_prop_object_internalize_match((ctx)->poic_tagname,	\
    120 				       (ctx)->poic_tagname_len,	\
    121 				       (t), strlen(t))
    122 
    123 #define	_PROP_TAGATTR_MATCH(ctx, a)				\
    124 	_prop_object_internalize_match((ctx)->poic_tagattr,	\
    125 				       (ctx)->poic_tagattr_len,	\
    126 				       (a), strlen(a))
    127 
    128 #define	_PROP_TAGATTRVAL_MATCH(ctx, a)				  \
    129 	_prop_object_internalize_match((ctx)->poic_tagattrval,	  \
    130 				       (ctx)->poic_tagattrval_len,\
    131 				       (a), strlen(a))
    132 
    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 *);
    144 
    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 *);
    149 
    150 #if !defined(_KERNEL) && !defined(_STANDALONE)
    151 bool	_prop_object_externalize_write_file(const char *,
    152 						    const char *, size_t);
    153 
    154 struct _prop_object_internalize_mapped_file {
    155 	char *	poimf_xml;
    156 	size_t	poimf_mapsize;
    157 };
    158 
    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 */
    164 
    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, prop_object_t *,
    168     struct _prop_object_internalize_context *, void *, prop_object_t);
    169 
    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 *);
    183 
    184 struct _prop_object_type {
    185 	/* type indicator */
    186 	uint32_t	pot_type;
    187 	/* func to free object */
    188 	int		(*pot_free)(prop_stack_t, prop_object_t *);
    189 	/*
    190 	 * func to free the child returned by pot_free with stack == NULL.
    191 	 *
    192 	 * Must be implemented if pot_free can return anything other than
    193 	 * _PROP_OBJECT_FREE_DONE.
    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)(prop_object_t, prop_object_t,
    201 			      void **, void **,
    202 			      prop_object_t *, prop_object_t *);
    203 	/*
    204 	 * func to finish equality iteration.
    205 	 *
    206 	 * Must be implemented if pot_equals can return
    207 	 * _PROP_OBJECT_EQUALS_RECURSE
    208 	 */
    209 	void	(*pot_equals_finish)(prop_object_t, prop_object_t);
    210 };
    211 
    212 struct _prop_object {
    213 	const struct _prop_object_type *po_type;/* type descriptor */
    214 	uint32_t	po_refcnt;		/* reference count */
    215 };
    216 
    217 void	_prop_object_init(struct _prop_object *,
    218 			  const struct _prop_object_type *);
    219 void	_prop_object_fini(struct _prop_object *);
    220 
    221 struct _prop_object_iterator {
    222 	prop_object_t	(*pi_next_object)(void *);
    223 	void		(*pi_reset)(void *);
    224 	prop_object_t	pi_obj;
    225 	uint32_t	pi_version;
    226 };
    227 
    228 #if defined(_KERNEL)
    229 
    230 /*
    231  * proplib in the kernel...
    232  */
    233 
    234 #include <sys/param.h>
    235 #include <sys/malloc.h>
    236 #include <sys/pool.h>
    237 #include <sys/systm.h>
    238 #include <sys/simplelock.h>
    239 #include <sys/rwlock.h>
    240 
    241 #define	_PROP_ASSERT(x)		KASSERT(x)
    242 
    243 #define	_PROP_MALLOC(s, t)	malloc((s), (t), M_WAITOK)
    244 #define	_PROP_CALLOC(s, t)	malloc((s), (t), M_WAITOK | M_ZERO)
    245 #define	_PROP_REALLOC(v, s, t)	realloc((v), (s), (t), M_WAITOK)
    246 #define	_PROP_FREE(v, t)	free((v), (t))
    247 
    248 #define	_PROP_POOL_GET(p)	pool_get(&(p), PR_WAITOK)
    249 #define	_PROP_POOL_PUT(p, v)	pool_put(&(p), (v))
    250 
    251 #define	_PROP_POOL_INIT(p, s, d)					\
    252 		POOL_INIT(p, s, 0, 0, 0, d, &pool_allocator_nointr, IPL_NONE);
    253 
    254 #define	_PROP_MALLOC_DEFINE(t, s, l)					\
    255 		MALLOC_DEFINE(t, s, l);
    256 
    257 #define	_PROP_MUTEX_DECL_STATIC(x)					\
    258 		static struct simplelock x = SIMPLELOCK_INITIALIZER;
    259 #define	_PROP_MUTEX_LOCK(x)	simple_lock(&(x))
    260 #define	_PROP_MUTEX_UNLOCK(x)	simple_unlock(&(x))
    261 
    262 #define	_PROP_RWLOCK_DECL(x)	krwlock_t x ;
    263 #define	_PROP_RWLOCK_INIT(x)	rw_init(&(x))
    264 #define	_PROP_RWLOCK_RDLOCK(x)	rw_enter(&(x), RW_READER)
    265 #define	_PROP_RWLOCK_WRLOCK(x)	rw_enter(&(x), RW_WRITER)
    266 #define	_PROP_RWLOCK_UNLOCK(x)	rw_exit(&(x))
    267 #define	_PROP_RWLOCK_DESTROY(x)	rw_destroy(&(x))
    268 #define	_PROP_RWLOCK_OWNED(x)	_PROP_ASSERT(rw_lock_held(&(x)))
    269 
    270 static __inline int
    271 _prop_rwlock_tryrdlock(krwlock_t *lock)
    272 {
    273 	return rw_tryenter(lock, RW_READER);
    274 }
    275 
    276 #elif defined(_STANDALONE)
    277 
    278 /*
    279  * proplib in a standalone environment...
    280  */
    281 
    282 #include <lib/libsa/stand.h>
    283 
    284 void *		_prop_standalone_calloc(size_t);
    285 void *		_prop_standalone_realloc(void *, size_t);
    286 
    287 #define	_PROP_ASSERT(x)		/* nothing */
    288 
    289 #define	_PROP_MALLOC(s, t)	alloc((s))
    290 #define	_PROP_CALLOC(s, t)	_prop_standalone_calloc((s))
    291 #define	_PROP_REALLOC(v, s, t)	_prop_standalone_realloc((v), (s))
    292 #define	_PROP_FREE(v, t)	dealloc((v), 0)		/* XXX */
    293 
    294 #define	_PROP_POOL_GET(p)	alloc((p))
    295 #define	_PROP_POOL_PUT(p, v)	dealloc((v), (p))
    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 #define	_PROP_MUTEX_DECL_STATIC(x)	/* nothing */
    302 #define	_PROP_MUTEX_LOCK(x)		/* nothing */
    303 #define	_PROP_MUTEX_UNLOCK(x)		/* nothing */
    304 
    305 #define	_PROP_RWLOCK_DECL(x)	/* nothing */
    306 #define	_PROP_RWLOCK_INIT(x)	/* nothing */
    307 #define	_PROP_RWLOCK_RDLOCK(x)	/* nothing */
    308 #define	_PROP_RWLOCK_WRLOCK(x)	/* nothing */
    309 #define	_PROP_RWLOCK_UNLOCK(x)	/* nothing */
    310 #define	_PROP_RWLOCK_DESTROY(x)	/* nothing */
    311 #define	_PROP_RWLOCK_OWNED(x)	/* nothing */
    312 
    313 static __inline int
    314 _prop_rwlock_tryrdlock(void *v __unused)
    315 {
    316 	return 0;		/* dummy */
    317 }
    318 
    319 #else
    320 
    321 /*
    322  * proplib in user space...
    323  */
    324 
    325 #include <assert.h>
    326 #include <string.h>
    327 #include <stdio.h>
    328 #include <stdlib.h>
    329 #include <stddef.h>
    330 
    331 #define	_PROP_ASSERT(x)		/*LINTED*/assert(x)
    332 
    333 #define	_PROP_MALLOC(s, t)	malloc((s))
    334 #define	_PROP_CALLOC(s, t)	calloc(1, (s))
    335 #define	_PROP_REALLOC(v, s, t)	realloc((v), (s))
    336 #define	_PROP_FREE(v, t)	free((v))
    337 
    338 #define	_PROP_POOL_GET(p)	malloc((p))
    339 #define	_PROP_POOL_PUT(p, v)	free((v))
    340 
    341 #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    342 
    343 #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    344 
    345 #if defined(__NetBSD__) && defined(_LIBPROP)
    346 /*
    347  * Use the same mechanism as libc; we get pthread mutexes for threaded
    348  * programs and do-nothing stubs for non-threaded programs.
    349  */
    350 #include "reentrant.h"
    351 #define	_PROP_MUTEX_DECL_STATIC(x)	static mutex_t x = MUTEX_INITIALIZER;
    352 #define	_PROP_MUTEX_LOCK(x)		mutex_lock(&(x))
    353 #define	_PROP_MUTEX_UNLOCK(x)		mutex_unlock(&(x))
    354 
    355 #define	_PROP_RWLOCK_DECL(x)	rwlock_t x ;
    356 #define	_PROP_RWLOCK_INIT(x)	rwlock_init(&(x), NULL)
    357 #define	_PROP_RWLOCK_RDLOCK(x)	rwlock_rdlock(&(x))
    358 #define	_PROP_RWLOCK_WRLOCK(x)	rwlock_wrlock(&(x))
    359 #define	_PROP_RWLOCK_UNLOCK(x)	rwlock_unlock(&(x))
    360 #define	_PROP_RWLOCK_DESTROY(x)	rwlock_destroy(&(x))
    361 #define	_PROP_RWLOCK_OWNED(x)	/* nothing */
    362 
    363 static __inline int
    364 _prop_rwlock_tryrdlock(rwlock_t *lock)
    365 {
    366 	return rwlock_tryrdlock(lock);
    367 }
    368 
    369 #elif defined(HAVE_NBTOOL_CONFIG_H)
    370 /*
    371  * None of NetBSD's build tools are multi-threaded.
    372  */
    373 #define	_PROP_MUTEX_DECL_STATIC(x)	/* nothing */
    374 #define	_PROP_MUTEX_LOCK(x)		/* nothing */
    375 #define	_PROP_MUTEX_UNLOCK(x)		/* nothing */
    376 
    377 #define	_PROP_RWLOCK_DECL(x)	/* nothing */
    378 #define	_PROP_RWLOCK_INIT(x)	/* nothing */
    379 #define	_PROP_RWLOCK_RDLOCK(x)	/* nothing */
    380 #define	_PROP_RWLOCK_WRLOCK(x)	/* nothing */
    381 #define	_PROP_RWLOCK_UNLOCK(x)	/* nothing */
    382 #define	_PROP_RWLOCK_DESTROY(x)	/* nothing */
    383 #define	_PROP_RWLOCK_OWNED(x)	/* nothing */
    384 
    385 static __inline int
    386 _prop_rwlock_tryrdlock(void *v __unused)
    387 {
    388 	return 0;		/* dummy */
    389 }
    390 
    391 #else
    392 /*
    393  * Use pthread mutexes everywhere else.
    394  */
    395 #include <pthread.h>
    396 #define	_PROP_MUTEX_DECL_STATIC(x)					\
    397 		static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
    398 #define	_PROP_MUTEX_LOCK(x)	pthread_mutex_lock(&(x))
    399 #define	_PROP_MUTEX_UNLOCK(x)	pthread_mutex_unlock(&(x))
    400 
    401 #define	_PROP_RWLOCK_DECL(x)	pthread_rwlock_t x ;
    402 #define	_PROP_RWLOCK_INIT(x)	pthread_rwlock_init(&(x), NULL)
    403 #define	_PROP_RWLOCK_RDLOCK(x)	pthread_rwlock_rdlock(&(x))
    404 #define	_PROP_RWLOCK_WRLOCK(x)	pthread_rwlock_wrlock(&(x))
    405 #define	_PROP_RWLOCK_UNLOCK(x)	pthread_rwlock_unlock(&(x))
    406 #define	_PROP_RWLOCK_DESTROY(x)	pthread_rwlock_destroy(&(x))
    407 #define	_PROP_RWLOCK_OWNED(x)	/* nothing */
    408 
    409 static __inline int
    410 _prop_rwlock_tryrdlock(pthread_rwlock_t *lock)
    411 {
    412 	return pthread_rwlock_tryrdlock(lock);
    413 }
    414 
    415 #endif
    416 
    417 #endif /* _KERNEL */
    418 
    419 /*
    420  * Language features.
    421  */
    422 #if defined(__NetBSD__)
    423 #include <sys/cdefs.h>
    424 #define		_PROP_ARG_UNUSED	__unused
    425 #else
    426 #define		_PROP_ARG_UNUSED	/* delete */
    427 #endif /* __NetBSD__ */
    428 
    429 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
    430