Janitorial duties to make autogen.sh portable.
[Samba/gebeck_regimport.git] / source3 / lib / bitmap.c
blob1023dd6541d8ea0f0a7be70d4c1dfce9e17e0474
1 /*
2 Unix SMB/CIFS implementation.
3 simple bitmap functions
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 /* these functions provide a simple way to allocate integers from a
24 pool without repetition */
26 /****************************************************************************
27 allocate a bitmap of the specified size
28 ****************************************************************************/
29 struct bitmap *bitmap_allocate(int n)
31 struct bitmap *bm;
33 bm = (struct bitmap *)malloc(sizeof(*bm));
35 if (!bm) return NULL;
37 bm->n = n;
38 bm->b = (uint32 *)malloc(sizeof(bm->b[0])*(n+31)/32);
39 if (!bm->b) {
40 SAFE_FREE(bm);
41 return NULL;
44 memset(bm->b, 0, sizeof(bm->b[0])*(n+31)/32);
46 return bm;
49 /****************************************************************************
50 free a bitmap.
51 ****************************************************************************/
53 void bitmap_free(struct bitmap *bm)
55 if (!bm)
56 return;
58 SAFE_FREE(bm->b);
59 SAFE_FREE(bm);
62 /****************************************************************************
63 talloc a bitmap
64 ****************************************************************************/
65 struct bitmap *bitmap_talloc(TALLOC_CTX *mem_ctx, int n)
67 struct bitmap *bm;
69 if (!mem_ctx) return NULL;
71 bm = (struct bitmap *)talloc(mem_ctx, sizeof(*bm));
73 if (!bm) return NULL;
75 bm->n = n;
76 bm->b = (uint32 *)talloc(mem_ctx, sizeof(bm->b[0])*(n+31)/32);
77 if (!bm->b) {
78 return NULL;
81 memset(bm->b, 0, sizeof(bm->b[0])*(n+31)/32);
83 return bm;
86 /****************************************************************************
87 set a bit in a bitmap
88 ****************************************************************************/
89 BOOL bitmap_set(struct bitmap *bm, unsigned i)
91 if (i >= bm->n) {
92 DEBUG(0,("Setting invalid bitmap entry %d (of %d)\n",
93 i, bm->n));
94 return False;
96 bm->b[i/32] |= (1<<(i%32));
97 return True;
100 /****************************************************************************
101 clear a bit in a bitmap
102 ****************************************************************************/
103 BOOL bitmap_clear(struct bitmap *bm, unsigned i)
105 if (i >= bm->n) {
106 DEBUG(0,("clearing invalid bitmap entry %d (of %d)\n",
107 i, bm->n));
108 return False;
110 bm->b[i/32] &= ~(1<<(i%32));
111 return True;
114 /****************************************************************************
115 query a bit in a bitmap
116 ****************************************************************************/
117 BOOL bitmap_query(struct bitmap *bm, unsigned i)
119 if (i >= bm->n) return False;
120 if (bm->b[i/32] & (1<<(i%32))) {
121 return True;
123 return False;
126 /****************************************************************************
127 find a zero bit in a bitmap starting at the specified offset, with
128 wraparound
129 ****************************************************************************/
130 int bitmap_find(struct bitmap *bm, unsigned ofs)
132 unsigned int i, j;
134 if (ofs > bm->n) ofs = 0;
136 i = ofs;
137 while (i < bm->n) {
138 if (~(bm->b[i/32])) {
139 j = i;
140 do {
141 if (!bitmap_query(bm, j)) return j;
142 j++;
143 } while (j & 31 && j < bm->n);
145 i += 32;
146 i &= ~31;
149 i = 0;
150 while (i < ofs) {
151 if (~(bm->b[i/32])) {
152 j = i;
153 do {
154 if (!bitmap_query(bm, j)) return j;
155 j++;
156 } while (j & 31 && j < bm->n);
158 i += 32;
159 i &= ~31;
162 return -1;