Update 4.2 Roadmap file
[mat/samba.git] / lib / util / bitmap.c
1 /*
2    Unix SMB/CIFS implementation.
3    simple bitmap functions
4    Copyright (C) Andrew Tridgell 1992-1998
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/util/bitmap.h"
22
23 struct bitmap {
24         unsigned int n;
25         uint32_t b[1];          /* We allocate more */
26 };
27
28 /* these functions provide a simple way to allocate integers from a
29    pool without repetition */
30
31 /****************************************************************************
32 talloc a bitmap
33 ****************************************************************************/
34 struct bitmap *bitmap_talloc(TALLOC_CTX *mem_ctx, int n)
35 {
36         struct bitmap *bm;
37
38         bm = (struct bitmap *)talloc_zero_size(
39                 mem_ctx,
40                 offsetof(struct bitmap, b) + sizeof(uint32_t)*((n+31)/32));
41
42         if (!bm) return NULL;
43
44         talloc_set_name_const(bm, "struct bitmap");
45
46         bm->n = n;
47         return bm;
48 }
49
50 /****************************************************************************
51 copy as much of the source bitmap as will fit in the destination bitmap.
52 ****************************************************************************/
53
54 int bitmap_copy(struct bitmap * const dst, const struct bitmap * const src)
55 {
56         int count = MIN(dst->n, src->n);
57
58         SMB_ASSERT(dst->b != src->b);
59         memcpy(dst->b, src->b, sizeof(uint32_t)*((count+31)/32));
60
61         return count;
62 }
63
64 /****************************************************************************
65 set a bit in a bitmap
66 ****************************************************************************/
67 bool bitmap_set(struct bitmap *bm, unsigned i)
68 {
69         if (i >= bm->n) {
70                 DEBUG(0,("Setting invalid bitmap entry %d (of %d)\n",
71                       i, bm->n));
72                 return false;
73         }
74         bm->b[i/32] |= (1<<(i%32));
75         return true;
76 }
77
78 /****************************************************************************
79 clear a bit in a bitmap
80 ****************************************************************************/
81 bool bitmap_clear(struct bitmap *bm, unsigned i)
82 {
83         if (i >= bm->n) {
84                 DEBUG(0,("clearing invalid bitmap entry %d (of %d)\n",
85                       i, bm->n));
86                 return false;
87         }
88         bm->b[i/32] &= ~(1<<(i%32));
89         return true;
90 }
91
92 /****************************************************************************
93 query a bit in a bitmap
94 ****************************************************************************/
95 bool bitmap_query(struct bitmap *bm, unsigned i)
96 {
97         if (i >= bm->n) return false;
98         if (bm->b[i/32] & (1<<(i%32))) {
99                 return true;
100         }
101         return false;
102 }
103
104 /****************************************************************************
105 find a zero bit in a bitmap starting at the specified offset, with
106 wraparound
107 ****************************************************************************/
108 int bitmap_find(struct bitmap *bm, unsigned ofs)
109 {
110         unsigned int i, j;
111
112         if (ofs > bm->n) ofs = 0;
113
114         i = ofs;
115         while (i < bm->n) {
116                 if (~(bm->b[i/32])) {
117                         j = i;
118                         do {
119                                 if (!bitmap_query(bm, j)) return j;
120                                 j++;
121                         } while (j & 31 && j < bm->n);
122                 }
123                 i += 32;
124                 i &= ~31;
125         }
126
127         i = 0;
128         while (i < ofs) {
129                 if (~(bm->b[i/32])) {
130                         j = i;
131                         do {
132                                 if (!bitmap_query(bm, j)) return j;
133                                 j++;
134                         } while (j & 31 && j < bm->n);
135                 }
136                 i += 32;
137                 i &= ~31;
138         }
139
140         return -1;
141 }