Home | History | Annotate | Line # | Download | only in libprop
prop_number.c revision 1.29
      1  1.29  pgoyette /*	$NetBSD: prop_number.c,v 1.29 2016/06/28 05:21:15 pgoyette 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.28  pgoyette #include <sys/rbtree.h>
     33   1.1   thorpej #include <prop/prop_number.h>
     34   1.1   thorpej #include "prop_object_impl.h"
     35   1.1   thorpej 
     36   1.1   thorpej #if defined(_KERNEL)
     37   1.1   thorpej #include <sys/systm.h>
     38   1.1   thorpej #elif defined(_STANDALONE)
     39   1.1   thorpej #include <sys/param.h>
     40   1.1   thorpej #include <lib/libkern/libkern.h>
     41   1.1   thorpej #else
     42   1.1   thorpej #include <errno.h>
     43   1.1   thorpej #include <stdlib.h>
     44   1.1   thorpej #endif
     45   1.1   thorpej 
     46  1.27      matt struct _prop_number_value {
     47  1.27      matt 	union {
     48  1.27      matt 		int64_t  pnu_signed;
     49  1.27      matt 		uint64_t pnu_unsigned;
     50  1.27      matt 	} pnv_un;
     51  1.27      matt #define	pnv_signed	pnv_un.pnu_signed
     52  1.27      matt #define	pnv_unsigned	pnv_un.pnu_unsigned
     53  1.27      matt 	unsigned int	pnv_is_unsigned	:1,
     54  1.27      matt 					:31;
     55  1.27      matt };
     56  1.27      matt 
     57   1.1   thorpej struct _prop_number {
     58   1.1   thorpej 	struct _prop_object	pn_obj;
     59   1.6   thorpej 	struct rb_node		pn_link;
     60  1.27      matt 	struct _prop_number_value pn_value;
     61   1.1   thorpej };
     62   1.1   thorpej 
     63   1.1   thorpej _PROP_POOL_INIT(_prop_number_pool, sizeof(struct _prop_number), "propnmbr")
     64   1.1   thorpej 
     65  1.19   thorpej static _prop_object_free_rv_t
     66  1.19   thorpej 		_prop_number_free(prop_stack_t, prop_object_t *);
     67  1.12   thorpej static bool	_prop_number_externalize(
     68   1.2   thorpej 				struct _prop_object_externalize_context *,
     69   1.2   thorpej 				void *);
     70  1.19   thorpej static _prop_object_equals_rv_t
     71  1.19   thorpej 		_prop_number_equals(prop_object_t, prop_object_t,
     72  1.14     joerg 				    void **, void **,
     73  1.14     joerg 				    prop_object_t *, prop_object_t *);
     74   1.2   thorpej 
     75  1.20      haad static void _prop_number_lock(void);
     76  1.20      haad static void _prop_number_unlock(void);
     77  1.20      haad 
     78   1.2   thorpej static const struct _prop_object_type _prop_object_type_number = {
     79   1.2   thorpej 	.pot_type	=	PROP_TYPE_NUMBER,
     80   1.2   thorpej 	.pot_free	=	_prop_number_free,
     81   1.2   thorpej 	.pot_extern	=	_prop_number_externalize,
     82   1.2   thorpej 	.pot_equals	=	_prop_number_equals,
     83  1.20      haad 	.pot_lock       =       _prop_number_lock,
     84  1.20      haad 	.pot_unlock     =    	_prop_number_unlock,
     85   1.2   thorpej };
     86   1.2   thorpej 
     87   1.2   thorpej #define	prop_object_is_number(x)	\
     88   1.5   thorpej 	((x) != NULL && (x)->pn_obj.po_type == &_prop_object_type_number)
     89   1.1   thorpej 
     90   1.6   thorpej /*
     91   1.6   thorpej  * Number objects are immutable, and we are likely to have many number
     92   1.6   thorpej  * objects that have the same value.  So, to save memory, we unique'ify
     93   1.6   thorpej  * numbers so we only have one copy of each.
     94   1.6   thorpej  */
     95   1.6   thorpej 
     96   1.6   thorpej static int
     97   1.8   thorpej _prop_number_compare_values(const struct _prop_number_value *pnv1,
     98   1.8   thorpej 			    const struct _prop_number_value *pnv2)
     99   1.8   thorpej {
    100   1.8   thorpej 
    101   1.8   thorpej 	/* Signed numbers are sorted before unsigned numbers. */
    102   1.8   thorpej 
    103   1.8   thorpej 	if (pnv1->pnv_is_unsigned) {
    104   1.8   thorpej 		if (! pnv2->pnv_is_unsigned)
    105   1.8   thorpej 			return (1);
    106   1.8   thorpej 		if (pnv1->pnv_unsigned < pnv2->pnv_unsigned)
    107   1.8   thorpej 			return (-1);
    108   1.8   thorpej 		if (pnv1->pnv_unsigned > pnv2->pnv_unsigned)
    109   1.8   thorpej 			return (1);
    110   1.8   thorpej 		return (0);
    111   1.8   thorpej 	}
    112   1.8   thorpej 
    113   1.8   thorpej 	if (pnv2->pnv_is_unsigned)
    114   1.8   thorpej 		return (-1);
    115   1.8   thorpej 	if (pnv1->pnv_signed < pnv2->pnv_signed)
    116   1.8   thorpej 		return (-1);
    117   1.8   thorpej 	if (pnv1->pnv_signed > pnv2->pnv_signed)
    118   1.8   thorpej 		return (1);
    119   1.8   thorpej 	return (0);
    120   1.8   thorpej }
    121   1.8   thorpej 
    122   1.8   thorpej static int
    123  1.23     rmind /*ARGSUSED*/
    124  1.24     pooka _prop_number_rb_compare_nodes(void *ctx _PROP_ARG_UNUSED,
    125  1.23     rmind 			      const void *n1, const void *n2)
    126   1.6   thorpej {
    127  1.23     rmind 	const struct _prop_number *pn1 = n1;
    128  1.23     rmind 	const struct _prop_number *pn2 = n2;
    129   1.6   thorpej 
    130  1.23     rmind 	return _prop_number_compare_values(&pn1->pn_value, &pn2->pn_value);
    131   1.6   thorpej }
    132   1.6   thorpej 
    133   1.6   thorpej static int
    134  1.23     rmind /*ARGSUSED*/
    135  1.24     pooka _prop_number_rb_compare_key(void *ctx _PROP_ARG_UNUSED,
    136  1.24     pooka 			    const void *n, const void *v)
    137   1.6   thorpej {
    138  1.23     rmind 	const struct _prop_number *pn = n;
    139   1.8   thorpej 	const struct _prop_number_value *pnv = v;
    140   1.6   thorpej 
    141  1.23     rmind 	return _prop_number_compare_values(&pn->pn_value, pnv);
    142   1.6   thorpej }
    143   1.6   thorpej 
    144  1.23     rmind static const rb_tree_ops_t _prop_number_rb_tree_ops = {
    145   1.6   thorpej 	.rbto_compare_nodes = _prop_number_rb_compare_nodes,
    146  1.23     rmind 	.rbto_compare_key = _prop_number_rb_compare_key,
    147  1.23     rmind 	.rbto_node_offset = offsetof(struct _prop_number, pn_link),
    148  1.23     rmind 	.rbto_context = NULL
    149   1.6   thorpej };
    150   1.6   thorpej 
    151   1.6   thorpej static struct rb_tree _prop_number_tree;
    152   1.7   thorpej _PROP_MUTEX_DECL_STATIC(_prop_number_tree_mutex)
    153   1.6   thorpej 
    154  1.13     joerg /* ARGSUSED */
    155  1.19   thorpej static _prop_object_free_rv_t
    156  1.13     joerg _prop_number_free(prop_stack_t stack, prop_object_t *obj)
    157   1.1   thorpej {
    158  1.13     joerg 	prop_number_t pn = *obj;
    159   1.6   thorpej 
    160  1.28  pgoyette 	rb_tree_remove_node(&_prop_number_tree, pn);
    161   1.1   thorpej 
    162   1.6   thorpej 	_PROP_POOL_PUT(_prop_number_pool, pn);
    163  1.13     joerg 
    164  1.13     joerg 	return (_PROP_OBJECT_FREE_DONE);
    165   1.1   thorpej }
    166   1.1   thorpej 
    167  1.21     pooka _PROP_ONCE_DECL(_prop_number_init_once)
    168  1.21     pooka 
    169  1.21     pooka static int
    170  1.21     pooka _prop_number_init(void)
    171  1.21     pooka {
    172  1.21     pooka 
    173  1.21     pooka 	_PROP_MUTEX_INIT(_prop_number_tree_mutex);
    174  1.28  pgoyette 	rb_tree_init(&_prop_number_tree, &_prop_number_rb_tree_ops);
    175  1.21     pooka 	return 0;
    176  1.21     pooka }
    177  1.21     pooka 
    178  1.20      haad static void
    179  1.22    cegger _prop_number_lock(void)
    180  1.20      haad {
    181  1.21     pooka 	/* XXX: init necessary? */
    182  1.21     pooka 	_PROP_ONCE_RUN(_prop_number_init_once, _prop_number_init);
    183  1.20      haad 	_PROP_MUTEX_LOCK(_prop_number_tree_mutex);
    184  1.20      haad }
    185  1.20      haad 
    186  1.20      haad static void
    187  1.22    cegger _prop_number_unlock(void)
    188  1.20      haad {
    189  1.20      haad 	_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
    190  1.20      haad }
    191  1.20      haad 
    192  1.12   thorpej static bool
    193   1.1   thorpej _prop_number_externalize(struct _prop_object_externalize_context *ctx,
    194   1.1   thorpej 			 void *v)
    195   1.1   thorpej {
    196   1.1   thorpej 	prop_number_t pn = v;
    197   1.1   thorpej 	char tmpstr[32];
    198   1.1   thorpej 
    199   1.8   thorpej 	/*
    200   1.8   thorpej 	 * For unsigned numbers, we output in hex.  For signed numbers,
    201   1.8   thorpej 	 * we output in decimal.
    202   1.8   thorpej 	 */
    203   1.8   thorpej 	if (pn->pn_value.pnv_is_unsigned)
    204  1.26  christos 		snprintf(tmpstr, sizeof(tmpstr), "0x%" PRIx64,
    205  1.26  christos 		    pn->pn_value.pnv_unsigned);
    206   1.8   thorpej 	else
    207  1.26  christos 		snprintf(tmpstr, sizeof(tmpstr), "%" PRIi64,
    208  1.26  christos 		    pn->pn_value.pnv_signed);
    209   1.1   thorpej 
    210  1.12   thorpej 	if (_prop_object_externalize_start_tag(ctx, "integer") == false ||
    211  1.12   thorpej 	    _prop_object_externalize_append_cstring(ctx, tmpstr) == false ||
    212  1.12   thorpej 	    _prop_object_externalize_end_tag(ctx, "integer") == false)
    213  1.12   thorpej 		return (false);
    214   1.1   thorpej 
    215  1.12   thorpej 	return (true);
    216   1.1   thorpej }
    217   1.1   thorpej 
    218  1.14     joerg /* ARGSUSED */
    219  1.19   thorpej static _prop_object_equals_rv_t
    220  1.14     joerg _prop_number_equals(prop_object_t v1, prop_object_t v2,
    221  1.14     joerg     void **stored_pointer1, void **stored_pointer2,
    222  1.14     joerg     prop_object_t *next_obj1, prop_object_t *next_obj2)
    223   1.2   thorpej {
    224   1.2   thorpej 	prop_number_t num1 = v1;
    225   1.2   thorpej 	prop_number_t num2 = v2;
    226   1.2   thorpej 
    227   1.6   thorpej 	/*
    228   1.6   thorpej 	 * There is only ever one copy of a number object at any given
    229   1.8   thorpej 	 * time, so we can reduce this to a simple pointer equality check
    230   1.8   thorpej 	 * in the common case.
    231   1.8   thorpej 	 */
    232   1.8   thorpej 	if (num1 == num2)
    233  1.14     joerg 		return (_PROP_OBJECT_EQUALS_TRUE);
    234   1.8   thorpej 
    235   1.8   thorpej 	/*
    236   1.8   thorpej 	 * If the numbers are the same signed-ness, then we know they
    237   1.8   thorpej 	 * cannot be equal because they would have had pointer equality.
    238   1.6   thorpej 	 */
    239   1.8   thorpej 	if (num1->pn_value.pnv_is_unsigned == num2->pn_value.pnv_is_unsigned)
    240  1.19   thorpej 		return (_PROP_OBJECT_EQUALS_FALSE);
    241   1.8   thorpej 
    242   1.8   thorpej 	/*
    243   1.8   thorpej 	 * We now have one signed value and one unsigned value.  We can
    244   1.8   thorpej 	 * compare them iff:
    245   1.8   thorpej 	 *	- The unsigned value is not larger than the signed value
    246   1.8   thorpej 	 *	  can represent.
    247   1.8   thorpej 	 *	- The signed value is not smaller than the unsigned value
    248   1.8   thorpej 	 *	  can represent.
    249   1.8   thorpej 	 */
    250   1.8   thorpej 	if (num1->pn_value.pnv_is_unsigned) {
    251   1.8   thorpej 		/*
    252   1.8   thorpej 		 * num1 is unsigned and num2 is signed.
    253   1.8   thorpej 		 */
    254   1.8   thorpej 		if (num1->pn_value.pnv_unsigned > INT64_MAX)
    255  1.14     joerg 			return (_PROP_OBJECT_EQUALS_FALSE);
    256   1.8   thorpej 		if (num2->pn_value.pnv_signed < 0)
    257  1.14     joerg 			return (_PROP_OBJECT_EQUALS_FALSE);
    258   1.8   thorpej 	} else {
    259   1.8   thorpej 		/*
    260   1.8   thorpej 		 * num1 is signed and num2 is unsigned.
    261   1.8   thorpej 		 */
    262   1.8   thorpej 		if (num1->pn_value.pnv_signed < 0)
    263  1.14     joerg 			return (_PROP_OBJECT_EQUALS_FALSE);
    264   1.8   thorpej 		if (num2->pn_value.pnv_unsigned > INT64_MAX)
    265  1.14     joerg 			return (_PROP_OBJECT_EQUALS_FALSE);
    266   1.8   thorpej 	}
    267   1.8   thorpej 
    268  1.14     joerg 	if (num1->pn_value.pnv_signed == num2->pn_value.pnv_signed)
    269  1.14     joerg 		return _PROP_OBJECT_EQUALS_TRUE;
    270  1.14     joerg 	else
    271  1.14     joerg 		return _PROP_OBJECT_EQUALS_FALSE;
    272   1.2   thorpej }
    273   1.2   thorpej 
    274   1.1   thorpej static prop_number_t
    275   1.8   thorpej _prop_number_alloc(const struct _prop_number_value *pnv)
    276   1.1   thorpej {
    277  1.23     rmind 	prop_number_t opn, pn, rpn;
    278   1.6   thorpej 
    279  1.21     pooka 	_PROP_ONCE_RUN(_prop_number_init_once, _prop_number_init);
    280  1.21     pooka 
    281   1.6   thorpej 	/*
    282   1.6   thorpej 	 * Check to see if this already exists in the tree.  If it does,
    283   1.6   thorpej 	 * we just retain it and return it.
    284   1.6   thorpej 	 */
    285   1.6   thorpej 	_PROP_MUTEX_LOCK(_prop_number_tree_mutex);
    286  1.29  pgoyette 	opn = rb_tree_find_node(&_prop_number_tree, pnv);
    287  1.23     rmind 	if (opn != NULL) {
    288  1.21     pooka 		prop_object_retain(opn);
    289  1.21     pooka 		_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
    290  1.21     pooka 		return (opn);
    291   1.6   thorpej 	}
    292   1.6   thorpej 	_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
    293   1.6   thorpej 
    294   1.6   thorpej 	/*
    295   1.6   thorpej 	 * Not in the tree.  Create it now.
    296   1.6   thorpej 	 */
    297   1.1   thorpej 
    298   1.1   thorpej 	pn = _PROP_POOL_GET(_prop_number_pool);
    299   1.6   thorpej 	if (pn == NULL)
    300   1.6   thorpej 		return (NULL);
    301   1.6   thorpej 
    302   1.6   thorpej 	_prop_object_init(&pn->pn_obj, &_prop_object_type_number);
    303   1.1   thorpej 
    304   1.8   thorpej 	pn->pn_value = *pnv;
    305   1.6   thorpej 
    306   1.6   thorpej 	/*
    307   1.6   thorpej 	 * We dropped the mutex when we allocated the new object, so
    308   1.6   thorpej 	 * we have to check again if it is in the tree.
    309   1.6   thorpej 	 */
    310   1.6   thorpej 	_PROP_MUTEX_LOCK(_prop_number_tree_mutex);
    311  1.29  pgoyette 	opn = rb_tree_find_node_node(&_prop_number_tree, pnv);
    312  1.23     rmind 	if (opn != NULL) {
    313   1.6   thorpej 		prop_object_retain(opn);
    314   1.6   thorpej 		_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
    315   1.6   thorpej 		_PROP_POOL_PUT(_prop_number_pool, pn);
    316   1.6   thorpej 		return (opn);
    317   1.1   thorpej 	}
    318  1.28  pgoyette 	rpn = rb_tree_insert_node(&_prop_number_tree, pn);
    319  1.23     rmind 	_PROP_ASSERT(rpn == pn);
    320   1.6   thorpej 	_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
    321  1.25    martin 	return (rpn);
    322   1.1   thorpej }
    323   1.1   thorpej 
    324   1.1   thorpej /*
    325   1.1   thorpej  * prop_number_create_integer --
    326   1.1   thorpej  *	Create a prop_number_t and initialize it with the
    327   1.1   thorpej  *	provided integer value.
    328   1.1   thorpej  */
    329   1.1   thorpej prop_number_t
    330   1.8   thorpej prop_number_create_integer(int64_t val)
    331   1.1   thorpej {
    332   1.9   thorpej 	struct _prop_number_value pnv;
    333   1.9   thorpej 
    334   1.9   thorpej 	memset(&pnv, 0, sizeof(pnv));
    335   1.9   thorpej 	pnv.pnv_signed = val;
    336  1.12   thorpej 	pnv.pnv_is_unsigned = false;
    337   1.1   thorpej 
    338   1.8   thorpej 	return (_prop_number_alloc(&pnv));
    339   1.8   thorpej }
    340   1.8   thorpej 
    341   1.8   thorpej /*
    342   1.8   thorpej  * prop_number_create_unsigned_integer --
    343   1.8   thorpej  *	Create a prop_number_t and initialize it with the
    344   1.8   thorpej  *	provided unsigned integer value.
    345   1.8   thorpej  */
    346   1.8   thorpej prop_number_t
    347   1.8   thorpej prop_number_create_unsigned_integer(uint64_t val)
    348   1.8   thorpej {
    349   1.9   thorpej 	struct _prop_number_value pnv;
    350   1.9   thorpej 
    351   1.9   thorpej 	memset(&pnv, 0, sizeof(pnv));
    352   1.9   thorpej 	pnv.pnv_unsigned = val;
    353  1.12   thorpej 	pnv.pnv_is_unsigned = true;
    354   1.8   thorpej 
    355   1.8   thorpej 	return (_prop_number_alloc(&pnv));
    356   1.1   thorpej }
    357   1.1   thorpej 
    358   1.1   thorpej /*
    359   1.1   thorpej  * prop_number_copy --
    360   1.1   thorpej  *	Copy a prop_number_t.
    361   1.1   thorpej  */
    362   1.1   thorpej prop_number_t
    363   1.1   thorpej prop_number_copy(prop_number_t opn)
    364   1.1   thorpej {
    365   1.1   thorpej 
    366   1.4   thorpej 	if (! prop_object_is_number(opn))
    367   1.4   thorpej 		return (NULL);
    368   1.1   thorpej 
    369   1.6   thorpej 	/*
    370   1.6   thorpej 	 * Because we only ever allocate one object for any given
    371   1.6   thorpej 	 * value, this can be reduced to a simple retain operation.
    372   1.6   thorpej 	 */
    373   1.6   thorpej 	prop_object_retain(opn);
    374   1.6   thorpej 	return (opn);
    375   1.1   thorpej }
    376   1.1   thorpej 
    377   1.1   thorpej /*
    378   1.8   thorpej  * prop_number_unsigned --
    379  1.12   thorpej  *	Returns true if the prop_number_t has an unsigned value.
    380   1.8   thorpej  */
    381  1.12   thorpej bool
    382   1.8   thorpej prop_number_unsigned(prop_number_t pn)
    383   1.8   thorpej {
    384   1.8   thorpej 
    385   1.8   thorpej 	return (pn->pn_value.pnv_is_unsigned);
    386   1.8   thorpej }
    387   1.8   thorpej 
    388   1.8   thorpej /*
    389   1.1   thorpej  * prop_number_size --
    390   1.1   thorpej  *	Return the size, in bits, required to hold the value of
    391   1.1   thorpej  *	the specified number.
    392   1.1   thorpej  */
    393   1.1   thorpej int
    394   1.1   thorpej prop_number_size(prop_number_t pn)
    395   1.1   thorpej {
    396   1.8   thorpej 	struct _prop_number_value *pnv;
    397   1.1   thorpej 
    398   1.4   thorpej 	if (! prop_object_is_number(pn))
    399   1.4   thorpej 		return (0);
    400   1.4   thorpej 
    401   1.8   thorpej 	pnv = &pn->pn_value;
    402   1.8   thorpej 
    403   1.8   thorpej 	if (pnv->pnv_is_unsigned) {
    404   1.8   thorpej 		if (pnv->pnv_unsigned > UINT32_MAX)
    405   1.8   thorpej 			return (64);
    406   1.8   thorpej 		if (pnv->pnv_unsigned > UINT16_MAX)
    407   1.8   thorpej 			return (32);
    408   1.8   thorpej 		if (pnv->pnv_unsigned > UINT8_MAX)
    409   1.8   thorpej 			return (16);
    410   1.8   thorpej 		return (8);
    411   1.8   thorpej 	}
    412   1.8   thorpej 
    413   1.8   thorpej 	if (pnv->pnv_signed > INT32_MAX || pnv->pnv_signed < INT32_MIN)
    414   1.8   thorpej 	    	return (64);
    415   1.8   thorpej 	if (pnv->pnv_signed > INT16_MAX || pnv->pnv_signed < INT16_MIN)
    416   1.1   thorpej 		return (32);
    417   1.8   thorpej 	if (pnv->pnv_signed > INT8_MAX  || pnv->pnv_signed < INT8_MIN)
    418   1.1   thorpej 		return (16);
    419   1.1   thorpej 	return (8);
    420   1.1   thorpej }
    421   1.1   thorpej 
    422   1.1   thorpej /*
    423   1.1   thorpej  * prop_number_integer_value --
    424   1.1   thorpej  *	Get the integer value of a prop_number_t.
    425   1.1   thorpej  */
    426   1.8   thorpej int64_t
    427   1.8   thorpej prop_number_integer_value(prop_number_t pn)
    428   1.8   thorpej {
    429   1.8   thorpej 
    430   1.8   thorpej 	/*
    431   1.8   thorpej 	 * XXX Impossible to distinguish between "not a prop_number_t"
    432   1.8   thorpej 	 * XXX and "prop_number_t has a value of 0".
    433   1.8   thorpej 	 */
    434   1.8   thorpej 	if (! prop_object_is_number(pn))
    435   1.8   thorpej 		return (0);
    436   1.8   thorpej 
    437   1.8   thorpej 	return (pn->pn_value.pnv_signed);
    438   1.8   thorpej }
    439   1.8   thorpej 
    440   1.8   thorpej /*
    441   1.8   thorpej  * prop_number_unsigned_integer_value --
    442   1.8   thorpej  *	Get the unsigned integer value of a prop_number_t.
    443   1.8   thorpej  */
    444   1.3   thorpej uint64_t
    445   1.8   thorpej prop_number_unsigned_integer_value(prop_number_t pn)
    446   1.1   thorpej {
    447   1.1   thorpej 
    448   1.4   thorpej 	/*
    449   1.4   thorpej 	 * XXX Impossible to distinguish between "not a prop_number_t"
    450   1.4   thorpej 	 * XXX and "prop_number_t has a value of 0".
    451   1.4   thorpej 	 */
    452   1.4   thorpej 	if (! prop_object_is_number(pn))
    453   1.4   thorpej 		return (0);
    454   1.4   thorpej 
    455   1.8   thorpej 	return (pn->pn_value.pnv_unsigned);
    456   1.1   thorpej }
    457   1.1   thorpej 
    458   1.1   thorpej /*
    459   1.1   thorpej  * prop_number_equals --
    460  1.12   thorpej  *	Return true if two numbers are equivalent.
    461   1.1   thorpej  */
    462  1.12   thorpej bool
    463   1.1   thorpej prop_number_equals(prop_number_t num1, prop_number_t num2)
    464   1.1   thorpej {
    465  1.14     joerg 	if (!prop_object_is_number(num1) || !prop_object_is_number(num2))
    466  1.14     joerg 		return (false);
    467   1.1   thorpej 
    468  1.14     joerg 	return (prop_object_equals(num1, num2));
    469   1.1   thorpej }
    470   1.1   thorpej 
    471   1.1   thorpej /*
    472   1.1   thorpej  * prop_number_equals_integer --
    473  1.12   thorpej  *	Return true if the number is equivalent to the specified integer.
    474   1.1   thorpej  */
    475  1.12   thorpej bool
    476   1.8   thorpej prop_number_equals_integer(prop_number_t pn, int64_t val)
    477   1.1   thorpej {
    478   1.1   thorpej 
    479   1.4   thorpej 	if (! prop_object_is_number(pn))
    480  1.12   thorpej 		return (false);
    481   1.4   thorpej 
    482   1.8   thorpej 	if (pn->pn_value.pnv_is_unsigned &&
    483   1.8   thorpej 	    (pn->pn_value.pnv_unsigned > INT64_MAX || val < 0))
    484  1.12   thorpej 		return (false);
    485   1.8   thorpej 
    486   1.8   thorpej 	return (pn->pn_value.pnv_signed == val);
    487   1.8   thorpej }
    488   1.8   thorpej 
    489   1.8   thorpej /*
    490   1.8   thorpej  * prop_number_equals_unsigned_integer --
    491  1.12   thorpej  *	Return true if the number is equivalent to the specified
    492   1.8   thorpej  *	unsigned integer.
    493   1.8   thorpej  */
    494  1.12   thorpej bool
    495   1.8   thorpej prop_number_equals_unsigned_integer(prop_number_t pn, uint64_t val)
    496   1.8   thorpej {
    497   1.8   thorpej 
    498   1.8   thorpej 	if (! prop_object_is_number(pn))
    499  1.12   thorpej 		return (false);
    500   1.8   thorpej 
    501   1.8   thorpej 	if (! pn->pn_value.pnv_is_unsigned &&
    502   1.8   thorpej 	    (pn->pn_value.pnv_signed < 0 || val > INT64_MAX))
    503  1.12   thorpej 		return (false);
    504   1.8   thorpej 
    505   1.8   thorpej 	return (pn->pn_value.pnv_unsigned == val);
    506   1.8   thorpej }
    507   1.8   thorpej 
    508  1.12   thorpej static bool
    509   1.8   thorpej _prop_number_internalize_unsigned(struct _prop_object_internalize_context *ctx,
    510   1.8   thorpej 				  struct _prop_number_value *pnv)
    511   1.8   thorpej {
    512   1.8   thorpej 	char *cp;
    513   1.8   thorpej 
    514   1.9   thorpej 	_PROP_ASSERT(/*CONSTCOND*/sizeof(unsigned long long) ==
    515   1.9   thorpej 		     sizeof(uint64_t));
    516   1.8   thorpej 
    517   1.8   thorpej #ifndef _KERNEL
    518   1.8   thorpej 	errno = 0;
    519   1.8   thorpej #endif
    520   1.8   thorpej 	pnv->pnv_unsigned = (uint64_t) strtoull(ctx->poic_cp, &cp, 0);
    521   1.8   thorpej #ifndef _KERNEL		/* XXX can't check for ERANGE in the kernel */
    522   1.8   thorpej 	if (pnv->pnv_unsigned == UINT64_MAX && errno == ERANGE)
    523  1.12   thorpej 		return (false);
    524   1.8   thorpej #endif
    525  1.12   thorpej 	pnv->pnv_is_unsigned = true;
    526   1.8   thorpej 	ctx->poic_cp = cp;
    527   1.8   thorpej 
    528  1.12   thorpej 	return (true);
    529   1.8   thorpej }
    530   1.8   thorpej 
    531  1.12   thorpej static bool
    532   1.8   thorpej _prop_number_internalize_signed(struct _prop_object_internalize_context *ctx,
    533   1.8   thorpej 				struct _prop_number_value *pnv)
    534   1.8   thorpej {
    535   1.8   thorpej 	char *cp;
    536   1.8   thorpej 
    537   1.9   thorpej 	_PROP_ASSERT(/*CONSTCOND*/sizeof(long long) == sizeof(int64_t));
    538   1.8   thorpej 
    539   1.8   thorpej #ifndef _KERNEL
    540   1.8   thorpej 	errno = 0;
    541   1.8   thorpej #endif
    542   1.8   thorpej 	pnv->pnv_signed = (int64_t) strtoll(ctx->poic_cp, &cp, 0);
    543   1.8   thorpej #ifndef _KERNEL		/* XXX can't check for ERANGE in the kernel */
    544   1.8   thorpej 	if ((pnv->pnv_signed == INT64_MAX || pnv->pnv_signed == INT64_MIN) &&
    545   1.8   thorpej 	    errno == ERANGE)
    546  1.12   thorpej 	    	return (false);
    547   1.8   thorpej #endif
    548  1.12   thorpej 	pnv->pnv_is_unsigned = false;
    549   1.8   thorpej 	ctx->poic_cp = cp;
    550   1.8   thorpej 
    551  1.12   thorpej 	return (true);
    552   1.1   thorpej }
    553   1.1   thorpej 
    554   1.1   thorpej /*
    555   1.1   thorpej  * _prop_number_internalize --
    556   1.1   thorpej  *	Parse a <number>...</number> and return the object created from
    557   1.1   thorpej  *	the external representation.
    558   1.1   thorpej  */
    559  1.13     joerg /* ARGSUSED */
    560  1.13     joerg bool
    561  1.13     joerg _prop_number_internalize(prop_stack_t stack, prop_object_t *obj,
    562  1.13     joerg     struct _prop_object_internalize_context *ctx)
    563   1.1   thorpej {
    564   1.9   thorpej 	struct _prop_number_value pnv;
    565   1.9   thorpej 
    566   1.9   thorpej 	memset(&pnv, 0, sizeof(pnv));
    567   1.1   thorpej 
    568   1.1   thorpej 	/* No attributes, no empty elements. */
    569   1.1   thorpej 	if (ctx->poic_tagattr != NULL || ctx->poic_is_empty_element)
    570  1.13     joerg 		return (true);
    571   1.1   thorpej 
    572   1.8   thorpej 	/*
    573   1.8   thorpej 	 * If the first character is '-', then we treat as signed.
    574   1.8   thorpej 	 * If the first two characters are "0x" (i.e. the number is
    575   1.8   thorpej 	 * in hex), then we treat as unsigned.  Otherwise, we try
    576   1.8   thorpej 	 * signed first, and if that fails (presumably due to ERANGE),
    577   1.8   thorpej 	 * then we switch to unsigned.
    578   1.8   thorpej 	 */
    579   1.8   thorpej 	if (ctx->poic_cp[0] == '-') {
    580  1.12   thorpej 		if (_prop_number_internalize_signed(ctx, &pnv) == false)
    581  1.13     joerg 			return (true);
    582   1.8   thorpej 	} else if (ctx->poic_cp[0] == '0' && ctx->poic_cp[1] == 'x') {
    583  1.12   thorpej 		if (_prop_number_internalize_unsigned(ctx, &pnv) == false)
    584  1.13     joerg 			return (true);
    585   1.8   thorpej 	} else {
    586  1.12   thorpej 		if (_prop_number_internalize_signed(ctx, &pnv) == false &&
    587  1.12   thorpej 		    _prop_number_internalize_unsigned(ctx, &pnv) == false)
    588  1.13     joerg 		    	return (true);
    589   1.8   thorpej 	}
    590   1.8   thorpej 
    591   1.1   thorpej 	if (_prop_object_internalize_find_tag(ctx, "integer",
    592  1.12   thorpej 					      _PROP_TAG_TYPE_END) == false)
    593  1.13     joerg 		return (true);
    594   1.1   thorpej 
    595  1.13     joerg 	*obj = _prop_number_alloc(&pnv);
    596  1.13     joerg 	return (true);
    597   1.1   thorpej }
    598