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