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