linux_idr.c revision 1.1.2.3 1 1.1.2.1 riastrad /* $NetBSD: linux_idr.c,v 1.1.2.3 2013/07/24 02:07:09 riastradh Exp $ */
2 1.1.2.1 riastrad
3 1.1.2.1 riastrad /*-
4 1.1.2.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1.2.1 riastrad * All rights reserved.
6 1.1.2.1 riastrad *
7 1.1.2.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 riastrad * by Taylor R. Campbell.
9 1.1.2.1 riastrad *
10 1.1.2.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 riastrad * modification, are permitted provided that the following conditions
12 1.1.2.1 riastrad * are met:
13 1.1.2.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1.2.1 riastrad *
19 1.1.2.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 riastrad */
31 1.1.2.1 riastrad
32 1.1.2.1 riastrad #include <sys/cdefs.h>
33 1.1.2.1 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_idr.c,v 1.1.2.3 2013/07/24 02:07:09 riastradh Exp $");
34 1.1.2.1 riastrad
35 1.1.2.1 riastrad #include <sys/param.h>
36 1.1.2.1 riastrad #include <sys/atomic.h>
37 1.1.2.1 riastrad #include <sys/kmem.h>
38 1.1.2.1 riastrad #include <sys/rbtree.h>
39 1.1.2.1 riastrad
40 1.1.2.1 riastrad #include <linux/idr.h>
41 1.1.2.1 riastrad
42 1.1.2.1 riastrad struct idr_node {
43 1.1.2.1 riastrad rb_node_t in_rb_node;
44 1.1.2.1 riastrad int in_index;
45 1.1.2.1 riastrad void *in_data;
46 1.1.2.1 riastrad };
47 1.1.2.1 riastrad
48 1.1.2.1 riastrad static signed int idr_tree_compare_nodes(void *, const void *, const void *);
49 1.1.2.1 riastrad static signed int idr_tree_compare_key(void *, const void *, const void *);
50 1.1.2.1 riastrad
51 1.1.2.1 riastrad static const rb_tree_ops_t idr_rb_ops = {
52 1.1.2.1 riastrad .rbto_compare_nodes = &idr_tree_compare_nodes,
53 1.1.2.1 riastrad .rbto_compare_key = &idr_tree_compare_key,
54 1.1.2.1 riastrad .rbto_node_offset = offsetof(struct idr_node, in_rb_node),
55 1.1.2.1 riastrad .rbto_context = NULL,
56 1.1.2.1 riastrad };
57 1.1.2.1 riastrad
58 1.1.2.1 riastrad static signed int
59 1.1.2.1 riastrad idr_tree_compare_nodes(void *ctx __unused, const void *na, const void *nb)
60 1.1.2.1 riastrad {
61 1.1.2.1 riastrad const int a = ((const struct idr_node *)na)->in_index;
62 1.1.2.1 riastrad const int b = ((const struct idr_node *)nb)->in_index;
63 1.1.2.1 riastrad
64 1.1.2.1 riastrad if (a < b)
65 1.1.2.1 riastrad return -1;
66 1.1.2.1 riastrad else if (b < a)
67 1.1.2.1 riastrad return +1;
68 1.1.2.1 riastrad else
69 1.1.2.1 riastrad return 0;
70 1.1.2.1 riastrad }
71 1.1.2.1 riastrad
72 1.1.2.1 riastrad static signed int
73 1.1.2.1 riastrad idr_tree_compare_key(void *ctx __unused, const void *n, const void *key)
74 1.1.2.1 riastrad {
75 1.1.2.1 riastrad const int a = ((const struct idr_node *)n)->in_index;
76 1.1.2.1 riastrad const int b = *(const int *)key;
77 1.1.2.1 riastrad
78 1.1.2.1 riastrad if (a < b)
79 1.1.2.1 riastrad return -1;
80 1.1.2.1 riastrad else if (b < a)
81 1.1.2.1 riastrad return +1;
82 1.1.2.1 riastrad else
83 1.1.2.1 riastrad return 0;
84 1.1.2.1 riastrad }
85 1.1.2.1 riastrad
86 1.1.2.1 riastrad void
87 1.1.2.1 riastrad idr_init(struct idr *idr)
88 1.1.2.1 riastrad {
89 1.1.2.1 riastrad
90 1.1.2.1 riastrad rw_init(&idr->idr_lock);
91 1.1.2.1 riastrad rb_tree_init(&idr->idr_tree, &idr_rb_ops);
92 1.1.2.1 riastrad idr->idr_temp = NULL;
93 1.1.2.1 riastrad }
94 1.1.2.1 riastrad
95 1.1.2.1 riastrad void
96 1.1.2.1 riastrad idr_destroy(struct idr *idr)
97 1.1.2.1 riastrad {
98 1.1.2.1 riastrad
99 1.1.2.1 riastrad if (idr->idr_temp != NULL) {
100 1.1.2.1 riastrad /* XXX Probably shouldn't happen. */
101 1.1.2.1 riastrad kmem_free(idr->idr_temp, sizeof(*idr->idr_temp));
102 1.1.2.1 riastrad idr->idr_temp = NULL;
103 1.1.2.1 riastrad }
104 1.1.2.1 riastrad #if 0 /* XXX No rb_tree_destroy? */
105 1.1.2.1 riastrad rb_tree_destroy(&idr->idr_tree);
106 1.1.2.1 riastrad #endif
107 1.1.2.1 riastrad rw_destroy(&idr->idr_lock);
108 1.1.2.1 riastrad }
109 1.1.2.1 riastrad
110 1.1.2.1 riastrad void *
111 1.1.2.1 riastrad idr_find(struct idr *idr, int id)
112 1.1.2.1 riastrad {
113 1.1.2.1 riastrad const struct idr_node *node;
114 1.1.2.1 riastrad void *data;
115 1.1.2.1 riastrad
116 1.1.2.1 riastrad rw_enter(&idr->idr_lock, RW_READER);
117 1.1.2.1 riastrad node = rb_tree_find_node(&idr->idr_tree, &id);
118 1.1.2.1 riastrad data = (node == NULL? NULL : node->in_data);
119 1.1.2.1 riastrad rw_exit(&idr->idr_lock);
120 1.1.2.1 riastrad
121 1.1.2.1 riastrad return data;
122 1.1.2.1 riastrad }
123 1.1.2.1 riastrad
124 1.1.2.1 riastrad void
125 1.1.2.1 riastrad idr_remove(struct idr *idr, int id)
126 1.1.2.1 riastrad {
127 1.1.2.1 riastrad struct idr_node *node;
128 1.1.2.1 riastrad
129 1.1.2.1 riastrad rw_enter(&idr->idr_lock, RW_WRITER);
130 1.1.2.1 riastrad node = rb_tree_find_node(&idr->idr_tree, &id);
131 1.1.2.1 riastrad KASSERT(node != NULL);
132 1.1.2.1 riastrad rb_tree_remove_node(&idr->idr_tree, node);
133 1.1.2.1 riastrad rw_exit(&idr->idr_lock);
134 1.1.2.1 riastrad kmem_free(node, sizeof(*node));
135 1.1.2.1 riastrad }
136 1.1.2.1 riastrad
137 1.1.2.3 riastrad void
138 1.1.2.3 riastrad idr_remove_all(struct idr *idr)
139 1.1.2.3 riastrad {
140 1.1.2.3 riastrad struct idr_node *node;
141 1.1.2.3 riastrad
142 1.1.2.3 riastrad rw_enter(&idr->idr_lock, RW_WRITER);
143 1.1.2.3 riastrad while ((node = rb_tree_iterate(&idr->idr_tree, NULL, RB_DIR_RIGHT)) !=
144 1.1.2.3 riastrad NULL) {
145 1.1.2.3 riastrad rb_tree_remove_node(&idr->idr_tree, node);
146 1.1.2.3 riastrad rw_exit(&idr->idr_lock);
147 1.1.2.3 riastrad kmem_free(node, sizeof(*node));
148 1.1.2.3 riastrad rw_enter(&idr->idr_lock, RW_WRITER);
149 1.1.2.3 riastrad }
150 1.1.2.3 riastrad rw_exit(&idr->idr_lock);
151 1.1.2.3 riastrad }
152 1.1.2.3 riastrad
153 1.1.2.1 riastrad int
154 1.1.2.1 riastrad idr_pre_get(struct idr *idr, int flags __unused /* XXX */)
155 1.1.2.1 riastrad {
156 1.1.2.1 riastrad struct idr_node *const temp = kmem_alloc(sizeof(*temp), KM_SLEEP);
157 1.1.2.1 riastrad
158 1.1.2.1 riastrad if (temp == NULL)
159 1.1.2.1 riastrad return 0;
160 1.1.2.1 riastrad
161 1.1.2.2 riastrad rw_enter(&idr->idr_lock, RW_WRITER);
162 1.1.2.2 riastrad if (idr->idr_temp == NULL)
163 1.1.2.2 riastrad idr->idr_temp = temp;
164 1.1.2.2 riastrad else
165 1.1.2.1 riastrad kmem_free(temp, sizeof(*temp));
166 1.1.2.2 riastrad rw_exit(&idr->idr_lock);
167 1.1.2.1 riastrad
168 1.1.2.1 riastrad return 1;
169 1.1.2.1 riastrad }
170 1.1.2.1 riastrad
171 1.1.2.1 riastrad int
172 1.1.2.1 riastrad idr_get_new_above(struct idr *idr, void *data, int min_id, int *id)
173 1.1.2.1 riastrad {
174 1.1.2.1 riastrad struct idr_node *node, *search, *collision __unused;
175 1.1.2.1 riastrad int want_id = min_id;
176 1.1.2.1 riastrad
177 1.1.2.1 riastrad rw_enter(&idr->idr_lock, RW_WRITER);
178 1.1.2.1 riastrad
179 1.1.2.2 riastrad node = idr->idr_temp;
180 1.1.2.2 riastrad if (node == NULL) {
181 1.1.2.2 riastrad rw_exit(&idr->idr_lock);
182 1.1.2.2 riastrad return -EAGAIN;
183 1.1.2.2 riastrad }
184 1.1.2.2 riastrad idr->idr_temp = NULL;
185 1.1.2.2 riastrad
186 1.1.2.1 riastrad search = rb_tree_find_node_geq(&idr->idr_tree, &min_id);
187 1.1.2.1 riastrad while ((search != NULL) && (search->in_index == want_id)) {
188 1.1.2.1 riastrad search = rb_tree_iterate(&idr->idr_tree, search, RB_DIR_RIGHT);
189 1.1.2.1 riastrad want_id++;
190 1.1.2.1 riastrad }
191 1.1.2.1 riastrad
192 1.1.2.1 riastrad node->in_index = want_id;
193 1.1.2.1 riastrad node->in_data = data;
194 1.1.2.1 riastrad
195 1.1.2.1 riastrad collision = rb_tree_insert_node(&idr->idr_tree, node);
196 1.1.2.1 riastrad KASSERT(collision == node);
197 1.1.2.1 riastrad
198 1.1.2.1 riastrad rw_exit(&idr->idr_lock);
199 1.1.2.1 riastrad
200 1.1.2.1 riastrad return 0;
201 1.1.2.1 riastrad }
202