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