Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[tprouty/samba.git] / source / lib / bitmap.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    simple bitmap functions
5    Copyright (C) Andrew Tridgell 1992-1998
6
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.
11
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.
16
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.
20 */
21
22 #include "includes.h"
23
24 /* these functions provide a simple way to allocate integers from a
25    pool without repitition */
26
27 /****************************************************************************
28 allocate a bitmap of the specified size
29 ****************************************************************************/
30 struct bitmap *bitmap_allocate(int n)
31 {
32         struct bitmap *bm;
33
34         bm = (struct bitmap *)malloc(sizeof(*bm));
35
36         if (!bm) return NULL;
37         
38         bm->n = n;
39         bm->b = (uint32 *)malloc(sizeof(bm->b[0])*(n+31)/32);
40         if (!bm->b) {
41                 SAFE_FREE(bm);
42                 return NULL;
43         }
44
45         memset(bm->b, 0, sizeof(bm->b[0])*(n+31)/32);
46
47         return bm;
48 }
49
50 /****************************************************************************
51 free a bitmap.
52 ****************************************************************************/
53
54 void bitmap_free(struct bitmap *bm)
55 {
56         if (!bm)
57                 return;
58
59         SAFE_FREE(bm->b);
60         SAFE_FREE(bm);
61 }
62
63 /****************************************************************************
64 set a bit in a bitmap
65 ****************************************************************************/
66 BOOL bitmap_set(struct bitmap *bm, unsigned i)
67 {
68         if (i >= bm->n) {
69                 DEBUG(0,("Setting invalid bitmap entry %d (of %d)\n",
70                       i, bm->n));
71                 return False;
72         }
73         bm->b[i/32] |= (1<<(i%32));
74         return True;
75 }
76
77 /****************************************************************************
78 clear a bit in a bitmap
79 ****************************************************************************/
80 BOOL bitmap_clear(struct bitmap *bm, unsigned i)
81 {
82         if (i >= bm->n) {
83                 DEBUG(0,("clearing invalid bitmap entry %d (of %d)\n",
84                       i, bm->n));
85                 return False;
86         }
87         bm->b[i/32] &= ~(1<<(i%32));
88         return True;
89 }
90
91 /****************************************************************************
92 query a bit in a bitmap
93 ****************************************************************************/
94 BOOL bitmap_query(struct bitmap *bm, unsigned i)
95 {
96         if (i >= bm->n) return False;
97         if (bm->b[i/32] & (1<<(i%32))) {
98                 return True;
99         }
100         return False;
101 }
102
103 /****************************************************************************
104 find a zero bit in a bitmap starting at the specified offset, with
105 wraparound
106 ****************************************************************************/
107 int bitmap_find(struct bitmap *bm, unsigned ofs)
108 {
109         int i, j;
110
111         if (ofs > bm->n) ofs = 0;
112
113         i = ofs;
114         while (i < bm->n) {
115                 if (~(bm->b[i/32])) {
116                         j = i;
117                         do {
118                                 if (!bitmap_query(bm, j)) return j;
119                                 j++;
120                         } while (j & 31 && j < bm->n);
121                 }
122                 i += 32;
123                 i &= ~31;
124         }
125
126         i = 0;
127         while (i < ofs) {
128                 if (~(bm->b[i/32])) {
129                         j = i;
130                         do {
131                                 if (!bitmap_query(bm, j)) return j;
132                                 j++;
133                         } while (j & 31 && j < bm->n);
134                 }
135                 i += 32;
136                 i &= ~31;
137         }
138
139         return -1;
140 }