1 /**************************************************************************
2 *
3 * Copyright 2017 Valve Corporation
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * A simple allocator that allocates and release "numbers".
31 *
32 * @author Samuel Pitoiset <samuel.pitoiset@gmail.com>
33 */
34
35 #include "util/u_idalloc.h"
36 #include "util/u_math.h"
37 #include <stdlib.h>
38
39 static void
util_idalloc_resize(struct util_idalloc * buf,unsigned new_num_elements)40 util_idalloc_resize(struct util_idalloc *buf, unsigned new_num_elements)
41 {
42 if (new_num_elements > buf->num_elements) {
43 buf->data = realloc(buf->data, new_num_elements * sizeof(*buf->data));
44 memset(&buf->data[buf->num_elements], 0,
45 (new_num_elements - buf->num_elements) * sizeof(*buf->data));
46 buf->num_elements = new_num_elements;
47 }
48 }
49
50 void
util_idalloc_init(struct util_idalloc * buf,unsigned initial_num_ids)51 util_idalloc_init(struct util_idalloc *buf, unsigned initial_num_ids)
52 {
53 memset(buf, 0, sizeof(*buf));
54 assert(initial_num_ids);
55 util_idalloc_resize(buf, DIV_ROUND_UP(initial_num_ids, 32));
56 }
57
58 void
util_idalloc_fini(struct util_idalloc * buf)59 util_idalloc_fini(struct util_idalloc *buf)
60 {
61 if (buf->data)
62 free(buf->data);
63 }
64
65 unsigned
util_idalloc_alloc(struct util_idalloc * buf)66 util_idalloc_alloc(struct util_idalloc *buf)
67 {
68 unsigned num_elements = buf->num_elements;
69
70 for (unsigned i = buf->lowest_free_idx; i < num_elements; i++) {
71 if (buf->data[i] == 0xffffffff)
72 continue;
73
74 unsigned bit = ffs(~buf->data[i]) - 1;
75 buf->data[i] |= 1u << bit;
76 buf->lowest_free_idx = i;
77 buf->num_set_elements = MAX2(buf->num_set_elements, i + 1);
78 return i * 32 + bit;
79 }
80
81 /* No slots available, resize and return the first free. */
82 util_idalloc_resize(buf, MAX2(num_elements, 1) * 2);
83
84 buf->lowest_free_idx = num_elements;
85 buf->data[num_elements] |= 1;
86 buf->num_set_elements = MAX2(buf->num_set_elements, num_elements + 1);
87 return num_elements * 32;
88 }
89
90 static unsigned
find_free_block(struct util_idalloc * buf,unsigned start)91 find_free_block(struct util_idalloc *buf, unsigned start)
92 {
93 for (unsigned i = start; i < buf->num_elements; i++) {
94 if (!buf->data[i])
95 return i;
96 }
97 return buf->num_elements;
98 }
99
100 /* Allocate a range of consecutive IDs. Return the first ID. */
101 unsigned
util_idalloc_alloc_range(struct util_idalloc * buf,unsigned num)102 util_idalloc_alloc_range(struct util_idalloc *buf, unsigned num)
103 {
104 if (num == 1)
105 return util_idalloc_alloc(buf);
106
107 unsigned num_alloc = DIV_ROUND_UP(num, 32);
108 unsigned num_elements = buf->num_elements;
109 unsigned base = find_free_block(buf, buf->lowest_free_idx);
110
111 while (1) {
112 unsigned i;
113 for (i = base;
114 i < num_elements && i - base < num_alloc && !buf->data[i]; i++);
115
116 if (i - base == num_alloc)
117 goto ret; /* found */
118
119 if (i == num_elements)
120 break; /* not found */
121
122 /* continue searching */
123 base = !buf->data[i] ? i : i + 1;
124 }
125
126 /* No slots available, allocate more. */
127 util_idalloc_resize(buf, num_elements * 2 + num_alloc);
128
129 ret:
130 /* Mark the bits as used. */
131 for (unsigned i = base; i < base + num_alloc - (num % 32 != 0); i++)
132 buf->data[i] = 0xffffffff;
133 if (num % 32 != 0)
134 buf->data[base + num_alloc - 1] |= BITFIELD_MASK(num % 32);
135
136 if (buf->lowest_free_idx == base)
137 buf->lowest_free_idx = base + num / 32;
138
139 buf->num_set_elements = MAX2(buf->num_set_elements, base + num_alloc);
140
141 /* Validate this algorithm. */
142 for (unsigned i = 0; i < num; i++)
143 assert(util_idalloc_exists(buf, base * 32 + i));
144
145 return base * 32;
146 }
147
148 void
util_idalloc_free(struct util_idalloc * buf,unsigned id)149 util_idalloc_free(struct util_idalloc *buf, unsigned id)
150 {
151 unsigned idx = id / 32;
152
153 if (idx >= buf->num_elements)
154 return;
155
156 buf->lowest_free_idx = MIN2(idx, buf->lowest_free_idx);
157 buf->data[idx] &= ~(1 << (id % 32));
158
159 /* Decrease num_used to the last used element + 1. */
160 if (buf->num_set_elements == idx + 1) {
161 while (buf->num_set_elements > 0 && !buf->data[buf->num_set_elements - 1])
162 buf->num_set_elements--;
163 }
164 }
165
166 void
util_idalloc_reserve(struct util_idalloc * buf,unsigned id)167 util_idalloc_reserve(struct util_idalloc *buf, unsigned id)
168 {
169 unsigned idx = id / 32;
170
171 if (idx >= buf->num_elements)
172 util_idalloc_resize(buf, (idx + 1) * 2);
173 buf->data[idx] |= 1u << (id % 32);
174 buf->num_set_elements = MAX2(buf->num_set_elements, idx + 1);
175 }
176
177 void
util_idalloc_mt_init(struct util_idalloc_mt * buf,unsigned initial_num_ids,bool skip_zero)178 util_idalloc_mt_init(struct util_idalloc_mt *buf,
179 unsigned initial_num_ids, bool skip_zero)
180 {
181 simple_mtx_init(&buf->mutex, mtx_plain);
182 util_idalloc_init(&buf->buf, initial_num_ids);
183 buf->skip_zero = skip_zero;
184
185 if (skip_zero) {
186 ASSERTED unsigned zero = util_idalloc_alloc(&buf->buf);
187 assert(zero == 0);
188 }
189 }
190
191 /* Callback for drivers using u_threaded_context (abbreviated as tc). */
192 void
util_idalloc_mt_init_tc(struct util_idalloc_mt * buf)193 util_idalloc_mt_init_tc(struct util_idalloc_mt *buf)
194 {
195 util_idalloc_mt_init(buf, 1 << 16, true);
196 }
197
198 void
util_idalloc_mt_fini(struct util_idalloc_mt * buf)199 util_idalloc_mt_fini(struct util_idalloc_mt *buf)
200 {
201 util_idalloc_fini(&buf->buf);
202 simple_mtx_destroy(&buf->mutex);
203 }
204
205 unsigned
util_idalloc_mt_alloc(struct util_idalloc_mt * buf)206 util_idalloc_mt_alloc(struct util_idalloc_mt *buf)
207 {
208 simple_mtx_lock(&buf->mutex);
209 unsigned id = util_idalloc_alloc(&buf->buf);
210 simple_mtx_unlock(&buf->mutex);
211 return id;
212 }
213
214 void
util_idalloc_mt_free(struct util_idalloc_mt * buf,unsigned id)215 util_idalloc_mt_free(struct util_idalloc_mt *buf, unsigned id)
216 {
217 if (id == 0 && buf->skip_zero)
218 return;
219
220 simple_mtx_lock(&buf->mutex);
221 util_idalloc_free(&buf->buf, id);
222 simple_mtx_unlock(&buf->mutex);
223 }
224