1*0a6a1f1dSLionel Sambuc /* $NetBSD: prop_object_impl.h,v 1.32 2015/05/11 16:50:35 christos Exp $ */ 26b6d114aSBen Gras 36b6d114aSBen Gras /*- 46b6d114aSBen Gras * Copyright (c) 2006 The NetBSD Foundation, Inc. 56b6d114aSBen Gras * All rights reserved. 66b6d114aSBen Gras * 76b6d114aSBen Gras * This code is derived from software contributed to The NetBSD Foundation 86b6d114aSBen Gras * by Jason R. Thorpe. 96b6d114aSBen Gras * 106b6d114aSBen Gras * Redistribution and use in source and binary forms, with or without 116b6d114aSBen Gras * modification, are permitted provided that the following conditions 126b6d114aSBen Gras * are met: 136b6d114aSBen Gras * 1. Redistributions of source code must retain the above copyright 146b6d114aSBen Gras * notice, this list of conditions and the following disclaimer. 156b6d114aSBen Gras * 2. Redistributions in binary form must reproduce the above copyright 166b6d114aSBen Gras * notice, this list of conditions and the following disclaimer in the 176b6d114aSBen Gras * documentation and/or other materials provided with the distribution. 186b6d114aSBen Gras * 196b6d114aSBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 206b6d114aSBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 216b6d114aSBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 226b6d114aSBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 236b6d114aSBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 246b6d114aSBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 256b6d114aSBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 266b6d114aSBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 276b6d114aSBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 286b6d114aSBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 296b6d114aSBen Gras * POSSIBILITY OF SUCH DAMAGE. 306b6d114aSBen Gras */ 316b6d114aSBen Gras 326b6d114aSBen Gras #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_ 336b6d114aSBen Gras #define _PROPLIB_PROP_OBJECT_IMPL_H_ 346b6d114aSBen Gras 356b6d114aSBen Gras #if defined(_KERNEL) || defined(_STANDALONE) 366b6d114aSBen Gras #include <lib/libkern/libkern.h> 376b6d114aSBen Gras #else 386b6d114aSBen Gras #include <inttypes.h> 396b6d114aSBen Gras #endif 406b6d114aSBen Gras 416b6d114aSBen Gras #include "prop_stack.h" 426b6d114aSBen Gras 436b6d114aSBen Gras struct _prop_object_externalize_context { 446b6d114aSBen Gras char * poec_buf; /* string buffer */ 456b6d114aSBen Gras size_t poec_capacity; /* capacity of buffer */ 466b6d114aSBen Gras size_t poec_len; /* current length of string */ 476b6d114aSBen Gras unsigned int poec_depth; /* nesting depth */ 486b6d114aSBen Gras }; 496b6d114aSBen Gras 506b6d114aSBen Gras bool _prop_object_externalize_start_tag( 516b6d114aSBen Gras struct _prop_object_externalize_context *, 526b6d114aSBen Gras const char *); 536b6d114aSBen Gras bool _prop_object_externalize_end_tag( 546b6d114aSBen Gras struct _prop_object_externalize_context *, 556b6d114aSBen Gras const char *); 566b6d114aSBen Gras bool _prop_object_externalize_empty_tag( 576b6d114aSBen Gras struct _prop_object_externalize_context *, 586b6d114aSBen Gras const char *); 596b6d114aSBen Gras bool _prop_object_externalize_append_cstring( 606b6d114aSBen Gras struct _prop_object_externalize_context *, 616b6d114aSBen Gras const char *); 626b6d114aSBen Gras bool _prop_object_externalize_append_encoded_cstring( 636b6d114aSBen Gras struct _prop_object_externalize_context *, 646b6d114aSBen Gras const char *); 656b6d114aSBen Gras bool _prop_object_externalize_append_char( 666b6d114aSBen Gras struct _prop_object_externalize_context *, 676b6d114aSBen Gras unsigned char); 686b6d114aSBen Gras bool _prop_object_externalize_header( 696b6d114aSBen Gras struct _prop_object_externalize_context *); 706b6d114aSBen Gras bool _prop_object_externalize_footer( 716b6d114aSBen Gras struct _prop_object_externalize_context *); 726b6d114aSBen Gras 736b6d114aSBen Gras struct _prop_object_externalize_context * 746b6d114aSBen Gras _prop_object_externalize_context_alloc(void); 756b6d114aSBen Gras void _prop_object_externalize_context_free( 766b6d114aSBen Gras struct _prop_object_externalize_context *); 776b6d114aSBen Gras 786b6d114aSBen Gras typedef enum { 796b6d114aSBen Gras _PROP_TAG_TYPE_START, /* e.g. <dict> */ 806b6d114aSBen Gras _PROP_TAG_TYPE_END, /* e.g. </dict> */ 816b6d114aSBen Gras _PROP_TAG_TYPE_EITHER 826b6d114aSBen Gras } _prop_tag_type_t; 836b6d114aSBen Gras 846b6d114aSBen Gras struct _prop_object_internalize_context { 856b6d114aSBen Gras const char *poic_xml; 866b6d114aSBen Gras const char *poic_cp; 876b6d114aSBen Gras 886b6d114aSBen Gras const char *poic_tag_start; 896b6d114aSBen Gras 906b6d114aSBen Gras const char *poic_tagname; 916b6d114aSBen Gras size_t poic_tagname_len; 926b6d114aSBen Gras const char *poic_tagattr; 936b6d114aSBen Gras size_t poic_tagattr_len; 946b6d114aSBen Gras const char *poic_tagattrval; 956b6d114aSBen Gras size_t poic_tagattrval_len; 966b6d114aSBen Gras 976b6d114aSBen Gras bool poic_is_empty_element; 986b6d114aSBen Gras _prop_tag_type_t poic_tag_type; 996b6d114aSBen Gras }; 1006b6d114aSBen Gras 1016b6d114aSBen Gras typedef enum { 1026b6d114aSBen Gras _PROP_OBJECT_FREE_DONE, 1036b6d114aSBen Gras _PROP_OBJECT_FREE_RECURSE, 1046b6d114aSBen Gras _PROP_OBJECT_FREE_FAILED 1056b6d114aSBen Gras } _prop_object_free_rv_t; 1066b6d114aSBen Gras 1076b6d114aSBen Gras typedef enum { 1086b6d114aSBen Gras _PROP_OBJECT_EQUALS_FALSE, 1096b6d114aSBen Gras _PROP_OBJECT_EQUALS_TRUE, 1106b6d114aSBen Gras _PROP_OBJECT_EQUALS_RECURSE 1116b6d114aSBen Gras } _prop_object_equals_rv_t; 1126b6d114aSBen Gras 1136b6d114aSBen Gras #define _PROP_EOF(c) ((c) == '\0') 1146b6d114aSBen Gras #define _PROP_ISSPACE(c) \ 115*0a6a1f1dSLionel Sambuc ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r') 1166b6d114aSBen Gras 1176b6d114aSBen Gras #define _PROP_TAG_MATCH(ctx, t) \ 1186b6d114aSBen Gras _prop_object_internalize_match((ctx)->poic_tagname, \ 1196b6d114aSBen Gras (ctx)->poic_tagname_len, \ 1206b6d114aSBen Gras (t), strlen(t)) 1216b6d114aSBen Gras 1226b6d114aSBen Gras #define _PROP_TAGATTR_MATCH(ctx, a) \ 1236b6d114aSBen Gras _prop_object_internalize_match((ctx)->poic_tagattr, \ 1246b6d114aSBen Gras (ctx)->poic_tagattr_len, \ 1256b6d114aSBen Gras (a), strlen(a)) 1266b6d114aSBen Gras 1276b6d114aSBen Gras #define _PROP_TAGATTRVAL_MATCH(ctx, a) \ 1286b6d114aSBen Gras _prop_object_internalize_match((ctx)->poic_tagattrval, \ 1296b6d114aSBen Gras (ctx)->poic_tagattrval_len,\ 1306b6d114aSBen Gras (a), strlen(a)) 1316b6d114aSBen Gras 1326b6d114aSBen Gras bool _prop_object_internalize_find_tag( 1336b6d114aSBen Gras struct _prop_object_internalize_context *, 1346b6d114aSBen Gras const char *, _prop_tag_type_t); 1356b6d114aSBen Gras bool _prop_object_internalize_match(const char *, size_t, 1366b6d114aSBen Gras const char *, size_t); 1376b6d114aSBen Gras prop_object_t _prop_object_internalize_by_tag( 1386b6d114aSBen Gras struct _prop_object_internalize_context *); 1396b6d114aSBen Gras bool _prop_object_internalize_decode_string( 1406b6d114aSBen Gras struct _prop_object_internalize_context *, 1416b6d114aSBen Gras char *, size_t, size_t *, const char **); 1426b6d114aSBen Gras prop_object_t _prop_generic_internalize(const char *, const char *); 1436b6d114aSBen Gras 1446b6d114aSBen Gras struct _prop_object_internalize_context * 1456b6d114aSBen Gras _prop_object_internalize_context_alloc(const char *); 1466b6d114aSBen Gras void _prop_object_internalize_context_free( 1476b6d114aSBen Gras struct _prop_object_internalize_context *); 1486b6d114aSBen Gras 1496b6d114aSBen Gras #if !defined(_KERNEL) && !defined(_STANDALONE) 1506b6d114aSBen Gras bool _prop_object_externalize_write_file(const char *, 1516b6d114aSBen Gras const char *, size_t); 1526b6d114aSBen Gras 1536b6d114aSBen Gras struct _prop_object_internalize_mapped_file { 1546b6d114aSBen Gras char * poimf_xml; 1556b6d114aSBen Gras size_t poimf_mapsize; 1566b6d114aSBen Gras }; 1576b6d114aSBen Gras 1586b6d114aSBen Gras struct _prop_object_internalize_mapped_file * 1596b6d114aSBen Gras _prop_object_internalize_map_file(const char *); 1606b6d114aSBen Gras void _prop_object_internalize_unmap_file( 1616b6d114aSBen Gras struct _prop_object_internalize_mapped_file *); 1626b6d114aSBen Gras #endif /* !_KERNEL && !_STANDALONE */ 1636b6d114aSBen Gras 1646b6d114aSBen Gras typedef bool (*prop_object_internalizer_t)(prop_stack_t, prop_object_t *, 1656b6d114aSBen Gras struct _prop_object_internalize_context *); 1666b6d114aSBen Gras typedef bool (*prop_object_internalizer_continue_t)(prop_stack_t, 1676b6d114aSBen Gras prop_object_t *, 1686b6d114aSBen Gras struct _prop_object_internalize_context *, 1696b6d114aSBen Gras void *, prop_object_t); 1706b6d114aSBen Gras 1716b6d114aSBen Gras /* These are here because they're required by shared code. */ 1726b6d114aSBen Gras bool _prop_array_internalize(prop_stack_t, prop_object_t *, 1736b6d114aSBen Gras struct _prop_object_internalize_context *); 1746b6d114aSBen Gras bool _prop_bool_internalize(prop_stack_t, prop_object_t *, 1756b6d114aSBen Gras struct _prop_object_internalize_context *); 1766b6d114aSBen Gras bool _prop_data_internalize(prop_stack_t, prop_object_t *, 1776b6d114aSBen Gras struct _prop_object_internalize_context *); 1786b6d114aSBen Gras bool _prop_dictionary_internalize(prop_stack_t, prop_object_t *, 1796b6d114aSBen Gras struct _prop_object_internalize_context *); 1806b6d114aSBen Gras bool _prop_number_internalize(prop_stack_t, prop_object_t *, 1816b6d114aSBen Gras struct _prop_object_internalize_context *); 1826b6d114aSBen Gras bool _prop_string_internalize(prop_stack_t, prop_object_t *, 1836b6d114aSBen Gras struct _prop_object_internalize_context *); 1846b6d114aSBen Gras 1856b6d114aSBen Gras struct _prop_object_type { 1866b6d114aSBen Gras /* type indicator */ 1876b6d114aSBen Gras uint32_t pot_type; 1886b6d114aSBen Gras /* func to free object */ 1896b6d114aSBen Gras _prop_object_free_rv_t 1906b6d114aSBen Gras (*pot_free)(prop_stack_t, prop_object_t *); 1916b6d114aSBen Gras /* 1926b6d114aSBen Gras * func to free the child returned by pot_free with stack == NULL. 1936b6d114aSBen Gras * 1946b6d114aSBen Gras * Must be implemented if pot_free can return anything other than 1956b6d114aSBen Gras * _PROP_OBJECT_FREE_DONE. 1966b6d114aSBen Gras */ 1976b6d114aSBen Gras void (*pot_emergency_free)(prop_object_t); 1986b6d114aSBen Gras /* func to externalize object */ 1996b6d114aSBen Gras bool (*pot_extern)(struct _prop_object_externalize_context *, 2006b6d114aSBen Gras void *); 2016b6d114aSBen Gras /* func to test quality */ 2026b6d114aSBen Gras _prop_object_equals_rv_t 2036b6d114aSBen Gras (*pot_equals)(prop_object_t, prop_object_t, 2046b6d114aSBen Gras void **, void **, 2056b6d114aSBen Gras prop_object_t *, prop_object_t *); 2066b6d114aSBen Gras /* 2076b6d114aSBen Gras * func to finish equality iteration. 2086b6d114aSBen Gras * 2096b6d114aSBen Gras * Must be implemented if pot_equals can return 2106b6d114aSBen Gras * _PROP_OBJECT_EQUALS_RECURSE 2116b6d114aSBen Gras */ 2126b6d114aSBen Gras void (*pot_equals_finish)(prop_object_t, prop_object_t); 2136b6d114aSBen Gras void (*pot_lock)(void); 2146b6d114aSBen Gras void (*pot_unlock)(void); 2156b6d114aSBen Gras }; 2166b6d114aSBen Gras 2176b6d114aSBen Gras struct _prop_object { 2186b6d114aSBen Gras const struct _prop_object_type *po_type;/* type descriptor */ 2196b6d114aSBen Gras uint32_t po_refcnt; /* reference count */ 2206b6d114aSBen Gras }; 2216b6d114aSBen Gras 2226b6d114aSBen Gras void _prop_object_init(struct _prop_object *, 2236b6d114aSBen Gras const struct _prop_object_type *); 2246b6d114aSBen Gras void _prop_object_fini(struct _prop_object *); 2256b6d114aSBen Gras 2266b6d114aSBen Gras struct _prop_object_iterator { 2276b6d114aSBen Gras prop_object_t (*pi_next_object)(void *); 2286b6d114aSBen Gras void (*pi_reset)(void *); 2296b6d114aSBen Gras prop_object_t pi_obj; 2306b6d114aSBen Gras uint32_t pi_version; 2316b6d114aSBen Gras }; 2326b6d114aSBen Gras 2336b6d114aSBen Gras #define _PROP_NOTHREAD_ONCE_DECL(x) static bool x = false; 2346b6d114aSBen Gras #define _PROP_NOTHREAD_ONCE_RUN(x,f) \ 2356b6d114aSBen Gras do { \ 2366b6d114aSBen Gras if ((x) == false) { \ 2376b6d114aSBen Gras f(); \ 2386b6d114aSBen Gras x = true; \ 2396b6d114aSBen Gras } \ 2406b6d114aSBen Gras } while (/*CONSTCOND*/0) 2416b6d114aSBen Gras 2426b6d114aSBen Gras #if defined(_KERNEL) 2436b6d114aSBen Gras 2446b6d114aSBen Gras /* 2456b6d114aSBen Gras * proplib in the kernel... 2466b6d114aSBen Gras */ 2476b6d114aSBen Gras 2486b6d114aSBen Gras #include <sys/param.h> 2496b6d114aSBen Gras #include <sys/malloc.h> 2506b6d114aSBen Gras #include <sys/pool.h> 2516b6d114aSBen Gras #include <sys/systm.h> 2526b6d114aSBen Gras #include <sys/rwlock.h> 2536b6d114aSBen Gras #include <sys/once.h> 2546b6d114aSBen Gras 2556b6d114aSBen Gras #define _PROP_ASSERT(x) KASSERT(x) 2566b6d114aSBen Gras 2576b6d114aSBen Gras #define _PROP_MALLOC(s, t) malloc((s), (t), M_WAITOK) 2586b6d114aSBen Gras #define _PROP_CALLOC(s, t) malloc((s), (t), M_WAITOK | M_ZERO) 2596b6d114aSBen Gras #define _PROP_REALLOC(v, s, t) realloc((v), (s), (t), M_WAITOK) 2606b6d114aSBen Gras #define _PROP_FREE(v, t) free((v), (t)) 2616b6d114aSBen Gras 2626b6d114aSBen Gras #define _PROP_POOL_GET(p) pool_get(&(p), PR_WAITOK) 2636b6d114aSBen Gras #define _PROP_POOL_PUT(p, v) pool_put(&(p), (v)) 2646b6d114aSBen Gras 2656b6d114aSBen Gras struct prop_pool_init { 2666b6d114aSBen Gras struct pool *pp; 2676b6d114aSBen Gras size_t size; 2686b6d114aSBen Gras const char *wchan; 2696b6d114aSBen Gras }; 2706b6d114aSBen Gras #define _PROP_POOL_INIT(pp, size, wchan) \ 2716b6d114aSBen Gras struct pool pp; \ 2726b6d114aSBen Gras static const struct prop_pool_init _link_ ## pp[1] = { \ 2736b6d114aSBen Gras { &pp, size, wchan } \ 2746b6d114aSBen Gras }; \ 2756b6d114aSBen Gras __link_set_add_rodata(prop_linkpools, _link_ ## pp); 2766b6d114aSBen Gras 2776b6d114aSBen Gras #define _PROP_MALLOC_DEFINE(t, s, l) \ 2786b6d114aSBen Gras MALLOC_DEFINE(t, s, l); 2796b6d114aSBen Gras 2806b6d114aSBen Gras #define _PROP_MUTEX_DECL_STATIC(x) static kmutex_t x; 2816b6d114aSBen Gras #define _PROP_MUTEX_INIT(x) mutex_init(&(x),MUTEX_DEFAULT,IPL_NONE) 2826b6d114aSBen Gras #define _PROP_MUTEX_LOCK(x) mutex_enter(&(x)) 2836b6d114aSBen Gras #define _PROP_MUTEX_UNLOCK(x) mutex_exit(&(x)) 2846b6d114aSBen Gras 2856b6d114aSBen Gras #define _PROP_RWLOCK_DECL(x) krwlock_t x ; 2866b6d114aSBen Gras #define _PROP_RWLOCK_INIT(x) rw_init(&(x)) 2876b6d114aSBen Gras #define _PROP_RWLOCK_RDLOCK(x) rw_enter(&(x), RW_READER) 2886b6d114aSBen Gras #define _PROP_RWLOCK_WRLOCK(x) rw_enter(&(x), RW_WRITER) 2896b6d114aSBen Gras #define _PROP_RWLOCK_UNLOCK(x) rw_exit(&(x)) 2906b6d114aSBen Gras #define _PROP_RWLOCK_DESTROY(x) rw_destroy(&(x)) 2916b6d114aSBen Gras 2926b6d114aSBen Gras #define _PROP_ONCE_DECL(x) static ONCE_DECL(x); 2936b6d114aSBen Gras #define _PROP_ONCE_RUN(x,f) RUN_ONCE(&(x), f) 2946b6d114aSBen Gras 295f14fb602SLionel Sambuc #include <sys/atomic.h> 296f14fb602SLionel Sambuc 297f14fb602SLionel Sambuc #define _PROP_ATOMIC_INC32(x) atomic_inc_32(x) 298f14fb602SLionel Sambuc #define _PROP_ATOMIC_DEC32(x) atomic_dec_32(x) 299f14fb602SLionel Sambuc #define _PROP_ATOMIC_INC32_NV(x, v) v = atomic_inc_32_nv(x) 300f14fb602SLionel Sambuc #define _PROP_ATOMIC_DEC32_NV(x, v) v = atomic_dec_32_nv(x) 301f14fb602SLionel Sambuc 3026b6d114aSBen Gras #elif defined(_STANDALONE) 3036b6d114aSBen Gras 3046b6d114aSBen Gras /* 3056b6d114aSBen Gras * proplib in a standalone environment... 3066b6d114aSBen Gras */ 3076b6d114aSBen Gras 3086b6d114aSBen Gras #include <lib/libsa/stand.h> 3096b6d114aSBen Gras 3106b6d114aSBen Gras void * _prop_standalone_calloc(size_t); 3116b6d114aSBen Gras void * _prop_standalone_realloc(void *, size_t); 3126b6d114aSBen Gras 3136b6d114aSBen Gras #define _PROP_ASSERT(x) /* nothing */ 3146b6d114aSBen Gras 3156b6d114aSBen Gras #define _PROP_MALLOC(s, t) alloc((s)) 3166b6d114aSBen Gras #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s)) 3176b6d114aSBen Gras #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s)) 3186b6d114aSBen Gras #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */ 3196b6d114aSBen Gras 3206b6d114aSBen Gras #define _PROP_POOL_GET(p) alloc((p)) 3216b6d114aSBen Gras #define _PROP_POOL_PUT(p, v) dealloc((v), (p)) 3226b6d114aSBen Gras 3236b6d114aSBen Gras #define _PROP_POOL_INIT(p, s, d) static const size_t p = s; 3246b6d114aSBen Gras 3256b6d114aSBen Gras #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */ 3266b6d114aSBen Gras 3276b6d114aSBen Gras #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */ 3286b6d114aSBen Gras #define _PROP_MUTEX_INIT(x) /* nothing */ 3296b6d114aSBen Gras #define _PROP_MUTEX_LOCK(x) /* nothing */ 3306b6d114aSBen Gras #define _PROP_MUTEX_UNLOCK(x) /* nothing */ 3316b6d114aSBen Gras 3326b6d114aSBen Gras #define _PROP_RWLOCK_DECL(x) /* nothing */ 3336b6d114aSBen Gras #define _PROP_RWLOCK_INIT(x) /* nothing */ 3346b6d114aSBen Gras #define _PROP_RWLOCK_RDLOCK(x) /* nothing */ 3356b6d114aSBen Gras #define _PROP_RWLOCK_WRLOCK(x) /* nothing */ 3366b6d114aSBen Gras #define _PROP_RWLOCK_UNLOCK(x) /* nothing */ 3376b6d114aSBen Gras #define _PROP_RWLOCK_DESTROY(x) /* nothing */ 3386b6d114aSBen Gras 3396b6d114aSBen Gras #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x) 3406b6d114aSBen Gras #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f) 3416b6d114aSBen Gras 342f14fb602SLionel Sambuc #define _PROP_ATOMIC_INC32(x) ++*(x) 343f14fb602SLionel Sambuc #define _PROP_ATOMIC_DEC32(x) --*(x) 344f14fb602SLionel Sambuc #define _PROP_ATOMIC_INC32_NV(x, v) v = ++*(x) 345f14fb602SLionel Sambuc #define _PROP_ATOMIC_DEC32_NV(x, v) v = --*(x) 346f14fb602SLionel Sambuc 3476b6d114aSBen Gras #else 3486b6d114aSBen Gras 3496b6d114aSBen Gras /* 3506b6d114aSBen Gras * proplib in user space... 3516b6d114aSBen Gras */ 3526b6d114aSBen Gras 3536b6d114aSBen Gras #include <assert.h> 3546b6d114aSBen Gras #include <string.h> 3556b6d114aSBen Gras #include <stdio.h> 3566b6d114aSBen Gras #include <stdlib.h> 3576b6d114aSBen Gras #include <stddef.h> 3586b6d114aSBen Gras 3596b6d114aSBen Gras #define _PROP_ASSERT(x) /*LINTED*/assert(x) 3606b6d114aSBen Gras 3616b6d114aSBen Gras #define _PROP_MALLOC(s, t) malloc((s)) 3626b6d114aSBen Gras #define _PROP_CALLOC(s, t) calloc(1, (s)) 3636b6d114aSBen Gras #define _PROP_REALLOC(v, s, t) realloc((v), (s)) 3646b6d114aSBen Gras #define _PROP_FREE(v, t) free((v)) 3656b6d114aSBen Gras 3666b6d114aSBen Gras #define _PROP_POOL_GET(p) malloc((p)) 3676b6d114aSBen Gras #define _PROP_POOL_PUT(p, v) free((v)) 3686b6d114aSBen Gras 3696b6d114aSBen Gras #define _PROP_POOL_INIT(p, s, d) static const size_t p = s; 3706b6d114aSBen Gras 3716b6d114aSBen Gras #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */ 3726b6d114aSBen Gras 373f14fb602SLionel Sambuc #if defined(__NetBSD__) && defined(_LIBPROP) 3746b6d114aSBen Gras /* 3756b6d114aSBen Gras * Use the same mechanism as libc; we get pthread mutexes for threaded 3766b6d114aSBen Gras * programs and do-nothing stubs for non-threaded programs. 3776b6d114aSBen Gras */ 378f14fb602SLionel Sambuc #include <sys/atomic.h> 3796b6d114aSBen Gras #include "reentrant.h" 3806b6d114aSBen Gras #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x; 3816b6d114aSBen Gras #define _PROP_MUTEX_INIT(x) mutex_init(&(x), NULL) 3826b6d114aSBen Gras #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x)) 3836b6d114aSBen Gras #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x)) 3846b6d114aSBen Gras 3856b6d114aSBen Gras #define _PROP_RWLOCK_DECL(x) rwlock_t x ; 3866b6d114aSBen Gras #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL) 3876b6d114aSBen Gras #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x)) 3886b6d114aSBen Gras #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x)) 3896b6d114aSBen Gras #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x)) 3906b6d114aSBen Gras #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x)) 3916b6d114aSBen Gras 3926b6d114aSBen Gras #define _PROP_ONCE_DECL(x) \ 3936b6d114aSBen Gras static pthread_once_t x = PTHREAD_ONCE_INIT; 3946b6d114aSBen Gras #define _PROP_ONCE_RUN(x,f) thr_once(&(x), (void(*)(void))f); 3956b6d114aSBen Gras 396f14fb602SLionel Sambuc #define _PROP_ATOMIC_INC32(x) atomic_inc_32(x) 397f14fb602SLionel Sambuc #define _PROP_ATOMIC_DEC32(x) atomic_dec_32(x) 398f14fb602SLionel Sambuc #define _PROP_ATOMIC_INC32_NV(x, v) v = atomic_inc_32_nv(x) 399f14fb602SLionel Sambuc #define _PROP_ATOMIC_DEC32_NV(x, v) v = atomic_dec_32_nv(x) 400f14fb602SLionel Sambuc 4016b6d114aSBen Gras #elif defined(HAVE_NBTOOL_CONFIG_H) || defined(__minix) 4026b6d114aSBen Gras /* 4036b6d114aSBen Gras * None of NetBSD's build tools are multi-threaded. 4046b6d114aSBen Gras */ 4056b6d114aSBen Gras #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */ 4066b6d114aSBen Gras #define _PROP_MUTEX_INIT(x) /* nothing */ 4076b6d114aSBen Gras #define _PROP_MUTEX_LOCK(x) /* nothing */ 4086b6d114aSBen Gras #define _PROP_MUTEX_UNLOCK(x) /* nothing */ 4096b6d114aSBen Gras 4106b6d114aSBen Gras #define _PROP_RWLOCK_DECL(x) /* nothing */ 4116b6d114aSBen Gras #define _PROP_RWLOCK_INIT(x) /* nothing */ 4126b6d114aSBen Gras #define _PROP_RWLOCK_RDLOCK(x) /* nothing */ 4136b6d114aSBen Gras #define _PROP_RWLOCK_WRLOCK(x) /* nothing */ 4146b6d114aSBen Gras #define _PROP_RWLOCK_UNLOCK(x) /* nothing */ 4156b6d114aSBen Gras #define _PROP_RWLOCK_DESTROY(x) /* nothing */ 4166b6d114aSBen Gras 4176b6d114aSBen Gras #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x) 4186b6d114aSBen Gras #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f) 419f14fb602SLionel Sambuc 420f14fb602SLionel Sambuc #define _PROP_ATOMIC_INC32(x) ++*(x) 421f14fb602SLionel Sambuc #define _PROP_ATOMIC_DEC32(x) --*(x) 422f14fb602SLionel Sambuc #define _PROP_ATOMIC_INC32_NV(x, v) v = ++*(x) 423f14fb602SLionel Sambuc #define _PROP_ATOMIC_DEC32_NV(x, v) v = --*(x) 424f14fb602SLionel Sambuc 4256b6d114aSBen Gras #else 4266b6d114aSBen Gras /* 4276b6d114aSBen Gras * Use pthread mutexes everywhere else. 4286b6d114aSBen Gras */ 4296b6d114aSBen Gras #include <pthread.h> 4306b6d114aSBen Gras #define _PROP_MUTEX_DECL_STATIC(x) static pthread_mutex_t x; 4316b6d114aSBen Gras #define _PROP_MUTEX_INIT(x) pthread_mutex_init(&(x), NULL) 4326b6d114aSBen Gras #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x)) 4336b6d114aSBen Gras #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) 4346b6d114aSBen Gras 4356b6d114aSBen Gras #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ; 4366b6d114aSBen Gras #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL) 4376b6d114aSBen Gras #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x)) 4386b6d114aSBen Gras #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x)) 4396b6d114aSBen Gras #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x)) 4406b6d114aSBen Gras #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x)) 4416b6d114aSBen Gras 4426b6d114aSBen Gras #define _PROP_ONCE_DECL(x) \ 4436b6d114aSBen Gras static pthread_once_t x = PTHREAD_ONCE_INIT; 4446b6d114aSBen Gras #define _PROP_ONCE_RUN(x,f) pthread_once(&(x),(void(*)(void))f) 4456b6d114aSBen Gras 446f14fb602SLionel Sambuc #define _PROP_NEED_REFCNT_MTX 447f14fb602SLionel Sambuc 448f14fb602SLionel Sambuc #define _PROP_ATOMIC_INC32(x) \ 449f14fb602SLionel Sambuc do { \ 450f14fb602SLionel Sambuc pthread_mutex_lock(&_prop_refcnt_mtx); \ 451f14fb602SLionel Sambuc (*(x))++; \ 452f14fb602SLionel Sambuc pthread_mutex_unlock(&_prop_refcnt_mtx); \ 453f14fb602SLionel Sambuc } while (/*CONSTCOND*/0) 454f14fb602SLionel Sambuc 455f14fb602SLionel Sambuc #define _PROP_ATOMIC_DEC32(x) \ 456f14fb602SLionel Sambuc do { \ 457f14fb602SLionel Sambuc pthread_mutex_lock(&_prop_refcnt_mtx); \ 458f14fb602SLionel Sambuc (*(x))--; \ 459f14fb602SLionel Sambuc pthread_mutex_unlock(&_prop_refcnt_mtx); \ 460f14fb602SLionel Sambuc } while (/*CONSTCOND*/0) 461f14fb602SLionel Sambuc 462f14fb602SLionel Sambuc #define _PROP_ATOMIC_INC32_NV(x, v) \ 463f14fb602SLionel Sambuc do { \ 464f14fb602SLionel Sambuc pthread_mutex_lock(&_prop_refcnt_mtx); \ 465f14fb602SLionel Sambuc v = ++(*(x)); \ 466f14fb602SLionel Sambuc pthread_mutex_unlock(&_prop_refcnt_mtx); \ 467f14fb602SLionel Sambuc } while (/*CONSTCOND*/0) 468f14fb602SLionel Sambuc 469f14fb602SLionel Sambuc #define _PROP_ATOMIC_DEC32_NV(x, v) \ 470f14fb602SLionel Sambuc do { \ 471f14fb602SLionel Sambuc pthread_mutex_lock(&_prop_refcnt_mtx); \ 472f14fb602SLionel Sambuc v = --(*(x)); \ 473f14fb602SLionel Sambuc pthread_mutex_unlock(&_prop_refcnt_mtx); \ 474f14fb602SLionel Sambuc } while (/*CONSTCOND*/0) 475f14fb602SLionel Sambuc 476f14fb602SLionel Sambuc #endif 4776b6d114aSBen Gras #endif /* _KERNEL */ 4786b6d114aSBen Gras 4796b6d114aSBen Gras /* 4806b6d114aSBen Gras * Language features. 4816b6d114aSBen Gras */ 4823260d16fSLionel Sambuc #if defined(__NetBSD__) || defined(__minix) 4836b6d114aSBen Gras #include <sys/cdefs.h> 4846b6d114aSBen Gras #define _PROP_ARG_UNUSED __unused 4856b6d114aSBen Gras #else 4866b6d114aSBen Gras #define _PROP_ARG_UNUSED /* delete */ 4876b6d114aSBen Gras #endif /* __NetBSD__ */ 4886b6d114aSBen Gras 4896b6d114aSBen Gras #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */ 490