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