• 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 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 struct util_idalloc
46 {
47    uint32_t *data;
48    unsigned num_elements;    /* number of allocated elements of "data" */
49    unsigned lowest_free_idx;
50 };
51 
52 void
53 util_idalloc_init(struct util_idalloc *buf, unsigned initial_num_ids);
54 
55 void
56 util_idalloc_fini(struct util_idalloc *buf);
57 
58 unsigned
59 util_idalloc_alloc(struct util_idalloc *buf);
60 
61 unsigned
62 util_idalloc_alloc_range(struct util_idalloc *buf, unsigned num);
63 
64 void
65 util_idalloc_free(struct util_idalloc *buf, unsigned id);
66 
67 void
68 util_idalloc_reserve(struct util_idalloc *buf, unsigned id);
69 
70 static inline bool
util_idalloc_exists(struct util_idalloc * buf,unsigned id)71 util_idalloc_exists(struct util_idalloc *buf, unsigned id)
72 {
73    return id / 32 < buf->num_elements &&
74           buf->data[id / 32] & BITFIELD_BIT(id % 32);
75 }
76 
77 #define util_idalloc_foreach(buf, id) \
78    for (uint32_t i = 0, mask = (buf)->num_elements ? (buf)->data[0] : 0, id, \
79                  count = (buf)->num_elements; \
80         i < count; mask = ++i < count ? (buf)->data[i] : 0) \
81       while (mask) \
82          if ((id = i * 32 + u_bit_scan(&mask)), true)
83 
84 
85 /* Thread-safe variant. */
86 struct util_idalloc_mt {
87    struct util_idalloc buf;
88    simple_mtx_t mutex;
89    bool skip_zero;
90 };
91 
92 void
93 util_idalloc_mt_init(struct util_idalloc_mt *buf,
94                      unsigned initial_num_ids, bool skip_zero);
95 
96 void
97 util_idalloc_mt_init_tc(struct util_idalloc_mt *buf);
98 
99 void
100 util_idalloc_mt_fini(struct util_idalloc_mt *buf);
101 
102 unsigned
103 util_idalloc_mt_alloc(struct util_idalloc_mt *buf);
104 
105 void
106 util_idalloc_mt_free(struct util_idalloc_mt *buf, unsigned id);
107 
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif /* U_IDALLOC_H */
114