few edits
[Samba.git] / source / lib / bitmap.c
blob6a327a5d95f4e7d30858cc5c71a04103f9e1dace
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 /* these functions provide a simple way to allocate integers from a
25 pool without repitition */
28 /****************************************************************************
29 allocate a bitmap of the specified size
30 ****************************************************************************/
31 struct bitmap *bitmap_allocate(int n)
33 struct bitmap *bm;
35 bm = (struct bitmap *)malloc(sizeof(*bm));
37 if (!bm) return NULL;
39 bm->n = n;
40 bm->b = (uint32 *)malloc(sizeof(bm->b[0])*(n+31)/32);
41 if (!bm->b) {
42 SAFE_FREE(bm);
43 return NULL;
46 memset(bm->b, 0, sizeof(bm->b[0])*(n+31)/32);
48 return bm;
51 /****************************************************************************
52 free a bitmap.
53 ****************************************************************************/
55 void bitmap_free(struct bitmap *bm)
57 if (!bm)
58 return;
60 SAFE_FREE(bm->b);
61 SAFE_FREE(bm);
64 /****************************************************************************
65 set a bit in a bitmap
66 ****************************************************************************/
67 BOOL bitmap_set(struct bitmap *bm, unsigned i)
69 if (i >= bm->n) {
70 DEBUG(0,("Setting invalid bitmap entry %d (of %d)\n",
71 i, bm->n));
72 return False;
74 bm->b[i/32] |= (1<<(i%32));
75 return True;
78 /****************************************************************************
79 clear a bit in a bitmap
80 ****************************************************************************/
81 BOOL bitmap_clear(struct bitmap *bm, unsigned i)
83 if (i >= bm->n) {
84 DEBUG(0,("clearing invalid bitmap entry %d (of %d)\n",
85 i, bm->n));
86 return False;
88 bm->b[i/32] &= ~(1<<(i%32));
89 return True;
92 /****************************************************************************
93 query a bit in a bitmap
94 ****************************************************************************/
95 BOOL bitmap_query(struct bitmap *bm, unsigned i)
97 if (i >= bm->n) return False;
98 if (bm->b[i/32] & (1<<(i%32))) {
99 return True;
101 return False;
104 /****************************************************************************
105 find a zero bit in a bitmap starting at the specified offset, with
106 wraparound
107 ****************************************************************************/
108 int bitmap_find(struct bitmap *bm, unsigned ofs)
110 int i, j;
112 if (ofs > bm->n) ofs = 0;
114 i = ofs;
115 while (i < bm->n) {
116 if (~(bm->b[i/32])) {
117 j = i;
118 do {
119 if (!bitmap_query(bm, j)) return j;
120 j++;
121 } while (j & 31 && j < bm->n);
123 i += 32;
124 i &= ~31;
127 i = 0;
128 while (i < ofs) {
129 if (~(bm->b[i/32])) {
130 j = i;
131 do {
132 if (!bitmap_query(bm, j)) return j;
133 j++;
134 } while (j & 31 && j < bm->n);
136 i += 32;
137 i &= ~31;
140 return -1;