xref: /aosp_15_r20/external/mesa3d/src/util/u_idalloc.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /**************************************************************************
2*61046927SAndroid Build Coastguard Worker  *
3*61046927SAndroid Build Coastguard Worker  * Copyright 2017 Valve Corporation
4*61046927SAndroid Build Coastguard Worker  * All Rights Reserved.
5*61046927SAndroid Build Coastguard Worker  *
6*61046927SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
7*61046927SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the
8*61046927SAndroid Build Coastguard Worker  * "Software"), to deal in the Software without restriction, including
9*61046927SAndroid Build Coastguard Worker  * without limitation the rights to use, copy, modify, merge, publish,
10*61046927SAndroid Build Coastguard Worker  * distribute, sub license, and/or sell copies of the Software, and to
11*61046927SAndroid Build Coastguard Worker  * permit persons to whom the Software is furnished to do so, subject to
12*61046927SAndroid Build Coastguard Worker  * the following conditions:
13*61046927SAndroid Build Coastguard Worker  *
14*61046927SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the
15*61046927SAndroid Build Coastguard Worker  * next paragraph) shall be included in all copies or substantial portions
16*61046927SAndroid Build Coastguard Worker  * of the Software.
17*61046927SAndroid Build Coastguard Worker  *
18*61046927SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19*61046927SAndroid Build Coastguard Worker  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20*61046927SAndroid Build Coastguard Worker  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21*61046927SAndroid Build Coastguard Worker  * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22*61046927SAndroid Build Coastguard Worker  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23*61046927SAndroid Build Coastguard Worker  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24*61046927SAndroid Build Coastguard Worker  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25*61046927SAndroid Build Coastguard Worker  *
26*61046927SAndroid Build Coastguard Worker  **************************************************************************/
27*61046927SAndroid Build Coastguard Worker 
28*61046927SAndroid Build Coastguard Worker /**
29*61046927SAndroid Build Coastguard Worker  * @file
30*61046927SAndroid Build Coastguard Worker  * A simple allocator that allocates and release "numbers".
31*61046927SAndroid Build Coastguard Worker  *
32*61046927SAndroid Build Coastguard Worker  * @author Samuel Pitoiset <[email protected]>
33*61046927SAndroid Build Coastguard Worker  */
34*61046927SAndroid Build Coastguard Worker 
35*61046927SAndroid Build Coastguard Worker #include "util/u_idalloc.h"
36*61046927SAndroid Build Coastguard Worker #include "util/u_math.h"
37*61046927SAndroid Build Coastguard Worker #include <stdlib.h>
38*61046927SAndroid Build Coastguard Worker 
39*61046927SAndroid Build Coastguard Worker ASSERTED static bool
util_idalloc_exists(struct util_idalloc * buf,unsigned id)40*61046927SAndroid Build Coastguard Worker util_idalloc_exists(struct util_idalloc *buf, unsigned id)
41*61046927SAndroid Build Coastguard Worker {
42*61046927SAndroid Build Coastguard Worker    return id / 32 < buf->num_set_elements &&
43*61046927SAndroid Build Coastguard Worker           buf->data[id / 32] & BITFIELD_BIT(id % 32);
44*61046927SAndroid Build Coastguard Worker }
45*61046927SAndroid Build Coastguard Worker 
46*61046927SAndroid Build Coastguard Worker static void
util_idalloc_resize(struct util_idalloc * buf,unsigned new_num_elements)47*61046927SAndroid Build Coastguard Worker util_idalloc_resize(struct util_idalloc *buf, unsigned new_num_elements)
48*61046927SAndroid Build Coastguard Worker {
49*61046927SAndroid Build Coastguard Worker    if (new_num_elements > buf->num_elements) {
50*61046927SAndroid Build Coastguard Worker       buf->data = realloc(buf->data, new_num_elements * sizeof(*buf->data));
51*61046927SAndroid Build Coastguard Worker       memset(&buf->data[buf->num_elements], 0,
52*61046927SAndroid Build Coastguard Worker              (new_num_elements - buf->num_elements) * sizeof(*buf->data));
53*61046927SAndroid Build Coastguard Worker       buf->num_elements = new_num_elements;
54*61046927SAndroid Build Coastguard Worker    }
55*61046927SAndroid Build Coastguard Worker }
56*61046927SAndroid Build Coastguard Worker 
57*61046927SAndroid Build Coastguard Worker void
util_idalloc_init(struct util_idalloc * buf,unsigned initial_num_ids)58*61046927SAndroid Build Coastguard Worker util_idalloc_init(struct util_idalloc *buf, unsigned initial_num_ids)
59*61046927SAndroid Build Coastguard Worker {
60*61046927SAndroid Build Coastguard Worker    memset(buf, 0, sizeof(*buf));
61*61046927SAndroid Build Coastguard Worker    assert(initial_num_ids);
62*61046927SAndroid Build Coastguard Worker    util_idalloc_resize(buf, DIV_ROUND_UP(initial_num_ids, 32));
63*61046927SAndroid Build Coastguard Worker }
64*61046927SAndroid Build Coastguard Worker 
65*61046927SAndroid Build Coastguard Worker void
util_idalloc_fini(struct util_idalloc * buf)66*61046927SAndroid Build Coastguard Worker util_idalloc_fini(struct util_idalloc *buf)
67*61046927SAndroid Build Coastguard Worker {
68*61046927SAndroid Build Coastguard Worker    if (buf->data)
69*61046927SAndroid Build Coastguard Worker       free(buf->data);
70*61046927SAndroid Build Coastguard Worker }
71*61046927SAndroid Build Coastguard Worker 
72*61046927SAndroid Build Coastguard Worker unsigned
util_idalloc_alloc(struct util_idalloc * buf)73*61046927SAndroid Build Coastguard Worker util_idalloc_alloc(struct util_idalloc *buf)
74*61046927SAndroid Build Coastguard Worker {
75*61046927SAndroid Build Coastguard Worker    unsigned num_elements = buf->num_elements;
76*61046927SAndroid Build Coastguard Worker 
77*61046927SAndroid Build Coastguard Worker    for (unsigned i = buf->lowest_free_idx; i < num_elements; i++) {
78*61046927SAndroid Build Coastguard Worker       if (buf->data[i] == 0xffffffff)
79*61046927SAndroid Build Coastguard Worker          continue;
80*61046927SAndroid Build Coastguard Worker 
81*61046927SAndroid Build Coastguard Worker       unsigned bit = ffs(~buf->data[i]) - 1;
82*61046927SAndroid Build Coastguard Worker       buf->data[i] |= 1u << bit;
83*61046927SAndroid Build Coastguard Worker       buf->lowest_free_idx = i;
84*61046927SAndroid Build Coastguard Worker       buf->num_set_elements = MAX2(buf->num_set_elements, i + 1);
85*61046927SAndroid Build Coastguard Worker       return i * 32 + bit;
86*61046927SAndroid Build Coastguard Worker    }
87*61046927SAndroid Build Coastguard Worker 
88*61046927SAndroid Build Coastguard Worker    /* No slots available, resize and return the first free. */
89*61046927SAndroid Build Coastguard Worker    util_idalloc_resize(buf, MAX2(num_elements, 1) * 2);
90*61046927SAndroid Build Coastguard Worker 
91*61046927SAndroid Build Coastguard Worker    buf->lowest_free_idx = num_elements;
92*61046927SAndroid Build Coastguard Worker    buf->data[num_elements] |= 1;
93*61046927SAndroid Build Coastguard Worker    buf->num_set_elements = MAX2(buf->num_set_elements, num_elements + 1);
94*61046927SAndroid Build Coastguard Worker    return num_elements * 32;
95*61046927SAndroid Build Coastguard Worker }
96*61046927SAndroid Build Coastguard Worker 
97*61046927SAndroid Build Coastguard Worker static unsigned
find_free_block(struct util_idalloc * buf,unsigned start)98*61046927SAndroid Build Coastguard Worker find_free_block(struct util_idalloc *buf, unsigned start)
99*61046927SAndroid Build Coastguard Worker {
100*61046927SAndroid Build Coastguard Worker    for (unsigned i = start; i < buf->num_elements; i++) {
101*61046927SAndroid Build Coastguard Worker       if (!buf->data[i])
102*61046927SAndroid Build Coastguard Worker          return i;
103*61046927SAndroid Build Coastguard Worker    }
104*61046927SAndroid Build Coastguard Worker    return buf->num_elements;
105*61046927SAndroid Build Coastguard Worker }
106*61046927SAndroid Build Coastguard Worker 
107*61046927SAndroid Build Coastguard Worker /* Allocate a range of consecutive IDs. Return the first ID. */
108*61046927SAndroid Build Coastguard Worker unsigned
util_idalloc_alloc_range(struct util_idalloc * buf,unsigned num)109*61046927SAndroid Build Coastguard Worker util_idalloc_alloc_range(struct util_idalloc *buf, unsigned num)
110*61046927SAndroid Build Coastguard Worker {
111*61046927SAndroid Build Coastguard Worker    if (num == 1)
112*61046927SAndroid Build Coastguard Worker       return util_idalloc_alloc(buf);
113*61046927SAndroid Build Coastguard Worker 
114*61046927SAndroid Build Coastguard Worker    unsigned num_alloc = DIV_ROUND_UP(num, 32);
115*61046927SAndroid Build Coastguard Worker    unsigned num_elements = buf->num_elements;
116*61046927SAndroid Build Coastguard Worker    unsigned base = find_free_block(buf, buf->lowest_free_idx);
117*61046927SAndroid Build Coastguard Worker 
118*61046927SAndroid Build Coastguard Worker    while (1) {
119*61046927SAndroid Build Coastguard Worker       unsigned i;
120*61046927SAndroid Build Coastguard Worker       for (i = base;
121*61046927SAndroid Build Coastguard Worker            i < num_elements && i - base < num_alloc && !buf->data[i]; i++);
122*61046927SAndroid Build Coastguard Worker 
123*61046927SAndroid Build Coastguard Worker       if (i - base == num_alloc)
124*61046927SAndroid Build Coastguard Worker          goto ret; /* found */
125*61046927SAndroid Build Coastguard Worker 
126*61046927SAndroid Build Coastguard Worker       if (i == num_elements)
127*61046927SAndroid Build Coastguard Worker          break; /* not found */
128*61046927SAndroid Build Coastguard Worker 
129*61046927SAndroid Build Coastguard Worker       /* continue searching */
130*61046927SAndroid Build Coastguard Worker       base = !buf->data[i] ? i : i + 1;
131*61046927SAndroid Build Coastguard Worker    }
132*61046927SAndroid Build Coastguard Worker 
133*61046927SAndroid Build Coastguard Worker    /* No slots available, allocate more. */
134*61046927SAndroid Build Coastguard Worker    util_idalloc_resize(buf, num_elements * 2 + num_alloc);
135*61046927SAndroid Build Coastguard Worker 
136*61046927SAndroid Build Coastguard Worker ret:
137*61046927SAndroid Build Coastguard Worker    /* Mark the bits as used. */
138*61046927SAndroid Build Coastguard Worker    for (unsigned i = base; i < base + num_alloc - (num % 32 != 0); i++)
139*61046927SAndroid Build Coastguard Worker       buf->data[i] = 0xffffffff;
140*61046927SAndroid Build Coastguard Worker    if (num % 32 != 0)
141*61046927SAndroid Build Coastguard Worker       buf->data[base + num_alloc - 1] |= BITFIELD_MASK(num % 32);
142*61046927SAndroid Build Coastguard Worker 
143*61046927SAndroid Build Coastguard Worker    if (buf->lowest_free_idx == base)
144*61046927SAndroid Build Coastguard Worker       buf->lowest_free_idx = base + num / 32;
145*61046927SAndroid Build Coastguard Worker 
146*61046927SAndroid Build Coastguard Worker    buf->num_set_elements = MAX2(buf->num_set_elements, base + num_alloc);
147*61046927SAndroid Build Coastguard Worker 
148*61046927SAndroid Build Coastguard Worker    /* Validate this algorithm. */
149*61046927SAndroid Build Coastguard Worker    for (unsigned i = 0; i < num; i++)
150*61046927SAndroid Build Coastguard Worker       assert(util_idalloc_exists(buf, base * 32 + i));
151*61046927SAndroid Build Coastguard Worker 
152*61046927SAndroid Build Coastguard Worker    return base * 32;
153*61046927SAndroid Build Coastguard Worker }
154*61046927SAndroid Build Coastguard Worker 
155*61046927SAndroid Build Coastguard Worker void
util_idalloc_free(struct util_idalloc * buf,unsigned id)156*61046927SAndroid Build Coastguard Worker util_idalloc_free(struct util_idalloc *buf, unsigned id)
157*61046927SAndroid Build Coastguard Worker {
158*61046927SAndroid Build Coastguard Worker    unsigned idx = id / 32;
159*61046927SAndroid Build Coastguard Worker 
160*61046927SAndroid Build Coastguard Worker    if (idx >= buf->num_elements)
161*61046927SAndroid Build Coastguard Worker        return;
162*61046927SAndroid Build Coastguard Worker 
163*61046927SAndroid Build Coastguard Worker    buf->lowest_free_idx = MIN2(idx, buf->lowest_free_idx);
164*61046927SAndroid Build Coastguard Worker    buf->data[idx] &= ~(1u << (id % 32));
165*61046927SAndroid Build Coastguard Worker 
166*61046927SAndroid Build Coastguard Worker    /* Decrease num_used to the last used element + 1. */
167*61046927SAndroid Build Coastguard Worker    if (buf->num_set_elements == idx + 1) {
168*61046927SAndroid Build Coastguard Worker       while (buf->num_set_elements > 0 && !buf->data[buf->num_set_elements - 1])
169*61046927SAndroid Build Coastguard Worker          buf->num_set_elements--;
170*61046927SAndroid Build Coastguard Worker    }
171*61046927SAndroid Build Coastguard Worker }
172*61046927SAndroid Build Coastguard Worker 
173*61046927SAndroid Build Coastguard Worker void
util_idalloc_reserve(struct util_idalloc * buf,unsigned id)174*61046927SAndroid Build Coastguard Worker util_idalloc_reserve(struct util_idalloc *buf, unsigned id)
175*61046927SAndroid Build Coastguard Worker {
176*61046927SAndroid Build Coastguard Worker    unsigned idx = id / 32;
177*61046927SAndroid Build Coastguard Worker 
178*61046927SAndroid Build Coastguard Worker    if (idx >= buf->num_elements)
179*61046927SAndroid Build Coastguard Worker       util_idalloc_resize(buf, (idx + 1) * 2);
180*61046927SAndroid Build Coastguard Worker    buf->data[idx] |= 1u << (id % 32);
181*61046927SAndroid Build Coastguard Worker    buf->num_set_elements = MAX2(buf->num_set_elements, idx + 1);
182*61046927SAndroid Build Coastguard Worker }
183*61046927SAndroid Build Coastguard Worker 
184*61046927SAndroid Build Coastguard Worker /*********************************************
185*61046927SAndroid Build Coastguard Worker  * util_idalloc_mt
186*61046927SAndroid Build Coastguard Worker  *********************************************/
187*61046927SAndroid Build Coastguard Worker 
188*61046927SAndroid Build Coastguard Worker void
util_idalloc_mt_init(struct util_idalloc_mt * buf,unsigned initial_num_ids,bool skip_zero)189*61046927SAndroid Build Coastguard Worker util_idalloc_mt_init(struct util_idalloc_mt *buf,
190*61046927SAndroid Build Coastguard Worker                      unsigned initial_num_ids, bool skip_zero)
191*61046927SAndroid Build Coastguard Worker {
192*61046927SAndroid Build Coastguard Worker    simple_mtx_init(&buf->mutex, mtx_plain);
193*61046927SAndroid Build Coastguard Worker    util_idalloc_init(&buf->buf, initial_num_ids);
194*61046927SAndroid Build Coastguard Worker    buf->skip_zero = skip_zero;
195*61046927SAndroid Build Coastguard Worker 
196*61046927SAndroid Build Coastguard Worker    if (skip_zero) {
197*61046927SAndroid Build Coastguard Worker       ASSERTED unsigned zero = util_idalloc_alloc(&buf->buf);
198*61046927SAndroid Build Coastguard Worker       assert(zero == 0);
199*61046927SAndroid Build Coastguard Worker    }
200*61046927SAndroid Build Coastguard Worker }
201*61046927SAndroid Build Coastguard Worker 
202*61046927SAndroid Build Coastguard Worker /* Callback for drivers using u_threaded_context (abbreviated as tc). */
203*61046927SAndroid Build Coastguard Worker void
util_idalloc_mt_init_tc(struct util_idalloc_mt * buf)204*61046927SAndroid Build Coastguard Worker util_idalloc_mt_init_tc(struct util_idalloc_mt *buf)
205*61046927SAndroid Build Coastguard Worker {
206*61046927SAndroid Build Coastguard Worker    util_idalloc_mt_init(buf, 1 << 16, true);
207*61046927SAndroid Build Coastguard Worker }
208*61046927SAndroid Build Coastguard Worker 
209*61046927SAndroid Build Coastguard Worker void
util_idalloc_mt_fini(struct util_idalloc_mt * buf)210*61046927SAndroid Build Coastguard Worker util_idalloc_mt_fini(struct util_idalloc_mt *buf)
211*61046927SAndroid Build Coastguard Worker {
212*61046927SAndroid Build Coastguard Worker    util_idalloc_fini(&buf->buf);
213*61046927SAndroid Build Coastguard Worker    simple_mtx_destroy(&buf->mutex);
214*61046927SAndroid Build Coastguard Worker }
215*61046927SAndroid Build Coastguard Worker 
216*61046927SAndroid Build Coastguard Worker unsigned
util_idalloc_mt_alloc(struct util_idalloc_mt * buf)217*61046927SAndroid Build Coastguard Worker util_idalloc_mt_alloc(struct util_idalloc_mt *buf)
218*61046927SAndroid Build Coastguard Worker {
219*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&buf->mutex);
220*61046927SAndroid Build Coastguard Worker    unsigned id = util_idalloc_alloc(&buf->buf);
221*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&buf->mutex);
222*61046927SAndroid Build Coastguard Worker    return id;
223*61046927SAndroid Build Coastguard Worker }
224*61046927SAndroid Build Coastguard Worker 
225*61046927SAndroid Build Coastguard Worker void
util_idalloc_mt_free(struct util_idalloc_mt * buf,unsigned id)226*61046927SAndroid Build Coastguard Worker util_idalloc_mt_free(struct util_idalloc_mt *buf, unsigned id)
227*61046927SAndroid Build Coastguard Worker {
228*61046927SAndroid Build Coastguard Worker    if (id == 0 && buf->skip_zero)
229*61046927SAndroid Build Coastguard Worker       return;
230*61046927SAndroid Build Coastguard Worker 
231*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&buf->mutex);
232*61046927SAndroid Build Coastguard Worker    util_idalloc_free(&buf->buf, id);
233*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&buf->mutex);
234*61046927SAndroid Build Coastguard Worker }
235*61046927SAndroid Build Coastguard Worker 
236*61046927SAndroid Build Coastguard Worker /*********************************************
237*61046927SAndroid Build Coastguard Worker  * util_idalloc_sparse
238*61046927SAndroid Build Coastguard Worker  *********************************************/
239*61046927SAndroid Build Coastguard Worker 
240*61046927SAndroid Build Coastguard Worker void
util_idalloc_sparse_init(struct util_idalloc_sparse * buf)241*61046927SAndroid Build Coastguard Worker util_idalloc_sparse_init(struct util_idalloc_sparse *buf)
242*61046927SAndroid Build Coastguard Worker {
243*61046927SAndroid Build Coastguard Worker    static_assert(IS_POT_NONZERO(ARRAY_SIZE(buf->segment)),
244*61046927SAndroid Build Coastguard Worker          "buf->segment[] must have a power of two number of elements");
245*61046927SAndroid Build Coastguard Worker 
246*61046927SAndroid Build Coastguard Worker    for (unsigned i = 0; i < ARRAY_SIZE(buf->segment); i++)
247*61046927SAndroid Build Coastguard Worker       util_idalloc_init(&buf->segment[i], 1);
248*61046927SAndroid Build Coastguard Worker }
249*61046927SAndroid Build Coastguard Worker 
250*61046927SAndroid Build Coastguard Worker void
util_idalloc_sparse_fini(struct util_idalloc_sparse * buf)251*61046927SAndroid Build Coastguard Worker util_idalloc_sparse_fini(struct util_idalloc_sparse *buf)
252*61046927SAndroid Build Coastguard Worker {
253*61046927SAndroid Build Coastguard Worker    for (unsigned i = 0; i < ARRAY_SIZE(buf->segment); i++)
254*61046927SAndroid Build Coastguard Worker       util_idalloc_fini(&buf->segment[i]);
255*61046927SAndroid Build Coastguard Worker }
256*61046927SAndroid Build Coastguard Worker 
257*61046927SAndroid Build Coastguard Worker unsigned
util_idalloc_sparse_alloc(struct util_idalloc_sparse * buf)258*61046927SAndroid Build Coastguard Worker util_idalloc_sparse_alloc(struct util_idalloc_sparse *buf)
259*61046927SAndroid Build Coastguard Worker {
260*61046927SAndroid Build Coastguard Worker    unsigned max_ids = UTIL_IDALLOC_MAX_IDS_PER_SEGMENT(buf);
261*61046927SAndroid Build Coastguard Worker 
262*61046927SAndroid Build Coastguard Worker    for (unsigned i = 0; i < ARRAY_SIZE(buf->segment); i++) {
263*61046927SAndroid Build Coastguard Worker       if (buf->segment[i].lowest_free_idx <
264*61046927SAndroid Build Coastguard Worker           UTIL_IDALLOC_MAX_ELEMS_PER_SEGMENT(buf))
265*61046927SAndroid Build Coastguard Worker          return max_ids * i + util_idalloc_alloc(&buf->segment[i]);
266*61046927SAndroid Build Coastguard Worker    }
267*61046927SAndroid Build Coastguard Worker 
268*61046927SAndroid Build Coastguard Worker    fprintf(stderr, "mesa: util_idalloc_sparse_alloc: "
269*61046927SAndroid Build Coastguard Worker                    "all 2^32 IDs are used, this shouldn't happen\n");
270*61046927SAndroid Build Coastguard Worker    assert(0);
271*61046927SAndroid Build Coastguard Worker    return 0;
272*61046927SAndroid Build Coastguard Worker }
273*61046927SAndroid Build Coastguard Worker 
274*61046927SAndroid Build Coastguard Worker unsigned
util_idalloc_sparse_alloc_range(struct util_idalloc_sparse * buf,unsigned num)275*61046927SAndroid Build Coastguard Worker util_idalloc_sparse_alloc_range(struct util_idalloc_sparse *buf, unsigned num)
276*61046927SAndroid Build Coastguard Worker {
277*61046927SAndroid Build Coastguard Worker    unsigned max_ids = UTIL_IDALLOC_MAX_IDS_PER_SEGMENT(buf);
278*61046927SAndroid Build Coastguard Worker    unsigned num_elems = DIV_ROUND_UP(num, 32);
279*61046927SAndroid Build Coastguard Worker 
280*61046927SAndroid Build Coastguard Worker    /* TODO: This doesn't try to find a range that spans 2 different segments */
281*61046927SAndroid Build Coastguard Worker    for (unsigned i = 0; i < ARRAY_SIZE(buf->segment); i++) {
282*61046927SAndroid Build Coastguard Worker       if (buf->segment[i].lowest_free_idx + num_elems <=
283*61046927SAndroid Build Coastguard Worker           UTIL_IDALLOC_MAX_ELEMS_PER_SEGMENT(buf)) {
284*61046927SAndroid Build Coastguard Worker          unsigned base = util_idalloc_alloc_range(&buf->segment[i], num);
285*61046927SAndroid Build Coastguard Worker 
286*61046927SAndroid Build Coastguard Worker          if (base + num <= max_ids)
287*61046927SAndroid Build Coastguard Worker             return max_ids * i + base;
288*61046927SAndroid Build Coastguard Worker 
289*61046927SAndroid Build Coastguard Worker          /* Back off the allocation and try again with the next segment. */
290*61046927SAndroid Build Coastguard Worker          for (unsigned i = 0; i < num; i++)
291*61046927SAndroid Build Coastguard Worker             util_idalloc_free(&buf->segment[i], base + i);
292*61046927SAndroid Build Coastguard Worker       }
293*61046927SAndroid Build Coastguard Worker    }
294*61046927SAndroid Build Coastguard Worker 
295*61046927SAndroid Build Coastguard Worker    fprintf(stderr, "mesa: util_idalloc_sparse_alloc_range: can't find a free consecutive range of IDs\n");
296*61046927SAndroid Build Coastguard Worker    assert(0);
297*61046927SAndroid Build Coastguard Worker    return 0;
298*61046927SAndroid Build Coastguard Worker }
299*61046927SAndroid Build Coastguard Worker 
300*61046927SAndroid Build Coastguard Worker void
util_idalloc_sparse_free(struct util_idalloc_sparse * buf,unsigned id)301*61046927SAndroid Build Coastguard Worker util_idalloc_sparse_free(struct util_idalloc_sparse *buf, unsigned id)
302*61046927SAndroid Build Coastguard Worker {
303*61046927SAndroid Build Coastguard Worker    unsigned max_ids = UTIL_IDALLOC_MAX_IDS_PER_SEGMENT(buf);
304*61046927SAndroid Build Coastguard Worker    util_idalloc_free(&buf->segment[id / max_ids], id % max_ids);
305*61046927SAndroid Build Coastguard Worker }
306*61046927SAndroid Build Coastguard Worker 
307*61046927SAndroid Build Coastguard Worker void
util_idalloc_sparse_reserve(struct util_idalloc_sparse * buf,unsigned id)308*61046927SAndroid Build Coastguard Worker util_idalloc_sparse_reserve(struct util_idalloc_sparse *buf, unsigned id)
309*61046927SAndroid Build Coastguard Worker {
310*61046927SAndroid Build Coastguard Worker    unsigned max_ids = UTIL_IDALLOC_MAX_IDS_PER_SEGMENT(buf);
311*61046927SAndroid Build Coastguard Worker    util_idalloc_reserve(&buf->segment[id / max_ids], id % max_ids);
312*61046927SAndroid Build Coastguard Worker }
313