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