1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2009 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/**
29bf215546Sopenharmony_ci * @file
30bf215546Sopenharmony_ci * Generic bitmask implementation.
31bf215546Sopenharmony_ci *
32bf215546Sopenharmony_ci * @author Jose Fonseca <jfonseca@vmware.com>
33bf215546Sopenharmony_ci */
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#include "pipe/p_compiler.h"
37bf215546Sopenharmony_ci#include "util/u_debug.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#include "util/u_memory.h"
40bf215546Sopenharmony_ci#include "util/u_bitmask.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_citypedef uint32_t util_bitmask_word;
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci#define UTIL_BITMASK_INITIAL_WORDS 16
47bf215546Sopenharmony_ci#define UTIL_BITMASK_BITS_PER_BYTE 8
48bf215546Sopenharmony_ci#define UTIL_BITMASK_BITS_PER_WORD (sizeof(util_bitmask_word) * UTIL_BITMASK_BITS_PER_BYTE)
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_cistruct util_bitmask
52bf215546Sopenharmony_ci{
53bf215546Sopenharmony_ci   util_bitmask_word *words;
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci   /** Number of bits we can currently hold */
56bf215546Sopenharmony_ci   unsigned size;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   /** Number of consecutive bits set at the start of the bitmask */
59bf215546Sopenharmony_ci   unsigned filled;
60bf215546Sopenharmony_ci};
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistruct util_bitmask *
64bf215546Sopenharmony_ciutil_bitmask_create(void)
65bf215546Sopenharmony_ci{
66bf215546Sopenharmony_ci   struct util_bitmask *bm;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   bm = MALLOC_STRUCT(util_bitmask);
69bf215546Sopenharmony_ci   if (!bm)
70bf215546Sopenharmony_ci      return NULL;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   bm->words = (util_bitmask_word *)
73bf215546Sopenharmony_ci      CALLOC(UTIL_BITMASK_INITIAL_WORDS, sizeof(util_bitmask_word));
74bf215546Sopenharmony_ci   if (!bm->words) {
75bf215546Sopenharmony_ci      FREE(bm);
76bf215546Sopenharmony_ci      return NULL;
77bf215546Sopenharmony_ci   }
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci   bm->size = UTIL_BITMASK_INITIAL_WORDS * UTIL_BITMASK_BITS_PER_WORD;
80bf215546Sopenharmony_ci   bm->filled = 0;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   return bm;
83bf215546Sopenharmony_ci}
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci/**
87bf215546Sopenharmony_ci * Resize the bitmask if necessary
88bf215546Sopenharmony_ci */
89bf215546Sopenharmony_cistatic inline boolean
90bf215546Sopenharmony_ciutil_bitmask_resize(struct util_bitmask *bm,
91bf215546Sopenharmony_ci                    unsigned minimum_index)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   const unsigned minimum_size = minimum_index + 1;
94bf215546Sopenharmony_ci   unsigned new_size;
95bf215546Sopenharmony_ci   util_bitmask_word *new_words;
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   /* Check integer overflow */
98bf215546Sopenharmony_ci   if (!minimum_size)
99bf215546Sopenharmony_ci      return FALSE;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   if (bm->size >= minimum_size)
102bf215546Sopenharmony_ci      return TRUE;
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   assert(bm->size % UTIL_BITMASK_BITS_PER_WORD == 0);
105bf215546Sopenharmony_ci   new_size = bm->size;
106bf215546Sopenharmony_ci   while (new_size < minimum_size) {
107bf215546Sopenharmony_ci      new_size *= 2;
108bf215546Sopenharmony_ci      /* Check integer overflow */
109bf215546Sopenharmony_ci      if (new_size < bm->size)
110bf215546Sopenharmony_ci         return FALSE;
111bf215546Sopenharmony_ci   }
112bf215546Sopenharmony_ci   assert(new_size);
113bf215546Sopenharmony_ci   assert(new_size % UTIL_BITMASK_BITS_PER_WORD == 0);
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   new_words = (util_bitmask_word *)
116bf215546Sopenharmony_ci      REALLOC((void *)bm->words,
117bf215546Sopenharmony_ci              bm->size / UTIL_BITMASK_BITS_PER_BYTE,
118bf215546Sopenharmony_ci              new_size / UTIL_BITMASK_BITS_PER_BYTE);
119bf215546Sopenharmony_ci   if (!new_words)
120bf215546Sopenharmony_ci      return FALSE;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   memset(new_words + bm->size/UTIL_BITMASK_BITS_PER_WORD,
123bf215546Sopenharmony_ci          0,
124bf215546Sopenharmony_ci          (new_size - bm->size)/UTIL_BITMASK_BITS_PER_BYTE);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   bm->size = new_size;
127bf215546Sopenharmony_ci   bm->words = new_words;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   return TRUE;
130bf215546Sopenharmony_ci}
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci/**
134bf215546Sopenharmony_ci * Check if we can increment the filled counter.
135bf215546Sopenharmony_ci */
136bf215546Sopenharmony_cistatic inline void
137bf215546Sopenharmony_ciutil_bitmask_filled_set(struct util_bitmask *bm,
138bf215546Sopenharmony_ci                        unsigned index)
139bf215546Sopenharmony_ci{
140bf215546Sopenharmony_ci   assert(bm->filled <= bm->size);
141bf215546Sopenharmony_ci   assert(index < bm->size);
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   if (index == bm->filled) {
144bf215546Sopenharmony_ci      ++bm->filled;
145bf215546Sopenharmony_ci      assert(bm->filled <= bm->size);
146bf215546Sopenharmony_ci   }
147bf215546Sopenharmony_ci}
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci/**
151bf215546Sopenharmony_ci * Check if we need to decrement the filled counter.
152bf215546Sopenharmony_ci */
153bf215546Sopenharmony_cistatic inline void
154bf215546Sopenharmony_ciutil_bitmask_filled_unset(struct util_bitmask *bm,
155bf215546Sopenharmony_ci                          unsigned index)
156bf215546Sopenharmony_ci{
157bf215546Sopenharmony_ci   assert(bm->filled <= bm->size);
158bf215546Sopenharmony_ci   assert(index < bm->size);
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci   if (index < bm->filled)
161bf215546Sopenharmony_ci      bm->filled = index;
162bf215546Sopenharmony_ci}
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ciunsigned
166bf215546Sopenharmony_ciutil_bitmask_add(struct util_bitmask *bm)
167bf215546Sopenharmony_ci{
168bf215546Sopenharmony_ci   unsigned word;
169bf215546Sopenharmony_ci   unsigned bit;
170bf215546Sopenharmony_ci   util_bitmask_word mask;
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   assert(bm);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   /* linear search for an empty index, starting at filled position */
175bf215546Sopenharmony_ci   word = bm->filled / UTIL_BITMASK_BITS_PER_WORD;
176bf215546Sopenharmony_ci   bit  = bm->filled % UTIL_BITMASK_BITS_PER_WORD;
177bf215546Sopenharmony_ci   mask = 1 << bit;
178bf215546Sopenharmony_ci   while (word < bm->size / UTIL_BITMASK_BITS_PER_WORD) {
179bf215546Sopenharmony_ci      while (bit < UTIL_BITMASK_BITS_PER_WORD) {
180bf215546Sopenharmony_ci         if (!(bm->words[word] & mask))
181bf215546Sopenharmony_ci            goto found;
182bf215546Sopenharmony_ci         ++bm->filled;
183bf215546Sopenharmony_ci         ++bit;
184bf215546Sopenharmony_ci         mask <<= 1;
185bf215546Sopenharmony_ci      }
186bf215546Sopenharmony_ci      ++word;
187bf215546Sopenharmony_ci      bit = 0;
188bf215546Sopenharmony_ci      mask = 1;
189bf215546Sopenharmony_ci   }
190bf215546Sopenharmony_cifound:
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci   /* grow the bitmask if necessary */
193bf215546Sopenharmony_ci   if (!util_bitmask_resize(bm, bm->filled))
194bf215546Sopenharmony_ci      return UTIL_BITMASK_INVALID_INDEX;
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   assert(!(bm->words[word] & mask));
197bf215546Sopenharmony_ci   bm->words[word] |= mask;
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci   return bm->filled++;
200bf215546Sopenharmony_ci}
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ciunsigned
204bf215546Sopenharmony_ciutil_bitmask_set(struct util_bitmask *bm,
205bf215546Sopenharmony_ci                 unsigned index)
206bf215546Sopenharmony_ci{
207bf215546Sopenharmony_ci   unsigned word;
208bf215546Sopenharmony_ci   unsigned bit;
209bf215546Sopenharmony_ci   util_bitmask_word mask;
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   assert(bm);
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci   /* grow the bitmask if necessary */
214bf215546Sopenharmony_ci   if (!util_bitmask_resize(bm, index))
215bf215546Sopenharmony_ci      return UTIL_BITMASK_INVALID_INDEX;
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci   word = index / UTIL_BITMASK_BITS_PER_WORD;
218bf215546Sopenharmony_ci   bit  = index % UTIL_BITMASK_BITS_PER_WORD;
219bf215546Sopenharmony_ci   mask = 1 << bit;
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci   bm->words[word] |= mask;
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_ci   util_bitmask_filled_set(bm, index);
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci   return index;
226bf215546Sopenharmony_ci}
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_civoid
230bf215546Sopenharmony_ciutil_bitmask_clear(struct util_bitmask *bm,
231bf215546Sopenharmony_ci                   unsigned index)
232bf215546Sopenharmony_ci{
233bf215546Sopenharmony_ci   unsigned word;
234bf215546Sopenharmony_ci   unsigned bit;
235bf215546Sopenharmony_ci   util_bitmask_word mask;
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci   assert(bm);
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci   if (index >= bm->size)
240bf215546Sopenharmony_ci      return;
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci   word = index / UTIL_BITMASK_BITS_PER_WORD;
243bf215546Sopenharmony_ci   bit  = index % UTIL_BITMASK_BITS_PER_WORD;
244bf215546Sopenharmony_ci   mask = 1 << bit;
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci   bm->words[word] &= ~mask;
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci   util_bitmask_filled_unset(bm, index);
249bf215546Sopenharmony_ci}
250bf215546Sopenharmony_ci
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ciboolean
253bf215546Sopenharmony_ciutil_bitmask_get(struct util_bitmask *bm,
254bf215546Sopenharmony_ci                 unsigned index)
255bf215546Sopenharmony_ci{
256bf215546Sopenharmony_ci   const unsigned word = index / UTIL_BITMASK_BITS_PER_WORD;
257bf215546Sopenharmony_ci   const unsigned bit  = index % UTIL_BITMASK_BITS_PER_WORD;
258bf215546Sopenharmony_ci   const util_bitmask_word mask = 1 << bit;
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci   assert(bm);
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci   if (index < bm->filled) {
263bf215546Sopenharmony_ci      assert(bm->words[word] & mask);
264bf215546Sopenharmony_ci      return TRUE;
265bf215546Sopenharmony_ci   }
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci   if (index >= bm->size)
268bf215546Sopenharmony_ci      return FALSE;
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci   if (bm->words[word] & mask) {
271bf215546Sopenharmony_ci      util_bitmask_filled_set(bm, index);
272bf215546Sopenharmony_ci      return TRUE;
273bf215546Sopenharmony_ci   }
274bf215546Sopenharmony_ci   else
275bf215546Sopenharmony_ci      return FALSE;
276bf215546Sopenharmony_ci}
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ciunsigned
280bf215546Sopenharmony_ciutil_bitmask_get_next_index(struct util_bitmask *bm,
281bf215546Sopenharmony_ci                            unsigned index)
282bf215546Sopenharmony_ci{
283bf215546Sopenharmony_ci   unsigned word = index / UTIL_BITMASK_BITS_PER_WORD;
284bf215546Sopenharmony_ci   unsigned bit  = index % UTIL_BITMASK_BITS_PER_WORD;
285bf215546Sopenharmony_ci   util_bitmask_word mask = 1 << bit;
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_ci   if (index < bm->filled) {
288bf215546Sopenharmony_ci      assert(bm->words[word] & mask);
289bf215546Sopenharmony_ci      return index;
290bf215546Sopenharmony_ci   }
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci   if (index >= bm->size) {
293bf215546Sopenharmony_ci      return UTIL_BITMASK_INVALID_INDEX;
294bf215546Sopenharmony_ci   }
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_ci   /* Do a linear search */
297bf215546Sopenharmony_ci   while (word < bm->size / UTIL_BITMASK_BITS_PER_WORD) {
298bf215546Sopenharmony_ci      while (bit < UTIL_BITMASK_BITS_PER_WORD) {
299bf215546Sopenharmony_ci         if (bm->words[word] & mask) {
300bf215546Sopenharmony_ci            if (index == bm->filled) {
301bf215546Sopenharmony_ci               ++bm->filled;
302bf215546Sopenharmony_ci               assert(bm->filled <= bm->size);
303bf215546Sopenharmony_ci            }
304bf215546Sopenharmony_ci            return index;
305bf215546Sopenharmony_ci         }
306bf215546Sopenharmony_ci         ++index;
307bf215546Sopenharmony_ci         ++bit;
308bf215546Sopenharmony_ci         mask <<= 1;
309bf215546Sopenharmony_ci      }
310bf215546Sopenharmony_ci      ++word;
311bf215546Sopenharmony_ci      bit = 0;
312bf215546Sopenharmony_ci      mask = 1;
313bf215546Sopenharmony_ci   }
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_ci   return UTIL_BITMASK_INVALID_INDEX;
316bf215546Sopenharmony_ci}
317bf215546Sopenharmony_ci
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ciunsigned
320bf215546Sopenharmony_ciutil_bitmask_get_first_index(struct util_bitmask *bm)
321bf215546Sopenharmony_ci{
322bf215546Sopenharmony_ci   return util_bitmask_get_next_index(bm, 0);
323bf215546Sopenharmony_ci}
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci
326bf215546Sopenharmony_civoid
327bf215546Sopenharmony_ciutil_bitmask_destroy(struct util_bitmask *bm)
328bf215546Sopenharmony_ci{
329bf215546Sopenharmony_ci   if (bm) {
330bf215546Sopenharmony_ci      FREE(bm->words);
331bf215546Sopenharmony_ci      FREE(bm);
332bf215546Sopenharmony_ci   }
333bf215546Sopenharmony_ci}
334