1ae2aaaf710a0c07aeeae8a625b62416fc82bc15
[metze/samba/wip.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         uint32_t *b;
25         unsigned int n;
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 = talloc(mem_ctx, struct bitmap);
39
40         if (!bm) return NULL;
41
42         bm->n = n;
43         bm->b = talloc_zero_array(bm, uint32_t, (n+31)/32);
44         if (!bm->b) {
45                 TALLOC_FREE(bm);
46                 return NULL;
47         }
48         return bm;
49 }
50
51 /****************************************************************************
52 copy as much of the source bitmap as will fit in the destination bitmap.
53 ****************************************************************************/
54
55 int bitmap_copy(struct bitmap * const dst, const struct bitmap * const src)
56 {
57         int count = MIN(dst->n, src->n);
58
59         SMB_ASSERT(dst->b != src->b);
60         memcpy(dst->b, src->b, sizeof(uint32_t)*((count+31)/32));
61
62         return count;
63 }
64
65 /****************************************************************************
66 set a bit in a bitmap
67 ****************************************************************************/
68 bool bitmap_set(struct bitmap *bm, unsigned i)
69 {
70         if (i >= bm->n) {
71                 DEBUG(0,("Setting invalid bitmap entry %d (of %d)\n",
72                       i, bm->n));
73                 return false;
74         }
75         bm->b[i/32] |= (1<<(i%32));
76         return true;
77 }
78
79 /****************************************************************************
80 clear a bit in a bitmap
81 ****************************************************************************/
82 bool bitmap_clear(struct bitmap *bm, unsigned i)
83 {
84         if (i >= bm->n) {
85                 DEBUG(0,("clearing invalid bitmap entry %d (of %d)\n",
86                       i, bm->n));
87                 return false;
88         }
89         bm->b[i/32] &= ~(1<<(i%32));
90         return true;
91 }
92
93 /****************************************************************************
94 query a bit in a bitmap
95 ****************************************************************************/
96 bool bitmap_query(struct bitmap *bm, unsigned i)
97 {
98         if (i >= bm->n) return false;
99         if (bm->b[i/32] & (1<<(i%32))) {
100                 return true;
101         }
102         return false;
103 }
104
105 /****************************************************************************
106 find a zero bit in a bitmap starting at the specified offset, with
107 wraparound
108 ****************************************************************************/
109 int bitmap_find(struct bitmap *bm, unsigned ofs)
110 {
111         unsigned int i, j;
112
113         if (ofs > bm->n) ofs = 0;
114
115         i = ofs;
116         while (i < bm->n) {
117                 if (~(bm->b[i/32])) {
118                         j = i;
119                         do {
120                                 if (!bitmap_query(bm, j)) return j;
121                                 j++;
122                         } while (j & 31 && j < bm->n);
123                 }
124                 i += 32;
125                 i &= ~31;
126         }
127
128         i = 0;
129         while (i < ofs) {
130                 if (~(bm->b[i/32])) {
131                         j = i;
132                         do {
133                                 if (!bitmap_query(bm, j)) return j;
134                                 j++;
135                         } while (j & 31 && j < bm->n);
136                 }
137                 i += 32;
138                 i &= ~31;
139         }
140
141         return -1;
142 }