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