Home | History | Annotate | Line # | Download | only in libprop
prop_number.c revision 1.11.2.2
      1  1.11.2.2  christos /*	$NetBSD: prop_number.c,v 1.11.2.2 2006/10/15 19:11:59 christos Exp $	*/
      2  1.11.2.2  christos 
      3  1.11.2.2  christos /*-
      4  1.11.2.2  christos  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.11.2.2  christos  * All rights reserved.
      6  1.11.2.2  christos  *
      7  1.11.2.2  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.11.2.2  christos  * by Jason R. Thorpe.
      9  1.11.2.2  christos  *
     10  1.11.2.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.11.2.2  christos  * modification, are permitted provided that the following conditions
     12  1.11.2.2  christos  * are met:
     13  1.11.2.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.11.2.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.11.2.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.11.2.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.11.2.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.11.2.2  christos  * 3. All advertising materials mentioning features or use of this software
     19  1.11.2.2  christos  *    must display the following acknowledgement:
     20  1.11.2.2  christos  *      This product includes software developed by the NetBSD
     21  1.11.2.2  christos  *      Foundation, Inc. and its contributors.
     22  1.11.2.2  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.11.2.2  christos  *    contributors may be used to endorse or promote products derived
     24  1.11.2.2  christos  *    from this software without specific prior written permission.
     25  1.11.2.2  christos  *
     26  1.11.2.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.11.2.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.11.2.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.11.2.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.11.2.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.11.2.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.11.2.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.11.2.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.11.2.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.11.2.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.11.2.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     37  1.11.2.2  christos  */
     38  1.11.2.2  christos 
     39  1.11.2.2  christos #include <prop/prop_number.h>
     40  1.11.2.2  christos #include "prop_object_impl.h"
     41  1.11.2.2  christos #include "prop_rb_impl.h"
     42  1.11.2.2  christos 
     43  1.11.2.2  christos #if defined(_KERNEL)
     44  1.11.2.2  christos #include <sys/systm.h>
     45  1.11.2.2  christos #elif defined(_STANDALONE)
     46  1.11.2.2  christos #include <sys/param.h>
     47  1.11.2.2  christos #include <lib/libkern/libkern.h>
     48  1.11.2.2  christos #else
     49  1.11.2.2  christos #include <errno.h>
     50  1.11.2.2  christos #include <stdlib.h>
     51  1.11.2.2  christos #endif
     52  1.11.2.2  christos 
     53  1.11.2.2  christos struct _prop_number {
     54  1.11.2.2  christos 	struct _prop_object	pn_obj;
     55  1.11.2.2  christos 	struct rb_node		pn_link;
     56  1.11.2.2  christos 	struct _prop_number_value {
     57  1.11.2.2  christos 		union {
     58  1.11.2.2  christos 			int64_t  pnu_signed;
     59  1.11.2.2  christos 			uint64_t pnu_unsigned;
     60  1.11.2.2  christos 		} pnv_un;
     61  1.11.2.2  christos #define	pnv_signed	pnv_un.pnu_signed
     62  1.11.2.2  christos #define	pnv_unsigned	pnv_un.pnu_unsigned
     63  1.11.2.2  christos 		unsigned int	pnv_is_unsigned	:1,
     64  1.11.2.2  christos 						:31;
     65  1.11.2.2  christos 	} pn_value;
     66  1.11.2.2  christos };
     67  1.11.2.2  christos 
     68  1.11.2.2  christos #define	RBNODE_TO_PN(n)							\
     69  1.11.2.2  christos 	((struct _prop_number *)					\
     70  1.11.2.2  christos 	 ((uintptr_t)n - offsetof(struct _prop_number, pn_link)))
     71  1.11.2.2  christos 
     72  1.11.2.2  christos _PROP_POOL_INIT(_prop_number_pool, sizeof(struct _prop_number), "propnmbr")
     73  1.11.2.2  christos 
     74  1.11.2.2  christos static void		_prop_number_free(void *);
     75  1.11.2.2  christos static boolean_t	_prop_number_externalize(
     76  1.11.2.2  christos 				struct _prop_object_externalize_context *,
     77  1.11.2.2  christos 				void *);
     78  1.11.2.2  christos static boolean_t	_prop_number_equals(void *, void *);
     79  1.11.2.2  christos 
     80  1.11.2.2  christos static const struct _prop_object_type _prop_object_type_number = {
     81  1.11.2.2  christos 	.pot_type	=	PROP_TYPE_NUMBER,
     82  1.11.2.2  christos 	.pot_free	=	_prop_number_free,
     83  1.11.2.2  christos 	.pot_extern	=	_prop_number_externalize,
     84  1.11.2.2  christos 	.pot_equals	=	_prop_number_equals,
     85  1.11.2.2  christos };
     86  1.11.2.2  christos 
     87  1.11.2.2  christos #define	prop_object_is_number(x)	\
     88  1.11.2.2  christos 	((x) != NULL && (x)->pn_obj.po_type == &_prop_object_type_number)
     89  1.11.2.2  christos 
     90  1.11.2.2  christos /*
     91  1.11.2.2  christos  * Number objects are immutable, and we are likely to have many number
     92  1.11.2.2  christos  * objects that have the same value.  So, to save memory, we unique'ify
     93  1.11.2.2  christos  * numbers so we only have one copy of each.
     94  1.11.2.2  christos  */
     95  1.11.2.2  christos 
     96  1.11.2.2  christos static int
     97  1.11.2.2  christos _prop_number_compare_values(const struct _prop_number_value *pnv1,
     98  1.11.2.2  christos 			    const struct _prop_number_value *pnv2)
     99  1.11.2.2  christos {
    100  1.11.2.2  christos 
    101  1.11.2.2  christos 	/* Signed numbers are sorted before unsigned numbers. */
    102  1.11.2.2  christos 
    103  1.11.2.2  christos 	if (pnv1->pnv_is_unsigned) {
    104  1.11.2.2  christos 		if (! pnv2->pnv_is_unsigned)
    105  1.11.2.2  christos 			return (1);
    106  1.11.2.2  christos 		if (pnv1->pnv_unsigned < pnv2->pnv_unsigned)
    107  1.11.2.2  christos 			return (-1);
    108  1.11.2.2  christos 		if (pnv1->pnv_unsigned > pnv2->pnv_unsigned)
    109  1.11.2.2  christos 			return (1);
    110  1.11.2.2  christos 		return (0);
    111  1.11.2.2  christos 	}
    112  1.11.2.2  christos 
    113  1.11.2.2  christos 	if (pnv2->pnv_is_unsigned)
    114  1.11.2.2  christos 		return (-1);
    115  1.11.2.2  christos 	if (pnv1->pnv_signed < pnv2->pnv_signed)
    116  1.11.2.2  christos 		return (-1);
    117  1.11.2.2  christos 	if (pnv1->pnv_signed > pnv2->pnv_signed)
    118  1.11.2.2  christos 		return (1);
    119  1.11.2.2  christos 	return (0);
    120  1.11.2.2  christos }
    121  1.11.2.2  christos 
    122  1.11.2.2  christos static int
    123  1.11.2.2  christos _prop_number_rb_compare_nodes(const struct rb_node *n1,
    124  1.11.2.2  christos 			      const struct rb_node *n2)
    125  1.11.2.2  christos {
    126  1.11.2.2  christos 	const prop_number_t pn1 = RBNODE_TO_PN(n1);
    127  1.11.2.2  christos 	const prop_number_t pn2 = RBNODE_TO_PN(n2);
    128  1.11.2.2  christos 
    129  1.11.2.2  christos 	return (_prop_number_compare_values(&pn1->pn_value, &pn2->pn_value));
    130  1.11.2.2  christos }
    131  1.11.2.2  christos 
    132  1.11.2.2  christos static int
    133  1.11.2.2  christos _prop_number_rb_compare_key(const struct rb_node *n,
    134  1.11.2.2  christos 			    const void *v)
    135  1.11.2.2  christos {
    136  1.11.2.2  christos 	const prop_number_t pn = RBNODE_TO_PN(n);
    137  1.11.2.2  christos 	const struct _prop_number_value *pnv = v;
    138  1.11.2.2  christos 
    139  1.11.2.2  christos 	return (_prop_number_compare_values(&pn->pn_value, pnv));
    140  1.11.2.2  christos }
    141  1.11.2.2  christos 
    142  1.11.2.2  christos static const struct rb_tree_ops _prop_number_rb_tree_ops = {
    143  1.11.2.2  christos 	.rbto_compare_nodes = _prop_number_rb_compare_nodes,
    144  1.11.2.2  christos 	.rbto_compare_key   = _prop_number_rb_compare_key,
    145  1.11.2.2  christos };
    146  1.11.2.2  christos 
    147  1.11.2.2  christos static struct rb_tree _prop_number_tree;
    148  1.11.2.2  christos static boolean_t _prop_number_tree_initialized;
    149  1.11.2.2  christos 
    150  1.11.2.2  christos _PROP_MUTEX_DECL_STATIC(_prop_number_tree_mutex)
    151  1.11.2.2  christos 
    152  1.11.2.2  christos static void
    153  1.11.2.2  christos _prop_number_free(void *v)
    154  1.11.2.2  christos {
    155  1.11.2.2  christos 	prop_number_t pn = v;
    156  1.11.2.2  christos 
    157  1.11.2.2  christos 	_PROP_MUTEX_LOCK(_prop_number_tree_mutex);
    158  1.11.2.2  christos 	_prop_rb_tree_remove_node(&_prop_number_tree, &pn->pn_link);
    159  1.11.2.2  christos 	_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
    160  1.11.2.2  christos 
    161  1.11.2.2  christos 	_PROP_POOL_PUT(_prop_number_pool, pn);
    162  1.11.2.2  christos }
    163  1.11.2.2  christos 
    164  1.11.2.2  christos static boolean_t
    165  1.11.2.2  christos _prop_number_externalize(struct _prop_object_externalize_context *ctx,
    166  1.11.2.2  christos 			 void *v)
    167  1.11.2.2  christos {
    168  1.11.2.2  christos 	prop_number_t pn = v;
    169  1.11.2.2  christos 	char tmpstr[32];
    170  1.11.2.2  christos 
    171  1.11.2.2  christos 	/*
    172  1.11.2.2  christos 	 * For unsigned numbers, we output in hex.  For signed numbers,
    173  1.11.2.2  christos 	 * we output in decimal.
    174  1.11.2.2  christos 	 */
    175  1.11.2.2  christos 	if (pn->pn_value.pnv_is_unsigned)
    176  1.11.2.2  christos 		sprintf(tmpstr, "0x%" PRIx64, pn->pn_value.pnv_unsigned);
    177  1.11.2.2  christos 	else
    178  1.11.2.2  christos 		sprintf(tmpstr, "%" PRIi64, pn->pn_value.pnv_signed);
    179  1.11.2.2  christos 
    180  1.11.2.2  christos 	if (_prop_object_externalize_start_tag(ctx, "integer") == FALSE ||
    181  1.11.2.2  christos 	    _prop_object_externalize_append_cstring(ctx, tmpstr) == FALSE ||
    182  1.11.2.2  christos 	    _prop_object_externalize_end_tag(ctx, "integer") == FALSE)
    183  1.11.2.2  christos 		return (FALSE);
    184  1.11.2.2  christos 
    185  1.11.2.2  christos 	return (TRUE);
    186  1.11.2.2  christos }
    187  1.11.2.2  christos 
    188  1.11.2.2  christos static boolean_t
    189  1.11.2.2  christos _prop_number_equals(void *v1, void *v2)
    190  1.11.2.2  christos {
    191  1.11.2.2  christos 	prop_number_t num1 = v1;
    192  1.11.2.2  christos 	prop_number_t num2 = v2;
    193  1.11.2.2  christos 
    194  1.11.2.2  christos 	if (! (prop_object_is_number(num1) &&
    195  1.11.2.2  christos 	       prop_object_is_number(num2)))
    196  1.11.2.2  christos 		return (FALSE);
    197  1.11.2.2  christos 
    198  1.11.2.2  christos 	/*
    199  1.11.2.2  christos 	 * There is only ever one copy of a number object at any given
    200  1.11.2.2  christos 	 * time, so we can reduce this to a simple pointer equality check
    201  1.11.2.2  christos 	 * in the common case.
    202  1.11.2.2  christos 	 */
    203  1.11.2.2  christos 	if (num1 == num2)
    204  1.11.2.2  christos 		return (TRUE);
    205  1.11.2.2  christos 
    206  1.11.2.2  christos 	/*
    207  1.11.2.2  christos 	 * If the numbers are the same signed-ness, then we know they
    208  1.11.2.2  christos 	 * cannot be equal because they would have had pointer equality.
    209  1.11.2.2  christos 	 */
    210  1.11.2.2  christos 	if (num1->pn_value.pnv_is_unsigned == num2->pn_value.pnv_is_unsigned)
    211  1.11.2.2  christos 		return (FALSE);
    212  1.11.2.2  christos 
    213  1.11.2.2  christos 	/*
    214  1.11.2.2  christos 	 * We now have one signed value and one unsigned value.  We can
    215  1.11.2.2  christos 	 * compare them iff:
    216  1.11.2.2  christos 	 *	- The unsigned value is not larger than the signed value
    217  1.11.2.2  christos 	 *	  can represent.
    218  1.11.2.2  christos 	 *	- The signed value is not smaller than the unsigned value
    219  1.11.2.2  christos 	 *	  can represent.
    220  1.11.2.2  christos 	 */
    221  1.11.2.2  christos 	if (num1->pn_value.pnv_is_unsigned) {
    222  1.11.2.2  christos 		/*
    223  1.11.2.2  christos 		 * num1 is unsigned and num2 is signed.
    224  1.11.2.2  christos 		 */
    225  1.11.2.2  christos 		if (num1->pn_value.pnv_unsigned > INT64_MAX)
    226  1.11.2.2  christos 			return (FALSE);
    227  1.11.2.2  christos 		if (num2->pn_value.pnv_signed < 0)
    228  1.11.2.2  christos 			return (FALSE);
    229  1.11.2.2  christos 	} else {
    230  1.11.2.2  christos 		/*
    231  1.11.2.2  christos 		 * num1 is signed and num2 is unsigned.
    232  1.11.2.2  christos 		 */
    233  1.11.2.2  christos 		if (num1->pn_value.pnv_signed < 0)
    234  1.11.2.2  christos 			return (FALSE);
    235  1.11.2.2  christos 		if (num2->pn_value.pnv_unsigned > INT64_MAX)
    236  1.11.2.2  christos 			return (FALSE);
    237  1.11.2.2  christos 	}
    238  1.11.2.2  christos 
    239  1.11.2.2  christos 	return (num1->pn_value.pnv_signed == num2->pn_value.pnv_signed);
    240  1.11.2.2  christos }
    241  1.11.2.2  christos 
    242  1.11.2.2  christos static prop_number_t
    243  1.11.2.2  christos _prop_number_alloc(const struct _prop_number_value *pnv)
    244  1.11.2.2  christos {
    245  1.11.2.2  christos 	prop_number_t opn, pn;
    246  1.11.2.2  christos 	struct rb_node *n;
    247  1.11.2.2  christos 
    248  1.11.2.2  christos 	/*
    249  1.11.2.2  christos 	 * Check to see if this already exists in the tree.  If it does,
    250  1.11.2.2  christos 	 * we just retain it and return it.
    251  1.11.2.2  christos 	 */
    252  1.11.2.2  christos 	_PROP_MUTEX_LOCK(_prop_number_tree_mutex);
    253  1.11.2.2  christos 	if (! _prop_number_tree_initialized) {
    254  1.11.2.2  christos 		_prop_rb_tree_init(&_prop_number_tree,
    255  1.11.2.2  christos 				   &_prop_number_rb_tree_ops);
    256  1.11.2.2  christos 		_prop_number_tree_initialized = TRUE;
    257  1.11.2.2  christos 	} else {
    258  1.11.2.2  christos 		n = _prop_rb_tree_find(&_prop_number_tree, pnv);
    259  1.11.2.2  christos 		if (n != NULL) {
    260  1.11.2.2  christos 			opn = RBNODE_TO_PN(n);
    261  1.11.2.2  christos 			prop_object_retain(opn);
    262  1.11.2.2  christos 			_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
    263  1.11.2.2  christos 			return (opn);
    264  1.11.2.2  christos 		}
    265  1.11.2.2  christos 	}
    266  1.11.2.2  christos 	_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
    267  1.11.2.2  christos 
    268  1.11.2.2  christos 	/*
    269  1.11.2.2  christos 	 * Not in the tree.  Create it now.
    270  1.11.2.2  christos 	 */
    271  1.11.2.2  christos 
    272  1.11.2.2  christos 	pn = _PROP_POOL_GET(_prop_number_pool);
    273  1.11.2.2  christos 	if (pn == NULL)
    274  1.11.2.2  christos 		return (NULL);
    275  1.11.2.2  christos 
    276  1.11.2.2  christos 	_prop_object_init(&pn->pn_obj, &_prop_object_type_number);
    277  1.11.2.2  christos 
    278  1.11.2.2  christos 	pn->pn_value = *pnv;
    279  1.11.2.2  christos 
    280  1.11.2.2  christos 	/*
    281  1.11.2.2  christos 	 * We dropped the mutex when we allocated the new object, so
    282  1.11.2.2  christos 	 * we have to check again if it is in the tree.
    283  1.11.2.2  christos 	 */
    284  1.11.2.2  christos 	_PROP_MUTEX_LOCK(_prop_number_tree_mutex);
    285  1.11.2.2  christos 	n = _prop_rb_tree_find(&_prop_number_tree, pnv);
    286  1.11.2.2  christos 	if (n != NULL) {
    287  1.11.2.2  christos 		opn = RBNODE_TO_PN(n);
    288  1.11.2.2  christos 		prop_object_retain(opn);
    289  1.11.2.2  christos 		_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
    290  1.11.2.2  christos 		_PROP_POOL_PUT(_prop_number_pool, pn);
    291  1.11.2.2  christos 		return (opn);
    292  1.11.2.2  christos 	}
    293  1.11.2.2  christos 	_prop_rb_tree_insert_node(&_prop_number_tree, &pn->pn_link);
    294  1.11.2.2  christos 	_PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
    295  1.11.2.2  christos 	return (pn);
    296  1.11.2.2  christos }
    297  1.11.2.2  christos 
    298  1.11.2.2  christos /*
    299  1.11.2.2  christos  * prop_number_create_integer --
    300  1.11.2.2  christos  *	Create a prop_number_t and initialize it with the
    301  1.11.2.2  christos  *	provided integer value.
    302  1.11.2.2  christos  */
    303  1.11.2.2  christos prop_number_t
    304  1.11.2.2  christos prop_number_create_integer(int64_t val)
    305  1.11.2.2  christos {
    306  1.11.2.2  christos 	struct _prop_number_value pnv;
    307  1.11.2.2  christos 
    308  1.11.2.2  christos 	memset(&pnv, 0, sizeof(pnv));
    309  1.11.2.2  christos 	pnv.pnv_signed = val;
    310  1.11.2.2  christos 	pnv.pnv_is_unsigned = FALSE;
    311  1.11.2.2  christos 
    312  1.11.2.2  christos 	return (_prop_number_alloc(&pnv));
    313  1.11.2.2  christos }
    314  1.11.2.2  christos 
    315  1.11.2.2  christos /*
    316  1.11.2.2  christos  * prop_number_create_unsigned_integer --
    317  1.11.2.2  christos  *	Create a prop_number_t and initialize it with the
    318  1.11.2.2  christos  *	provided unsigned integer value.
    319  1.11.2.2  christos  */
    320  1.11.2.2  christos prop_number_t
    321  1.11.2.2  christos prop_number_create_unsigned_integer(uint64_t val)
    322  1.11.2.2  christos {
    323  1.11.2.2  christos 	struct _prop_number_value pnv;
    324  1.11.2.2  christos 
    325  1.11.2.2  christos 	memset(&pnv, 0, sizeof(pnv));
    326  1.11.2.2  christos 	pnv.pnv_unsigned = val;
    327  1.11.2.2  christos 	pnv.pnv_is_unsigned = TRUE;
    328  1.11.2.2  christos 
    329  1.11.2.2  christos 	return (_prop_number_alloc(&pnv));
    330  1.11.2.2  christos }
    331  1.11.2.2  christos 
    332  1.11.2.2  christos /*
    333  1.11.2.2  christos  * prop_number_copy --
    334  1.11.2.2  christos  *	Copy a prop_number_t.
    335  1.11.2.2  christos  */
    336  1.11.2.2  christos prop_number_t
    337  1.11.2.2  christos prop_number_copy(prop_number_t opn)
    338  1.11.2.2  christos {
    339  1.11.2.2  christos 
    340  1.11.2.2  christos 	if (! prop_object_is_number(opn))
    341  1.11.2.2  christos 		return (NULL);
    342  1.11.2.2  christos 
    343  1.11.2.2  christos 	/*
    344  1.11.2.2  christos 	 * Because we only ever allocate one object for any given
    345  1.11.2.2  christos 	 * value, this can be reduced to a simple retain operation.
    346  1.11.2.2  christos 	 */
    347  1.11.2.2  christos 	prop_object_retain(opn);
    348  1.11.2.2  christos 	return (opn);
    349  1.11.2.2  christos }
    350  1.11.2.2  christos 
    351  1.11.2.2  christos /*
    352  1.11.2.2  christos  * prop_number_unsigned --
    353  1.11.2.2  christos  *	Returns TRUE if the prop_number_t has an unsigned value.
    354  1.11.2.2  christos  */
    355  1.11.2.2  christos boolean_t
    356  1.11.2.2  christos prop_number_unsigned(prop_number_t pn)
    357  1.11.2.2  christos {
    358  1.11.2.2  christos 
    359  1.11.2.2  christos 	return (pn->pn_value.pnv_is_unsigned);
    360  1.11.2.2  christos }
    361  1.11.2.2  christos 
    362  1.11.2.2  christos /*
    363  1.11.2.2  christos  * prop_number_size --
    364  1.11.2.2  christos  *	Return the size, in bits, required to hold the value of
    365  1.11.2.2  christos  *	the specified number.
    366  1.11.2.2  christos  */
    367  1.11.2.2  christos int
    368  1.11.2.2  christos prop_number_size(prop_number_t pn)
    369  1.11.2.2  christos {
    370  1.11.2.2  christos 	struct _prop_number_value *pnv;
    371  1.11.2.2  christos 
    372  1.11.2.2  christos 	if (! prop_object_is_number(pn))
    373  1.11.2.2  christos 		return (0);
    374  1.11.2.2  christos 
    375  1.11.2.2  christos 	pnv = &pn->pn_value;
    376  1.11.2.2  christos 
    377  1.11.2.2  christos 	if (pnv->pnv_is_unsigned) {
    378  1.11.2.2  christos 		if (pnv->pnv_unsigned > UINT32_MAX)
    379  1.11.2.2  christos 			return (64);
    380  1.11.2.2  christos 		if (pnv->pnv_unsigned > UINT16_MAX)
    381  1.11.2.2  christos 			return (32);
    382  1.11.2.2  christos 		if (pnv->pnv_unsigned > UINT8_MAX)
    383  1.11.2.2  christos 			return (16);
    384  1.11.2.2  christos 		return (8);
    385  1.11.2.2  christos 	}
    386  1.11.2.2  christos 
    387  1.11.2.2  christos 	if (pnv->pnv_signed > INT32_MAX || pnv->pnv_signed < INT32_MIN)
    388  1.11.2.2  christos 	    	return (64);
    389  1.11.2.2  christos 	if (pnv->pnv_signed > INT16_MAX || pnv->pnv_signed < INT16_MIN)
    390  1.11.2.2  christos 		return (32);
    391  1.11.2.2  christos 	if (pnv->pnv_signed > INT8_MAX  || pnv->pnv_signed < INT8_MIN)
    392  1.11.2.2  christos 		return (16);
    393  1.11.2.2  christos 	return (8);
    394  1.11.2.2  christos }
    395  1.11.2.2  christos 
    396  1.11.2.2  christos /*
    397  1.11.2.2  christos  * prop_number_integer_value --
    398  1.11.2.2  christos  *	Get the integer value of a prop_number_t.
    399  1.11.2.2  christos  */
    400  1.11.2.2  christos int64_t
    401  1.11.2.2  christos prop_number_integer_value(prop_number_t pn)
    402  1.11.2.2  christos {
    403  1.11.2.2  christos 
    404  1.11.2.2  christos 	/*
    405  1.11.2.2  christos 	 * XXX Impossible to distinguish between "not a prop_number_t"
    406  1.11.2.2  christos 	 * XXX and "prop_number_t has a value of 0".
    407  1.11.2.2  christos 	 */
    408  1.11.2.2  christos 	if (! prop_object_is_number(pn))
    409  1.11.2.2  christos 		return (0);
    410  1.11.2.2  christos 
    411  1.11.2.2  christos 	return (pn->pn_value.pnv_signed);
    412  1.11.2.2  christos }
    413  1.11.2.2  christos 
    414  1.11.2.2  christos /*
    415  1.11.2.2  christos  * prop_number_unsigned_integer_value --
    416  1.11.2.2  christos  *	Get the unsigned integer value of a prop_number_t.
    417  1.11.2.2  christos  */
    418  1.11.2.2  christos uint64_t
    419  1.11.2.2  christos prop_number_unsigned_integer_value(prop_number_t pn)
    420  1.11.2.2  christos {
    421  1.11.2.2  christos 
    422  1.11.2.2  christos 	/*
    423  1.11.2.2  christos 	 * XXX Impossible to distinguish between "not a prop_number_t"
    424  1.11.2.2  christos 	 * XXX and "prop_number_t has a value of 0".
    425  1.11.2.2  christos 	 */
    426  1.11.2.2  christos 	if (! prop_object_is_number(pn))
    427  1.11.2.2  christos 		return (0);
    428  1.11.2.2  christos 
    429  1.11.2.2  christos 	return (pn->pn_value.pnv_unsigned);
    430  1.11.2.2  christos }
    431  1.11.2.2  christos 
    432  1.11.2.2  christos /*
    433  1.11.2.2  christos  * prop_number_equals --
    434  1.11.2.2  christos  *	Return TRUE if two numbers are equivalent.
    435  1.11.2.2  christos  */
    436  1.11.2.2  christos boolean_t
    437  1.11.2.2  christos prop_number_equals(prop_number_t num1, prop_number_t num2)
    438  1.11.2.2  christos {
    439  1.11.2.2  christos 
    440  1.11.2.2  christos 	return (_prop_number_equals(num1, num2));
    441  1.11.2.2  christos }
    442  1.11.2.2  christos 
    443  1.11.2.2  christos /*
    444  1.11.2.2  christos  * prop_number_equals_integer --
    445  1.11.2.2  christos  *	Return TRUE if the number is equivalent to the specified integer.
    446  1.11.2.2  christos  */
    447  1.11.2.2  christos boolean_t
    448  1.11.2.2  christos prop_number_equals_integer(prop_number_t pn, int64_t val)
    449  1.11.2.2  christos {
    450  1.11.2.2  christos 
    451  1.11.2.2  christos 	if (! prop_object_is_number(pn))
    452  1.11.2.2  christos 		return (FALSE);
    453  1.11.2.2  christos 
    454  1.11.2.2  christos 	if (pn->pn_value.pnv_is_unsigned &&
    455  1.11.2.2  christos 	    (pn->pn_value.pnv_unsigned > INT64_MAX || val < 0))
    456  1.11.2.2  christos 		return (FALSE);
    457  1.11.2.2  christos 
    458  1.11.2.2  christos 	return (pn->pn_value.pnv_signed == val);
    459  1.11.2.2  christos }
    460  1.11.2.2  christos 
    461  1.11.2.2  christos /*
    462  1.11.2.2  christos  * prop_number_equals_unsigned_integer --
    463  1.11.2.2  christos  *	Return TRUE if the number is equivalent to the specified
    464  1.11.2.2  christos  *	unsigned integer.
    465  1.11.2.2  christos  */
    466  1.11.2.2  christos boolean_t
    467  1.11.2.2  christos prop_number_equals_unsigned_integer(prop_number_t pn, uint64_t val)
    468  1.11.2.2  christos {
    469  1.11.2.2  christos 
    470  1.11.2.2  christos 	if (! prop_object_is_number(pn))
    471  1.11.2.2  christos 		return (FALSE);
    472  1.11.2.2  christos 
    473  1.11.2.2  christos 	if (! pn->pn_value.pnv_is_unsigned &&
    474  1.11.2.2  christos 	    (pn->pn_value.pnv_signed < 0 || val > INT64_MAX))
    475  1.11.2.2  christos 		return (FALSE);
    476  1.11.2.2  christos 
    477  1.11.2.2  christos 	return (pn->pn_value.pnv_unsigned == val);
    478  1.11.2.2  christos }
    479  1.11.2.2  christos 
    480  1.11.2.2  christos static boolean_t
    481  1.11.2.2  christos _prop_number_internalize_unsigned(struct _prop_object_internalize_context *ctx,
    482  1.11.2.2  christos 				  struct _prop_number_value *pnv)
    483  1.11.2.2  christos {
    484  1.11.2.2  christos 	char *cp;
    485  1.11.2.2  christos 
    486  1.11.2.2  christos 	_PROP_ASSERT(/*CONSTCOND*/sizeof(unsigned long long) ==
    487  1.11.2.2  christos 		     sizeof(uint64_t));
    488  1.11.2.2  christos 
    489  1.11.2.2  christos #ifndef _KERNEL
    490  1.11.2.2  christos 	errno = 0;
    491  1.11.2.2  christos #endif
    492  1.11.2.2  christos 	pnv->pnv_unsigned = (uint64_t) strtoull(ctx->poic_cp, &cp, 0);
    493  1.11.2.2  christos #ifndef _KERNEL		/* XXX can't check for ERANGE in the kernel */
    494  1.11.2.2  christos 	if (pnv->pnv_unsigned == UINT64_MAX && errno == ERANGE)
    495  1.11.2.2  christos 		return (FALSE);
    496  1.11.2.2  christos #endif
    497  1.11.2.2  christos 	pnv->pnv_is_unsigned = TRUE;
    498  1.11.2.2  christos 	ctx->poic_cp = cp;
    499  1.11.2.2  christos 
    500  1.11.2.2  christos 	return (TRUE);
    501  1.11.2.2  christos }
    502  1.11.2.2  christos 
    503  1.11.2.2  christos static boolean_t
    504  1.11.2.2  christos _prop_number_internalize_signed(struct _prop_object_internalize_context *ctx,
    505  1.11.2.2  christos 				struct _prop_number_value *pnv)
    506  1.11.2.2  christos {
    507  1.11.2.2  christos 	char *cp;
    508  1.11.2.2  christos 
    509  1.11.2.2  christos 	_PROP_ASSERT(/*CONSTCOND*/sizeof(long long) == sizeof(int64_t));
    510  1.11.2.2  christos 
    511  1.11.2.2  christos #ifndef _KERNEL
    512  1.11.2.2  christos 	errno = 0;
    513  1.11.2.2  christos #endif
    514  1.11.2.2  christos 	pnv->pnv_signed = (int64_t) strtoll(ctx->poic_cp, &cp, 0);
    515  1.11.2.2  christos #ifndef _KERNEL		/* XXX can't check for ERANGE in the kernel */
    516  1.11.2.2  christos 	if ((pnv->pnv_signed == INT64_MAX || pnv->pnv_signed == INT64_MIN) &&
    517  1.11.2.2  christos 	    errno == ERANGE)
    518  1.11.2.2  christos 	    	return (FALSE);
    519  1.11.2.2  christos #endif
    520  1.11.2.2  christos 	pnv->pnv_is_unsigned = FALSE;
    521  1.11.2.2  christos 	ctx->poic_cp = cp;
    522  1.11.2.2  christos 
    523  1.11.2.2  christos 	return (TRUE);
    524  1.11.2.2  christos }
    525  1.11.2.2  christos 
    526  1.11.2.2  christos /*
    527  1.11.2.2  christos  * _prop_number_internalize --
    528  1.11.2.2  christos  *	Parse a <number>...</number> and return the object created from
    529  1.11.2.2  christos  *	the external representation.
    530  1.11.2.2  christos  */
    531  1.11.2.2  christos prop_object_t
    532  1.11.2.2  christos _prop_number_internalize(struct _prop_object_internalize_context *ctx)
    533  1.11.2.2  christos {
    534  1.11.2.2  christos 	struct _prop_number_value pnv;
    535  1.11.2.2  christos 
    536  1.11.2.2  christos 	memset(&pnv, 0, sizeof(pnv));
    537  1.11.2.2  christos 
    538  1.11.2.2  christos 	/* No attributes, no empty elements. */
    539  1.11.2.2  christos 	if (ctx->poic_tagattr != NULL || ctx->poic_is_empty_element)
    540  1.11.2.2  christos 		return (NULL);
    541  1.11.2.2  christos 
    542  1.11.2.2  christos 	/*
    543  1.11.2.2  christos 	 * If the first character is '-', then we treat as signed.
    544  1.11.2.2  christos 	 * If the first two characters are "0x" (i.e. the number is
    545  1.11.2.2  christos 	 * in hex), then we treat as unsigned.  Otherwise, we try
    546  1.11.2.2  christos 	 * signed first, and if that fails (presumably due to ERANGE),
    547  1.11.2.2  christos 	 * then we switch to unsigned.
    548  1.11.2.2  christos 	 */
    549  1.11.2.2  christos 	if (ctx->poic_cp[0] == '-') {
    550  1.11.2.2  christos 		if (_prop_number_internalize_signed(ctx, &pnv) == FALSE)
    551  1.11.2.2  christos 			return (NULL);
    552  1.11.2.2  christos 	} else if (ctx->poic_cp[0] == '0' && ctx->poic_cp[1] == 'x') {
    553  1.11.2.2  christos 		if (_prop_number_internalize_unsigned(ctx, &pnv) == FALSE)
    554  1.11.2.2  christos 			return (NULL);
    555  1.11.2.2  christos 	} else {
    556  1.11.2.2  christos 		if (_prop_number_internalize_signed(ctx, &pnv) == FALSE &&
    557  1.11.2.2  christos 		    _prop_number_internalize_unsigned(ctx, &pnv) == FALSE)
    558  1.11.2.2  christos 		    	return (NULL);
    559  1.11.2.2  christos 	}
    560  1.11.2.2  christos 
    561  1.11.2.2  christos 	if (_prop_object_internalize_find_tag(ctx, "integer",
    562  1.11.2.2  christos 					      _PROP_TAG_TYPE_END) == FALSE)
    563  1.11.2.2  christos 		return (NULL);
    564  1.11.2.2  christos 
    565  1.11.2.2  christos 	return (_prop_number_alloc(&pnv));
    566  1.11.2.2  christos }
    567