1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
26 **************************************************************************/
30 * Generic bitmask implementation.
32 * @author Jose Fonseca <jfonseca@vmware.com>
36 #include "pipe/p_compiler.h"
37 #include "util/u_debug.h"
39 #include "util/u_memory.h"
40 #include "util/u_bitmask.h"
43 typedef uint32_t util_bitmask_word;
46 #define UTIL_BITMASK_INITIAL_WORDS 16
47 #define UTIL_BITMASK_BITS_PER_BYTE 8
48 #define UTIL_BITMASK_BITS_PER_WORD (sizeof(util_bitmask_word) * UTIL_BITMASK_BITS_PER_BYTE)
53 util_bitmask_word *words;
55 /** Number of bits we can currently hold */
58 /** Number of consecutive bits set at the start of the bitmask */
64 util_bitmask_create(void)
66 struct util_bitmask *bm;
68 bm = MALLOC_STRUCT(util_bitmask);
72 bm->words = (util_bitmask_word *)CALLOC(UTIL_BITMASK_INITIAL_WORDS, sizeof(util_bitmask_word));
78 bm->size = UTIL_BITMASK_INITIAL_WORDS * UTIL_BITMASK_BITS_PER_WORD;
86 * Resize the bitmask if necessary
89 util_bitmask_resize(struct util_bitmask *bm,
90 unsigned minimum_index)
92 unsigned minimum_size = minimum_index + 1;
94 util_bitmask_word *new_words;
96 /* Check integer overflow */
100 if(bm->size >= minimum_size)
103 assert(bm->size % UTIL_BITMASK_BITS_PER_WORD == 0);
105 while(new_size < minimum_size) {
107 /* Check integer overflow */
108 if(new_size < bm->size)
112 assert(new_size % UTIL_BITMASK_BITS_PER_WORD == 0);
114 new_words = (util_bitmask_word *)REALLOC((void *)bm->words,
115 bm->size / UTIL_BITMASK_BITS_PER_BYTE,
116 new_size / UTIL_BITMASK_BITS_PER_BYTE);
120 memset(new_words + bm->size/UTIL_BITMASK_BITS_PER_WORD,
122 (new_size - bm->size)/UTIL_BITMASK_BITS_PER_BYTE);
125 bm->words = new_words;
132 * Lazily update the filled.
135 util_bitmask_filled_set(struct util_bitmask *bm,
138 assert(bm->filled <= bm->size);
139 assert(index < bm->size);
141 if(index == bm->filled) {
143 assert(bm->filled <= bm->size);
148 util_bitmask_filled_unset(struct util_bitmask *bm,
151 assert(bm->filled <= bm->size);
152 assert(index < bm->size);
154 if(index < bm->filled)
160 util_bitmask_add(struct util_bitmask *bm)
164 util_bitmask_word mask;
168 /* linear search for an empty index */
169 word = bm->filled / UTIL_BITMASK_BITS_PER_WORD;
170 bit = bm->filled % UTIL_BITMASK_BITS_PER_WORD;
172 while(word < bm->size / UTIL_BITMASK_BITS_PER_WORD) {
173 while(bit < UTIL_BITMASK_BITS_PER_WORD) {
174 if(!(bm->words[word] & mask))
186 /* grow the bitmask if necessary */
187 if(!util_bitmask_resize(bm, bm->filled))
188 return UTIL_BITMASK_INVALID_INDEX;
190 assert(!(bm->words[word] & mask));
191 bm->words[word] |= mask;
198 util_bitmask_set(struct util_bitmask *bm,
203 util_bitmask_word mask;
207 /* grow the bitmask if necessary */
208 if(!util_bitmask_resize(bm, index))
209 return UTIL_BITMASK_INVALID_INDEX;
211 word = index / UTIL_BITMASK_BITS_PER_WORD;
212 bit = index % UTIL_BITMASK_BITS_PER_WORD;
215 bm->words[word] |= mask;
217 util_bitmask_filled_set(bm, index);
224 util_bitmask_clear(struct util_bitmask *bm,
229 util_bitmask_word mask;
233 if(index >= bm->size)
236 word = index / UTIL_BITMASK_BITS_PER_WORD;
237 bit = index % UTIL_BITMASK_BITS_PER_WORD;
240 bm->words[word] &= ~mask;
242 util_bitmask_filled_unset(bm, index);
247 util_bitmask_get(struct util_bitmask *bm,
250 unsigned word = index / UTIL_BITMASK_BITS_PER_WORD;
251 unsigned bit = index % UTIL_BITMASK_BITS_PER_WORD;
252 util_bitmask_word mask = 1 << bit;
256 if(index < bm->filled) {
257 assert(bm->words[word] & mask);
261 if(index >= bm->size)
264 if(bm->words[word] & mask) {
265 util_bitmask_filled_set(bm, index);
274 util_bitmask_get_next_index(struct util_bitmask *bm,
277 unsigned word = index / UTIL_BITMASK_BITS_PER_WORD;
278 unsigned bit = index % UTIL_BITMASK_BITS_PER_WORD;
279 util_bitmask_word mask = 1 << bit;
281 if(index < bm->filled) {
282 assert(bm->words[word] & mask);
286 if(index >= bm->size) {
287 return UTIL_BITMASK_INVALID_INDEX;
290 /* Do a linear search */
291 while(word < bm->size / UTIL_BITMASK_BITS_PER_WORD) {
292 while(bit < UTIL_BITMASK_BITS_PER_WORD) {
293 if(bm->words[word] & mask) {
294 if(index == bm->filled) {
296 assert(bm->filled <= bm->size);
309 return UTIL_BITMASK_INVALID_INDEX;
314 util_bitmask_get_first_index(struct util_bitmask *bm)
316 return util_bitmask_get_next_index(bm, 0);
321 util_bitmask_destroy(struct util_bitmask *bm)