• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* Allocator of IDs (e.g. OpenGL object IDs), or simply an allocator of
29  * numbers.
30  *
31  * The allocator uses a bit array to track allocated IDs.
32  */
33 
34 #ifndef U_IDALLOC_H
35 #define U_IDALLOC_H
36 
37 #include <inttypes.h>
38 #include <stdbool.h>
39 #include "simple_mtx.h"
40 #include "bitscan.h"
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 struct util_idalloc
47 {
48    uint32_t *data;
49    unsigned num_elements;     /* number of allocated elements of "data" */
50    unsigned num_set_elements; /* the last non-zero element of "data" + 1 */
51    unsigned lowest_free_idx;
52 };
53 
54 void
55 util_idalloc_init(struct util_idalloc *buf, unsigned initial_num_ids);
56 
57 void
58 util_idalloc_fini(struct util_idalloc *buf);
59 
60 unsigned
61 util_idalloc_alloc(struct util_idalloc *buf);
62 
63 unsigned
64 util_idalloc_alloc_range(struct util_idalloc *buf, unsigned num);
65 
66 void
67 util_idalloc_free(struct util_idalloc *buf, unsigned id);
68 
69 void
70 util_idalloc_reserve(struct util_idalloc *buf, unsigned id);
71 
72 static inline bool
util_idalloc_exists(struct util_idalloc * buf,unsigned id)73 util_idalloc_exists(struct util_idalloc *buf, unsigned id)
74 {
75    return id / 32 < buf->num_set_elements &&
76           buf->data[id / 32] & BITFIELD_BIT(id % 32);
77 }
78 
79 #define util_idalloc_foreach(buf, id) \
80    for (uint32_t i = 0, mask = (buf)->num_set_elements ? (buf)->data[0] : 0, id, \
81                  count = (buf)->num_used; \
82         i < count; mask = ++i < count ? (buf)->data[i] : 0) \
83       while (mask) \
84          if ((id = i * 32 + u_bit_scan(&mask)), true)
85 
86 /* This allows freeing IDs while iterating, excluding ID=0. */
87 #define util_idalloc_foreach_no_zero_safe(buf, id) \
88    for (uint32_t i = 0, bit, id, count = (buf)->num_set_elements, \
89          mask = count ? (buf)->data[0] & ~0x1 : 0; \
90         i < count; mask = ++i < count ? (buf)->data[i] : 0) \
91       while (mask) \
92          if ((bit = u_bit_scan(&mask), id = i * 32 + bit), \
93              (buf)->data[i] & BITFIELD_BIT(bit))
94 
95 /* Thread-safe variant. */
96 struct util_idalloc_mt {
97    struct util_idalloc buf;
98    simple_mtx_t mutex;
99    bool skip_zero;
100 };
101 
102 void
103 util_idalloc_mt_init(struct util_idalloc_mt *buf,
104                      unsigned initial_num_ids, bool skip_zero);
105 
106 void
107 util_idalloc_mt_init_tc(struct util_idalloc_mt *buf);
108 
109 void
110 util_idalloc_mt_fini(struct util_idalloc_mt *buf);
111 
112 unsigned
113 util_idalloc_mt_alloc(struct util_idalloc_mt *buf);
114 
115 void
116 util_idalloc_mt_free(struct util_idalloc_mt *buf, unsigned id);
117 
118 
119 #ifdef __cplusplus
120 }
121 #endif
122 
123 #endif /* U_IDALLOC_H */
124