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