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