prop_number.c revision 1.8 1 /* $NetBSD: prop_number.c,v 1.8 2006/10/12 04:46:56 thorpej 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 void _prop_number_free(void *);
75 static boolean_t _prop_number_externalize(
76 struct _prop_object_externalize_context *,
77 void *);
78 static boolean_t _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 boolean_t _prop_number_tree_initialized;
149
150 _PROP_MUTEX_DECL_STATIC(_prop_number_tree_mutex)
151
152 static void
153 _prop_number_free(void *v)
154 {
155 prop_number_t pn = v;
156
157 _PROP_MUTEX_LOCK(_prop_number_tree_mutex);
158 _prop_rb_tree_remove_node(&_prop_number_tree, &pn->pn_link);
159 _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
160
161 _PROP_POOL_PUT(_prop_number_pool, pn);
162 }
163
164 static boolean_t
165 _prop_number_externalize(struct _prop_object_externalize_context *ctx,
166 void *v)
167 {
168 prop_number_t pn = v;
169 char tmpstr[32];
170
171 /*
172 * For unsigned numbers, we output in hex. For signed numbers,
173 * we output in decimal.
174 */
175 if (pn->pn_value.pnv_is_unsigned)
176 sprintf(tmpstr, "0x%" PRIx64, pn->pn_value.pnv_unsigned);
177 else
178 sprintf(tmpstr, "%" PRIi64, pn->pn_value.pnv_signed);
179
180 if (_prop_object_externalize_start_tag(ctx, "integer") == FALSE ||
181 _prop_object_externalize_append_cstring(ctx, tmpstr) == FALSE ||
182 _prop_object_externalize_end_tag(ctx, "integer") == FALSE)
183 return (FALSE);
184
185 return (TRUE);
186 }
187
188 static boolean_t
189 _prop_number_equals(void *v1, void *v2)
190 {
191 prop_number_t num1 = v1;
192 prop_number_t num2 = v2;
193
194 if (! (prop_object_is_number(num1) &&
195 prop_object_is_number(num2)))
196 return (FALSE);
197
198 /*
199 * There is only ever one copy of a number object at any given
200 * time, so we can reduce this to a simple pointer equality check
201 * in the common case.
202 */
203 if (num1 == num2)
204 return (TRUE);
205
206 /*
207 * If the numbers are the same signed-ness, then we know they
208 * cannot be equal because they would have had pointer equality.
209 */
210 if (num1->pn_value.pnv_is_unsigned == num2->pn_value.pnv_is_unsigned)
211 return (FALSE);
212
213 /*
214 * We now have one signed value and one unsigned value. We can
215 * compare them iff:
216 * - The unsigned value is not larger than the signed value
217 * can represent.
218 * - The signed value is not smaller than the unsigned value
219 * can represent.
220 */
221 if (num1->pn_value.pnv_is_unsigned) {
222 /*
223 * num1 is unsigned and num2 is signed.
224 */
225 if (num1->pn_value.pnv_unsigned > INT64_MAX)
226 return (FALSE);
227 if (num2->pn_value.pnv_signed < 0)
228 return (FALSE);
229 } else {
230 /*
231 * num1 is signed and num2 is unsigned.
232 */
233 if (num1->pn_value.pnv_signed < 0)
234 return (FALSE);
235 if (num2->pn_value.pnv_unsigned > INT64_MAX)
236 return (FALSE);
237 }
238
239 return (num1->pn_value.pnv_signed == num2->pn_value.pnv_signed);
240 }
241
242 static prop_number_t
243 _prop_number_alloc(const struct _prop_number_value *pnv)
244 {
245 prop_number_t opn, pn;
246 struct rb_node *n;
247
248 /*
249 * Check to see if this already exists in the tree. If it does,
250 * we just retain it and return it.
251 */
252 _PROP_MUTEX_LOCK(_prop_number_tree_mutex);
253 if (! _prop_number_tree_initialized) {
254 _prop_rb_tree_init(&_prop_number_tree,
255 &_prop_number_rb_tree_ops);
256 _prop_number_tree_initialized = TRUE;
257 } else {
258 n = _prop_rb_tree_find(&_prop_number_tree, pnv);
259 if (n != NULL) {
260 opn = RBNODE_TO_PN(n);
261 prop_object_retain(opn);
262 _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
263 return (opn);
264 }
265 }
266 _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
267
268 /*
269 * Not in the tree. Create it now.
270 */
271
272 pn = _PROP_POOL_GET(_prop_number_pool);
273 if (pn == NULL)
274 return (NULL);
275
276 _prop_object_init(&pn->pn_obj, &_prop_object_type_number);
277
278 pn->pn_value = *pnv;
279
280 /*
281 * We dropped the mutex when we allocated the new object, so
282 * we have to check again if it is in the tree.
283 */
284 _PROP_MUTEX_LOCK(_prop_number_tree_mutex);
285 n = _prop_rb_tree_find(&_prop_number_tree, pnv);
286 if (n != NULL) {
287 opn = RBNODE_TO_PN(n);
288 prop_object_retain(opn);
289 _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
290 _PROP_POOL_PUT(_prop_number_pool, pn);
291 return (opn);
292 }
293 _prop_rb_tree_insert_node(&_prop_number_tree, &pn->pn_link);
294 _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
295 return (pn);
296 }
297
298 /*
299 * prop_number_create_integer --
300 * Create a prop_number_t and initialize it with the
301 * provided integer value.
302 */
303 prop_number_t
304 prop_number_create_integer(int64_t val)
305 {
306 const struct _prop_number_value pnv = {
307 .pnv_signed = val,
308 .pnv_is_unsigned = FALSE,
309 };
310
311 return (_prop_number_alloc(&pnv));
312 }
313
314 /*
315 * prop_number_create_unsigned_integer --
316 * Create a prop_number_t and initialize it with the
317 * provided unsigned integer value.
318 */
319 prop_number_t
320 prop_number_create_unsigned_integer(uint64_t val)
321 {
322 const struct _prop_number_value pnv = {
323 .pnv_unsigned = val,
324 .pnv_is_unsigned = TRUE,
325 };
326
327 return (_prop_number_alloc(&pnv));
328 }
329
330 /*
331 * prop_number_copy --
332 * Copy a prop_number_t.
333 */
334 prop_number_t
335 prop_number_copy(prop_number_t opn)
336 {
337
338 if (! prop_object_is_number(opn))
339 return (NULL);
340
341 /*
342 * Because we only ever allocate one object for any given
343 * value, this can be reduced to a simple retain operation.
344 */
345 prop_object_retain(opn);
346 return (opn);
347 }
348
349 /*
350 * prop_number_unsigned --
351 * Returns TRUE if the prop_number_t has an unsigned value.
352 */
353 boolean_t
354 prop_number_unsigned(prop_number_t pn)
355 {
356
357 return (pn->pn_value.pnv_is_unsigned);
358 }
359
360 /*
361 * prop_number_size --
362 * Return the size, in bits, required to hold the value of
363 * the specified number.
364 */
365 int
366 prop_number_size(prop_number_t pn)
367 {
368 struct _prop_number_value *pnv;
369
370 if (! prop_object_is_number(pn))
371 return (0);
372
373 pnv = &pn->pn_value;
374
375 if (pnv->pnv_is_unsigned) {
376 if (pnv->pnv_unsigned > UINT32_MAX)
377 return (64);
378 if (pnv->pnv_unsigned > UINT16_MAX)
379 return (32);
380 if (pnv->pnv_unsigned > UINT8_MAX)
381 return (16);
382 return (8);
383 }
384
385 if (pnv->pnv_signed > INT32_MAX || pnv->pnv_signed < INT32_MIN)
386 return (64);
387 if (pnv->pnv_signed > INT16_MAX || pnv->pnv_signed < INT16_MIN)
388 return (32);
389 if (pnv->pnv_signed > INT8_MAX || pnv->pnv_signed < INT8_MIN)
390 return (16);
391 return (8);
392 }
393
394 /*
395 * prop_number_integer_value --
396 * Get the integer value of a prop_number_t.
397 */
398 int64_t
399 prop_number_integer_value(prop_number_t pn)
400 {
401
402 /*
403 * XXX Impossible to distinguish between "not a prop_number_t"
404 * XXX and "prop_number_t has a value of 0".
405 */
406 if (! prop_object_is_number(pn))
407 return (0);
408
409 return (pn->pn_value.pnv_signed);
410 }
411
412 /*
413 * prop_number_unsigned_integer_value --
414 * Get the unsigned integer value of a prop_number_t.
415 */
416 uint64_t
417 prop_number_unsigned_integer_value(prop_number_t pn)
418 {
419
420 /*
421 * XXX Impossible to distinguish between "not a prop_number_t"
422 * XXX and "prop_number_t has a value of 0".
423 */
424 if (! prop_object_is_number(pn))
425 return (0);
426
427 return (pn->pn_value.pnv_unsigned);
428 }
429
430 /*
431 * prop_number_equals --
432 * Return TRUE if two numbers are equivalent.
433 */
434 boolean_t
435 prop_number_equals(prop_number_t num1, prop_number_t num2)
436 {
437
438 return (_prop_number_equals(num1, num2));
439 }
440
441 /*
442 * prop_number_equals_integer --
443 * Return TRUE if the number is equivalent to the specified integer.
444 */
445 boolean_t
446 prop_number_equals_integer(prop_number_t pn, int64_t val)
447 {
448
449 if (! prop_object_is_number(pn))
450 return (FALSE);
451
452 if (pn->pn_value.pnv_is_unsigned &&
453 (pn->pn_value.pnv_unsigned > INT64_MAX || val < 0))
454 return (FALSE);
455
456 return (pn->pn_value.pnv_signed == val);
457 }
458
459 /*
460 * prop_number_equals_unsigned_integer --
461 * Return TRUE if the number is equivalent to the specified
462 * unsigned integer.
463 */
464 boolean_t
465 prop_number_equals_unsigned_integer(prop_number_t pn, uint64_t val)
466 {
467
468 if (! prop_object_is_number(pn))
469 return (FALSE);
470
471 if (! pn->pn_value.pnv_is_unsigned &&
472 (pn->pn_value.pnv_signed < 0 || val > INT64_MAX))
473 return (FALSE);
474
475 return (pn->pn_value.pnv_unsigned == val);
476 }
477
478 static boolean_t
479 _prop_number_internalize_unsigned(struct _prop_object_internalize_context *ctx,
480 struct _prop_number_value *pnv)
481 {
482 char *cp;
483
484 _PROP_ASSERT(sizeof(unsigned long long) == sizeof(uint64_t));
485
486 #ifndef _KERNEL
487 errno = 0;
488 #endif
489 pnv->pnv_unsigned = (uint64_t) strtoull(ctx->poic_cp, &cp, 0);
490 #ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */
491 if (pnv->pnv_unsigned == UINT64_MAX && errno == ERANGE)
492 return (FALSE);
493 #endif
494 pnv->pnv_is_unsigned = TRUE;
495 ctx->poic_cp = cp;
496
497 return (TRUE);
498 }
499
500 static boolean_t
501 _prop_number_internalize_signed(struct _prop_object_internalize_context *ctx,
502 struct _prop_number_value *pnv)
503 {
504 char *cp;
505
506 _PROP_ASSERT(sizeof(long long) == sizeof(int64_t));
507
508 #ifndef _KERNEL
509 errno = 0;
510 #endif
511 pnv->pnv_signed = (int64_t) strtoll(ctx->poic_cp, &cp, 0);
512 #ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */
513 if ((pnv->pnv_signed == INT64_MAX || pnv->pnv_signed == INT64_MIN) &&
514 errno == ERANGE)
515 return (FALSE);
516 #endif
517 pnv->pnv_is_unsigned = FALSE;
518 ctx->poic_cp = cp;
519
520 return (TRUE);
521 }
522
523 /*
524 * _prop_number_internalize --
525 * Parse a <number>...</number> and return the object created from
526 * the external representation.
527 */
528 prop_object_t
529 _prop_number_internalize(struct _prop_object_internalize_context *ctx)
530 {
531 struct _prop_number_value pnv = {
532 .pnv_unsigned = 0,
533 .pnv_is_unsigned = FALSE,
534 };
535
536 /* No attributes, no empty elements. */
537 if (ctx->poic_tagattr != NULL || ctx->poic_is_empty_element)
538 return (NULL);
539
540 /*
541 * If the first character is '-', then we treat as signed.
542 * If the first two characters are "0x" (i.e. the number is
543 * in hex), then we treat as unsigned. Otherwise, we try
544 * signed first, and if that fails (presumably due to ERANGE),
545 * then we switch to unsigned.
546 */
547 if (ctx->poic_cp[0] == '-') {
548 if (_prop_number_internalize_signed(ctx, &pnv) == FALSE)
549 return (NULL);
550 } else if (ctx->poic_cp[0] == '0' && ctx->poic_cp[1] == 'x') {
551 if (_prop_number_internalize_unsigned(ctx, &pnv) == FALSE)
552 return (NULL);
553 } else {
554 if (_prop_number_internalize_signed(ctx, &pnv) == FALSE &&
555 _prop_number_internalize_unsigned(ctx, &pnv) == FALSE)
556 return (NULL);
557 }
558
559 if (_prop_object_internalize_find_tag(ctx, "integer",
560 _PROP_TAG_TYPE_END) == FALSE)
561 return (NULL);
562
563 return (_prop_number_alloc(&pnv));
564 }
565