2 Unix SMB/CIFS implementation.
3 simple bitmap functions
4 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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/>.
22 #include "lib/util/bitmap.h"
23 #include "lib/util/debug.h"
24 #include "lib/util/fault.h"
28 uint32_t b
[1]; /* We allocate more */
31 /* these functions provide a simple way to allocate integers from a
32 pool without repetition */
34 /****************************************************************************
36 ****************************************************************************/
37 struct bitmap
*bitmap_talloc(TALLOC_CTX
*mem_ctx
, int n
)
41 bm
= (struct bitmap
*)talloc_zero_size(
43 offsetof(struct bitmap
, b
) + sizeof(uint32_t)*((n
+31)/32));
47 talloc_set_name_const(bm
, "struct bitmap");
53 /****************************************************************************
54 copy as much of the source bitmap as will fit in the destination bitmap.
55 ****************************************************************************/
57 int bitmap_copy(struct bitmap
* const dst
, const struct bitmap
* const src
)
59 int count
= MIN(dst
->n
, src
->n
);
61 SMB_ASSERT(dst
->b
!= src
->b
);
62 memcpy(dst
->b
, src
->b
, sizeof(uint32_t)*((count
+31)/32));
67 /****************************************************************************
69 ****************************************************************************/
70 bool bitmap_set(struct bitmap
*bm
, unsigned i
)
73 DEBUG(0,("Setting invalid bitmap entry %d (of %d)\n",
77 bm
->b
[i
/32] |= (1<<(i
%32));
81 /****************************************************************************
82 clear a bit in a bitmap
83 ****************************************************************************/
84 bool bitmap_clear(struct bitmap
*bm
, unsigned i
)
87 DEBUG(0,("clearing invalid bitmap entry %d (of %d)\n",
91 bm
->b
[i
/32] &= ~(1<<(i
%32));
95 /****************************************************************************
96 query a bit in a bitmap
97 ****************************************************************************/
98 bool bitmap_query(struct bitmap
*bm
, unsigned i
)
100 if (i
>= bm
->n
) return false;
101 if (bm
->b
[i
/32] & (1<<(i
%32))) {
107 /****************************************************************************
108 find a zero bit in a bitmap starting at the specified offset, with
110 ****************************************************************************/
111 int bitmap_find(struct bitmap
*bm
, unsigned ofs
)
115 if (ofs
> bm
->n
) ofs
= 0;
119 if (~(bm
->b
[i
/32])) {
122 if (!bitmap_query(bm
, j
)) return j
;
124 } while (j
& 31 && j
< bm
->n
);
132 if (~(bm
->b
[i
/32])) {
135 if (!bitmap_query(bm
, j
)) return j
;
137 } while (j
& 31 && j
< bm
->n
);