prop_number.c revision 1.6 1 1.6 thorpej /* $NetBSD: prop_number.c,v 1.6 2006/09/09 06:59:28 thorpej 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.1 thorpej #include <prop/prop_number.h>
40 1.1 thorpej #include "prop_object_impl.h"
41 1.6 thorpej #include "prop_rb_impl.h"
42 1.1 thorpej
43 1.1 thorpej #if defined(_KERNEL)
44 1.1 thorpej #include <sys/systm.h>
45 1.1 thorpej #elif defined(_STANDALONE)
46 1.1 thorpej #include <sys/param.h>
47 1.1 thorpej #include <lib/libkern/libkern.h>
48 1.1 thorpej #else
49 1.1 thorpej #include <errno.h>
50 1.1 thorpej #include <stdlib.h>
51 1.1 thorpej #endif
52 1.1 thorpej
53 1.1 thorpej struct _prop_number {
54 1.1 thorpej struct _prop_object pn_obj;
55 1.6 thorpej struct rb_node pn_link;
56 1.3 thorpej uint64_t pn_number;
57 1.1 thorpej };
58 1.1 thorpej
59 1.6 thorpej #define RBNODE_TO_PN(n) \
60 1.6 thorpej ((struct _prop_number *) \
61 1.6 thorpej ((uintptr_t)n - offsetof(struct _prop_number, pn_link)))
62 1.6 thorpej
63 1.1 thorpej _PROP_POOL_INIT(_prop_number_pool, sizeof(struct _prop_number), "propnmbr")
64 1.1 thorpej
65 1.2 thorpej static void _prop_number_free(void *);
66 1.2 thorpej static boolean_t _prop_number_externalize(
67 1.2 thorpej struct _prop_object_externalize_context *,
68 1.2 thorpej void *);
69 1.2 thorpej static boolean_t _prop_number_equals(void *, void *);
70 1.2 thorpej
71 1.2 thorpej static const struct _prop_object_type _prop_object_type_number = {
72 1.2 thorpej .pot_type = PROP_TYPE_NUMBER,
73 1.2 thorpej .pot_free = _prop_number_free,
74 1.2 thorpej .pot_extern = _prop_number_externalize,
75 1.2 thorpej .pot_equals = _prop_number_equals,
76 1.2 thorpej };
77 1.2 thorpej
78 1.2 thorpej #define prop_object_is_number(x) \
79 1.5 thorpej ((x) != NULL && (x)->pn_obj.po_type == &_prop_object_type_number)
80 1.1 thorpej
81 1.6 thorpej /*
82 1.6 thorpej * Number objects are immutable, and we are likely to have many number
83 1.6 thorpej * objects that have the same value. So, to save memory, we unique'ify
84 1.6 thorpej * numbers so we only have one copy of each.
85 1.6 thorpej */
86 1.6 thorpej
87 1.6 thorpej static int
88 1.6 thorpej _prop_number_rb_compare_nodes(const struct rb_node *n1,
89 1.6 thorpej const struct rb_node *n2)
90 1.6 thorpej {
91 1.6 thorpej const prop_number_t pn1 = RBNODE_TO_PN(n1);
92 1.6 thorpej const prop_number_t pn2 = RBNODE_TO_PN(n2);
93 1.6 thorpej
94 1.6 thorpej if (pn1->pn_number < pn2->pn_number)
95 1.6 thorpej return (-1);
96 1.6 thorpej if (pn1->pn_number > pn2->pn_number)
97 1.6 thorpej return (1);
98 1.6 thorpej return (0);
99 1.6 thorpej }
100 1.6 thorpej
101 1.6 thorpej static int
102 1.6 thorpej _prop_number_rb_compare_key(const struct rb_node *n,
103 1.6 thorpej const void *v)
104 1.6 thorpej {
105 1.6 thorpej const prop_number_t pn = RBNODE_TO_PN(n);
106 1.6 thorpej const uint64_t *valp = v;
107 1.6 thorpej
108 1.6 thorpej if (pn->pn_number < *valp)
109 1.6 thorpej return (-1);
110 1.6 thorpej if (pn->pn_number > *valp)
111 1.6 thorpej return (1);
112 1.6 thorpej return (0);
113 1.6 thorpej }
114 1.6 thorpej
115 1.6 thorpej static const struct rb_tree_ops _prop_number_rb_tree_ops = {
116 1.6 thorpej .rbto_compare_nodes = _prop_number_rb_compare_nodes,
117 1.6 thorpej .rbto_compare_key = _prop_number_rb_compare_key,
118 1.6 thorpej };
119 1.6 thorpej
120 1.6 thorpej static struct rb_tree _prop_number_tree;
121 1.6 thorpej static boolean_t _prop_number_tree_initialized;
122 1.6 thorpej
123 1.6 thorpej _PROP_MUTEX_DECL(_prop_number_tree_mutex)
124 1.6 thorpej
125 1.1 thorpej static void
126 1.1 thorpej _prop_number_free(void *v)
127 1.1 thorpej {
128 1.6 thorpej prop_number_t pn = v;
129 1.6 thorpej
130 1.6 thorpej _PROP_MUTEX_LOCK(_prop_number_tree_mutex);
131 1.6 thorpej _prop_rb_tree_remove_node(&_prop_number_tree, &pn->pn_link);
132 1.6 thorpej _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
133 1.1 thorpej
134 1.6 thorpej _PROP_POOL_PUT(_prop_number_pool, pn);
135 1.1 thorpej }
136 1.1 thorpej
137 1.1 thorpej static boolean_t
138 1.1 thorpej _prop_number_externalize(struct _prop_object_externalize_context *ctx,
139 1.1 thorpej void *v)
140 1.1 thorpej {
141 1.1 thorpej prop_number_t pn = v;
142 1.1 thorpej char tmpstr[32];
143 1.1 thorpej
144 1.3 thorpej sprintf(tmpstr, "0x%" PRIx64, pn->pn_number);
145 1.1 thorpej
146 1.1 thorpej if (_prop_object_externalize_start_tag(ctx, "integer") == FALSE ||
147 1.1 thorpej _prop_object_externalize_append_cstring(ctx, tmpstr) == FALSE ||
148 1.1 thorpej _prop_object_externalize_end_tag(ctx, "integer") == FALSE)
149 1.1 thorpej return (FALSE);
150 1.1 thorpej
151 1.1 thorpej return (TRUE);
152 1.1 thorpej }
153 1.1 thorpej
154 1.2 thorpej static boolean_t
155 1.2 thorpej _prop_number_equals(void *v1, void *v2)
156 1.2 thorpej {
157 1.2 thorpej prop_number_t num1 = v1;
158 1.2 thorpej prop_number_t num2 = v2;
159 1.2 thorpej
160 1.4 thorpej if (! (prop_object_is_number(num1) &&
161 1.4 thorpej prop_object_is_number(num2)))
162 1.4 thorpej return (FALSE);
163 1.4 thorpej
164 1.6 thorpej /*
165 1.6 thorpej * There is only ever one copy of a number object at any given
166 1.6 thorpej * time, so we can reduce this to a simple pointer equality check.
167 1.6 thorpej */
168 1.6 thorpej return (num1 == num2);
169 1.2 thorpej }
170 1.2 thorpej
171 1.1 thorpej static prop_number_t
172 1.3 thorpej _prop_number_alloc(uint64_t val)
173 1.1 thorpej {
174 1.6 thorpej prop_number_t opn, pn;
175 1.6 thorpej struct rb_node *n;
176 1.6 thorpej
177 1.6 thorpej /*
178 1.6 thorpej * Check to see if this already exists in the tree. If it does,
179 1.6 thorpej * we just retain it and return it.
180 1.6 thorpej */
181 1.6 thorpej _PROP_MUTEX_LOCK(_prop_number_tree_mutex);
182 1.6 thorpej if (! _prop_number_tree_initialized) {
183 1.6 thorpej _prop_rb_tree_init(&_prop_number_tree,
184 1.6 thorpej &_prop_number_rb_tree_ops);
185 1.6 thorpej _prop_number_tree_initialized = TRUE;
186 1.6 thorpej } else {
187 1.6 thorpej n = _prop_rb_tree_find(&_prop_number_tree, &val);
188 1.6 thorpej if (n != NULL) {
189 1.6 thorpej opn = RBNODE_TO_PN(n);
190 1.6 thorpej prop_object_retain(opn);
191 1.6 thorpej _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
192 1.6 thorpej return (opn);
193 1.6 thorpej }
194 1.6 thorpej }
195 1.6 thorpej _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
196 1.6 thorpej
197 1.6 thorpej /*
198 1.6 thorpej * Not in the tree. Create it now.
199 1.6 thorpej */
200 1.1 thorpej
201 1.1 thorpej pn = _PROP_POOL_GET(_prop_number_pool);
202 1.6 thorpej if (pn == NULL)
203 1.6 thorpej return (NULL);
204 1.6 thorpej
205 1.6 thorpej _prop_object_init(&pn->pn_obj, &_prop_object_type_number);
206 1.1 thorpej
207 1.6 thorpej pn->pn_number = val;
208 1.6 thorpej
209 1.6 thorpej /*
210 1.6 thorpej * We dropped the mutex when we allocated the new object, so
211 1.6 thorpej * we have to check again if it is in the tree.
212 1.6 thorpej */
213 1.6 thorpej _PROP_MUTEX_LOCK(_prop_number_tree_mutex);
214 1.6 thorpej n = _prop_rb_tree_find(&_prop_number_tree, &val);
215 1.6 thorpej if (n != NULL) {
216 1.6 thorpej opn = RBNODE_TO_PN(n);
217 1.6 thorpej prop_object_retain(opn);
218 1.6 thorpej _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
219 1.6 thorpej _PROP_POOL_PUT(_prop_number_pool, pn);
220 1.6 thorpej return (opn);
221 1.1 thorpej }
222 1.6 thorpej _prop_rb_tree_insert_node(&_prop_number_tree, &pn->pn_link);
223 1.6 thorpej _PROP_MUTEX_UNLOCK(_prop_number_tree_mutex);
224 1.1 thorpej return (pn);
225 1.1 thorpej }
226 1.1 thorpej
227 1.1 thorpej /*
228 1.1 thorpej * prop_number_create_integer --
229 1.1 thorpej * Create a prop_number_t and initialize it with the
230 1.1 thorpej * provided integer value.
231 1.1 thorpej */
232 1.1 thorpej prop_number_t
233 1.3 thorpej prop_number_create_integer(uint64_t val)
234 1.1 thorpej {
235 1.1 thorpej
236 1.1 thorpej return (_prop_number_alloc(val));
237 1.1 thorpej }
238 1.1 thorpej
239 1.1 thorpej /*
240 1.1 thorpej * prop_number_copy --
241 1.1 thorpej * Copy a prop_number_t.
242 1.1 thorpej */
243 1.1 thorpej prop_number_t
244 1.1 thorpej prop_number_copy(prop_number_t opn)
245 1.1 thorpej {
246 1.1 thorpej
247 1.4 thorpej if (! prop_object_is_number(opn))
248 1.4 thorpej return (NULL);
249 1.1 thorpej
250 1.6 thorpej /*
251 1.6 thorpej * Because we only ever allocate one object for any given
252 1.6 thorpej * value, this can be reduced to a simple retain operation.
253 1.6 thorpej */
254 1.6 thorpej prop_object_retain(opn);
255 1.6 thorpej return (opn);
256 1.1 thorpej }
257 1.1 thorpej
258 1.1 thorpej /*
259 1.1 thorpej * prop_number_size --
260 1.1 thorpej * Return the size, in bits, required to hold the value of
261 1.1 thorpej * the specified number.
262 1.1 thorpej */
263 1.1 thorpej int
264 1.1 thorpej prop_number_size(prop_number_t pn)
265 1.1 thorpej {
266 1.1 thorpej
267 1.4 thorpej if (! prop_object_is_number(pn))
268 1.4 thorpej return (0);
269 1.4 thorpej
270 1.1 thorpej if (pn->pn_number > UINT32_MAX)
271 1.1 thorpej return (64);
272 1.1 thorpej if (pn->pn_number > UINT16_MAX)
273 1.1 thorpej return (32);
274 1.1 thorpej if (pn->pn_number > UINT8_MAX)
275 1.1 thorpej return (16);
276 1.1 thorpej return (8);
277 1.1 thorpej }
278 1.1 thorpej
279 1.1 thorpej /*
280 1.1 thorpej * prop_number_integer_value --
281 1.1 thorpej * Get the integer value of a prop_number_t.
282 1.1 thorpej */
283 1.3 thorpej uint64_t
284 1.1 thorpej prop_number_integer_value(prop_number_t pn)
285 1.1 thorpej {
286 1.1 thorpej
287 1.4 thorpej /*
288 1.4 thorpej * XXX Impossible to distinguish between "not a prop_number_t"
289 1.4 thorpej * XXX and "prop_number_t has a value of 0".
290 1.4 thorpej */
291 1.4 thorpej if (! prop_object_is_number(pn))
292 1.4 thorpej return (0);
293 1.4 thorpej
294 1.1 thorpej return (pn->pn_number);
295 1.1 thorpej }
296 1.1 thorpej
297 1.1 thorpej /*
298 1.1 thorpej * prop_number_equals --
299 1.1 thorpej * Return TRUE if two numbers are equivalent.
300 1.1 thorpej */
301 1.1 thorpej boolean_t
302 1.1 thorpej prop_number_equals(prop_number_t num1, prop_number_t num2)
303 1.1 thorpej {
304 1.1 thorpej
305 1.2 thorpej return (_prop_number_equals(num1, num2));
306 1.1 thorpej }
307 1.1 thorpej
308 1.1 thorpej /*
309 1.1 thorpej * prop_number_equals_integer --
310 1.1 thorpej * Return TRUE if the number is equivalent to the specified integer.
311 1.1 thorpej */
312 1.1 thorpej boolean_t
313 1.3 thorpej prop_number_equals_integer(prop_number_t pn, uint64_t val)
314 1.1 thorpej {
315 1.1 thorpej
316 1.4 thorpej if (! prop_object_is_number(pn))
317 1.4 thorpej return (FALSE);
318 1.4 thorpej
319 1.1 thorpej return (pn->pn_number == val);
320 1.1 thorpej }
321 1.1 thorpej
322 1.1 thorpej /*
323 1.1 thorpej * _prop_number_internalize --
324 1.1 thorpej * Parse a <number>...</number> and return the object created from
325 1.1 thorpej * the external representation.
326 1.1 thorpej */
327 1.1 thorpej prop_object_t
328 1.1 thorpej _prop_number_internalize(struct _prop_object_internalize_context *ctx)
329 1.1 thorpej {
330 1.3 thorpej uint64_t val = 0;
331 1.1 thorpej char *cp;
332 1.1 thorpej
333 1.1 thorpej /* No attributes, no empty elements. */
334 1.1 thorpej if (ctx->poic_tagattr != NULL || ctx->poic_is_empty_element)
335 1.1 thorpej return (NULL);
336 1.1 thorpej
337 1.1 thorpej #ifndef _KERNEL
338 1.1 thorpej errno = 0;
339 1.1 thorpej #endif
340 1.1 thorpej val = strtoumax(ctx->poic_cp, &cp, 0);
341 1.1 thorpej #ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */
342 1.1 thorpej if (val == UINTMAX_MAX && errno == ERANGE)
343 1.1 thorpej return (NULL);
344 1.1 thorpej #endif
345 1.1 thorpej ctx->poic_cp = cp;
346 1.1 thorpej
347 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, "integer",
348 1.1 thorpej _PROP_TAG_TYPE_END) == FALSE)
349 1.1 thorpej return (NULL);
350 1.1 thorpej
351 1.1 thorpej return (_prop_number_alloc(val));
352 1.1 thorpej }
353