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