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