Home | History | Annotate | Line # | Download | only in libprop
prop_object_impl.h revision 1.3
      1 /*	$NetBSD: prop_object_impl.h,v 1.3 2006/05/18 16:23:55 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 struct _prop_object_externalize_context {
     43 	char *		poec_buf;		/* string buffer */
     44 	size_t		poec_capacity;		/* capacity of buffer */
     45 	size_t		poec_len;		/* current length of string */
     46 	unsigned int	poec_depth;		/* nesting depth */
     47 };
     48 
     49 boolean_t	_prop_object_externalize_start_tag(
     50 				struct _prop_object_externalize_context *,
     51 				const char *);
     52 boolean_t	_prop_object_externalize_end_tag(
     53 				struct _prop_object_externalize_context *,
     54 				const char *);
     55 boolean_t	_prop_object_externalize_empty_tag(
     56 				struct _prop_object_externalize_context *,
     57 				const char *);
     58 boolean_t	_prop_object_externalize_append_cstring(
     59 				struct _prop_object_externalize_context *,
     60 				const char *);
     61 boolean_t	_prop_object_externalize_append_encoded_cstring(
     62 				struct _prop_object_externalize_context *,
     63 				const char *);
     64 boolean_t	_prop_object_externalize_append_char(
     65 				struct _prop_object_externalize_context *,
     66 				unsigned char);
     67 
     68 struct _prop_object_externalize_context *
     69 		_prop_object_externalize_context_alloc(void);
     70 void		_prop_object_externalize_context_free(
     71 				struct _prop_object_externalize_context *);
     72 
     73 typedef enum {
     74 	_PROP_TAG_TYPE_START,			/* e.g. <dict> */
     75 	_PROP_TAG_TYPE_END,			/* e.g. </dict> */
     76 	_PROP_TAG_TYPE_EITHER
     77 } _prop_tag_type_t;
     78 
     79 struct _prop_object_internalize_context {
     80 	const char *poic_xml;
     81 	const char *poic_cp;
     82 
     83 	const char *poic_tag_start;
     84 
     85 	const char *poic_tagname;
     86 	size_t      poic_tagname_len;
     87 	const char *poic_tagattr;
     88 	size_t      poic_tagattr_len;
     89 	const char *poic_tagattrval;
     90 	size_t      poic_tagattrval_len;
     91 
     92 	boolean_t   poic_is_empty_element;
     93 	_prop_tag_type_t poic_tag_type;
     94 };
     95 
     96 #define	_PROP_EOF(c)		((c) == '\0')
     97 #define	_PROP_ISSPACE(c)	\
     98 	((c) == ' ' || (c) == '\t' || (c) == '\n' || _PROP_EOF(c))
     99 
    100 #define	_PROP_TAG_MATCH(ctx, t)					\
    101 	_prop_object_internalize_match((ctx)->poic_tagname,	\
    102 				       (ctx)->poic_tagname_len,	\
    103 				       (t), strlen(t))
    104 
    105 #define	_PROP_TAGATTR_MATCH(ctx, a)				\
    106 	_prop_object_internalize_match((ctx)->poic_tagattr,	\
    107 				       (ctx)->poic_tagattr_len,	\
    108 				       (a), strlen(a))
    109 
    110 #define	_PROP_TAGATTRVAL_MATCH(ctx, a)				  \
    111 	_prop_object_internalize_match((ctx)->poic_tagattrval,	  \
    112 				       (ctx)->poic_tagattrval_len,\
    113 				       (a), strlen(a))
    114 
    115 boolean_t	_prop_object_internalize_find_tag(
    116 				struct _prop_object_internalize_context *,
    117 				const char *, _prop_tag_type_t);
    118 boolean_t	_prop_object_internalize_match(const char *, size_t,
    119 					       const char *, size_t);
    120 prop_object_t	_prop_object_internalize_by_tag(
    121 				struct _prop_object_internalize_context *);
    122 boolean_t	_prop_object_internalize_decode_string(
    123 				struct _prop_object_internalize_context *,
    124 				char *, size_t, size_t *, const char **);
    125 
    126 struct _prop_object_internalize_context *
    127 		_prop_object_internalize_context_alloc(const char *);
    128 void		_prop_object_internalize_context_free(
    129 				struct _prop_object_internalize_context *);
    130 
    131 	/* These are here because they're required by shared code. */
    132 prop_object_t	_prop_array_internalize(
    133 				struct _prop_object_internalize_context *);
    134 prop_object_t	_prop_bool_internalize(
    135 				struct _prop_object_internalize_context *);
    136 prop_object_t	_prop_data_internalize(
    137 				struct _prop_object_internalize_context *);
    138 prop_object_t	_prop_dictionary_internalize(
    139 				struct _prop_object_internalize_context *);
    140 prop_object_t	_prop_number_internalize(
    141 				struct _prop_object_internalize_context *);
    142 prop_object_t	_prop_string_internalize(
    143 				struct _prop_object_internalize_context *);
    144 
    145 struct _prop_object_type {
    146 	uint32_t	pot_type;		/* type indicator */
    147 	void		(*pot_free)(void *);	/* func to free object */
    148 	boolean_t	(*pot_extern)		/* func to externalize object */
    149 			    (struct _prop_object_externalize_context *,
    150 			     void *);
    151 	boolean_t	(*pot_equals)		/* func to test quality */
    152 			    (void *, void *);
    153 };
    154 
    155 struct _prop_object {
    156 	const struct _prop_object_type *po_type;/* type descriptor */
    157 	uint32_t	po_refcnt;		/* reference count */
    158 };
    159 
    160 void	_prop_object_init(struct _prop_object *,
    161 			  const struct _prop_object_type *);
    162 void	_prop_object_fini(struct _prop_object *);
    163 
    164 struct _prop_object_iterator {
    165 	prop_object_t	(*pi_next_object)(void *);
    166 	void		(*pi_reset)(void *);
    167 	prop_object_t	pi_obj;
    168 	uint32_t	pi_version;
    169 };
    170 
    171 #if defined(_KERNEL)
    172 
    173 /*
    174  * proplib in the kernel...
    175  */
    176 
    177 #include <sys/malloc.h>
    178 #include <sys/pool.h>
    179 #include <sys/systm.h>
    180 #include <sys/lock.h>
    181 
    182 #define	_PROP_ASSERT(x)		KASSERT(x)
    183 
    184 #define	_PROP_MALLOC(s, t)	malloc((s), (t), M_WAITOK)
    185 #define	_PROP_CALLOC(s, t)	malloc((s), (t), M_WAITOK | M_ZERO)
    186 #define	_PROP_REALLOC(v, s, t)	realloc((v), (s), (t), M_WAITOK)
    187 #define	_PROP_FREE(v, t)	free((v), (t))
    188 
    189 #define	_PROP_POOL_GET(p)	pool_get(&(p), PR_WAITOK)
    190 #define	_PROP_POOL_PUT(p, v)	pool_put(&(p), (v))
    191 
    192 #define	_PROP_POOL_INIT(p, s, d)					\
    193 		POOL_INIT(p, s, 0, 0, 0, d, &pool_allocator_nointr);
    194 
    195 #define	_PROP_MALLOC_DEFINE(t, s, l)					\
    196 		MALLOC_DEFINE(t, s, l);
    197 
    198 #define	_PROP_MUTEX_DECL(x)						\
    199 		static struct simplelock x = SIMPLELOCK_INITIALIZER;
    200 #define	_PROP_MUTEX_LOCK(x)	simple_lock(&(x))
    201 #define	_PROP_MUTEX_UNLOCK(x)	simple_unlock(&(x))
    202 
    203 #elif defined(_STANDALONE)
    204 
    205 /*
    206  * proplib in a standalone environment...
    207  */
    208 
    209 #include <lib/libsa/stand.h>
    210 #include <lib/libkern/libkern.h>
    211 
    212 void *		_prop_standalone_calloc(size_t);
    213 void *		_prop_standalone_realloc(void *, size_t);
    214 
    215 #define	_PROP_ASSERT(x)		/* nothing */
    216 
    217 #define	_PROP_MALLOC(s, t)	alloc((s))
    218 #define	_PROP_CALLOC(s, t)	_prop_standalone_calloc((s))
    219 #define	_PROP_REALLOC(v, s, t)	_prop_standalone_realloc((v), (s))
    220 #define	_PROP_FREE(v, t)	dealloc((v), 0)		/* XXX */
    221 
    222 #define	_PROP_POOL_GET(p)	alloc((p))
    223 #define	_PROP_POOL_PUT(p, v)	dealloc((v), (p))
    224 
    225 #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    226 
    227 #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    228 
    229 #define	_PROP_MUTEX_DECL(x)	/* nothing */
    230 #define	_PROP_MUTEX_LOCK(x)	/* nothing */
    231 #define	_PROP_MUTEX_UNLOCK(x)	/* nothing */
    232 
    233 #else
    234 
    235 /*
    236  * proplib in user space...
    237  */
    238 
    239 #include <assert.h>
    240 #include <string.h>
    241 #include <stdio.h>
    242 #include <stdlib.h>
    243 
    244 #define	_PROP_ASSERT(x)		assert(x)
    245 
    246 #define	_PROP_MALLOC(s, t)	malloc((s))
    247 #define	_PROP_CALLOC(s, t)	calloc(1, (s))
    248 #define	_PROP_REALLOC(v, s, t)	realloc((v), (s))
    249 #define	_PROP_FREE(v, t)	free((v))
    250 
    251 #define	_PROP_POOL_GET(p)	malloc((p))
    252 #define	_PROP_POOL_PUT(p, v)	free((v))
    253 
    254 #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    255 
    256 #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    257 
    258 #if defined(__NetBSD__) && defined(_LIBPROP)
    259 /*
    260  * Use the same mechanism as libc; we get pthread mutexes for threaded
    261  * programs and do-nothing stubs for non-threaded programs.
    262  */
    263 #include "reentrant.h"
    264 #define	_PROP_MUTEX_DECL(x)	static mutex_t x = MUTEX_INITIALIZER;
    265 #define	_PROP_MUTEX_LOCK(x)	mutex_lock(&(x))
    266 #define	_PROP_MUTEX_UNLOCK(x)	mutex_unlock(&(x))
    267 #elif defined(HAVE_NBTOOL_CONFIG_H)
    268 /*
    269  * None of NetBSD's build tools are multi-threaded.
    270  */
    271 #define	_PROP_MUTEX_DECL(x)	/* nothing */
    272 #define	_PROP_MUTEX_LOCK(x)	/* nothing */
    273 #define	_PROP_MUTEX_UNLOCK(x)	/* nothing */
    274 #else
    275 /*
    276  * Use pthread mutexes everywhere else.
    277  */
    278 #include <pthread.h>
    279 #define	_PROP_MUTEX_DECL(x)						\
    280 		static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
    281 #define	_PROP_MUTEX_LOCK(x)	pthread_mutex_lock(&(x))
    282 #define	_PROP_MUTEX_UNLOCK(x)	pthread_mutex_unlock(&(x))
    283 #endif
    284 
    285 #endif /* _KERNEL */
    286 
    287 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
    288