Home | History | Annotate | Line # | Download | only in libprop
      1  1.41   thorpej /*	$NetBSD: prop_object_impl.h,v 1.41 2025/05/14 03:25:46 thorpej Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4  1.37   thorpej  * Copyright (c) 2006, 2020, 2025 The NetBSD Foundation, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1   thorpej  * by Jason R. Thorpe.
      9   1.1   thorpej  *
     10   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.1   thorpej  * modification, are permitted provided that the following conditions
     12   1.1   thorpej  * are met:
     13   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.1   thorpej  *
     19   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1   thorpej  */
     31   1.1   thorpej 
     32   1.1   thorpej #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_
     33   1.1   thorpej #define	_PROPLIB_PROP_OBJECT_IMPL_H_
     34   1.1   thorpej 
     35  1.33   thorpej #if defined(HAVE_NBTOOL_CONFIG_H)
     36  1.33   thorpej #include "nbtool_config.h"
     37  1.33   thorpej #endif
     38  1.33   thorpej 
     39   1.5   thorpej #if defined(_KERNEL) || defined(_STANDALONE)
     40   1.5   thorpej #include <lib/libkern/libkern.h>
     41   1.5   thorpej #else
     42   1.5   thorpej #include <inttypes.h>
     43   1.5   thorpej #endif
     44   1.5   thorpej 
     45  1.17     joerg #include "prop_stack.h"
     46  1.17     joerg 
     47  1.37   thorpej struct _prop_object;
     48  1.37   thorpej 
     49   1.1   thorpej struct _prop_object_externalize_context {
     50   1.1   thorpej 	char *		poec_buf;		/* string buffer */
     51   1.1   thorpej 	size_t		poec_capacity;		/* capacity of buffer */
     52   1.1   thorpej 	size_t		poec_len;		/* current length of string */
     53   1.1   thorpej 	unsigned int	poec_depth;		/* nesting depth */
     54  1.37   thorpej 	prop_format_t	poec_format;		/* output format */
     55  1.37   thorpej };
     56  1.37   thorpej 
     57  1.37   thorpej struct _prop_object_type_tags {
     58  1.37   thorpej 	const char	*xml_tag;
     59  1.37   thorpej 	const char	*json_open_tag;
     60  1.37   thorpej 	const char	*json_close_tag;
     61  1.37   thorpej 	const char	*json_empty_sep;
     62   1.1   thorpej };
     63   1.1   thorpej 
     64  1.41   thorpej bool		_prop_extern_append_char(
     65  1.41   thorpej 				struct _prop_object_externalize_context *,
     66  1.41   thorpej 				unsigned char);
     67  1.41   thorpej bool		_prop_extern_append_cstring(
     68  1.41   thorpej 				struct _prop_object_externalize_context *,
     69  1.41   thorpej 				const char *);
     70  1.41   thorpej bool		_prop_extern_start_line(
     71  1.37   thorpej 				struct _prop_object_externalize_context *);
     72  1.41   thorpej bool		_prop_extern_end_line(
     73  1.37   thorpej 				struct _prop_object_externalize_context *,
     74  1.37   thorpej 				const char *);
     75  1.41   thorpej 
     76  1.41   thorpej bool		_prop_extern_append_start_tag(
     77   1.1   thorpej 				struct _prop_object_externalize_context *,
     78  1.37   thorpej 				const struct _prop_object_type_tags *,
     79   1.1   thorpej 				const char *);
     80  1.41   thorpej bool		_prop_extern_append_end_tag(
     81   1.1   thorpej 				struct _prop_object_externalize_context *,
     82  1.37   thorpej 				const struct _prop_object_type_tags *);
     83  1.41   thorpej bool		_prop_extern_append_empty_tag(
     84   1.1   thorpej 				struct _prop_object_externalize_context *,
     85  1.37   thorpej 				const struct _prop_object_type_tags *);
     86  1.41   thorpej 
     87  1.41   thorpej bool		_prop_extern_append_encoded_cstring(
     88  1.40   thorpej 				struct _prop_object_externalize_context *,
     89  1.40   thorpej 				const char *);
     90   1.1   thorpej 
     91  1.37   thorpej bool		_prop_object_externalize_to_file(struct _prop_object *,
     92  1.37   thorpej 				const char *, prop_format_t);
     93  1.41   thorpej char *		_prop_object_externalize(struct _prop_object *,
     94  1.41   thorpej 				prop_format_t fmt);
     95   1.1   thorpej 
     96   1.1   thorpej typedef enum {
     97   1.1   thorpej 	_PROP_TAG_TYPE_START,			/* e.g. <dict> */
     98   1.1   thorpej 	_PROP_TAG_TYPE_END,			/* e.g. </dict> */
     99   1.1   thorpej 	_PROP_TAG_TYPE_EITHER
    100   1.1   thorpej } _prop_tag_type_t;
    101   1.1   thorpej 
    102   1.1   thorpej struct _prop_object_internalize_context {
    103  1.37   thorpej 	prop_format_t poic_format;
    104  1.37   thorpej 
    105  1.37   thorpej 	const char *poic_data;
    106   1.1   thorpej 	const char *poic_cp;
    107   1.1   thorpej 
    108   1.1   thorpej 	const char *poic_tag_start;
    109   1.1   thorpej 
    110   1.1   thorpej 	const char *poic_tagname;
    111   1.1   thorpej 	size_t      poic_tagname_len;
    112   1.1   thorpej 	const char *poic_tagattr;
    113   1.1   thorpej 	size_t      poic_tagattr_len;
    114   1.1   thorpej 	const char *poic_tagattrval;
    115   1.1   thorpej 	size_t      poic_tagattrval_len;
    116   1.1   thorpej 
    117  1.16   thorpej 	bool   poic_is_empty_element;
    118   1.1   thorpej 	_prop_tag_type_t poic_tag_type;
    119   1.1   thorpej };
    120   1.1   thorpej 
    121  1.27   thorpej typedef enum {
    122  1.17     joerg 	_PROP_OBJECT_FREE_DONE,
    123  1.17     joerg 	_PROP_OBJECT_FREE_RECURSE,
    124  1.17     joerg 	_PROP_OBJECT_FREE_FAILED
    125  1.27   thorpej } _prop_object_free_rv_t;
    126  1.17     joerg 
    127  1.27   thorpej typedef enum {
    128  1.18     joerg 	_PROP_OBJECT_EQUALS_FALSE,
    129  1.18     joerg 	_PROP_OBJECT_EQUALS_TRUE,
    130  1.18     joerg 	_PROP_OBJECT_EQUALS_RECURSE
    131  1.27   thorpej } _prop_object_equals_rv_t;
    132  1.18     joerg 
    133   1.1   thorpej #define	_PROP_EOF(c)		((c) == '\0')
    134   1.1   thorpej #define	_PROP_ISSPACE(c)	\
    135  1.32  christos 	((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
    136   1.1   thorpej 
    137  1.41   thorpej #define	_PROP_TAG_MATCH(ctx, t)				\
    138  1.41   thorpej 	_prop_intern_match((ctx)->poic_tagname,		\
    139  1.41   thorpej 			   (ctx)->poic_tagname_len,	\
    140  1.41   thorpej 			   (t), strlen(t))
    141  1.41   thorpej 
    142  1.41   thorpej #define	_PROP_TAGATTR_MATCH(ctx, a)			\
    143  1.41   thorpej 	_prop_intern_match((ctx)->poic_tagattr,		\
    144  1.41   thorpej 			   (ctx)->poic_tagattr_len,	\
    145  1.41   thorpej 			   (a), strlen(a))
    146  1.41   thorpej 
    147  1.41   thorpej #define	_PROP_TAGATTRVAL_MATCH(ctx, a)			\
    148  1.41   thorpej 	_prop_intern_match((ctx)->poic_tagattrval,	\
    149  1.41   thorpej 			   (ctx)->poic_tagattrval_len,	\
    150  1.41   thorpej 			   (a), strlen(a))
    151  1.41   thorpej 
    152  1.41   thorpej const char *	_prop_intern_skip_whitespace(const char *);
    153  1.41   thorpej bool		_prop_intern_match(const char *, size_t, const char *, size_t);
    154  1.41   thorpej 
    155  1.41   thorpej bool		_prop_intern_decode_string(
    156  1.41   thorpej 				struct _prop_object_internalize_context *,
    157  1.41   thorpej 				char *, size_t, size_t *, const char **);
    158  1.39   thorpej 
    159  1.41   thorpej bool		_prop_xml_intern_find_tag(
    160  1.40   thorpej 				struct _prop_object_internalize_context *,
    161  1.40   thorpej 				const char *, _prop_tag_type_t);
    162  1.41   thorpej 
    163  1.37   thorpej prop_object_t	_prop_object_internalize(const char *,
    164  1.37   thorpej 				const struct _prop_object_type_tags *);
    165  1.37   thorpej prop_object_t	_prop_object_internalize_from_file(const char *,
    166  1.37   thorpej 				const struct _prop_object_type_tags *);
    167   1.1   thorpej 
    168  1.27   thorpej typedef bool (*prop_object_internalizer_t)(prop_stack_t, prop_object_t *,
    169  1.27   thorpej 				struct _prop_object_internalize_context *);
    170  1.27   thorpej typedef bool (*prop_object_internalizer_continue_t)(prop_stack_t,
    171  1.27   thorpej 				prop_object_t *,
    172  1.27   thorpej 				struct _prop_object_internalize_context *,
    173  1.27   thorpej 				void *, prop_object_t);
    174  1.17     joerg 
    175   1.1   thorpej 	/* These are here because they're required by shared code. */
    176  1.27   thorpej bool		_prop_array_internalize(prop_stack_t, prop_object_t *,
    177  1.27   thorpej 				struct _prop_object_internalize_context *);
    178  1.27   thorpej bool		_prop_bool_internalize(prop_stack_t, prop_object_t *,
    179  1.27   thorpej 				struct _prop_object_internalize_context *);
    180  1.27   thorpej bool		_prop_data_internalize(prop_stack_t, prop_object_t *,
    181  1.27   thorpej 				struct _prop_object_internalize_context *);
    182  1.27   thorpej bool		_prop_dictionary_internalize(prop_stack_t, prop_object_t *,
    183  1.27   thorpej 				struct _prop_object_internalize_context *);
    184  1.27   thorpej bool		_prop_number_internalize(prop_stack_t, prop_object_t *,
    185  1.27   thorpej 				struct _prop_object_internalize_context *);
    186  1.27   thorpej bool		_prop_string_internalize(prop_stack_t, prop_object_t *,
    187  1.27   thorpej 				struct _prop_object_internalize_context *);
    188   1.1   thorpej 
    189  1.37   thorpej bool		_prop_string_externalize_internal(
    190  1.37   thorpej 				struct _prop_object_externalize_context *,
    191  1.37   thorpej 				const struct _prop_object_type_tags *,
    192  1.37   thorpej 				const char *);
    193  1.37   thorpej 
    194   1.2   thorpej struct _prop_object_type {
    195  1.17     joerg 	/* type indicator */
    196  1.17     joerg 	uint32_t	pot_type;
    197  1.17     joerg 	/* func to free object */
    198  1.27   thorpej 	_prop_object_free_rv_t
    199  1.27   thorpej 			(*pot_free)(prop_stack_t, prop_object_t *);
    200  1.17     joerg 	/*
    201  1.17     joerg 	 * func to free the child returned by pot_free with stack == NULL.
    202  1.17     joerg 	 *
    203  1.18     joerg 	 * Must be implemented if pot_free can return anything other than
    204  1.18     joerg 	 * _PROP_OBJECT_FREE_DONE.
    205  1.17     joerg 	 */
    206  1.17     joerg 	void	(*pot_emergency_free)(prop_object_t);
    207  1.17     joerg 	/* func to externalize object */
    208  1.17     joerg 	bool	(*pot_extern)(struct _prop_object_externalize_context *,
    209  1.17     joerg 			      void *);
    210  1.17     joerg 	/* func to test quality */
    211  1.27   thorpej 	_prop_object_equals_rv_t
    212  1.27   thorpej 		(*pot_equals)(prop_object_t, prop_object_t,
    213  1.18     joerg 			      void **, void **,
    214  1.18     joerg 			      prop_object_t *, prop_object_t *);
    215  1.18     joerg 	/*
    216  1.18     joerg 	 * func to finish equality iteration.
    217  1.18     joerg 	 *
    218  1.18     joerg 	 * Must be implemented if pot_equals can return
    219  1.18     joerg 	 * _PROP_OBJECT_EQUALS_RECURSE
    220  1.18     joerg 	 */
    221  1.18     joerg 	void	(*pot_equals_finish)(prop_object_t, prop_object_t);
    222  1.28      haad 	void    (*pot_lock)(void);
    223  1.28      haad 	void    (*pot_unlock)(void);
    224   1.2   thorpej };
    225   1.2   thorpej 
    226   1.1   thorpej struct _prop_object {
    227   1.2   thorpej 	const struct _prop_object_type *po_type;/* type descriptor */
    228   1.1   thorpej 	uint32_t	po_refcnt;		/* reference count */
    229   1.1   thorpej };
    230   1.1   thorpej 
    231  1.27   thorpej void		_prop_object_init(struct _prop_object *,
    232  1.27   thorpej 				  const struct _prop_object_type *);
    233  1.27   thorpej void		_prop_object_fini(struct _prop_object *);
    234   1.1   thorpej 
    235   1.1   thorpej struct _prop_object_iterator {
    236   1.1   thorpej 	prop_object_t	(*pi_next_object)(void *);
    237   1.1   thorpej 	void		(*pi_reset)(void *);
    238   1.1   thorpej 	prop_object_t	pi_obj;
    239   1.1   thorpej 	uint32_t	pi_version;
    240   1.1   thorpej };
    241   1.1   thorpej 
    242  1.29     pooka #define _PROP_NOTHREAD_ONCE_DECL(x)	static bool x = false;
    243  1.29     pooka #define _PROP_NOTHREAD_ONCE_RUN(x,f)					\
    244  1.29     pooka 	do {								\
    245  1.29     pooka 		if ((x) == false) {					\
    246  1.29     pooka 			f();						\
    247  1.29     pooka 			x = true;					\
    248  1.29     pooka 		}							\
    249  1.29     pooka 	} while (/*CONSTCOND*/0)
    250  1.29     pooka 
    251   1.1   thorpej #if defined(_KERNEL)
    252   1.1   thorpej 
    253   1.1   thorpej /*
    254   1.1   thorpej  * proplib in the kernel...
    255   1.1   thorpej  */
    256   1.1   thorpej 
    257   1.6   thorpej #include <sys/param.h>
    258   1.1   thorpej #include <sys/malloc.h>
    259   1.1   thorpej #include <sys/pool.h>
    260   1.1   thorpej #include <sys/systm.h>
    261  1.15        ad #include <sys/rwlock.h>
    262  1.29     pooka #include <sys/once.h>
    263   1.1   thorpej 
    264  1.27   thorpej #define	_PROP_ASSERT(x)			KASSERT(x)
    265   1.1   thorpej 
    266  1.27   thorpej #define	_PROP_MALLOC(s, t)		malloc((s), (t), M_WAITOK)
    267  1.27   thorpej #define	_PROP_CALLOC(s, t)		malloc((s), (t), M_WAITOK | M_ZERO)
    268  1.27   thorpej #define	_PROP_REALLOC(v, s, t)		realloc((v), (s), (t), M_WAITOK)
    269  1.27   thorpej #define	_PROP_FREE(v, t)		free((v), (t))
    270   1.1   thorpej 
    271  1.27   thorpej #define	_PROP_POOL_GET(p)		pool_get(&(p), PR_WAITOK)
    272  1.27   thorpej #define	_PROP_POOL_PUT(p, v)		pool_put(&(p), (v))
    273   1.1   thorpej 
    274  1.30     pooka struct prop_pool_init {
    275  1.30     pooka 	struct pool *pp;
    276  1.30     pooka 	size_t size;
    277  1.30     pooka 	const char *wchan;
    278  1.30     pooka };
    279  1.30     pooka #define	_PROP_POOL_INIT(pp, size, wchan)				\
    280  1.30     pooka struct pool pp;								\
    281  1.30     pooka static const struct prop_pool_init _link_ ## pp[1] = {			\
    282  1.30     pooka 	{ &pp, size, wchan }						\
    283  1.30     pooka };									\
    284  1.30     pooka __link_set_add_rodata(prop_linkpools, _link_ ## pp);
    285   1.1   thorpej 
    286   1.1   thorpej #define	_PROP_MALLOC_DEFINE(t, s, l)					\
    287   1.1   thorpej 		MALLOC_DEFINE(t, s, l);
    288   1.1   thorpej 
    289  1.29     pooka #define	_PROP_MUTEX_DECL_STATIC(x)	static kmutex_t x;
    290  1.29     pooka #define	_PROP_MUTEX_INIT(x)		mutex_init(&(x),MUTEX_DEFAULT,IPL_NONE)
    291  1.29     pooka #define	_PROP_MUTEX_LOCK(x)		mutex_enter(&(x))
    292  1.29     pooka #define	_PROP_MUTEX_UNLOCK(x)		mutex_exit(&(x))
    293   1.3   thorpej 
    294  1.27   thorpej #define	_PROP_RWLOCK_DECL(x)		krwlock_t x ;
    295  1.27   thorpej #define	_PROP_RWLOCK_INIT(x)		rw_init(&(x))
    296  1.27   thorpej #define	_PROP_RWLOCK_RDLOCK(x)		rw_enter(&(x), RW_READER)
    297  1.27   thorpej #define	_PROP_RWLOCK_WRLOCK(x)		rw_enter(&(x), RW_WRITER)
    298  1.27   thorpej #define	_PROP_RWLOCK_UNLOCK(x)		rw_exit(&(x))
    299  1.27   thorpej #define	_PROP_RWLOCK_DESTROY(x)		rw_destroy(&(x))
    300  1.22   xtraeme 
    301  1.29     pooka #define _PROP_ONCE_DECL(x)		static ONCE_DECL(x);
    302  1.29     pooka #define _PROP_ONCE_RUN(x,f)		RUN_ONCE(&(x), f)
    303  1.29     pooka 
    304  1.31     pooka #include <sys/atomic.h>
    305  1.31     pooka 
    306  1.34   thorpej #define	_PROP_ATOMIC_LOAD(x)		atomic_load_relaxed(x)
    307  1.31     pooka #define _PROP_ATOMIC_INC32(x)		atomic_inc_32(x)
    308  1.31     pooka #define _PROP_ATOMIC_DEC32(x)		atomic_dec_32(x)
    309  1.31     pooka #define _PROP_ATOMIC_INC32_NV(x, v)	v = atomic_inc_32_nv(x)
    310  1.31     pooka #define _PROP_ATOMIC_DEC32_NV(x, v)	v = atomic_dec_32_nv(x)
    311  1.31     pooka 
    312   1.1   thorpej #elif defined(_STANDALONE)
    313   1.1   thorpej 
    314   1.1   thorpej /*
    315   1.1   thorpej  * proplib in a standalone environment...
    316   1.1   thorpej  */
    317   1.1   thorpej 
    318   1.1   thorpej #include <lib/libsa/stand.h>
    319   1.1   thorpej 
    320   1.1   thorpej void *		_prop_standalone_calloc(size_t);
    321   1.2   thorpej void *		_prop_standalone_realloc(void *, size_t);
    322   1.1   thorpej 
    323  1.27   thorpej #define	_PROP_ASSERT(x)			/* nothing */
    324   1.1   thorpej 
    325  1.27   thorpej #define	_PROP_MALLOC(s, t)		alloc((s))
    326  1.27   thorpej #define	_PROP_CALLOC(s, t)		_prop_standalone_calloc((s))
    327  1.27   thorpej #define	_PROP_REALLOC(v, s, t)		_prop_standalone_realloc((v), (s))
    328  1.27   thorpej #define	_PROP_FREE(v, t)		dealloc((v), 0)		/* XXX */
    329   1.1   thorpej 
    330  1.27   thorpej #define	_PROP_POOL_GET(p)		alloc((p))
    331  1.27   thorpej #define	_PROP_POOL_PUT(p, v)		dealloc((v), (p))
    332   1.1   thorpej 
    333   1.1   thorpej #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    334   1.1   thorpej 
    335   1.1   thorpej #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    336   1.1   thorpej 
    337   1.6   thorpej #define	_PROP_MUTEX_DECL_STATIC(x)	/* nothing */
    338  1.29     pooka #define	_PROP_MUTEX_INIT(x)		/* nothing */
    339   1.6   thorpej #define	_PROP_MUTEX_LOCK(x)		/* nothing */
    340   1.6   thorpej #define	_PROP_MUTEX_UNLOCK(x)		/* nothing */
    341   1.6   thorpej 
    342  1.27   thorpej #define	_PROP_RWLOCK_DECL(x)		/* nothing */
    343  1.27   thorpej #define	_PROP_RWLOCK_INIT(x)		/* nothing */
    344  1.27   thorpej #define	_PROP_RWLOCK_RDLOCK(x)		/* nothing */
    345  1.27   thorpej #define	_PROP_RWLOCK_WRLOCK(x)		/* nothing */
    346  1.27   thorpej #define	_PROP_RWLOCK_UNLOCK(x)		/* nothing */
    347  1.27   thorpej #define	_PROP_RWLOCK_DESTROY(x)		/* nothing */
    348  1.22   xtraeme 
    349  1.29     pooka #define _PROP_ONCE_DECL(x)		_PROP_NOTHREAD_ONCE_DECL(x)
    350  1.29     pooka #define _PROP_ONCE_RUN(x,f)		_PROP_NOTHREAD_ONCE_RUN(x,f)
    351  1.29     pooka 
    352  1.34   thorpej #define	_PROP_ATOMIC_LOAD(x)		*(x)
    353  1.31     pooka #define _PROP_ATOMIC_INC32(x)		++*(x)
    354  1.31     pooka #define _PROP_ATOMIC_DEC32(x)		--*(x)
    355  1.31     pooka #define _PROP_ATOMIC_INC32_NV(x, v)	v = ++*(x)
    356  1.31     pooka #define _PROP_ATOMIC_DEC32_NV(x, v)	v = --*(x)
    357  1.31     pooka 
    358   1.1   thorpej #else
    359   1.1   thorpej 
    360   1.1   thorpej /*
    361   1.1   thorpej  * proplib in user space...
    362   1.1   thorpej  */
    363   1.1   thorpej 
    364   1.1   thorpej #include <assert.h>
    365   1.1   thorpej #include <string.h>
    366   1.1   thorpej #include <stdio.h>
    367   1.1   thorpej #include <stdlib.h>
    368   1.5   thorpej #include <stddef.h>
    369   1.1   thorpej 
    370  1.27   thorpej #define	_PROP_ASSERT(x)			/*LINTED*/assert(x)
    371   1.1   thorpej 
    372  1.27   thorpej #define	_PROP_MALLOC(s, t)		malloc((s))
    373  1.27   thorpej #define	_PROP_CALLOC(s, t)		calloc(1, (s))
    374  1.27   thorpej #define	_PROP_REALLOC(v, s, t)		realloc((v), (s))
    375  1.27   thorpej #define	_PROP_FREE(v, t)		free((v))
    376   1.1   thorpej 
    377  1.27   thorpej #define	_PROP_POOL_GET(p)		malloc((p))
    378  1.27   thorpej #define	_PROP_POOL_PUT(p, v)		free((v))
    379   1.1   thorpej 
    380   1.1   thorpej #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    381   1.1   thorpej 
    382   1.1   thorpej #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    383   1.1   thorpej 
    384   1.3   thorpej #if defined(__NetBSD__) && defined(_LIBPROP)
    385   1.3   thorpej /*
    386   1.3   thorpej  * Use the same mechanism as libc; we get pthread mutexes for threaded
    387   1.3   thorpej  * programs and do-nothing stubs for non-threaded programs.
    388   1.3   thorpej  */
    389  1.31     pooka #include <sys/atomic.h>
    390   1.3   thorpej #include "reentrant.h"
    391  1.29     pooka #define	_PROP_MUTEX_DECL_STATIC(x)	static mutex_t x;
    392  1.29     pooka #define	_PROP_MUTEX_INIT(x)		mutex_init(&(x), NULL)
    393   1.6   thorpej #define	_PROP_MUTEX_LOCK(x)		mutex_lock(&(x))
    394   1.6   thorpej #define	_PROP_MUTEX_UNLOCK(x)		mutex_unlock(&(x))
    395   1.6   thorpej 
    396  1.27   thorpej #define	_PROP_RWLOCK_DECL(x)		rwlock_t x ;
    397  1.27   thorpej #define	_PROP_RWLOCK_INIT(x)		rwlock_init(&(x), NULL)
    398  1.27   thorpej #define	_PROP_RWLOCK_RDLOCK(x)		rwlock_rdlock(&(x))
    399  1.27   thorpej #define	_PROP_RWLOCK_WRLOCK(x)		rwlock_wrlock(&(x))
    400  1.27   thorpej #define	_PROP_RWLOCK_UNLOCK(x)		rwlock_unlock(&(x))
    401  1.27   thorpej #define	_PROP_RWLOCK_DESTROY(x)		rwlock_destroy(&(x))
    402  1.29     pooka 
    403  1.29     pooka #define _PROP_ONCE_DECL(x)						\
    404  1.29     pooka 	static pthread_once_t x = PTHREAD_ONCE_INIT;
    405  1.29     pooka #define _PROP_ONCE_RUN(x,f)		thr_once(&(x), (void(*)(void))f);
    406  1.29     pooka 
    407  1.34   thorpej #define	_PROP_ATOMIC_LOAD(x)		*(x)
    408  1.31     pooka #define _PROP_ATOMIC_INC32(x)		atomic_inc_32(x)
    409  1.31     pooka #define _PROP_ATOMIC_DEC32(x)		atomic_dec_32(x)
    410  1.31     pooka #define _PROP_ATOMIC_INC32_NV(x, v)	v = atomic_inc_32_nv(x)
    411  1.31     pooka #define _PROP_ATOMIC_DEC32_NV(x, v)	v = atomic_dec_32_nv(x)
    412  1.31     pooka 
    413  1.38   thorpej #define	_PROP_EXPORT			__attribute__((visibility("default")))
    414  1.38   thorpej 
    415   1.3   thorpej #elif defined(HAVE_NBTOOL_CONFIG_H)
    416   1.3   thorpej /*
    417   1.3   thorpej  * None of NetBSD's build tools are multi-threaded.
    418   1.3   thorpej  */
    419   1.6   thorpej #define	_PROP_MUTEX_DECL_STATIC(x)	/* nothing */
    420  1.29     pooka #define	_PROP_MUTEX_INIT(x)		/* nothing */
    421   1.6   thorpej #define	_PROP_MUTEX_LOCK(x)		/* nothing */
    422   1.6   thorpej #define	_PROP_MUTEX_UNLOCK(x)		/* nothing */
    423   1.6   thorpej 
    424  1.27   thorpej #define	_PROP_RWLOCK_DECL(x)		/* nothing */
    425  1.27   thorpej #define	_PROP_RWLOCK_INIT(x)		/* nothing */
    426  1.27   thorpej #define	_PROP_RWLOCK_RDLOCK(x)		/* nothing */
    427  1.27   thorpej #define	_PROP_RWLOCK_WRLOCK(x)		/* nothing */
    428  1.27   thorpej #define	_PROP_RWLOCK_UNLOCK(x)		/* nothing */
    429  1.27   thorpej #define	_PROP_RWLOCK_DESTROY(x)		/* nothing */
    430  1.29     pooka 
    431  1.29     pooka #define _PROP_ONCE_DECL(x)		_PROP_NOTHREAD_ONCE_DECL(x)
    432  1.29     pooka #define _PROP_ONCE_RUN(x,f)		_PROP_NOTHREAD_ONCE_RUN(x,f)
    433  1.31     pooka 
    434  1.34   thorpej #define	_PROP_ATOMIC_LOAD(x)		*(x)
    435  1.31     pooka #define _PROP_ATOMIC_INC32(x)		++*(x)
    436  1.31     pooka #define _PROP_ATOMIC_DEC32(x)		--*(x)
    437  1.31     pooka #define _PROP_ATOMIC_INC32_NV(x, v)	v = ++*(x)
    438  1.31     pooka #define _PROP_ATOMIC_DEC32_NV(x, v)	v = --*(x)
    439  1.31     pooka 
    440   1.3   thorpej #else
    441   1.3   thorpej /*
    442   1.3   thorpej  * Use pthread mutexes everywhere else.
    443   1.3   thorpej  */
    444   1.3   thorpej #include <pthread.h>
    445  1.29     pooka #define	_PROP_MUTEX_DECL_STATIC(x)	static pthread_mutex_t x;
    446  1.29     pooka #define	_PROP_MUTEX_INIT(x)		pthread_mutex_init(&(x), NULL)
    447  1.27   thorpej #define	_PROP_MUTEX_LOCK(x)		pthread_mutex_lock(&(x))
    448  1.27   thorpej #define	_PROP_MUTEX_UNLOCK(x)		pthread_mutex_unlock(&(x))
    449   1.6   thorpej 
    450  1.27   thorpej #define	_PROP_RWLOCK_DECL(x)		pthread_rwlock_t x ;
    451  1.27   thorpej #define	_PROP_RWLOCK_INIT(x)		pthread_rwlock_init(&(x), NULL)
    452  1.27   thorpej #define	_PROP_RWLOCK_RDLOCK(x)		pthread_rwlock_rdlock(&(x))
    453  1.27   thorpej #define	_PROP_RWLOCK_WRLOCK(x)		pthread_rwlock_wrlock(&(x))
    454  1.27   thorpej #define	_PROP_RWLOCK_UNLOCK(x)		pthread_rwlock_unlock(&(x))
    455  1.27   thorpej #define	_PROP_RWLOCK_DESTROY(x)		pthread_rwlock_destroy(&(x))
    456  1.29     pooka 
    457  1.29     pooka #define _PROP_ONCE_DECL(x)						\
    458  1.29     pooka 	static pthread_once_t x = PTHREAD_ONCE_INIT;
    459  1.29     pooka #define _PROP_ONCE_RUN(x,f)		pthread_once(&(x),(void(*)(void))f)
    460  1.31     pooka 
    461  1.31     pooka #define _PROP_NEED_REFCNT_MTX
    462  1.31     pooka 
    463  1.34   thorpej #define	_PROP_ATOMIC_LOAD(x)		*(x)
    464  1.34   thorpej 
    465  1.31     pooka #define _PROP_ATOMIC_INC32(x)						\
    466  1.31     pooka do {									\
    467  1.31     pooka 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
    468  1.31     pooka 	(*(x))++;							\
    469  1.31     pooka 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
    470  1.31     pooka } while (/*CONSTCOND*/0)
    471  1.31     pooka 
    472  1.31     pooka #define _PROP_ATOMIC_DEC32(x)						\
    473  1.31     pooka do {									\
    474  1.31     pooka 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
    475  1.31     pooka 	(*(x))--;							\
    476  1.31     pooka 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
    477  1.31     pooka } while (/*CONSTCOND*/0)
    478  1.31     pooka 
    479  1.31     pooka #define _PROP_ATOMIC_INC32_NV(x, v)					\
    480  1.31     pooka do {									\
    481  1.31     pooka 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
    482  1.31     pooka 	v = ++(*(x));							\
    483  1.31     pooka 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
    484  1.31     pooka } while (/*CONSTCOND*/0)
    485  1.31     pooka 
    486  1.31     pooka #define _PROP_ATOMIC_DEC32_NV(x, v)					\
    487  1.31     pooka do {									\
    488  1.31     pooka 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
    489  1.31     pooka 	v = --(*(x));							\
    490  1.31     pooka 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
    491  1.31     pooka } while (/*CONSTCOND*/0)
    492  1.31     pooka 
    493   1.3   thorpej #endif
    494   1.1   thorpej #endif /* _KERNEL */
    495   1.1   thorpej 
    496  1.38   thorpej #ifndef _PROP_EXPORT
    497  1.38   thorpej #define	_PROP_EXPORT			/* nothing */
    498  1.38   thorpej #endif
    499  1.38   thorpej 
    500   1.9   thorpej /*
    501   1.9   thorpej  * Language features.
    502   1.9   thorpej  */
    503   1.9   thorpej #if defined(__NetBSD__)
    504   1.9   thorpej #include <sys/cdefs.h>
    505  1.27   thorpej #define	_PROP_ARG_UNUSED		__unused
    506  1.36   thorpej #if defined(__clang__)
    507  1.36   thorpej #define	_PROP_DEPRECATED(s, m)		/* delete */
    508  1.36   thorpej #else /* ! __clang__ */
    509  1.36   thorpej #define	_PROP_DEPRECATED(s, m)		__warn_references(s, m)
    510  1.36   thorpej #endif /* __clang__ */
    511  1.37   thorpej #define	_PROP_UNCONST(x)		__UNCONST(x)
    512   1.9   thorpej #else
    513  1.27   thorpej #define	_PROP_ARG_UNUSED		/* delete */
    514  1.34   thorpej #define	_PROP_DEPRECATED(s, m)		/* delete */
    515  1.37   thorpej #define	_PROP_UNCONST(x)	((void *)(unsigned long)(const void *)(x))
    516   1.9   thorpej #endif /* __NetBSD__ */
    517   1.9   thorpej 
    518   1.1   thorpej #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
    519