Home | History | Annotate | Line # | Download | only in libprop
prop_object_impl.h revision 1.33
      1  1.33   thorpej /*	$NetBSD: prop_object_impl.h,v 1.33 2019/05/08 02:25:50 thorpej Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4   1.1   thorpej  * Copyright (c) 2006 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.1   thorpej struct _prop_object_externalize_context {
     48   1.1   thorpej 	char *		poec_buf;		/* string buffer */
     49   1.1   thorpej 	size_t		poec_capacity;		/* capacity of buffer */
     50   1.1   thorpej 	size_t		poec_len;		/* current length of string */
     51   1.1   thorpej 	unsigned int	poec_depth;		/* nesting depth */
     52   1.1   thorpej };
     53   1.1   thorpej 
     54  1.27   thorpej bool		_prop_object_externalize_start_tag(
     55   1.1   thorpej 				struct _prop_object_externalize_context *,
     56   1.1   thorpej 				const char *);
     57  1.27   thorpej bool		_prop_object_externalize_end_tag(
     58   1.1   thorpej 				struct _prop_object_externalize_context *,
     59   1.1   thorpej 				const char *);
     60  1.27   thorpej bool		_prop_object_externalize_empty_tag(
     61   1.1   thorpej 				struct _prop_object_externalize_context *,
     62   1.1   thorpej 				const char *);
     63  1.27   thorpej bool		_prop_object_externalize_append_cstring(
     64   1.1   thorpej 				struct _prop_object_externalize_context *,
     65   1.1   thorpej 				const char *);
     66  1.27   thorpej bool		_prop_object_externalize_append_encoded_cstring(
     67   1.1   thorpej 				struct _prop_object_externalize_context *,
     68   1.1   thorpej 				const char *);
     69  1.27   thorpej bool		_prop_object_externalize_append_char(
     70   1.1   thorpej 				struct _prop_object_externalize_context *,
     71   1.1   thorpej 				unsigned char);
     72  1.27   thorpej bool		_prop_object_externalize_header(
     73   1.4   thorpej 				struct _prop_object_externalize_context *);
     74  1.27   thorpej bool		_prop_object_externalize_footer(
     75   1.4   thorpej 				struct _prop_object_externalize_context *);
     76   1.1   thorpej 
     77   1.1   thorpej struct _prop_object_externalize_context *
     78  1.27   thorpej 	_prop_object_externalize_context_alloc(void);
     79  1.27   thorpej void	_prop_object_externalize_context_free(
     80   1.1   thorpej 				struct _prop_object_externalize_context *);
     81   1.1   thorpej 
     82   1.1   thorpej typedef enum {
     83   1.1   thorpej 	_PROP_TAG_TYPE_START,			/* e.g. <dict> */
     84   1.1   thorpej 	_PROP_TAG_TYPE_END,			/* e.g. </dict> */
     85   1.1   thorpej 	_PROP_TAG_TYPE_EITHER
     86   1.1   thorpej } _prop_tag_type_t;
     87   1.1   thorpej 
     88   1.1   thorpej struct _prop_object_internalize_context {
     89   1.1   thorpej 	const char *poic_xml;
     90   1.1   thorpej 	const char *poic_cp;
     91   1.1   thorpej 
     92   1.1   thorpej 	const char *poic_tag_start;
     93   1.1   thorpej 
     94   1.1   thorpej 	const char *poic_tagname;
     95   1.1   thorpej 	size_t      poic_tagname_len;
     96   1.1   thorpej 	const char *poic_tagattr;
     97   1.1   thorpej 	size_t      poic_tagattr_len;
     98   1.1   thorpej 	const char *poic_tagattrval;
     99   1.1   thorpej 	size_t      poic_tagattrval_len;
    100   1.1   thorpej 
    101  1.16   thorpej 	bool   poic_is_empty_element;
    102   1.1   thorpej 	_prop_tag_type_t poic_tag_type;
    103   1.1   thorpej };
    104   1.1   thorpej 
    105  1.27   thorpej typedef enum {
    106  1.17     joerg 	_PROP_OBJECT_FREE_DONE,
    107  1.17     joerg 	_PROP_OBJECT_FREE_RECURSE,
    108  1.17     joerg 	_PROP_OBJECT_FREE_FAILED
    109  1.27   thorpej } _prop_object_free_rv_t;
    110  1.17     joerg 
    111  1.27   thorpej typedef enum {
    112  1.18     joerg 	_PROP_OBJECT_EQUALS_FALSE,
    113  1.18     joerg 	_PROP_OBJECT_EQUALS_TRUE,
    114  1.18     joerg 	_PROP_OBJECT_EQUALS_RECURSE
    115  1.27   thorpej } _prop_object_equals_rv_t;
    116  1.18     joerg 
    117   1.1   thorpej #define	_PROP_EOF(c)		((c) == '\0')
    118   1.1   thorpej #define	_PROP_ISSPACE(c)	\
    119  1.32  christos 	((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
    120   1.1   thorpej 
    121   1.1   thorpej #define	_PROP_TAG_MATCH(ctx, t)					\
    122   1.1   thorpej 	_prop_object_internalize_match((ctx)->poic_tagname,	\
    123   1.1   thorpej 				       (ctx)->poic_tagname_len,	\
    124   1.1   thorpej 				       (t), strlen(t))
    125   1.1   thorpej 
    126   1.1   thorpej #define	_PROP_TAGATTR_MATCH(ctx, a)				\
    127   1.1   thorpej 	_prop_object_internalize_match((ctx)->poic_tagattr,	\
    128   1.1   thorpej 				       (ctx)->poic_tagattr_len,	\
    129   1.1   thorpej 				       (a), strlen(a))
    130   1.1   thorpej 
    131   1.1   thorpej #define	_PROP_TAGATTRVAL_MATCH(ctx, a)				  \
    132   1.1   thorpej 	_prop_object_internalize_match((ctx)->poic_tagattrval,	  \
    133   1.1   thorpej 				       (ctx)->poic_tagattrval_len,\
    134   1.1   thorpej 				       (a), strlen(a))
    135   1.1   thorpej 
    136  1.27   thorpej bool		_prop_object_internalize_find_tag(
    137   1.1   thorpej 				struct _prop_object_internalize_context *,
    138   1.1   thorpej 				const char *, _prop_tag_type_t);
    139  1.27   thorpej bool		_prop_object_internalize_match(const char *, size_t,
    140   1.1   thorpej 					       const char *, size_t);
    141   1.1   thorpej prop_object_t	_prop_object_internalize_by_tag(
    142   1.1   thorpej 				struct _prop_object_internalize_context *);
    143  1.27   thorpej bool		_prop_object_internalize_decode_string(
    144   1.1   thorpej 				struct _prop_object_internalize_context *,
    145   1.1   thorpej 				char *, size_t, size_t *, const char **);
    146  1.13     joerg prop_object_t	_prop_generic_internalize(const char *, const char *);
    147   1.1   thorpej 
    148   1.1   thorpej struct _prop_object_internalize_context *
    149   1.1   thorpej 		_prop_object_internalize_context_alloc(const char *);
    150   1.1   thorpej void		_prop_object_internalize_context_free(
    151   1.1   thorpej 				struct _prop_object_internalize_context *);
    152   1.1   thorpej 
    153   1.4   thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
    154  1.27   thorpej bool		_prop_object_externalize_write_file(const char *,
    155   1.4   thorpej 						    const char *, size_t);
    156   1.4   thorpej 
    157   1.4   thorpej struct _prop_object_internalize_mapped_file {
    158   1.4   thorpej 	char *	poimf_xml;
    159   1.4   thorpej 	size_t	poimf_mapsize;
    160   1.4   thorpej };
    161   1.4   thorpej 
    162   1.4   thorpej struct _prop_object_internalize_mapped_file *
    163   1.4   thorpej 		_prop_object_internalize_map_file(const char *);
    164   1.4   thorpej void		_prop_object_internalize_unmap_file(
    165   1.4   thorpej 				struct _prop_object_internalize_mapped_file *);
    166   1.4   thorpej #endif /* !_KERNEL && !_STANDALONE */
    167   1.4   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.2   thorpej struct _prop_object_type {
    190  1.17     joerg 	/* type indicator */
    191  1.17     joerg 	uint32_t	pot_type;
    192  1.17     joerg 	/* func to free object */
    193  1.27   thorpej 	_prop_object_free_rv_t
    194  1.27   thorpej 			(*pot_free)(prop_stack_t, prop_object_t *);
    195  1.17     joerg 	/*
    196  1.17     joerg 	 * func to free the child returned by pot_free with stack == NULL.
    197  1.17     joerg 	 *
    198  1.18     joerg 	 * Must be implemented if pot_free can return anything other than
    199  1.18     joerg 	 * _PROP_OBJECT_FREE_DONE.
    200  1.17     joerg 	 */
    201  1.17     joerg 	void	(*pot_emergency_free)(prop_object_t);
    202  1.17     joerg 	/* func to externalize object */
    203  1.17     joerg 	bool	(*pot_extern)(struct _prop_object_externalize_context *,
    204  1.17     joerg 			      void *);
    205  1.17     joerg 	/* func to test quality */
    206  1.27   thorpej 	_prop_object_equals_rv_t
    207  1.27   thorpej 		(*pot_equals)(prop_object_t, prop_object_t,
    208  1.18     joerg 			      void **, void **,
    209  1.18     joerg 			      prop_object_t *, prop_object_t *);
    210  1.18     joerg 	/*
    211  1.18     joerg 	 * func to finish equality iteration.
    212  1.18     joerg 	 *
    213  1.18     joerg 	 * Must be implemented if pot_equals can return
    214  1.18     joerg 	 * _PROP_OBJECT_EQUALS_RECURSE
    215  1.18     joerg 	 */
    216  1.18     joerg 	void	(*pot_equals_finish)(prop_object_t, prop_object_t);
    217  1.28      haad 	void    (*pot_lock)(void);
    218  1.28      haad 	void    (*pot_unlock)(void);
    219   1.2   thorpej };
    220   1.2   thorpej 
    221   1.1   thorpej struct _prop_object {
    222   1.2   thorpej 	const struct _prop_object_type *po_type;/* type descriptor */
    223   1.1   thorpej 	uint32_t	po_refcnt;		/* reference count */
    224   1.1   thorpej };
    225   1.1   thorpej 
    226  1.27   thorpej void		_prop_object_init(struct _prop_object *,
    227  1.27   thorpej 				  const struct _prop_object_type *);
    228  1.27   thorpej void		_prop_object_fini(struct _prop_object *);
    229   1.1   thorpej 
    230   1.1   thorpej struct _prop_object_iterator {
    231   1.1   thorpej 	prop_object_t	(*pi_next_object)(void *);
    232   1.1   thorpej 	void		(*pi_reset)(void *);
    233   1.1   thorpej 	prop_object_t	pi_obj;
    234   1.1   thorpej 	uint32_t	pi_version;
    235   1.1   thorpej };
    236   1.1   thorpej 
    237  1.29     pooka #define _PROP_NOTHREAD_ONCE_DECL(x)	static bool x = false;
    238  1.29     pooka #define _PROP_NOTHREAD_ONCE_RUN(x,f)					\
    239  1.29     pooka 	do {								\
    240  1.29     pooka 		if ((x) == false) {					\
    241  1.29     pooka 			f();						\
    242  1.29     pooka 			x = true;					\
    243  1.29     pooka 		}							\
    244  1.29     pooka 	} while (/*CONSTCOND*/0)
    245  1.29     pooka 
    246   1.1   thorpej #if defined(_KERNEL)
    247   1.1   thorpej 
    248   1.1   thorpej /*
    249   1.1   thorpej  * proplib in the kernel...
    250   1.1   thorpej  */
    251   1.1   thorpej 
    252   1.6   thorpej #include <sys/param.h>
    253   1.1   thorpej #include <sys/malloc.h>
    254   1.1   thorpej #include <sys/pool.h>
    255   1.1   thorpej #include <sys/systm.h>
    256  1.15        ad #include <sys/rwlock.h>
    257  1.29     pooka #include <sys/once.h>
    258   1.1   thorpej 
    259  1.27   thorpej #define	_PROP_ASSERT(x)			KASSERT(x)
    260   1.1   thorpej 
    261  1.27   thorpej #define	_PROP_MALLOC(s, t)		malloc((s), (t), M_WAITOK)
    262  1.27   thorpej #define	_PROP_CALLOC(s, t)		malloc((s), (t), M_WAITOK | M_ZERO)
    263  1.27   thorpej #define	_PROP_REALLOC(v, s, t)		realloc((v), (s), (t), M_WAITOK)
    264  1.27   thorpej #define	_PROP_FREE(v, t)		free((v), (t))
    265   1.1   thorpej 
    266  1.27   thorpej #define	_PROP_POOL_GET(p)		pool_get(&(p), PR_WAITOK)
    267  1.27   thorpej #define	_PROP_POOL_PUT(p, v)		pool_put(&(p), (v))
    268   1.1   thorpej 
    269  1.30     pooka struct prop_pool_init {
    270  1.30     pooka 	struct pool *pp;
    271  1.30     pooka 	size_t size;
    272  1.30     pooka 	const char *wchan;
    273  1.30     pooka };
    274  1.30     pooka #define	_PROP_POOL_INIT(pp, size, wchan)				\
    275  1.30     pooka struct pool pp;								\
    276  1.30     pooka static const struct prop_pool_init _link_ ## pp[1] = {			\
    277  1.30     pooka 	{ &pp, size, wchan }						\
    278  1.30     pooka };									\
    279  1.30     pooka __link_set_add_rodata(prop_linkpools, _link_ ## pp);
    280   1.1   thorpej 
    281   1.1   thorpej #define	_PROP_MALLOC_DEFINE(t, s, l)					\
    282   1.1   thorpej 		MALLOC_DEFINE(t, s, l);
    283   1.1   thorpej 
    284  1.29     pooka #define	_PROP_MUTEX_DECL_STATIC(x)	static kmutex_t x;
    285  1.29     pooka #define	_PROP_MUTEX_INIT(x)		mutex_init(&(x),MUTEX_DEFAULT,IPL_NONE)
    286  1.29     pooka #define	_PROP_MUTEX_LOCK(x)		mutex_enter(&(x))
    287  1.29     pooka #define	_PROP_MUTEX_UNLOCK(x)		mutex_exit(&(x))
    288   1.3   thorpej 
    289  1.27   thorpej #define	_PROP_RWLOCK_DECL(x)		krwlock_t x ;
    290  1.27   thorpej #define	_PROP_RWLOCK_INIT(x)		rw_init(&(x))
    291  1.27   thorpej #define	_PROP_RWLOCK_RDLOCK(x)		rw_enter(&(x), RW_READER)
    292  1.27   thorpej #define	_PROP_RWLOCK_WRLOCK(x)		rw_enter(&(x), RW_WRITER)
    293  1.27   thorpej #define	_PROP_RWLOCK_UNLOCK(x)		rw_exit(&(x))
    294  1.27   thorpej #define	_PROP_RWLOCK_DESTROY(x)		rw_destroy(&(x))
    295  1.22   xtraeme 
    296  1.29     pooka #define _PROP_ONCE_DECL(x)		static ONCE_DECL(x);
    297  1.29     pooka #define _PROP_ONCE_RUN(x,f)		RUN_ONCE(&(x), f)
    298  1.29     pooka 
    299  1.31     pooka #include <sys/atomic.h>
    300  1.31     pooka 
    301  1.31     pooka #define _PROP_ATOMIC_INC32(x)		atomic_inc_32(x)
    302  1.31     pooka #define _PROP_ATOMIC_DEC32(x)		atomic_dec_32(x)
    303  1.31     pooka #define _PROP_ATOMIC_INC32_NV(x, v)	v = atomic_inc_32_nv(x)
    304  1.31     pooka #define _PROP_ATOMIC_DEC32_NV(x, v)	v = atomic_dec_32_nv(x)
    305  1.31     pooka 
    306   1.1   thorpej #elif defined(_STANDALONE)
    307   1.1   thorpej 
    308   1.1   thorpej /*
    309   1.1   thorpej  * proplib in a standalone environment...
    310   1.1   thorpej  */
    311   1.1   thorpej 
    312   1.1   thorpej #include <lib/libsa/stand.h>
    313   1.1   thorpej 
    314   1.1   thorpej void *		_prop_standalone_calloc(size_t);
    315   1.2   thorpej void *		_prop_standalone_realloc(void *, size_t);
    316   1.1   thorpej 
    317  1.27   thorpej #define	_PROP_ASSERT(x)			/* nothing */
    318   1.1   thorpej 
    319  1.27   thorpej #define	_PROP_MALLOC(s, t)		alloc((s))
    320  1.27   thorpej #define	_PROP_CALLOC(s, t)		_prop_standalone_calloc((s))
    321  1.27   thorpej #define	_PROP_REALLOC(v, s, t)		_prop_standalone_realloc((v), (s))
    322  1.27   thorpej #define	_PROP_FREE(v, t)		dealloc((v), 0)		/* XXX */
    323   1.1   thorpej 
    324  1.27   thorpej #define	_PROP_POOL_GET(p)		alloc((p))
    325  1.27   thorpej #define	_PROP_POOL_PUT(p, v)		dealloc((v), (p))
    326   1.1   thorpej 
    327   1.1   thorpej #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    328   1.1   thorpej 
    329   1.1   thorpej #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    330   1.1   thorpej 
    331   1.6   thorpej #define	_PROP_MUTEX_DECL_STATIC(x)	/* nothing */
    332  1.29     pooka #define	_PROP_MUTEX_INIT(x)		/* nothing */
    333   1.6   thorpej #define	_PROP_MUTEX_LOCK(x)		/* nothing */
    334   1.6   thorpej #define	_PROP_MUTEX_UNLOCK(x)		/* nothing */
    335   1.6   thorpej 
    336  1.27   thorpej #define	_PROP_RWLOCK_DECL(x)		/* nothing */
    337  1.27   thorpej #define	_PROP_RWLOCK_INIT(x)		/* nothing */
    338  1.27   thorpej #define	_PROP_RWLOCK_RDLOCK(x)		/* nothing */
    339  1.27   thorpej #define	_PROP_RWLOCK_WRLOCK(x)		/* nothing */
    340  1.27   thorpej #define	_PROP_RWLOCK_UNLOCK(x)		/* nothing */
    341  1.27   thorpej #define	_PROP_RWLOCK_DESTROY(x)		/* nothing */
    342  1.22   xtraeme 
    343  1.29     pooka #define _PROP_ONCE_DECL(x)		_PROP_NOTHREAD_ONCE_DECL(x)
    344  1.29     pooka #define _PROP_ONCE_RUN(x,f)		_PROP_NOTHREAD_ONCE_RUN(x,f)
    345  1.29     pooka 
    346  1.31     pooka #define _PROP_ATOMIC_INC32(x)		++*(x)
    347  1.31     pooka #define _PROP_ATOMIC_DEC32(x)		--*(x)
    348  1.31     pooka #define _PROP_ATOMIC_INC32_NV(x, v)	v = ++*(x)
    349  1.31     pooka #define _PROP_ATOMIC_DEC32_NV(x, v)	v = --*(x)
    350  1.31     pooka 
    351   1.1   thorpej #else
    352   1.1   thorpej 
    353   1.1   thorpej /*
    354   1.1   thorpej  * proplib in user space...
    355   1.1   thorpej  */
    356   1.1   thorpej 
    357   1.1   thorpej #include <assert.h>
    358   1.1   thorpej #include <string.h>
    359   1.1   thorpej #include <stdio.h>
    360   1.1   thorpej #include <stdlib.h>
    361   1.5   thorpej #include <stddef.h>
    362   1.1   thorpej 
    363  1.27   thorpej #define	_PROP_ASSERT(x)			/*LINTED*/assert(x)
    364   1.1   thorpej 
    365  1.27   thorpej #define	_PROP_MALLOC(s, t)		malloc((s))
    366  1.27   thorpej #define	_PROP_CALLOC(s, t)		calloc(1, (s))
    367  1.27   thorpej #define	_PROP_REALLOC(v, s, t)		realloc((v), (s))
    368  1.27   thorpej #define	_PROP_FREE(v, t)		free((v))
    369   1.1   thorpej 
    370  1.27   thorpej #define	_PROP_POOL_GET(p)		malloc((p))
    371  1.27   thorpej #define	_PROP_POOL_PUT(p, v)		free((v))
    372   1.1   thorpej 
    373   1.1   thorpej #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    374   1.1   thorpej 
    375   1.1   thorpej #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    376   1.1   thorpej 
    377   1.3   thorpej #if defined(__NetBSD__) && defined(_LIBPROP)
    378   1.3   thorpej /*
    379   1.3   thorpej  * Use the same mechanism as libc; we get pthread mutexes for threaded
    380   1.3   thorpej  * programs and do-nothing stubs for non-threaded programs.
    381   1.3   thorpej  */
    382  1.31     pooka #include <sys/atomic.h>
    383   1.3   thorpej #include "reentrant.h"
    384  1.29     pooka #define	_PROP_MUTEX_DECL_STATIC(x)	static mutex_t x;
    385  1.29     pooka #define	_PROP_MUTEX_INIT(x)		mutex_init(&(x), NULL)
    386   1.6   thorpej #define	_PROP_MUTEX_LOCK(x)		mutex_lock(&(x))
    387   1.6   thorpej #define	_PROP_MUTEX_UNLOCK(x)		mutex_unlock(&(x))
    388   1.6   thorpej 
    389  1.27   thorpej #define	_PROP_RWLOCK_DECL(x)		rwlock_t x ;
    390  1.27   thorpej #define	_PROP_RWLOCK_INIT(x)		rwlock_init(&(x), NULL)
    391  1.27   thorpej #define	_PROP_RWLOCK_RDLOCK(x)		rwlock_rdlock(&(x))
    392  1.27   thorpej #define	_PROP_RWLOCK_WRLOCK(x)		rwlock_wrlock(&(x))
    393  1.27   thorpej #define	_PROP_RWLOCK_UNLOCK(x)		rwlock_unlock(&(x))
    394  1.27   thorpej #define	_PROP_RWLOCK_DESTROY(x)		rwlock_destroy(&(x))
    395  1.29     pooka 
    396  1.29     pooka #define _PROP_ONCE_DECL(x)						\
    397  1.29     pooka 	static pthread_once_t x = PTHREAD_ONCE_INIT;
    398  1.29     pooka #define _PROP_ONCE_RUN(x,f)		thr_once(&(x), (void(*)(void))f);
    399  1.29     pooka 
    400  1.31     pooka #define _PROP_ATOMIC_INC32(x)		atomic_inc_32(x)
    401  1.31     pooka #define _PROP_ATOMIC_DEC32(x)		atomic_dec_32(x)
    402  1.31     pooka #define _PROP_ATOMIC_INC32_NV(x, v)	v = atomic_inc_32_nv(x)
    403  1.31     pooka #define _PROP_ATOMIC_DEC32_NV(x, v)	v = atomic_dec_32_nv(x)
    404  1.31     pooka 
    405   1.3   thorpej #elif defined(HAVE_NBTOOL_CONFIG_H)
    406   1.3   thorpej /*
    407   1.3   thorpej  * None of NetBSD's build tools are multi-threaded.
    408   1.3   thorpej  */
    409   1.6   thorpej #define	_PROP_MUTEX_DECL_STATIC(x)	/* nothing */
    410  1.29     pooka #define	_PROP_MUTEX_INIT(x)		/* nothing */
    411   1.6   thorpej #define	_PROP_MUTEX_LOCK(x)		/* nothing */
    412   1.6   thorpej #define	_PROP_MUTEX_UNLOCK(x)		/* nothing */
    413   1.6   thorpej 
    414  1.27   thorpej #define	_PROP_RWLOCK_DECL(x)		/* nothing */
    415  1.27   thorpej #define	_PROP_RWLOCK_INIT(x)		/* nothing */
    416  1.27   thorpej #define	_PROP_RWLOCK_RDLOCK(x)		/* nothing */
    417  1.27   thorpej #define	_PROP_RWLOCK_WRLOCK(x)		/* nothing */
    418  1.27   thorpej #define	_PROP_RWLOCK_UNLOCK(x)		/* nothing */
    419  1.27   thorpej #define	_PROP_RWLOCK_DESTROY(x)		/* nothing */
    420  1.29     pooka 
    421  1.29     pooka #define _PROP_ONCE_DECL(x)		_PROP_NOTHREAD_ONCE_DECL(x)
    422  1.29     pooka #define _PROP_ONCE_RUN(x,f)		_PROP_NOTHREAD_ONCE_RUN(x,f)
    423  1.31     pooka 
    424  1.31     pooka #define _PROP_ATOMIC_INC32(x)		++*(x)
    425  1.31     pooka #define _PROP_ATOMIC_DEC32(x)		--*(x)
    426  1.31     pooka #define _PROP_ATOMIC_INC32_NV(x, v)	v = ++*(x)
    427  1.31     pooka #define _PROP_ATOMIC_DEC32_NV(x, v)	v = --*(x)
    428  1.31     pooka 
    429   1.3   thorpej #else
    430   1.3   thorpej /*
    431   1.3   thorpej  * Use pthread mutexes everywhere else.
    432   1.3   thorpej  */
    433   1.3   thorpej #include <pthread.h>
    434  1.29     pooka #define	_PROP_MUTEX_DECL_STATIC(x)	static pthread_mutex_t x;
    435  1.29     pooka #define	_PROP_MUTEX_INIT(x)		pthread_mutex_init(&(x), NULL)
    436  1.27   thorpej #define	_PROP_MUTEX_LOCK(x)		pthread_mutex_lock(&(x))
    437  1.27   thorpej #define	_PROP_MUTEX_UNLOCK(x)		pthread_mutex_unlock(&(x))
    438   1.6   thorpej 
    439  1.27   thorpej #define	_PROP_RWLOCK_DECL(x)		pthread_rwlock_t x ;
    440  1.27   thorpej #define	_PROP_RWLOCK_INIT(x)		pthread_rwlock_init(&(x), NULL)
    441  1.27   thorpej #define	_PROP_RWLOCK_RDLOCK(x)		pthread_rwlock_rdlock(&(x))
    442  1.27   thorpej #define	_PROP_RWLOCK_WRLOCK(x)		pthread_rwlock_wrlock(&(x))
    443  1.27   thorpej #define	_PROP_RWLOCK_UNLOCK(x)		pthread_rwlock_unlock(&(x))
    444  1.27   thorpej #define	_PROP_RWLOCK_DESTROY(x)		pthread_rwlock_destroy(&(x))
    445  1.29     pooka 
    446  1.29     pooka #define _PROP_ONCE_DECL(x)						\
    447  1.29     pooka 	static pthread_once_t x = PTHREAD_ONCE_INIT;
    448  1.29     pooka #define _PROP_ONCE_RUN(x,f)		pthread_once(&(x),(void(*)(void))f)
    449  1.31     pooka 
    450  1.31     pooka #define _PROP_NEED_REFCNT_MTX
    451  1.31     pooka 
    452  1.31     pooka #define _PROP_ATOMIC_INC32(x)						\
    453  1.31     pooka do {									\
    454  1.31     pooka 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
    455  1.31     pooka 	(*(x))++;							\
    456  1.31     pooka 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
    457  1.31     pooka } while (/*CONSTCOND*/0)
    458  1.31     pooka 
    459  1.31     pooka #define _PROP_ATOMIC_DEC32(x)						\
    460  1.31     pooka do {									\
    461  1.31     pooka 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
    462  1.31     pooka 	(*(x))--;							\
    463  1.31     pooka 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
    464  1.31     pooka } while (/*CONSTCOND*/0)
    465  1.31     pooka 
    466  1.31     pooka #define _PROP_ATOMIC_INC32_NV(x, v)					\
    467  1.31     pooka do {									\
    468  1.31     pooka 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
    469  1.31     pooka 	v = ++(*(x));							\
    470  1.31     pooka 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
    471  1.31     pooka } while (/*CONSTCOND*/0)
    472  1.31     pooka 
    473  1.31     pooka #define _PROP_ATOMIC_DEC32_NV(x, v)					\
    474  1.31     pooka do {									\
    475  1.31     pooka 	pthread_mutex_lock(&_prop_refcnt_mtx);				\
    476  1.31     pooka 	v = --(*(x));							\
    477  1.31     pooka 	pthread_mutex_unlock(&_prop_refcnt_mtx);			\
    478  1.31     pooka } while (/*CONSTCOND*/0)
    479  1.31     pooka 
    480   1.3   thorpej #endif
    481   1.1   thorpej #endif /* _KERNEL */
    482   1.1   thorpej 
    483   1.9   thorpej /*
    484   1.9   thorpej  * Language features.
    485   1.9   thorpej  */
    486   1.9   thorpej #if defined(__NetBSD__)
    487   1.9   thorpej #include <sys/cdefs.h>
    488  1.27   thorpej #define	_PROP_ARG_UNUSED		__unused
    489   1.9   thorpej #else
    490  1.27   thorpej #define	_PROP_ARG_UNUSED		/* delete */
    491   1.9   thorpej #endif /* __NetBSD__ */
    492   1.9   thorpej 
    493   1.1   thorpej #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
    494