19f95f3e0SAlex Hornung /* $NetBSD: prop_object_impl.h,v 1.30 2009/09/13 18:45:10 pooka Exp $ */ 29f95f3e0SAlex Hornung 39f95f3e0SAlex Hornung /*- 49f95f3e0SAlex Hornung * Copyright (c) 2006 The NetBSD Foundation, Inc. 59f95f3e0SAlex Hornung * All rights reserved. 69f95f3e0SAlex Hornung * 79f95f3e0SAlex Hornung * This code is derived from software contributed to The NetBSD Foundation 89f95f3e0SAlex Hornung * by Jason R. Thorpe. 99f95f3e0SAlex Hornung * 109f95f3e0SAlex Hornung * Redistribution and use in source and binary forms, with or without 119f95f3e0SAlex Hornung * modification, are permitted provided that the following conditions 129f95f3e0SAlex Hornung * are met: 139f95f3e0SAlex Hornung * 1. Redistributions of source code must retain the above copyright 149f95f3e0SAlex Hornung * notice, this list of conditions and the following disclaimer. 159f95f3e0SAlex Hornung * 2. Redistributions in binary form must reproduce the above copyright 169f95f3e0SAlex Hornung * notice, this list of conditions and the following disclaimer in the 179f95f3e0SAlex Hornung * documentation and/or other materials provided with the distribution. 189f95f3e0SAlex Hornung * 199f95f3e0SAlex Hornung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 209f95f3e0SAlex Hornung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 219f95f3e0SAlex Hornung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 229f95f3e0SAlex Hornung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 239f95f3e0SAlex Hornung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 249f95f3e0SAlex Hornung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 259f95f3e0SAlex Hornung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 269f95f3e0SAlex Hornung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 279f95f3e0SAlex Hornung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 289f95f3e0SAlex Hornung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 299f95f3e0SAlex Hornung * POSSIBILITY OF SUCH DAMAGE. 309f95f3e0SAlex Hornung */ 319f95f3e0SAlex Hornung 329f95f3e0SAlex Hornung #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_ 339f95f3e0SAlex Hornung #define _PROPLIB_PROP_OBJECT_IMPL_H_ 349f95f3e0SAlex Hornung 35*0c36ed34SSascha Wildner #if !defined(_KERNEL) && !defined(_STANDALONE) 369f95f3e0SAlex Hornung #include <inttypes.h> 379f95f3e0SAlex Hornung #endif 389f95f3e0SAlex Hornung 399f95f3e0SAlex Hornung #include "prop_stack.h" 409f95f3e0SAlex Hornung 419f95f3e0SAlex Hornung struct _prop_object_externalize_context { 429f95f3e0SAlex Hornung char * poec_buf; /* string buffer */ 439f95f3e0SAlex Hornung size_t poec_capacity; /* capacity of buffer */ 449f95f3e0SAlex Hornung size_t poec_len; /* current length of string */ 459f95f3e0SAlex Hornung unsigned int poec_depth; /* nesting depth */ 469f95f3e0SAlex Hornung }; 479f95f3e0SAlex Hornung 489f95f3e0SAlex Hornung bool _prop_object_externalize_start_tag( 499f95f3e0SAlex Hornung struct _prop_object_externalize_context *, 509f95f3e0SAlex Hornung const char *); 519f95f3e0SAlex Hornung bool _prop_object_externalize_end_tag( 529f95f3e0SAlex Hornung struct _prop_object_externalize_context *, 539f95f3e0SAlex Hornung const char *); 549f95f3e0SAlex Hornung bool _prop_object_externalize_empty_tag( 559f95f3e0SAlex Hornung struct _prop_object_externalize_context *, 569f95f3e0SAlex Hornung const char *); 579f95f3e0SAlex Hornung bool _prop_object_externalize_append_cstring( 589f95f3e0SAlex Hornung struct _prop_object_externalize_context *, 599f95f3e0SAlex Hornung const char *); 609f95f3e0SAlex Hornung bool _prop_object_externalize_append_encoded_cstring( 619f95f3e0SAlex Hornung struct _prop_object_externalize_context *, 629f95f3e0SAlex Hornung const char *); 639f95f3e0SAlex Hornung bool _prop_object_externalize_append_char( 649f95f3e0SAlex Hornung struct _prop_object_externalize_context *, 659f95f3e0SAlex Hornung unsigned char); 669f95f3e0SAlex Hornung bool _prop_object_externalize_header( 679f95f3e0SAlex Hornung struct _prop_object_externalize_context *); 689f95f3e0SAlex Hornung bool _prop_object_externalize_footer( 699f95f3e0SAlex Hornung struct _prop_object_externalize_context *); 709f95f3e0SAlex Hornung 719f95f3e0SAlex Hornung struct _prop_object_externalize_context * 729f95f3e0SAlex Hornung _prop_object_externalize_context_alloc(void); 739f95f3e0SAlex Hornung void _prop_object_externalize_context_free( 749f95f3e0SAlex Hornung struct _prop_object_externalize_context *); 759f95f3e0SAlex Hornung 769f95f3e0SAlex Hornung typedef enum { 779f95f3e0SAlex Hornung _PROP_TAG_TYPE_START, /* e.g. <dict> */ 789f95f3e0SAlex Hornung _PROP_TAG_TYPE_END, /* e.g. </dict> */ 799f95f3e0SAlex Hornung _PROP_TAG_TYPE_EITHER 809f95f3e0SAlex Hornung } _prop_tag_type_t; 819f95f3e0SAlex Hornung 829f95f3e0SAlex Hornung struct _prop_object_internalize_context { 839f95f3e0SAlex Hornung const char *poic_xml; 849f95f3e0SAlex Hornung const char *poic_cp; 859f95f3e0SAlex Hornung 869f95f3e0SAlex Hornung const char *poic_tag_start; 879f95f3e0SAlex Hornung 889f95f3e0SAlex Hornung const char *poic_tagname; 899f95f3e0SAlex Hornung size_t poic_tagname_len; 909f95f3e0SAlex Hornung const char *poic_tagattr; 919f95f3e0SAlex Hornung size_t poic_tagattr_len; 929f95f3e0SAlex Hornung const char *poic_tagattrval; 939f95f3e0SAlex Hornung size_t poic_tagattrval_len; 949f95f3e0SAlex Hornung 959f95f3e0SAlex Hornung bool poic_is_empty_element; 969f95f3e0SAlex Hornung _prop_tag_type_t poic_tag_type; 979f95f3e0SAlex Hornung }; 989f95f3e0SAlex Hornung 999f95f3e0SAlex Hornung typedef enum { 1009f95f3e0SAlex Hornung _PROP_OBJECT_FREE_DONE, 1019f95f3e0SAlex Hornung _PROP_OBJECT_FREE_RECURSE, 1029f95f3e0SAlex Hornung _PROP_OBJECT_FREE_FAILED 1039f95f3e0SAlex Hornung } _prop_object_free_rv_t; 1049f95f3e0SAlex Hornung 1059f95f3e0SAlex Hornung typedef enum { 1069f95f3e0SAlex Hornung _PROP_OBJECT_EQUALS_FALSE, 1079f95f3e0SAlex Hornung _PROP_OBJECT_EQUALS_TRUE, 1089f95f3e0SAlex Hornung _PROP_OBJECT_EQUALS_RECURSE 1099f95f3e0SAlex Hornung } _prop_object_equals_rv_t; 1109f95f3e0SAlex Hornung 1119f95f3e0SAlex Hornung #define _PROP_EOF(c) ((c) == '\0') 1129f95f3e0SAlex Hornung #define _PROP_ISSPACE(c) \ 1139f95f3e0SAlex Hornung ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || \ 1149f95f3e0SAlex Hornung _PROP_EOF(c)) 1159f95f3e0SAlex Hornung 1169f95f3e0SAlex Hornung #define _PROP_TAG_MATCH(ctx, t) \ 1179f95f3e0SAlex Hornung _prop_object_internalize_match((ctx)->poic_tagname, \ 1189f95f3e0SAlex Hornung (ctx)->poic_tagname_len, \ 1199f95f3e0SAlex Hornung (t), strlen(t)) 1209f95f3e0SAlex Hornung 1219f95f3e0SAlex Hornung #define _PROP_TAGATTR_MATCH(ctx, a) \ 1229f95f3e0SAlex Hornung _prop_object_internalize_match((ctx)->poic_tagattr, \ 1239f95f3e0SAlex Hornung (ctx)->poic_tagattr_len, \ 1249f95f3e0SAlex Hornung (a), strlen(a)) 1259f95f3e0SAlex Hornung 1269f95f3e0SAlex Hornung #define _PROP_TAGATTRVAL_MATCH(ctx, a) \ 1279f95f3e0SAlex Hornung _prop_object_internalize_match((ctx)->poic_tagattrval, \ 1289f95f3e0SAlex Hornung (ctx)->poic_tagattrval_len,\ 1299f95f3e0SAlex Hornung (a), strlen(a)) 1309f95f3e0SAlex Hornung 1319f95f3e0SAlex Hornung bool _prop_object_internalize_find_tag( 1329f95f3e0SAlex Hornung struct _prop_object_internalize_context *, 1339f95f3e0SAlex Hornung const char *, _prop_tag_type_t); 1349f95f3e0SAlex Hornung bool _prop_object_internalize_match(const char *, size_t, 1359f95f3e0SAlex Hornung const char *, size_t); 1369f95f3e0SAlex Hornung prop_object_t _prop_object_internalize_by_tag( 1379f95f3e0SAlex Hornung struct _prop_object_internalize_context *); 1389f95f3e0SAlex Hornung bool _prop_object_internalize_decode_string( 1399f95f3e0SAlex Hornung struct _prop_object_internalize_context *, 1409f95f3e0SAlex Hornung char *, size_t, size_t *, const char **); 1419f95f3e0SAlex Hornung prop_object_t _prop_generic_internalize(const char *, const char *); 1429f95f3e0SAlex Hornung 1439f95f3e0SAlex Hornung struct _prop_object_internalize_context * 1449f95f3e0SAlex Hornung _prop_object_internalize_context_alloc(const char *); 1459f95f3e0SAlex Hornung void _prop_object_internalize_context_free( 1469f95f3e0SAlex Hornung struct _prop_object_internalize_context *); 1479f95f3e0SAlex Hornung 1489f95f3e0SAlex Hornung #if !defined(_KERNEL) && !defined(_STANDALONE) 1499f95f3e0SAlex Hornung bool _prop_object_externalize_write_file(const char *, 1509f95f3e0SAlex Hornung const char *, size_t); 1519f95f3e0SAlex Hornung 1529f95f3e0SAlex Hornung struct _prop_object_internalize_mapped_file { 1539f95f3e0SAlex Hornung char * poimf_xml; 1549f95f3e0SAlex Hornung size_t poimf_mapsize; 1559f95f3e0SAlex Hornung }; 1569f95f3e0SAlex Hornung 1579f95f3e0SAlex Hornung struct _prop_object_internalize_mapped_file * 1589f95f3e0SAlex Hornung _prop_object_internalize_map_file(const char *); 1599f95f3e0SAlex Hornung void _prop_object_internalize_unmap_file( 1609f95f3e0SAlex Hornung struct _prop_object_internalize_mapped_file *); 1619f95f3e0SAlex Hornung #endif /* !_KERNEL && !_STANDALONE */ 1629f95f3e0SAlex Hornung 1639f95f3e0SAlex Hornung typedef bool (*prop_object_internalizer_t)(prop_stack_t, prop_object_t *, 1649f95f3e0SAlex Hornung struct _prop_object_internalize_context *); 1659f95f3e0SAlex Hornung typedef bool (*prop_object_internalizer_continue_t)(prop_stack_t, 1669f95f3e0SAlex Hornung prop_object_t *, 1679f95f3e0SAlex Hornung struct _prop_object_internalize_context *, 1689f95f3e0SAlex Hornung void *, prop_object_t); 1699f95f3e0SAlex Hornung 1709f95f3e0SAlex Hornung /* These are here because they're required by shared code. */ 1719f95f3e0SAlex Hornung bool _prop_array_internalize(prop_stack_t, prop_object_t *, 1729f95f3e0SAlex Hornung struct _prop_object_internalize_context *); 1739f95f3e0SAlex Hornung bool _prop_bool_internalize(prop_stack_t, prop_object_t *, 1749f95f3e0SAlex Hornung struct _prop_object_internalize_context *); 1759f95f3e0SAlex Hornung bool _prop_data_internalize(prop_stack_t, prop_object_t *, 1769f95f3e0SAlex Hornung struct _prop_object_internalize_context *); 1779f95f3e0SAlex Hornung bool _prop_dictionary_internalize(prop_stack_t, prop_object_t *, 1789f95f3e0SAlex Hornung struct _prop_object_internalize_context *); 1799f95f3e0SAlex Hornung bool _prop_number_internalize(prop_stack_t, prop_object_t *, 1809f95f3e0SAlex Hornung struct _prop_object_internalize_context *); 1819f95f3e0SAlex Hornung bool _prop_string_internalize(prop_stack_t, prop_object_t *, 1829f95f3e0SAlex Hornung struct _prop_object_internalize_context *); 1839f95f3e0SAlex Hornung 1849f95f3e0SAlex Hornung struct _prop_object_type { 1859f95f3e0SAlex Hornung /* type indicator */ 1869f95f3e0SAlex Hornung uint32_t pot_type; 1879f95f3e0SAlex Hornung /* func to free object */ 1889f95f3e0SAlex Hornung _prop_object_free_rv_t 1899f95f3e0SAlex Hornung (*pot_free)(prop_stack_t, prop_object_t *); 1909f95f3e0SAlex Hornung /* 1919f95f3e0SAlex Hornung * func to free the child returned by pot_free with stack == NULL. 1929f95f3e0SAlex Hornung * 1939f95f3e0SAlex Hornung * Must be implemented if pot_free can return anything other than 1949f95f3e0SAlex Hornung * _PROP_OBJECT_FREE_DONE. 1959f95f3e0SAlex Hornung */ 1969f95f3e0SAlex Hornung void (*pot_emergency_free)(prop_object_t); 1979f95f3e0SAlex Hornung /* func to externalize object */ 1989f95f3e0SAlex Hornung bool (*pot_extern)(struct _prop_object_externalize_context *, 1999f95f3e0SAlex Hornung void *); 2009f95f3e0SAlex Hornung /* func to test quality */ 2019f95f3e0SAlex Hornung _prop_object_equals_rv_t 2029f95f3e0SAlex Hornung (*pot_equals)(prop_object_t, prop_object_t, 2039f95f3e0SAlex Hornung void **, void **, 2049f95f3e0SAlex Hornung prop_object_t *, prop_object_t *); 2059f95f3e0SAlex Hornung /* 2069f95f3e0SAlex Hornung * func to finish equality iteration. 2079f95f3e0SAlex Hornung * 2089f95f3e0SAlex Hornung * Must be implemented if pot_equals can return 2099f95f3e0SAlex Hornung * _PROP_OBJECT_EQUALS_RECURSE 2109f95f3e0SAlex Hornung */ 2119f95f3e0SAlex Hornung void (*pot_equals_finish)(prop_object_t, prop_object_t); 2129f95f3e0SAlex Hornung void (*pot_lock)(void); 2139f95f3e0SAlex Hornung void (*pot_unlock)(void); 2149f95f3e0SAlex Hornung }; 2159f95f3e0SAlex Hornung 2169f95f3e0SAlex Hornung struct _prop_object { 2179f95f3e0SAlex Hornung const struct _prop_object_type *po_type;/* type descriptor */ 2189f95f3e0SAlex Hornung uint32_t po_refcnt; /* reference count */ 2199f95f3e0SAlex Hornung }; 2209f95f3e0SAlex Hornung 2219f95f3e0SAlex Hornung void _prop_object_init(struct _prop_object *, 2229f95f3e0SAlex Hornung const struct _prop_object_type *); 2239f95f3e0SAlex Hornung void _prop_object_fini(struct _prop_object *); 2249f95f3e0SAlex Hornung 2259f95f3e0SAlex Hornung struct _prop_object_iterator { 2269f95f3e0SAlex Hornung prop_object_t (*pi_next_object)(void *); 2279f95f3e0SAlex Hornung void (*pi_reset)(void *); 2289f95f3e0SAlex Hornung prop_object_t pi_obj; 2299f95f3e0SAlex Hornung uint32_t pi_version; 2309f95f3e0SAlex Hornung }; 2319f95f3e0SAlex Hornung 2329f95f3e0SAlex Hornung #define _PROP_NOTHREAD_ONCE_DECL(x) static bool x = false; 2339f95f3e0SAlex Hornung #define _PROP_NOTHREAD_ONCE_RUN(x,f) \ 2349f95f3e0SAlex Hornung do { \ 2359f95f3e0SAlex Hornung if ((x) == false) { \ 2369f95f3e0SAlex Hornung f(); \ 2379f95f3e0SAlex Hornung x = true; \ 2389f95f3e0SAlex Hornung } \ 2399f95f3e0SAlex Hornung } while (/*CONSTCOND*/0) 2409f95f3e0SAlex Hornung 2419f95f3e0SAlex Hornung #if defined(_KERNEL) 2429f95f3e0SAlex Hornung 2439f95f3e0SAlex Hornung /* 2449f95f3e0SAlex Hornung * proplib in the kernel... 2459f95f3e0SAlex Hornung */ 2469f95f3e0SAlex Hornung 2479f95f3e0SAlex Hornung #include <sys/kernel.h> 2489f95f3e0SAlex Hornung #include <sys/types.h> 2499f95f3e0SAlex Hornung #include <sys/param.h> 2509f95f3e0SAlex Hornung #include <machine/inttypes.h> 2519f95f3e0SAlex Hornung #include <sys/malloc.h> 2529f95f3e0SAlex Hornung #include <sys/objcache.h> 2539f95f3e0SAlex Hornung #include <sys/systm.h> 254ba974ec4SMatthew Dillon #include <sys/globaldata.h> 255ba974ec4SMatthew Dillon #include <sys/mutex2.h> 2569f95f3e0SAlex Hornung #include <sys/lock.h> 2579f95f3e0SAlex Hornung 2589f95f3e0SAlex Hornung #define _PROP_ASSERT(x) KKASSERT(x) 2599f95f3e0SAlex Hornung 2609f95f3e0SAlex Hornung #define _PROP_MALLOC(s, t) kmalloc((s), (t), M_WAITOK) 2619f95f3e0SAlex Hornung #define _PROP_CALLOC(s, t) kmalloc((s), (t), M_WAITOK | M_ZERO) 2629f95f3e0SAlex Hornung #define _PROP_REALLOC(v, s, t) krealloc((v), (s), (t), M_WAITOK) 2639f95f3e0SAlex Hornung #define _PROP_FREE(v, t) kfree((v), (t)) 2649f95f3e0SAlex Hornung 2659f95f3e0SAlex Hornung #define _PROP_POOL_GET(p) objcache_get((p), M_WAITOK) 2669f95f3e0SAlex Hornung #define _PROP_POOL_PUT(p, v) objcache_put((p), (v)) 2679f95f3e0SAlex Hornung 2689f95f3e0SAlex Hornung struct prop_pool_init { 2699f95f3e0SAlex Hornung struct pool *pp; 2709f95f3e0SAlex Hornung size_t size; 2719f95f3e0SAlex Hornung const char *wchan; 2729f95f3e0SAlex Hornung }; 2739f95f3e0SAlex Hornung #define _PROP_POOL_INIT(pp, size, wchan) \ 2749f95f3e0SAlex Hornung MALLOC_DEFINE(M_##pp, wchan, wchan); \ 2759f95f3e0SAlex Hornung struct objcache *pp; \ 2769f95f3e0SAlex Hornung static void \ 2779f95f3e0SAlex Hornung pp##_init(void) \ 2789f95f3e0SAlex Hornung { \ 2799f95f3e0SAlex Hornung pp = objcache_create_simple(M_##pp, size); \ 2809f95f3e0SAlex Hornung } \ 281c0739b3cSMatthew Dillon SYSINIT(pp##_init, SI_SUB_PROP, SI_ORDER_ANY, pp##_init, NULL) 2829f95f3e0SAlex Hornung 2839f95f3e0SAlex Hornung #define _PROP_MALLOC_DEFINE(t, s, l) \ 2849f95f3e0SAlex Hornung MALLOC_DEFINE(t, s, l); 2859f95f3e0SAlex Hornung 286ba974ec4SMatthew Dillon /* 287ba974ec4SMatthew Dillon * NOTE: These locks might be held through a sleep so no spinlocks 288ba974ec4SMatthew Dillon * can be used. 289ba974ec4SMatthew Dillon */ 2909f95f3e0SAlex Hornung #define _PROP_MUTEX_DECL_STATIC(x) static struct lock x; 2919f95f3e0SAlex Hornung #define _PROP_MUTEX_INIT(x) lockinit(&(x),"proplib",0,LK_CANRECURSE) 2929f95f3e0SAlex Hornung #define _PROP_MUTEX_LOCK(x) lockmgr(&(x), LK_EXCLUSIVE) 2939f95f3e0SAlex Hornung #define _PROP_MUTEX_UNLOCK(x) lockmgr(&(x), LK_RELEASE) 2949f95f3e0SAlex Hornung 295ba974ec4SMatthew Dillon #define _PROP_RWLOCK_DECL(x) struct mtx x; 296cabfc9f6SMatthew Dillon #define _PROP_RWLOCK_INIT(x) mtx_init(&(x), "prop") 297ba974ec4SMatthew Dillon #define _PROP_RWLOCK_RDLOCK(x) mtx_lock(&(x)) 298ba974ec4SMatthew Dillon #define _PROP_RWLOCK_WRLOCK(x) mtx_lock(&(x)) 299ba974ec4SMatthew Dillon #define _PROP_RWLOCK_UNLOCK(x) mtx_unlock(&(x)) 300ba974ec4SMatthew Dillon #define _PROP_RWLOCK_DESTROY(x) mtx_uninit(&(x)) 3019f95f3e0SAlex Hornung 3029f95f3e0SAlex Hornung #define _PROP_ONCE_DECL(x) static int x = 0; 3039f95f3e0SAlex Hornung #define _PROP_ONCE_RUN(x,f) if (atomic_cmpset_int(&(x), 0, 1)) f() 3049f95f3e0SAlex Hornung 3059f95f3e0SAlex Hornung #elif defined(_STANDALONE) 3069f95f3e0SAlex Hornung 3079f95f3e0SAlex Hornung /* 3089f95f3e0SAlex Hornung * proplib in a standalone environment... 3099f95f3e0SAlex Hornung */ 3109f95f3e0SAlex Hornung 3119f95f3e0SAlex Hornung #include <lib/libsa/stand.h> 3129f95f3e0SAlex Hornung 3139f95f3e0SAlex Hornung void * _prop_standalone_calloc(size_t); 3149f95f3e0SAlex Hornung void * _prop_standalone_realloc(void *, size_t); 3159f95f3e0SAlex Hornung 3169f95f3e0SAlex Hornung #define _PROP_ASSERT(x) /* nothing */ 3179f95f3e0SAlex Hornung 3189f95f3e0SAlex Hornung #define _PROP_MALLOC(s, t) alloc((s)) 3199f95f3e0SAlex Hornung #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s)) 3209f95f3e0SAlex Hornung #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s)) 3219f95f3e0SAlex Hornung #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */ 3229f95f3e0SAlex Hornung 3239f95f3e0SAlex Hornung #define _PROP_POOL_GET(p) alloc((p)) 3249f95f3e0SAlex Hornung #define _PROP_POOL_PUT(p, v) dealloc((v), (p)) 3259f95f3e0SAlex Hornung 3269f95f3e0SAlex Hornung #define _PROP_POOL_INIT(p, s, d) static const size_t p = s; 3279f95f3e0SAlex Hornung 3289f95f3e0SAlex Hornung #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */ 3299f95f3e0SAlex Hornung 3309f95f3e0SAlex Hornung #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */ 3319f95f3e0SAlex Hornung #define _PROP_MUTEX_INIT(x) /* nothing */ 3329f95f3e0SAlex Hornung #define _PROP_MUTEX_LOCK(x) /* nothing */ 3339f95f3e0SAlex Hornung #define _PROP_MUTEX_UNLOCK(x) /* nothing */ 3349f95f3e0SAlex Hornung 3359f95f3e0SAlex Hornung #define _PROP_RWLOCK_DECL(x) /* nothing */ 3369f95f3e0SAlex Hornung #define _PROP_RWLOCK_INIT(x) /* nothing */ 3379f95f3e0SAlex Hornung #define _PROP_RWLOCK_RDLOCK(x) /* nothing */ 3389f95f3e0SAlex Hornung #define _PROP_RWLOCK_WRLOCK(x) /* nothing */ 3399f95f3e0SAlex Hornung #define _PROP_RWLOCK_UNLOCK(x) /* nothing */ 3409f95f3e0SAlex Hornung #define _PROP_RWLOCK_DESTROY(x) /* nothing */ 3419f95f3e0SAlex Hornung 3429f95f3e0SAlex Hornung #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x) 3439f95f3e0SAlex Hornung #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f) 3449f95f3e0SAlex Hornung 3459f95f3e0SAlex Hornung #else 3469f95f3e0SAlex Hornung 3479f95f3e0SAlex Hornung /* 3489f95f3e0SAlex Hornung * proplib in user space... 3499f95f3e0SAlex Hornung */ 3509f95f3e0SAlex Hornung 3519f95f3e0SAlex Hornung #include <assert.h> 3529f95f3e0SAlex Hornung #include <string.h> 3539f95f3e0SAlex Hornung #include <stdio.h> 3549f95f3e0SAlex Hornung #include <stdlib.h> 3559f95f3e0SAlex Hornung #include <stddef.h> 3569f95f3e0SAlex Hornung 3579f95f3e0SAlex Hornung #define _PROP_ASSERT(x) /*LINTED*/assert(x) 3589f95f3e0SAlex Hornung 3599f95f3e0SAlex Hornung #define _PROP_MALLOC(s, t) malloc((s)) 3609f95f3e0SAlex Hornung #define _PROP_CALLOC(s, t) calloc(1, (s)) 3619f95f3e0SAlex Hornung #define _PROP_REALLOC(v, s, t) realloc((v), (s)) 3629f95f3e0SAlex Hornung #define _PROP_FREE(v, t) free((v)) 3639f95f3e0SAlex Hornung 3649f95f3e0SAlex Hornung #define _PROP_POOL_GET(p) malloc((p)) 3659f95f3e0SAlex Hornung #define _PROP_POOL_PUT(p, v) free((v)) 3669f95f3e0SAlex Hornung 3679f95f3e0SAlex Hornung #define _PROP_POOL_INIT(p, s, d) static const size_t p = s; 3689f95f3e0SAlex Hornung 3699f95f3e0SAlex Hornung #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */ 3709f95f3e0SAlex Hornung 3719f95f3e0SAlex Hornung #if defined(__NetBSD__) && defined(_LIBPROP) 3729f95f3e0SAlex Hornung /* 3739f95f3e0SAlex Hornung * Use the same mechanism as libc; we get pthread mutexes for threaded 3749f95f3e0SAlex Hornung * programs and do-nothing stubs for non-threaded programs. 3759f95f3e0SAlex Hornung */ 3769f95f3e0SAlex Hornung #include "reentrant.h" 3779f95f3e0SAlex Hornung #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x; 3789f95f3e0SAlex Hornung #define _PROP_MUTEX_INIT(x) mutex_init(&(x), NULL) 3799f95f3e0SAlex Hornung #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x)) 3809f95f3e0SAlex Hornung #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x)) 3819f95f3e0SAlex Hornung 3829f95f3e0SAlex Hornung #define _PROP_RWLOCK_DECL(x) rwlock_t x ; 3839f95f3e0SAlex Hornung #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL) 3849f95f3e0SAlex Hornung #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x)) 3859f95f3e0SAlex Hornung #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x)) 3869f95f3e0SAlex Hornung #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x)) 3879f95f3e0SAlex Hornung #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x)) 3889f95f3e0SAlex Hornung 3899f95f3e0SAlex Hornung #define _PROP_ONCE_DECL(x) \ 3909f95f3e0SAlex Hornung static pthread_once_t x = PTHREAD_ONCE_INIT; 3919f95f3e0SAlex Hornung #define _PROP_ONCE_RUN(x,f) thr_once(&(x), (void(*)(void))f); 3929f95f3e0SAlex Hornung 3939f95f3e0SAlex Hornung #elif defined(HAVE_NBTOOL_CONFIG_H) 3949f95f3e0SAlex Hornung /* 3959f95f3e0SAlex Hornung * None of NetBSD's build tools are multi-threaded. 3969f95f3e0SAlex Hornung */ 3979f95f3e0SAlex Hornung #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */ 3989f95f3e0SAlex Hornung #define _PROP_MUTEX_INIT(x) /* nothing */ 3999f95f3e0SAlex Hornung #define _PROP_MUTEX_LOCK(x) /* nothing */ 4009f95f3e0SAlex Hornung #define _PROP_MUTEX_UNLOCK(x) /* nothing */ 4019f95f3e0SAlex Hornung 4029f95f3e0SAlex Hornung #define _PROP_RWLOCK_DECL(x) /* nothing */ 4039f95f3e0SAlex Hornung #define _PROP_RWLOCK_INIT(x) /* nothing */ 4049f95f3e0SAlex Hornung #define _PROP_RWLOCK_RDLOCK(x) /* nothing */ 4059f95f3e0SAlex Hornung #define _PROP_RWLOCK_WRLOCK(x) /* nothing */ 4069f95f3e0SAlex Hornung #define _PROP_RWLOCK_UNLOCK(x) /* nothing */ 4079f95f3e0SAlex Hornung #define _PROP_RWLOCK_DESTROY(x) /* nothing */ 4089f95f3e0SAlex Hornung 4099f95f3e0SAlex Hornung #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x) 4109f95f3e0SAlex Hornung #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f) 4119f95f3e0SAlex Hornung #else 4129f95f3e0SAlex Hornung /* 4139f95f3e0SAlex Hornung * Use pthread mutexes everywhere else. 4149f95f3e0SAlex Hornung */ 4159f95f3e0SAlex Hornung #include <pthread.h> 4169f95f3e0SAlex Hornung #define _PROP_MUTEX_DECL_STATIC(x) static pthread_mutex_t x; 4179f95f3e0SAlex Hornung #define _PROP_MUTEX_INIT(x) pthread_mutex_init(&(x), NULL) 4189f95f3e0SAlex Hornung #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x)) 4199f95f3e0SAlex Hornung #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) 4209f95f3e0SAlex Hornung 4219f95f3e0SAlex Hornung #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ; 4229f95f3e0SAlex Hornung #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL) 4239f95f3e0SAlex Hornung #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x)) 4249f95f3e0SAlex Hornung #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x)) 4259f95f3e0SAlex Hornung #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x)) 4269f95f3e0SAlex Hornung #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x)) 4279f95f3e0SAlex Hornung 4289f95f3e0SAlex Hornung #define _PROP_ONCE_DECL(x) \ 4299f95f3e0SAlex Hornung static pthread_once_t x = PTHREAD_ONCE_INIT; 4309f95f3e0SAlex Hornung #define _PROP_ONCE_RUN(x,f) pthread_once(&(x),(void(*)(void))f) 4319f95f3e0SAlex Hornung #endif 4329f95f3e0SAlex Hornung 4339f95f3e0SAlex Hornung #endif /* _KERNEL */ 4349f95f3e0SAlex Hornung 4359f95f3e0SAlex Hornung /* 4369f95f3e0SAlex Hornung * Language features. 4379f95f3e0SAlex Hornung */ 4389f95f3e0SAlex Hornung #include <sys/cdefs.h> 4399f95f3e0SAlex Hornung #define _PROP_ARG_UNUSED __unused 4409f95f3e0SAlex Hornung 4419f95f3e0SAlex Hornung #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */ 442