Home | History | Annotate | Line # | Download | only in libprop
prop_object_impl.h revision 1.3.2.1
      1  1.3.2.1     tron /*	$NetBSD: prop_object_impl.h,v 1.3.2.1 2006/08/23 21:21:14 tron Exp $	*/
      2      1.1  thorpej 
      3      1.1  thorpej /*-
      4      1.1  thorpej  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5      1.1  thorpej  * All rights reserved.
      6      1.1  thorpej  *
      7      1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  thorpej  * by Jason R. Thorpe.
      9      1.1  thorpej  *
     10      1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11      1.1  thorpej  * modification, are permitted provided that the following conditions
     12      1.1  thorpej  * are met:
     13      1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14      1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15      1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18      1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19      1.1  thorpej  *    must display the following acknowledgement:
     20      1.1  thorpej  *      This product includes software developed by the NetBSD
     21      1.1  thorpej  *      Foundation, Inc. and its contributors.
     22      1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1  thorpej  *    contributors may be used to endorse or promote products derived
     24      1.1  thorpej  *    from this software without specific prior written permission.
     25      1.1  thorpej  *
     26      1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1  thorpej  */
     38      1.1  thorpej 
     39      1.1  thorpej #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_
     40      1.1  thorpej #define	_PROPLIB_PROP_OBJECT_IMPL_H_
     41      1.1  thorpej 
     42      1.1  thorpej struct _prop_object_externalize_context {
     43      1.1  thorpej 	char *		poec_buf;		/* string buffer */
     44      1.1  thorpej 	size_t		poec_capacity;		/* capacity of buffer */
     45      1.1  thorpej 	size_t		poec_len;		/* current length of string */
     46      1.1  thorpej 	unsigned int	poec_depth;		/* nesting depth */
     47      1.1  thorpej };
     48      1.1  thorpej 
     49      1.1  thorpej boolean_t	_prop_object_externalize_start_tag(
     50      1.1  thorpej 				struct _prop_object_externalize_context *,
     51      1.1  thorpej 				const char *);
     52      1.1  thorpej boolean_t	_prop_object_externalize_end_tag(
     53      1.1  thorpej 				struct _prop_object_externalize_context *,
     54      1.1  thorpej 				const char *);
     55      1.1  thorpej boolean_t	_prop_object_externalize_empty_tag(
     56      1.1  thorpej 				struct _prop_object_externalize_context *,
     57      1.1  thorpej 				const char *);
     58      1.1  thorpej boolean_t	_prop_object_externalize_append_cstring(
     59      1.1  thorpej 				struct _prop_object_externalize_context *,
     60      1.1  thorpej 				const char *);
     61      1.1  thorpej boolean_t	_prop_object_externalize_append_encoded_cstring(
     62      1.1  thorpej 				struct _prop_object_externalize_context *,
     63      1.1  thorpej 				const char *);
     64      1.1  thorpej boolean_t	_prop_object_externalize_append_char(
     65      1.1  thorpej 				struct _prop_object_externalize_context *,
     66      1.1  thorpej 				unsigned char);
     67  1.3.2.1     tron boolean_t	_prop_object_externalize_header(
     68  1.3.2.1     tron 				struct _prop_object_externalize_context *);
     69  1.3.2.1     tron boolean_t	_prop_object_externalize_footer(
     70  1.3.2.1     tron 				struct _prop_object_externalize_context *);
     71      1.1  thorpej 
     72      1.1  thorpej struct _prop_object_externalize_context *
     73      1.1  thorpej 		_prop_object_externalize_context_alloc(void);
     74      1.1  thorpej void		_prop_object_externalize_context_free(
     75      1.1  thorpej 				struct _prop_object_externalize_context *);
     76      1.1  thorpej 
     77      1.1  thorpej typedef enum {
     78      1.1  thorpej 	_PROP_TAG_TYPE_START,			/* e.g. <dict> */
     79      1.1  thorpej 	_PROP_TAG_TYPE_END,			/* e.g. </dict> */
     80      1.1  thorpej 	_PROP_TAG_TYPE_EITHER
     81      1.1  thorpej } _prop_tag_type_t;
     82      1.1  thorpej 
     83      1.1  thorpej struct _prop_object_internalize_context {
     84      1.1  thorpej 	const char *poic_xml;
     85      1.1  thorpej 	const char *poic_cp;
     86      1.1  thorpej 
     87      1.1  thorpej 	const char *poic_tag_start;
     88      1.1  thorpej 
     89      1.1  thorpej 	const char *poic_tagname;
     90      1.1  thorpej 	size_t      poic_tagname_len;
     91      1.1  thorpej 	const char *poic_tagattr;
     92      1.1  thorpej 	size_t      poic_tagattr_len;
     93      1.1  thorpej 	const char *poic_tagattrval;
     94      1.1  thorpej 	size_t      poic_tagattrval_len;
     95      1.1  thorpej 
     96      1.1  thorpej 	boolean_t   poic_is_empty_element;
     97      1.1  thorpej 	_prop_tag_type_t poic_tag_type;
     98      1.1  thorpej };
     99      1.1  thorpej 
    100      1.1  thorpej #define	_PROP_EOF(c)		((c) == '\0')
    101      1.1  thorpej #define	_PROP_ISSPACE(c)	\
    102      1.1  thorpej 	((c) == ' ' || (c) == '\t' || (c) == '\n' || _PROP_EOF(c))
    103      1.1  thorpej 
    104      1.1  thorpej #define	_PROP_TAG_MATCH(ctx, t)					\
    105      1.1  thorpej 	_prop_object_internalize_match((ctx)->poic_tagname,	\
    106      1.1  thorpej 				       (ctx)->poic_tagname_len,	\
    107      1.1  thorpej 				       (t), strlen(t))
    108      1.1  thorpej 
    109      1.1  thorpej #define	_PROP_TAGATTR_MATCH(ctx, a)				\
    110      1.1  thorpej 	_prop_object_internalize_match((ctx)->poic_tagattr,	\
    111      1.1  thorpej 				       (ctx)->poic_tagattr_len,	\
    112      1.1  thorpej 				       (a), strlen(a))
    113      1.1  thorpej 
    114      1.1  thorpej #define	_PROP_TAGATTRVAL_MATCH(ctx, a)				  \
    115      1.1  thorpej 	_prop_object_internalize_match((ctx)->poic_tagattrval,	  \
    116      1.1  thorpej 				       (ctx)->poic_tagattrval_len,\
    117      1.1  thorpej 				       (a), strlen(a))
    118      1.1  thorpej 
    119      1.1  thorpej boolean_t	_prop_object_internalize_find_tag(
    120      1.1  thorpej 				struct _prop_object_internalize_context *,
    121      1.1  thorpej 				const char *, _prop_tag_type_t);
    122      1.1  thorpej boolean_t	_prop_object_internalize_match(const char *, size_t,
    123      1.1  thorpej 					       const char *, size_t);
    124      1.1  thorpej prop_object_t	_prop_object_internalize_by_tag(
    125      1.1  thorpej 				struct _prop_object_internalize_context *);
    126      1.1  thorpej boolean_t	_prop_object_internalize_decode_string(
    127      1.1  thorpej 				struct _prop_object_internalize_context *,
    128      1.1  thorpej 				char *, size_t, size_t *, const char **);
    129      1.1  thorpej 
    130      1.1  thorpej struct _prop_object_internalize_context *
    131      1.1  thorpej 		_prop_object_internalize_context_alloc(const char *);
    132      1.1  thorpej void		_prop_object_internalize_context_free(
    133      1.1  thorpej 				struct _prop_object_internalize_context *);
    134      1.1  thorpej 
    135  1.3.2.1     tron #if !defined(_KERNEL) && !defined(_STANDALONE)
    136  1.3.2.1     tron boolean_t	_prop_object_externalize_write_file(const char *,
    137  1.3.2.1     tron 						    const char *, size_t);
    138  1.3.2.1     tron 
    139  1.3.2.1     tron struct _prop_object_internalize_mapped_file {
    140  1.3.2.1     tron 	char *	poimf_xml;
    141  1.3.2.1     tron 	size_t	poimf_mapsize;
    142  1.3.2.1     tron };
    143  1.3.2.1     tron 
    144  1.3.2.1     tron struct _prop_object_internalize_mapped_file *
    145  1.3.2.1     tron 		_prop_object_internalize_map_file(const char *);
    146  1.3.2.1     tron void		_prop_object_internalize_unmap_file(
    147  1.3.2.1     tron 				struct _prop_object_internalize_mapped_file *);
    148  1.3.2.1     tron #endif /* !_KERNEL && !_STANDALONE */
    149  1.3.2.1     tron 
    150      1.1  thorpej 	/* These are here because they're required by shared code. */
    151      1.1  thorpej prop_object_t	_prop_array_internalize(
    152      1.1  thorpej 				struct _prop_object_internalize_context *);
    153      1.1  thorpej prop_object_t	_prop_bool_internalize(
    154      1.1  thorpej 				struct _prop_object_internalize_context *);
    155      1.1  thorpej prop_object_t	_prop_data_internalize(
    156      1.1  thorpej 				struct _prop_object_internalize_context *);
    157      1.1  thorpej prop_object_t	_prop_dictionary_internalize(
    158      1.1  thorpej 				struct _prop_object_internalize_context *);
    159      1.1  thorpej prop_object_t	_prop_number_internalize(
    160      1.1  thorpej 				struct _prop_object_internalize_context *);
    161      1.1  thorpej prop_object_t	_prop_string_internalize(
    162      1.1  thorpej 				struct _prop_object_internalize_context *);
    163      1.1  thorpej 
    164      1.2  thorpej struct _prop_object_type {
    165      1.2  thorpej 	uint32_t	pot_type;		/* type indicator */
    166      1.2  thorpej 	void		(*pot_free)(void *);	/* func to free object */
    167      1.2  thorpej 	boolean_t	(*pot_extern)		/* func to externalize object */
    168      1.2  thorpej 			    (struct _prop_object_externalize_context *,
    169      1.2  thorpej 			     void *);
    170      1.2  thorpej 	boolean_t	(*pot_equals)		/* func to test quality */
    171      1.2  thorpej 			    (void *, void *);
    172      1.2  thorpej };
    173      1.2  thorpej 
    174      1.1  thorpej struct _prop_object {
    175      1.2  thorpej 	const struct _prop_object_type *po_type;/* type descriptor */
    176      1.1  thorpej 	uint32_t	po_refcnt;		/* reference count */
    177      1.1  thorpej };
    178      1.1  thorpej 
    179      1.2  thorpej void	_prop_object_init(struct _prop_object *,
    180      1.2  thorpej 			  const struct _prop_object_type *);
    181      1.1  thorpej void	_prop_object_fini(struct _prop_object *);
    182      1.1  thorpej 
    183      1.1  thorpej struct _prop_object_iterator {
    184      1.1  thorpej 	prop_object_t	(*pi_next_object)(void *);
    185      1.1  thorpej 	void		(*pi_reset)(void *);
    186      1.1  thorpej 	prop_object_t	pi_obj;
    187      1.1  thorpej 	uint32_t	pi_version;
    188      1.1  thorpej };
    189      1.1  thorpej 
    190      1.1  thorpej #if defined(_KERNEL)
    191      1.1  thorpej 
    192      1.1  thorpej /*
    193      1.1  thorpej  * proplib in the kernel...
    194      1.1  thorpej  */
    195      1.1  thorpej 
    196      1.1  thorpej #include <sys/malloc.h>
    197      1.1  thorpej #include <sys/pool.h>
    198      1.1  thorpej #include <sys/systm.h>
    199      1.3  thorpej #include <sys/lock.h>
    200      1.1  thorpej 
    201      1.1  thorpej #define	_PROP_ASSERT(x)		KASSERT(x)
    202      1.1  thorpej 
    203      1.1  thorpej #define	_PROP_MALLOC(s, t)	malloc((s), (t), M_WAITOK)
    204      1.1  thorpej #define	_PROP_CALLOC(s, t)	malloc((s), (t), M_WAITOK | M_ZERO)
    205      1.2  thorpej #define	_PROP_REALLOC(v, s, t)	realloc((v), (s), (t), M_WAITOK)
    206      1.1  thorpej #define	_PROP_FREE(v, t)	free((v), (t))
    207      1.1  thorpej 
    208      1.1  thorpej #define	_PROP_POOL_GET(p)	pool_get(&(p), PR_WAITOK)
    209      1.1  thorpej #define	_PROP_POOL_PUT(p, v)	pool_put(&(p), (v))
    210      1.1  thorpej 
    211      1.1  thorpej #define	_PROP_POOL_INIT(p, s, d)					\
    212      1.1  thorpej 		POOL_INIT(p, s, 0, 0, 0, d, &pool_allocator_nointr);
    213      1.1  thorpej 
    214      1.1  thorpej #define	_PROP_MALLOC_DEFINE(t, s, l)					\
    215      1.1  thorpej 		MALLOC_DEFINE(t, s, l);
    216      1.1  thorpej 
    217      1.3  thorpej #define	_PROP_MUTEX_DECL(x)						\
    218      1.3  thorpej 		static struct simplelock x = SIMPLELOCK_INITIALIZER;
    219      1.3  thorpej #define	_PROP_MUTEX_LOCK(x)	simple_lock(&(x))
    220      1.3  thorpej #define	_PROP_MUTEX_UNLOCK(x)	simple_unlock(&(x))
    221      1.3  thorpej 
    222      1.1  thorpej #elif defined(_STANDALONE)
    223      1.1  thorpej 
    224      1.1  thorpej /*
    225      1.1  thorpej  * proplib in a standalone environment...
    226      1.1  thorpej  */
    227      1.1  thorpej 
    228      1.1  thorpej #include <lib/libsa/stand.h>
    229      1.1  thorpej #include <lib/libkern/libkern.h>
    230      1.1  thorpej 
    231      1.1  thorpej void *		_prop_standalone_calloc(size_t);
    232      1.2  thorpej void *		_prop_standalone_realloc(void *, size_t);
    233      1.1  thorpej 
    234      1.1  thorpej #define	_PROP_ASSERT(x)		/* nothing */
    235      1.1  thorpej 
    236      1.1  thorpej #define	_PROP_MALLOC(s, t)	alloc((s))
    237      1.1  thorpej #define	_PROP_CALLOC(s, t)	_prop_standalone_calloc((s))
    238      1.2  thorpej #define	_PROP_REALLOC(v, s, t)	_prop_standalone_realloc((v), (s))
    239      1.1  thorpej #define	_PROP_FREE(v, t)	dealloc((v), 0)		/* XXX */
    240      1.1  thorpej 
    241      1.1  thorpej #define	_PROP_POOL_GET(p)	alloc((p))
    242      1.1  thorpej #define	_PROP_POOL_PUT(p, v)	dealloc((v), (p))
    243      1.1  thorpej 
    244      1.1  thorpej #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    245      1.1  thorpej 
    246      1.1  thorpej #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    247      1.1  thorpej 
    248      1.3  thorpej #define	_PROP_MUTEX_DECL(x)	/* nothing */
    249      1.3  thorpej #define	_PROP_MUTEX_LOCK(x)	/* nothing */
    250      1.3  thorpej #define	_PROP_MUTEX_UNLOCK(x)	/* nothing */
    251      1.3  thorpej 
    252      1.1  thorpej #else
    253      1.1  thorpej 
    254      1.1  thorpej /*
    255      1.1  thorpej  * proplib in user space...
    256      1.1  thorpej  */
    257      1.1  thorpej 
    258      1.1  thorpej #include <assert.h>
    259      1.1  thorpej #include <string.h>
    260      1.1  thorpej #include <stdio.h>
    261      1.1  thorpej #include <stdlib.h>
    262      1.1  thorpej 
    263      1.1  thorpej #define	_PROP_ASSERT(x)		assert(x)
    264      1.1  thorpej 
    265      1.1  thorpej #define	_PROP_MALLOC(s, t)	malloc((s))
    266      1.1  thorpej #define	_PROP_CALLOC(s, t)	calloc(1, (s))
    267      1.2  thorpej #define	_PROP_REALLOC(v, s, t)	realloc((v), (s))
    268      1.1  thorpej #define	_PROP_FREE(v, t)	free((v))
    269      1.1  thorpej 
    270      1.1  thorpej #define	_PROP_POOL_GET(p)	malloc((p))
    271      1.1  thorpej #define	_PROP_POOL_PUT(p, v)	free((v))
    272      1.1  thorpej 
    273      1.1  thorpej #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    274      1.1  thorpej 
    275      1.1  thorpej #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    276      1.1  thorpej 
    277      1.3  thorpej #if defined(__NetBSD__) && defined(_LIBPROP)
    278      1.3  thorpej /*
    279      1.3  thorpej  * Use the same mechanism as libc; we get pthread mutexes for threaded
    280      1.3  thorpej  * programs and do-nothing stubs for non-threaded programs.
    281      1.3  thorpej  */
    282      1.3  thorpej #include "reentrant.h"
    283      1.3  thorpej #define	_PROP_MUTEX_DECL(x)	static mutex_t x = MUTEX_INITIALIZER;
    284      1.3  thorpej #define	_PROP_MUTEX_LOCK(x)	mutex_lock(&(x))
    285      1.3  thorpej #define	_PROP_MUTEX_UNLOCK(x)	mutex_unlock(&(x))
    286      1.3  thorpej #elif defined(HAVE_NBTOOL_CONFIG_H)
    287      1.3  thorpej /*
    288      1.3  thorpej  * None of NetBSD's build tools are multi-threaded.
    289      1.3  thorpej  */
    290      1.3  thorpej #define	_PROP_MUTEX_DECL(x)	/* nothing */
    291      1.3  thorpej #define	_PROP_MUTEX_LOCK(x)	/* nothing */
    292      1.3  thorpej #define	_PROP_MUTEX_UNLOCK(x)	/* nothing */
    293      1.3  thorpej #else
    294      1.3  thorpej /*
    295      1.3  thorpej  * Use pthread mutexes everywhere else.
    296      1.3  thorpej  */
    297      1.3  thorpej #include <pthread.h>
    298      1.3  thorpej #define	_PROP_MUTEX_DECL(x)						\
    299      1.3  thorpej 		static pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
    300      1.3  thorpej #define	_PROP_MUTEX_LOCK(x)	pthread_mutex_lock(&(x))
    301      1.3  thorpej #define	_PROP_MUTEX_UNLOCK(x)	pthread_mutex_unlock(&(x))
    302      1.3  thorpej #endif
    303      1.3  thorpej 
    304      1.1  thorpej #endif /* _KERNEL */
    305      1.1  thorpej 
    306      1.1  thorpej #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
    307