make proto
[Samba/gbeck.git] / source / lib / bitmap.c
blob7625f529095e9ee04549862a230eeb07b0677a70
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
22 #include "includes.h"
24 extern int DEBUGLEVEL;
26 /* these functions provide a simple way to allocate integers from a
27 pool without repitition */
30 /****************************************************************************
31 allocate a bitmap of the specified size
32 ****************************************************************************/
33 struct bitmap *bitmap_allocate(int n)
35 struct bitmap *bm;
37 bm = (struct bitmap *)malloc(sizeof(*bm));
39 if (!bm) return NULL;
41 bm->n = n;
42 bm->b = (uint32 *)malloc(sizeof(bm->b[0])*(n+31)/32);
43 if (!bm->b) {
44 free(bm);
45 return NULL;
48 memset(bm->b, 0, sizeof(bm->b[0])*(n+31)/32);
50 return bm;
53 /****************************************************************************
54 free a bitmap.
55 ****************************************************************************/
57 void bitmap_free(struct bitmap *bm)
59 if (!bm)
60 return;
62 if(bm->b)
63 free(bm->b);
65 free(bm);
68 /****************************************************************************
69 set a bit in a bitmap
70 ****************************************************************************/
71 BOOL bitmap_set(struct bitmap *bm, unsigned i)
73 if (i >= bm->n) {
74 DEBUG(0,("Setting invalid bitmap entry %d (of %d)\n",
75 i, bm->n));
76 return False;
78 bm->b[i/32] |= (1<<(i%32));
79 return True;
82 /****************************************************************************
83 clear a bit in a bitmap
84 ****************************************************************************/
85 BOOL bitmap_clear(struct bitmap *bm, unsigned i)
87 if (i >= bm->n) {
88 DEBUG(0,("clearing invalid bitmap entry %d (of %d)\n",
89 i, bm->n));
90 return False;
92 bm->b[i/32] &= ~(1<<(i%32));
93 return True;
96 /****************************************************************************
97 query a bit in a bitmap
98 ****************************************************************************/
99 BOOL bitmap_query(struct bitmap *bm, unsigned i)
101 if (i >= bm->n) return False;
102 if (bm->b[i/32] & (1<<(i%32))) {
103 return True;
105 return False;
108 /****************************************************************************
109 find a zero bit in a bitmap starting at the specified offset, with
110 wraparound
111 ****************************************************************************/
112 int bitmap_find(struct bitmap *bm, unsigned ofs)
114 int i, j;
116 if (ofs > bm->n) ofs = 0;
118 i = ofs;
119 while (i < bm->n) {
120 if (~(bm->b[i/32])) {
121 j = i;
122 do {
123 if (!bitmap_query(bm, j)) return j;
124 j++;
125 } while (j & 31 && j < bm->n);
127 i += 32;
128 i &= ~31;
131 i = 0;
132 while (i < ofs) {
133 if (~(bm->b[i/32])) {
134 j = i;
135 do {
136 if (!bitmap_query(bm, j)) return j;
137 j++;
138 } while (j & 31 && j < bm->n);
140 i += 32;
141 i &= ~31;
144 return -1;