2 Unix SMB/Netbios implementation.
4 simple bitmap functions
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* these functions provide a simple way to allocate integers from a
25 pool without repetition */
27 /****************************************************************************
28 allocate a bitmap of the specified size
29 ****************************************************************************/
30 struct bitmap *bitmap_allocate(int n)
34 bm = (struct bitmap *)malloc(sizeof(*bm));
39 bm->b = (uint32 *)malloc(sizeof(bm->b[0])*(n+31)/32);
45 memset(bm->b, 0, sizeof(bm->b[0])*(n+31)/32);
50 /****************************************************************************
52 ****************************************************************************/
54 void bitmap_free(struct bitmap *bm)
63 /****************************************************************************
65 ****************************************************************************/
66 BOOL bitmap_set(struct bitmap *bm, unsigned i)
69 DEBUG(0,("Setting invalid bitmap entry %d (of %d)\n",
73 bm->b[i/32] |= (1<<(i%32));
77 /****************************************************************************
78 clear a bit in a bitmap
79 ****************************************************************************/
80 BOOL bitmap_clear(struct bitmap *bm, unsigned i)
83 DEBUG(0,("clearing invalid bitmap entry %d (of %d)\n",
87 bm->b[i/32] &= ~(1<<(i%32));
91 /****************************************************************************
92 query a bit in a bitmap
93 ****************************************************************************/
94 BOOL bitmap_query(struct bitmap *bm, unsigned i)
96 if (i >= bm->n) return False;
97 if (bm->b[i/32] & (1<<(i%32))) {
103 /****************************************************************************
104 find a zero bit in a bitmap starting at the specified offset, with
106 ****************************************************************************/
107 int bitmap_find(struct bitmap *bm, unsigned ofs)
111 if (ofs > bm->n) ofs = 0;
115 if (~(bm->b[i/32])) {
118 if (!bitmap_query(bm, j)) return j;
120 } while (j & 31 && j < bm->n);
128 if (~(bm->b[i/32])) {
131 if (!bitmap_query(bm, j)) return j;
133 } while (j & 31 && j < bm->n);