prop_object_impl.h revision 1.36 1 /* $NetBSD: prop_object_impl.h,v 1.36 2020/06/12 00:02:26 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2020 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_LOAD(x) atomic_load_relaxed(x)
302 #define _PROP_ATOMIC_INC32(x) atomic_inc_32(x)
303 #define _PROP_ATOMIC_DEC32(x) atomic_dec_32(x)
304 #define _PROP_ATOMIC_INC32_NV(x, v) v = atomic_inc_32_nv(x)
305 #define _PROP_ATOMIC_DEC32_NV(x, v) v = atomic_dec_32_nv(x)
306
307 #elif defined(_STANDALONE)
308
309 /*
310 * proplib in a standalone environment...
311 */
312
313 #include <lib/libsa/stand.h>
314
315 void * _prop_standalone_calloc(size_t);
316 void * _prop_standalone_realloc(void *, size_t);
317
318 #define _PROP_ASSERT(x) /* nothing */
319
320 #define _PROP_MALLOC(s, t) alloc((s))
321 #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s))
322 #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s))
323 #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */
324
325 #define _PROP_POOL_GET(p) alloc((p))
326 #define _PROP_POOL_PUT(p, v) dealloc((v), (p))
327
328 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
329
330 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
331
332 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
333 #define _PROP_MUTEX_INIT(x) /* nothing */
334 #define _PROP_MUTEX_LOCK(x) /* nothing */
335 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
336
337 #define _PROP_RWLOCK_DECL(x) /* nothing */
338 #define _PROP_RWLOCK_INIT(x) /* nothing */
339 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
340 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
341 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
342 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
343
344 #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x)
345 #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f)
346
347 #define _PROP_ATOMIC_LOAD(x) *(x)
348 #define _PROP_ATOMIC_INC32(x) ++*(x)
349 #define _PROP_ATOMIC_DEC32(x) --*(x)
350 #define _PROP_ATOMIC_INC32_NV(x, v) v = ++*(x)
351 #define _PROP_ATOMIC_DEC32_NV(x, v) v = --*(x)
352
353 #else
354
355 /*
356 * proplib in user space...
357 */
358
359 #include <assert.h>
360 #include <string.h>
361 #include <stdio.h>
362 #include <stdlib.h>
363 #include <stddef.h>
364
365 #define _PROP_ASSERT(x) /*LINTED*/assert(x)
366
367 #define _PROP_MALLOC(s, t) malloc((s))
368 #define _PROP_CALLOC(s, t) calloc(1, (s))
369 #define _PROP_REALLOC(v, s, t) realloc((v), (s))
370 #define _PROP_FREE(v, t) free((v))
371
372 #define _PROP_POOL_GET(p) malloc((p))
373 #define _PROP_POOL_PUT(p, v) free((v))
374
375 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s;
376
377 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */
378
379 #if defined(__NetBSD__) && defined(_LIBPROP)
380 /*
381 * Use the same mechanism as libc; we get pthread mutexes for threaded
382 * programs and do-nothing stubs for non-threaded programs.
383 */
384 #include <sys/atomic.h>
385 #include "reentrant.h"
386 #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x;
387 #define _PROP_MUTEX_INIT(x) mutex_init(&(x), NULL)
388 #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x))
389 #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x))
390
391 #define _PROP_RWLOCK_DECL(x) rwlock_t x ;
392 #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL)
393 #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x))
394 #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x))
395 #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x))
396 #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x))
397
398 #define _PROP_ONCE_DECL(x) \
399 static pthread_once_t x = PTHREAD_ONCE_INIT;
400 #define _PROP_ONCE_RUN(x,f) thr_once(&(x), (void(*)(void))f);
401
402 #define _PROP_ATOMIC_LOAD(x) *(x)
403 #define _PROP_ATOMIC_INC32(x) atomic_inc_32(x)
404 #define _PROP_ATOMIC_DEC32(x) atomic_dec_32(x)
405 #define _PROP_ATOMIC_INC32_NV(x, v) v = atomic_inc_32_nv(x)
406 #define _PROP_ATOMIC_DEC32_NV(x, v) v = atomic_dec_32_nv(x)
407
408 #elif defined(HAVE_NBTOOL_CONFIG_H)
409 /*
410 * None of NetBSD's build tools are multi-threaded.
411 */
412 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */
413 #define _PROP_MUTEX_INIT(x) /* nothing */
414 #define _PROP_MUTEX_LOCK(x) /* nothing */
415 #define _PROP_MUTEX_UNLOCK(x) /* nothing */
416
417 #define _PROP_RWLOCK_DECL(x) /* nothing */
418 #define _PROP_RWLOCK_INIT(x) /* nothing */
419 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */
420 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */
421 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */
422 #define _PROP_RWLOCK_DESTROY(x) /* nothing */
423
424 #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x)
425 #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f)
426
427 #define _PROP_ATOMIC_LOAD(x) *(x)
428 #define _PROP_ATOMIC_INC32(x) ++*(x)
429 #define _PROP_ATOMIC_DEC32(x) --*(x)
430 #define _PROP_ATOMIC_INC32_NV(x, v) v = ++*(x)
431 #define _PROP_ATOMIC_DEC32_NV(x, v) v = --*(x)
432
433 #else
434 /*
435 * Use pthread mutexes everywhere else.
436 */
437 #include <pthread.h>
438 #define _PROP_MUTEX_DECL_STATIC(x) static pthread_mutex_t x;
439 #define _PROP_MUTEX_INIT(x) pthread_mutex_init(&(x), NULL)
440 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x))
441 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
442
443 #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ;
444 #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL)
445 #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x))
446 #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x))
447 #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x))
448 #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x))
449
450 #define _PROP_ONCE_DECL(x) \
451 static pthread_once_t x = PTHREAD_ONCE_INIT;
452 #define _PROP_ONCE_RUN(x,f) pthread_once(&(x),(void(*)(void))f)
453
454 #define _PROP_NEED_REFCNT_MTX
455
456 #define _PROP_ATOMIC_LOAD(x) *(x)
457
458 #define _PROP_ATOMIC_INC32(x) \
459 do { \
460 pthread_mutex_lock(&_prop_refcnt_mtx); \
461 (*(x))++; \
462 pthread_mutex_unlock(&_prop_refcnt_mtx); \
463 } while (/*CONSTCOND*/0)
464
465 #define _PROP_ATOMIC_DEC32(x) \
466 do { \
467 pthread_mutex_lock(&_prop_refcnt_mtx); \
468 (*(x))--; \
469 pthread_mutex_unlock(&_prop_refcnt_mtx); \
470 } while (/*CONSTCOND*/0)
471
472 #define _PROP_ATOMIC_INC32_NV(x, v) \
473 do { \
474 pthread_mutex_lock(&_prop_refcnt_mtx); \
475 v = ++(*(x)); \
476 pthread_mutex_unlock(&_prop_refcnt_mtx); \
477 } while (/*CONSTCOND*/0)
478
479 #define _PROP_ATOMIC_DEC32_NV(x, v) \
480 do { \
481 pthread_mutex_lock(&_prop_refcnt_mtx); \
482 v = --(*(x)); \
483 pthread_mutex_unlock(&_prop_refcnt_mtx); \
484 } while (/*CONSTCOND*/0)
485
486 #endif
487 #endif /* _KERNEL */
488
489 /*
490 * Language features.
491 */
492 #if defined(__NetBSD__)
493 #include <sys/cdefs.h>
494 #define _PROP_ARG_UNUSED __unused
495 #if defined(__clang__)
496 #define _PROP_DEPRECATED(s, m) /* delete */
497 #else /* ! __clang__ */
498 #define _PROP_DEPRECATED(s, m) __warn_references(s, m)
499 #endif /* __clang__ */
500 #else
501 #define _PROP_ARG_UNUSED /* delete */
502 #define _PROP_DEPRECATED(s, m) /* delete */
503 #endif /* __NetBSD__ */
504
505 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
506