xref: /netbsd-src/common/lib/libprop/prop_object_impl.h (revision 76c7fc5f6b13ed0b1508e6b313e88e59977ed78e)
1 /*	$NetBSD: prop_object_impl.h,v 1.33 2019/05/08 02:25:50 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  *
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(HAVE_NBTOOL_CONFIG_H)
36 #include "nbtool_config.h"
37 #endif
38 
39 #if defined(_KERNEL) || defined(_STANDALONE)
40 #include <lib/libkern/libkern.h>
41 #else
42 #include <inttypes.h>
43 #endif
44 
45 #include "prop_stack.h"
46 
47 struct _prop_object_externalize_context {
48 	char *		poec_buf;		/* string buffer */
49 	size_t		poec_capacity;		/* capacity of buffer */
50 	size_t		poec_len;		/* current length of string */
51 	unsigned int	poec_depth;		/* nesting depth */
52 };
53 
54 bool		_prop_object_externalize_start_tag(
55 				struct _prop_object_externalize_context *,
56 				const char *);
57 bool		_prop_object_externalize_end_tag(
58 				struct _prop_object_externalize_context *,
59 				const char *);
60 bool		_prop_object_externalize_empty_tag(
61 				struct _prop_object_externalize_context *,
62 				const char *);
63 bool		_prop_object_externalize_append_cstring(
64 				struct _prop_object_externalize_context *,
65 				const char *);
66 bool		_prop_object_externalize_append_encoded_cstring(
67 				struct _prop_object_externalize_context *,
68 				const char *);
69 bool		_prop_object_externalize_append_char(
70 				struct _prop_object_externalize_context *,
71 				unsigned char);
72 bool		_prop_object_externalize_header(
73 				struct _prop_object_externalize_context *);
74 bool		_prop_object_externalize_footer(
75 				struct _prop_object_externalize_context *);
76 
77 struct _prop_object_externalize_context *
78 	_prop_object_externalize_context_alloc(void);
79 void	_prop_object_externalize_context_free(
80 				struct _prop_object_externalize_context *);
81 
82 typedef enum {
83 	_PROP_TAG_TYPE_START,			/* e.g. <dict> */
84 	_PROP_TAG_TYPE_END,			/* e.g. </dict> */
85 	_PROP_TAG_TYPE_EITHER
86 } _prop_tag_type_t;
87 
88 struct _prop_object_internalize_context {
89 	const char *poic_xml;
90 	const char *poic_cp;
91 
92 	const char *poic_tag_start;
93 
94 	const char *poic_tagname;
95 	size_t      poic_tagname_len;
96 	const char *poic_tagattr;
97 	size_t      poic_tagattr_len;
98 	const char *poic_tagattrval;
99 	size_t      poic_tagattrval_len;
100 
101 	bool   poic_is_empty_element;
102 	_prop_tag_type_t poic_tag_type;
103 };
104 
105 typedef enum {
106 	_PROP_OBJECT_FREE_DONE,
107 	_PROP_OBJECT_FREE_RECURSE,
108 	_PROP_OBJECT_FREE_FAILED
109 } _prop_object_free_rv_t;
110 
111 typedef enum {
112 	_PROP_OBJECT_EQUALS_FALSE,
113 	_PROP_OBJECT_EQUALS_TRUE,
114 	_PROP_OBJECT_EQUALS_RECURSE
115 } _prop_object_equals_rv_t;
116 
117 #define	_PROP_EOF(c)		((c) == '\0')
118 #define	_PROP_ISSPACE(c)	\
119 	((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
120 
121 #define	_PROP_TAG_MATCH(ctx, t)					\
122 	_prop_object_internalize_match((ctx)->poic_tagname,	\
123 				       (ctx)->poic_tagname_len,	\
124 				       (t), strlen(t))
125 
126 #define	_PROP_TAGATTR_MATCH(ctx, a)				\
127 	_prop_object_internalize_match((ctx)->poic_tagattr,	\
128 				       (ctx)->poic_tagattr_len,	\
129 				       (a), strlen(a))
130 
131 #define	_PROP_TAGATTRVAL_MATCH(ctx, a)				  \
132 	_prop_object_internalize_match((ctx)->poic_tagattrval,	  \
133 				       (ctx)->poic_tagattrval_len,\
134 				       (a), strlen(a))
135 
136 bool		_prop_object_internalize_find_tag(
137 				struct _prop_object_internalize_context *,
138 				const char *, _prop_tag_type_t);
139 bool		_prop_object_internalize_match(const char *, size_t,
140 					       const char *, size_t);
141 prop_object_t	_prop_object_internalize_by_tag(
142 				struct _prop_object_internalize_context *);
143 bool		_prop_object_internalize_decode_string(
144 				struct _prop_object_internalize_context *,
145 				char *, size_t, size_t *, const char **);
146 prop_object_t	_prop_generic_internalize(const char *, const char *);
147 
148 struct _prop_object_internalize_context *
149 		_prop_object_internalize_context_alloc(const char *);
150 void		_prop_object_internalize_context_free(
151 				struct _prop_object_internalize_context *);
152 
153 #if !defined(_KERNEL) && !defined(_STANDALONE)
154 bool		_prop_object_externalize_write_file(const char *,
155 						    const char *, size_t);
156 
157 struct _prop_object_internalize_mapped_file {
158 	char *	poimf_xml;
159 	size_t	poimf_mapsize;
160 };
161 
162 struct _prop_object_internalize_mapped_file *
163 		_prop_object_internalize_map_file(const char *);
164 void		_prop_object_internalize_unmap_file(
165 				struct _prop_object_internalize_mapped_file *);
166 #endif /* !_KERNEL && !_STANDALONE */
167 
168 typedef bool (*prop_object_internalizer_t)(prop_stack_t, prop_object_t *,
169 				struct _prop_object_internalize_context *);
170 typedef bool (*prop_object_internalizer_continue_t)(prop_stack_t,
171 				prop_object_t *,
172 				struct _prop_object_internalize_context *,
173 				void *, prop_object_t);
174 
175 	/* These are here because they're required by shared code. */
176 bool		_prop_array_internalize(prop_stack_t, prop_object_t *,
177 				struct _prop_object_internalize_context *);
178 bool		_prop_bool_internalize(prop_stack_t, prop_object_t *,
179 				struct _prop_object_internalize_context *);
180 bool		_prop_data_internalize(prop_stack_t, prop_object_t *,
181 				struct _prop_object_internalize_context *);
182 bool		_prop_dictionary_internalize(prop_stack_t, prop_object_t *,
183 				struct _prop_object_internalize_context *);
184 bool		_prop_number_internalize(prop_stack_t, prop_object_t *,
185 				struct _prop_object_internalize_context *);
186 bool		_prop_string_internalize(prop_stack_t, prop_object_t *,
187 				struct _prop_object_internalize_context *);
188 
189 struct _prop_object_type {
190 	/* type indicator */
191 	uint32_t	pot_type;
192 	/* func to free object */
193 	_prop_object_free_rv_t
194 			(*pot_free)(prop_stack_t, prop_object_t *);
195 	/*
196 	 * func to free the child returned by pot_free with stack == NULL.
197 	 *
198 	 * Must be implemented if pot_free can return anything other than
199 	 * _PROP_OBJECT_FREE_DONE.
200 	 */
201 	void	(*pot_emergency_free)(prop_object_t);
202 	/* func to externalize object */
203 	bool	(*pot_extern)(struct _prop_object_externalize_context *,
204 			      void *);
205 	/* func to test quality */
206 	_prop_object_equals_rv_t
207 		(*pot_equals)(prop_object_t, prop_object_t,
208 			      void **, void **,
209 			      prop_object_t *, prop_object_t *);
210 	/*
211 	 * func to finish equality iteration.
212 	 *
213 	 * Must be implemented if pot_equals can return
214 	 * _PROP_OBJECT_EQUALS_RECURSE
215 	 */
216 	void	(*pot_equals_finish)(prop_object_t, prop_object_t);
217 	void    (*pot_lock)(void);
218 	void    (*pot_unlock)(void);
219 };
220 
221 struct _prop_object {
222 	const struct _prop_object_type *po_type;/* type descriptor */
223 	uint32_t	po_refcnt;		/* reference count */
224 };
225 
226 void		_prop_object_init(struct _prop_object *,
227 				  const struct _prop_object_type *);
228 void		_prop_object_fini(struct _prop_object *);
229 
230 struct _prop_object_iterator {
231 	prop_object_t	(*pi_next_object)(void *);
232 	void		(*pi_reset)(void *);
233 	prop_object_t	pi_obj;
234 	uint32_t	pi_version;
235 };
236 
237 #define _PROP_NOTHREAD_ONCE_DECL(x)	static bool x = false;
238 #define _PROP_NOTHREAD_ONCE_RUN(x,f)					\
239 	do {								\
240 		if ((x) == false) {					\
241 			f();						\
242 			x = true;					\
243 		}							\
244 	} while (/*CONSTCOND*/0)
245 
246 #if defined(_KERNEL)
247 
248 /*
249  * proplib in the kernel...
250  */
251 
252 #include <sys/param.h>
253 #include <sys/malloc.h>
254 #include <sys/pool.h>
255 #include <sys/systm.h>
256 #include <sys/rwlock.h>
257 #include <sys/once.h>
258 
259 #define	_PROP_ASSERT(x)			KASSERT(x)
260 
261 #define	_PROP_MALLOC(s, t)		malloc((s), (t), M_WAITOK)
262 #define	_PROP_CALLOC(s, t)		malloc((s), (t), M_WAITOK | M_ZERO)
263 #define	_PROP_REALLOC(v, s, t)		realloc((v), (s), (t), M_WAITOK)
264 #define	_PROP_FREE(v, t)		free((v), (t))
265 
266 #define	_PROP_POOL_GET(p)		pool_get(&(p), PR_WAITOK)
267 #define	_PROP_POOL_PUT(p, v)		pool_put(&(p), (v))
268 
269 struct prop_pool_init {
270 	struct pool *pp;
271 	size_t size;
272 	const char *wchan;
273 };
274 #define	_PROP_POOL_INIT(pp, size, wchan)				\
275 struct pool pp;								\
276 static const struct prop_pool_init _link_ ## pp[1] = {			\
277 	{ &pp, size, wchan }						\
278 };									\
279 __link_set_add_rodata(prop_linkpools, _link_ ## pp);
280 
281 #define	_PROP_MALLOC_DEFINE(t, s, l)					\
282 		MALLOC_DEFINE(t, s, l);
283 
284 #define	_PROP_MUTEX_DECL_STATIC(x)	static kmutex_t x;
285 #define	_PROP_MUTEX_INIT(x)		mutex_init(&(x),MUTEX_DEFAULT,IPL_NONE)
286 #define	_PROP_MUTEX_LOCK(x)		mutex_enter(&(x))
287 #define	_PROP_MUTEX_UNLOCK(x)		mutex_exit(&(x))
288 
289 #define	_PROP_RWLOCK_DECL(x)		krwlock_t x ;
290 #define	_PROP_RWLOCK_INIT(x)		rw_init(&(x))
291 #define	_PROP_RWLOCK_RDLOCK(x)		rw_enter(&(x), RW_READER)
292 #define	_PROP_RWLOCK_WRLOCK(x)		rw_enter(&(x), RW_WRITER)
293 #define	_PROP_RWLOCK_UNLOCK(x)		rw_exit(&(x))
294 #define	_PROP_RWLOCK_DESTROY(x)		rw_destroy(&(x))
295 
296 #define _PROP_ONCE_DECL(x)		static ONCE_DECL(x);
297 #define _PROP_ONCE_RUN(x,f)		RUN_ONCE(&(x), f)
298 
299 #include <sys/atomic.h>
300 
301 #define _PROP_ATOMIC_INC32(x)		atomic_inc_32(x)
302 #define _PROP_ATOMIC_DEC32(x)		atomic_dec_32(x)
303 #define _PROP_ATOMIC_INC32_NV(x, v)	v = atomic_inc_32_nv(x)
304 #define _PROP_ATOMIC_DEC32_NV(x, v)	v = atomic_dec_32_nv(x)
305 
306 #elif defined(_STANDALONE)
307 
308 /*
309  * proplib in a standalone environment...
310  */
311 
312 #include <lib/libsa/stand.h>
313 
314 void *		_prop_standalone_calloc(size_t);
315 void *		_prop_standalone_realloc(void *, size_t);
316 
317 #define	_PROP_ASSERT(x)			/* nothing */
318 
319 #define	_PROP_MALLOC(s, t)		alloc((s))
320 #define	_PROP_CALLOC(s, t)		_prop_standalone_calloc((s))
321 #define	_PROP_REALLOC(v, s, t)		_prop_standalone_realloc((v), (s))
322 #define	_PROP_FREE(v, t)		dealloc((v), 0)		/* XXX */
323 
324 #define	_PROP_POOL_GET(p)		alloc((p))
325 #define	_PROP_POOL_PUT(p, v)		dealloc((v), (p))
326 
327 #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
328 
329 #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
330 
331 #define	_PROP_MUTEX_DECL_STATIC(x)	/* nothing */
332 #define	_PROP_MUTEX_INIT(x)		/* nothing */
333 #define	_PROP_MUTEX_LOCK(x)		/* nothing */
334 #define	_PROP_MUTEX_UNLOCK(x)		/* nothing */
335 
336 #define	_PROP_RWLOCK_DECL(x)		/* nothing */
337 #define	_PROP_RWLOCK_INIT(x)		/* nothing */
338 #define	_PROP_RWLOCK_RDLOCK(x)		/* nothing */
339 #define	_PROP_RWLOCK_WRLOCK(x)		/* nothing */
340 #define	_PROP_RWLOCK_UNLOCK(x)		/* nothing */
341 #define	_PROP_RWLOCK_DESTROY(x)		/* nothing */
342 
343 #define _PROP_ONCE_DECL(x)		_PROP_NOTHREAD_ONCE_DECL(x)
344 #define _PROP_ONCE_RUN(x,f)		_PROP_NOTHREAD_ONCE_RUN(x,f)
345 
346 #define _PROP_ATOMIC_INC32(x)		++*(x)
347 #define _PROP_ATOMIC_DEC32(x)		--*(x)
348 #define _PROP_ATOMIC_INC32_NV(x, v)	v = ++*(x)
349 #define _PROP_ATOMIC_DEC32_NV(x, v)	v = --*(x)
350 
351 #else
352 
353 /*
354  * proplib in user space...
355  */
356 
357 #include <assert.h>
358 #include <string.h>
359 #include <stdio.h>
360 #include <stdlib.h>
361 #include <stddef.h>
362 
363 #define	_PROP_ASSERT(x)			/*LINTED*/assert(x)
364 
365 #define	_PROP_MALLOC(s, t)		malloc((s))
366 #define	_PROP_CALLOC(s, t)		calloc(1, (s))
367 #define	_PROP_REALLOC(v, s, t)		realloc((v), (s))
368 #define	_PROP_FREE(v, t)		free((v))
369 
370 #define	_PROP_POOL_GET(p)		malloc((p))
371 #define	_PROP_POOL_PUT(p, v)		free((v))
372 
373 #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
374 
375 #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
376 
377 #if defined(__NetBSD__) && defined(_LIBPROP)
378 /*
379  * Use the same mechanism as libc; we get pthread mutexes for threaded
380  * programs and do-nothing stubs for non-threaded programs.
381  */
382 #include <sys/atomic.h>
383 #include "reentrant.h"
384 #define	_PROP_MUTEX_DECL_STATIC(x)	static mutex_t x;
385 #define	_PROP_MUTEX_INIT(x)		mutex_init(&(x), NULL)
386 #define	_PROP_MUTEX_LOCK(x)		mutex_lock(&(x))
387 #define	_PROP_MUTEX_UNLOCK(x)		mutex_unlock(&(x))
388 
389 #define	_PROP_RWLOCK_DECL(x)		rwlock_t x ;
390 #define	_PROP_RWLOCK_INIT(x)		rwlock_init(&(x), NULL)
391 #define	_PROP_RWLOCK_RDLOCK(x)		rwlock_rdlock(&(x))
392 #define	_PROP_RWLOCK_WRLOCK(x)		rwlock_wrlock(&(x))
393 #define	_PROP_RWLOCK_UNLOCK(x)		rwlock_unlock(&(x))
394 #define	_PROP_RWLOCK_DESTROY(x)		rwlock_destroy(&(x))
395 
396 #define _PROP_ONCE_DECL(x)						\
397 	static pthread_once_t x = PTHREAD_ONCE_INIT;
398 #define _PROP_ONCE_RUN(x,f)		thr_once(&(x), (void(*)(void))f);
399 
400 #define _PROP_ATOMIC_INC32(x)		atomic_inc_32(x)
401 #define _PROP_ATOMIC_DEC32(x)		atomic_dec_32(x)
402 #define _PROP_ATOMIC_INC32_NV(x, v)	v = atomic_inc_32_nv(x)
403 #define _PROP_ATOMIC_DEC32_NV(x, v)	v = atomic_dec_32_nv(x)
404 
405 #elif defined(HAVE_NBTOOL_CONFIG_H)
406 /*
407  * None of NetBSD's build tools are multi-threaded.
408  */
409 #define	_PROP_MUTEX_DECL_STATIC(x)	/* nothing */
410 #define	_PROP_MUTEX_INIT(x)		/* nothing */
411 #define	_PROP_MUTEX_LOCK(x)		/* nothing */
412 #define	_PROP_MUTEX_UNLOCK(x)		/* nothing */
413 
414 #define	_PROP_RWLOCK_DECL(x)		/* nothing */
415 #define	_PROP_RWLOCK_INIT(x)		/* nothing */
416 #define	_PROP_RWLOCK_RDLOCK(x)		/* nothing */
417 #define	_PROP_RWLOCK_WRLOCK(x)		/* nothing */
418 #define	_PROP_RWLOCK_UNLOCK(x)		/* nothing */
419 #define	_PROP_RWLOCK_DESTROY(x)		/* nothing */
420 
421 #define _PROP_ONCE_DECL(x)		_PROP_NOTHREAD_ONCE_DECL(x)
422 #define _PROP_ONCE_RUN(x,f)		_PROP_NOTHREAD_ONCE_RUN(x,f)
423 
424 #define _PROP_ATOMIC_INC32(x)		++*(x)
425 #define _PROP_ATOMIC_DEC32(x)		--*(x)
426 #define _PROP_ATOMIC_INC32_NV(x, v)	v = ++*(x)
427 #define _PROP_ATOMIC_DEC32_NV(x, v)	v = --*(x)
428 
429 #else
430 /*
431  * Use pthread mutexes everywhere else.
432  */
433 #include <pthread.h>
434 #define	_PROP_MUTEX_DECL_STATIC(x)	static pthread_mutex_t x;
435 #define	_PROP_MUTEX_INIT(x)		pthread_mutex_init(&(x), NULL)
436 #define	_PROP_MUTEX_LOCK(x)		pthread_mutex_lock(&(x))
437 #define	_PROP_MUTEX_UNLOCK(x)		pthread_mutex_unlock(&(x))
438 
439 #define	_PROP_RWLOCK_DECL(x)		pthread_rwlock_t x ;
440 #define	_PROP_RWLOCK_INIT(x)		pthread_rwlock_init(&(x), NULL)
441 #define	_PROP_RWLOCK_RDLOCK(x)		pthread_rwlock_rdlock(&(x))
442 #define	_PROP_RWLOCK_WRLOCK(x)		pthread_rwlock_wrlock(&(x))
443 #define	_PROP_RWLOCK_UNLOCK(x)		pthread_rwlock_unlock(&(x))
444 #define	_PROP_RWLOCK_DESTROY(x)		pthread_rwlock_destroy(&(x))
445 
446 #define _PROP_ONCE_DECL(x)						\
447 	static pthread_once_t x = PTHREAD_ONCE_INIT;
448 #define _PROP_ONCE_RUN(x,f)		pthread_once(&(x),(void(*)(void))f)
449 
450 #define _PROP_NEED_REFCNT_MTX
451 
452 #define _PROP_ATOMIC_INC32(x)						\
453 do {									\
454 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
455 	(*(x))++;							\
456 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
457 } while (/*CONSTCOND*/0)
458 
459 #define _PROP_ATOMIC_DEC32(x)						\
460 do {									\
461 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
462 	(*(x))--;							\
463 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
464 } while (/*CONSTCOND*/0)
465 
466 #define _PROP_ATOMIC_INC32_NV(x, v)					\
467 do {									\
468 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
469 	v = ++(*(x));							\
470 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
471 } while (/*CONSTCOND*/0)
472 
473 #define _PROP_ATOMIC_DEC32_NV(x, v)					\
474 do {									\
475 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
476 	v = --(*(x));							\
477 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
478 } while (/*CONSTCOND*/0)
479 
480 #endif
481 #endif /* _KERNEL */
482 
483 /*
484  * Language features.
485  */
486 #if defined(__NetBSD__)
487 #include <sys/cdefs.h>
488 #define	_PROP_ARG_UNUSED		__unused
489 #else
490 #define	_PROP_ARG_UNUSED		/* delete */
491 #endif /* __NetBSD__ */
492 
493 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
494