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