Subversion Repositories Kolibri OS

Rev

Rev 5056 | Rev 6082 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5056 Rev 5270
1
/*
1
/*
2
 * lib/bitmap.c
2
 * lib/bitmap.c
3
 * Helper functions for bitmap.h.
3
 * Helper functions for bitmap.h.
4
 *
4
 *
5
 * This source code is licensed under the GNU General Public License,
5
 * This source code is licensed under the GNU General Public License,
6
 * Version 2.  See the file COPYING for more details.
6
 * Version 2.  See the file COPYING for more details.
7
 */
7
 */
8
#include 
8
#include 
9
#include 
9
#include 
10
//#include 
10
//#include 
11
#include 
11
#include 
12
#include 
12
#include 
13
#include 
13
#include 
14
#include 
14
#include 
15
#include 
15
#include 
16
//#include 
16
//#include 
17
 
17
 
18
/*
18
/*
19
 * bitmaps provide an array of bits, implemented using an an
19
 * bitmaps provide an array of bits, implemented using an an
20
 * array of unsigned longs.  The number of valid bits in a
20
 * array of unsigned longs.  The number of valid bits in a
21
 * given bitmap does _not_ need to be an exact multiple of
21
 * given bitmap does _not_ need to be an exact multiple of
22
 * BITS_PER_LONG.
22
 * BITS_PER_LONG.
23
 *
23
 *
24
 * The possible unused bits in the last, partially used word
24
 * The possible unused bits in the last, partially used word
25
 * of a bitmap are 'don't care'.  The implementation makes
25
 * of a bitmap are 'don't care'.  The implementation makes
26
 * no particular effort to keep them zero.  It ensures that
26
 * no particular effort to keep them zero.  It ensures that
27
 * their value will not affect the results of any operation.
27
 * their value will not affect the results of any operation.
28
 * The bitmap operations that return Boolean (bitmap_empty,
28
 * The bitmap operations that return Boolean (bitmap_empty,
29
 * for example) or scalar (bitmap_weight, for example) results
29
 * for example) or scalar (bitmap_weight, for example) results
30
 * carefully filter out these unused bits from impacting their
30
 * carefully filter out these unused bits from impacting their
31
 * results.
31
 * results.
32
 *
32
 *
33
 * These operations actually hold to a slightly stronger rule:
33
 * These operations actually hold to a slightly stronger rule:
34
 * if you don't input any bitmaps to these ops that have some
34
 * if you don't input any bitmaps to these ops that have some
35
 * unused bits set, then they won't output any set unused bits
35
 * unused bits set, then they won't output any set unused bits
36
 * in output bitmaps.
36
 * in output bitmaps.
37
 *
37
 *
38
 * The byte ordering of bitmaps is more natural on little
38
 * The byte ordering of bitmaps is more natural on little
39
 * endian architectures.  See the big-endian headers
39
 * endian architectures.  See the big-endian headers
40
 * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
40
 * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
41
 * for the best explanations of this ordering.
41
 * for the best explanations of this ordering.
42
 */
42
 */
43
 
43
 
44
int __bitmap_empty(const unsigned long *bitmap, unsigned int bits)
44
int __bitmap_empty(const unsigned long *bitmap, unsigned int bits)
45
{
45
{
46
	unsigned int k, lim = bits/BITS_PER_LONG;
46
	unsigned int k, lim = bits/BITS_PER_LONG;
47
	for (k = 0; k < lim; ++k)
47
	for (k = 0; k < lim; ++k)
48
		if (bitmap[k])
48
		if (bitmap[k])
49
			return 0;
49
			return 0;
50
 
50
 
51
	if (bits % BITS_PER_LONG)
51
	if (bits % BITS_PER_LONG)
52
		if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
52
		if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
53
			return 0;
53
			return 0;
54
 
54
 
55
	return 1;
55
	return 1;
56
}
56
}
57
EXPORT_SYMBOL(__bitmap_empty);
57
EXPORT_SYMBOL(__bitmap_empty);
58
 
58
 
59
int __bitmap_full(const unsigned long *bitmap, unsigned int bits)
59
int __bitmap_full(const unsigned long *bitmap, unsigned int bits)
60
{
60
{
61
	unsigned int k, lim = bits/BITS_PER_LONG;
61
	unsigned int k, lim = bits/BITS_PER_LONG;
62
	for (k = 0; k < lim; ++k)
62
	for (k = 0; k < lim; ++k)
63
		if (~bitmap[k])
63
		if (~bitmap[k])
64
			return 0;
64
			return 0;
65
 
65
 
66
	if (bits % BITS_PER_LONG)
66
	if (bits % BITS_PER_LONG)
67
		if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
67
		if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
68
			return 0;
68
			return 0;
69
 
69
 
70
	return 1;
70
	return 1;
71
}
71
}
72
EXPORT_SYMBOL(__bitmap_full);
72
EXPORT_SYMBOL(__bitmap_full);
73
 
73
 
74
int __bitmap_equal(const unsigned long *bitmap1,
74
int __bitmap_equal(const unsigned long *bitmap1,
75
		const unsigned long *bitmap2, unsigned int bits)
75
		const unsigned long *bitmap2, unsigned int bits)
76
{
76
{
77
	unsigned int k, lim = bits/BITS_PER_LONG;
77
	unsigned int k, lim = bits/BITS_PER_LONG;
78
	for (k = 0; k < lim; ++k)
78
	for (k = 0; k < lim; ++k)
79
		if (bitmap1[k] != bitmap2[k])
79
		if (bitmap1[k] != bitmap2[k])
80
			return 0;
80
			return 0;
81
 
81
 
82
	if (bits % BITS_PER_LONG)
82
	if (bits % BITS_PER_LONG)
83
		if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
83
		if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
84
			return 0;
84
			return 0;
85
 
85
 
86
	return 1;
86
	return 1;
87
}
87
}
88
EXPORT_SYMBOL(__bitmap_equal);
88
EXPORT_SYMBOL(__bitmap_equal);
89
 
89
 
90
void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
90
void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
91
{
91
{
92
	unsigned int k, lim = bits/BITS_PER_LONG;
92
	unsigned int k, lim = bits/BITS_PER_LONG;
93
	for (k = 0; k < lim; ++k)
93
	for (k = 0; k < lim; ++k)
94
		dst[k] = ~src[k];
94
		dst[k] = ~src[k];
95
 
95
 
96
	if (bits % BITS_PER_LONG)
96
	if (bits % BITS_PER_LONG)
97
		dst[k] = ~src[k];
97
		dst[k] = ~src[k];
98
}
98
}
99
EXPORT_SYMBOL(__bitmap_complement);
99
EXPORT_SYMBOL(__bitmap_complement);
100
 
100
 
101
/**
101
/**
102
 * __bitmap_shift_right - logical right shift of the bits in a bitmap
102
 * __bitmap_shift_right - logical right shift of the bits in a bitmap
103
 *   @dst : destination bitmap
103
 *   @dst : destination bitmap
104
 *   @src : source bitmap
104
 *   @src : source bitmap
105
 *   @shift : shift by this many bits
105
 *   @shift : shift by this many bits
106
 *   @bits : bitmap size, in bits
106
 *   @bits : bitmap size, in bits
107
 *
107
 *
108
 * Shifting right (dividing) means moving bits in the MS -> LS bit
108
 * Shifting right (dividing) means moving bits in the MS -> LS bit
109
 * direction.  Zeros are fed into the vacated MS positions and the
109
 * direction.  Zeros are fed into the vacated MS positions and the
110
 * LS bits shifted off the bottom are lost.
110
 * LS bits shifted off the bottom are lost.
111
 */
111
 */
112
void __bitmap_shift_right(unsigned long *dst,
112
void __bitmap_shift_right(unsigned long *dst,
113
			const unsigned long *src, int shift, int bits)
113
			const unsigned long *src, int shift, int bits)
114
{
114
{
115
	int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
115
	int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
116
	int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
116
	int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
117
	unsigned long mask = (1UL << left) - 1;
117
	unsigned long mask = (1UL << left) - 1;
118
	for (k = 0; off + k < lim; ++k) {
118
	for (k = 0; off + k < lim; ++k) {
119
		unsigned long upper, lower;
119
		unsigned long upper, lower;
120
 
120
 
121
		/*
121
		/*
122
		 * If shift is not word aligned, take lower rem bits of
122
		 * If shift is not word aligned, take lower rem bits of
123
		 * word above and make them the top rem bits of result.
123
		 * word above and make them the top rem bits of result.
124
		 */
124
		 */
125
		if (!rem || off + k + 1 >= lim)
125
		if (!rem || off + k + 1 >= lim)
126
			upper = 0;
126
			upper = 0;
127
		else {
127
		else {
128
			upper = src[off + k + 1];
128
			upper = src[off + k + 1];
129
			if (off + k + 1 == lim - 1 && left)
129
			if (off + k + 1 == lim - 1 && left)
130
				upper &= mask;
130
				upper &= mask;
131
		}
131
		}
132
		lower = src[off + k];
132
		lower = src[off + k];
133
		if (left && off + k == lim - 1)
133
		if (left && off + k == lim - 1)
134
			lower &= mask;
134
			lower &= mask;
-
 
135
		dst[k] = lower >> rem;
-
 
136
		if (rem)
135
		dst[k] = upper << (BITS_PER_LONG - rem) | lower >> rem;
137
			dst[k] |= upper << (BITS_PER_LONG - rem);
136
		if (left && k == lim - 1)
138
		if (left && k == lim - 1)
137
			dst[k] &= mask;
139
			dst[k] &= mask;
138
	}
140
	}
139
	if (off)
141
	if (off)
140
		memset(&dst[lim - off], 0, off*sizeof(unsigned long));
142
		memset(&dst[lim - off], 0, off*sizeof(unsigned long));
141
}
143
}
142
EXPORT_SYMBOL(__bitmap_shift_right);
144
EXPORT_SYMBOL(__bitmap_shift_right);
143
 
145
 
144
 
146
 
145
/**
147
/**
146
 * __bitmap_shift_left - logical left shift of the bits in a bitmap
148
 * __bitmap_shift_left - logical left shift of the bits in a bitmap
147
 *   @dst : destination bitmap
149
 *   @dst : destination bitmap
148
 *   @src : source bitmap
150
 *   @src : source bitmap
149
 *   @shift : shift by this many bits
151
 *   @shift : shift by this many bits
150
 *   @bits : bitmap size, in bits
152
 *   @bits : bitmap size, in bits
151
 *
153
 *
152
 * Shifting left (multiplying) means moving bits in the LS -> MS
154
 * Shifting left (multiplying) means moving bits in the LS -> MS
153
 * direction.  Zeros are fed into the vacated LS bit positions
155
 * direction.  Zeros are fed into the vacated LS bit positions
154
 * and those MS bits shifted off the top are lost.
156
 * and those MS bits shifted off the top are lost.
155
 */
157
 */
156
 
158
 
157
void __bitmap_shift_left(unsigned long *dst,
159
void __bitmap_shift_left(unsigned long *dst,
158
			const unsigned long *src, int shift, int bits)
160
			const unsigned long *src, int shift, int bits)
159
{
161
{
160
	int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
162
	int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
161
	int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
163
	int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
162
	for (k = lim - off - 1; k >= 0; --k) {
164
	for (k = lim - off - 1; k >= 0; --k) {
163
		unsigned long upper, lower;
165
		unsigned long upper, lower;
164
 
166
 
165
		/*
167
		/*
166
		 * If shift is not word aligned, take upper rem bits of
168
		 * If shift is not word aligned, take upper rem bits of
167
		 * word below and make them the bottom rem bits of result.
169
		 * word below and make them the bottom rem bits of result.
168
		 */
170
		 */
169
		if (rem && k > 0)
171
		if (rem && k > 0)
170
			lower = src[k - 1];
172
			lower = src[k - 1];
171
		else
173
		else
172
			lower = 0;
174
			lower = 0;
173
		upper = src[k];
175
		upper = src[k];
174
		if (left && k == lim - 1)
176
		if (left && k == lim - 1)
175
			upper &= (1UL << left) - 1;
177
			upper &= (1UL << left) - 1;
-
 
178
		dst[k + off] = upper << rem;
-
 
179
		if (rem)
176
		dst[k + off] = lower  >> (BITS_PER_LONG - rem) | upper << rem;
180
			dst[k + off] |= lower >> (BITS_PER_LONG - rem);
177
		if (left && k + off == lim - 1)
181
		if (left && k + off == lim - 1)
178
			dst[k + off] &= (1UL << left) - 1;
182
			dst[k + off] &= (1UL << left) - 1;
179
	}
183
	}
180
	if (off)
184
	if (off)
181
		memset(dst, 0, off*sizeof(unsigned long));
185
		memset(dst, 0, off*sizeof(unsigned long));
182
}
186
}
183
EXPORT_SYMBOL(__bitmap_shift_left);
187
EXPORT_SYMBOL(__bitmap_shift_left);
184
 
188
 
185
int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
189
int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
186
				const unsigned long *bitmap2, unsigned int bits)
190
				const unsigned long *bitmap2, unsigned int bits)
187
{
191
{
188
	unsigned int k;
192
	unsigned int k;
189
	unsigned int lim = bits/BITS_PER_LONG;
193
	unsigned int lim = bits/BITS_PER_LONG;
190
	unsigned long result = 0;
194
	unsigned long result = 0;
191
 
195
 
192
	for (k = 0; k < lim; k++)
196
	for (k = 0; k < lim; k++)
193
		result |= (dst[k] = bitmap1[k] & bitmap2[k]);
197
		result |= (dst[k] = bitmap1[k] & bitmap2[k]);
194
	if (bits % BITS_PER_LONG)
198
	if (bits % BITS_PER_LONG)
195
		result |= (dst[k] = bitmap1[k] & bitmap2[k] &
199
		result |= (dst[k] = bitmap1[k] & bitmap2[k] &
196
			   BITMAP_LAST_WORD_MASK(bits));
200
			   BITMAP_LAST_WORD_MASK(bits));
197
	return result != 0;
201
	return result != 0;
198
}
202
}
199
EXPORT_SYMBOL(__bitmap_and);
203
EXPORT_SYMBOL(__bitmap_and);
200
 
204
 
201
void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
205
void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
202
				const unsigned long *bitmap2, unsigned int bits)
206
				const unsigned long *bitmap2, unsigned int bits)
203
{
207
{
204
	unsigned int k;
208
	unsigned int k;
205
	unsigned int nr = BITS_TO_LONGS(bits);
209
	unsigned int nr = BITS_TO_LONGS(bits);
206
 
210
 
207
	for (k = 0; k < nr; k++)
211
	for (k = 0; k < nr; k++)
208
		dst[k] = bitmap1[k] | bitmap2[k];
212
		dst[k] = bitmap1[k] | bitmap2[k];
209
}
213
}
210
EXPORT_SYMBOL(__bitmap_or);
214
EXPORT_SYMBOL(__bitmap_or);
211
 
215
 
212
void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
216
void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
213
				const unsigned long *bitmap2, unsigned int bits)
217
				const unsigned long *bitmap2, unsigned int bits)
214
{
218
{
215
	unsigned int k;
219
	unsigned int k;
216
	unsigned int nr = BITS_TO_LONGS(bits);
220
	unsigned int nr = BITS_TO_LONGS(bits);
217
 
221
 
218
	for (k = 0; k < nr; k++)
222
	for (k = 0; k < nr; k++)
219
		dst[k] = bitmap1[k] ^ bitmap2[k];
223
		dst[k] = bitmap1[k] ^ bitmap2[k];
220
}
224
}
221
EXPORT_SYMBOL(__bitmap_xor);
225
EXPORT_SYMBOL(__bitmap_xor);
222
 
226
 
223
int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
227
int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
224
				const unsigned long *bitmap2, unsigned int bits)
228
				const unsigned long *bitmap2, unsigned int bits)
225
{
229
{
226
	unsigned int k;
230
	unsigned int k;
227
	unsigned int lim = bits/BITS_PER_LONG;
231
	unsigned int lim = bits/BITS_PER_LONG;
228
	unsigned long result = 0;
232
	unsigned long result = 0;
229
 
233
 
230
	for (k = 0; k < lim; k++)
234
	for (k = 0; k < lim; k++)
231
		result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
235
		result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
232
	if (bits % BITS_PER_LONG)
236
	if (bits % BITS_PER_LONG)
233
		result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
237
		result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
234
			   BITMAP_LAST_WORD_MASK(bits));
238
			   BITMAP_LAST_WORD_MASK(bits));
235
	return result != 0;
239
	return result != 0;
236
}
240
}
237
EXPORT_SYMBOL(__bitmap_andnot);
241
EXPORT_SYMBOL(__bitmap_andnot);
238
 
242
 
239
int __bitmap_intersects(const unsigned long *bitmap1,
243
int __bitmap_intersects(const unsigned long *bitmap1,
240
			const unsigned long *bitmap2, unsigned int bits)
244
			const unsigned long *bitmap2, unsigned int bits)
241
{
245
{
242
	unsigned int k, lim = bits/BITS_PER_LONG;
246
	unsigned int k, lim = bits/BITS_PER_LONG;
243
	for (k = 0; k < lim; ++k)
247
	for (k = 0; k < lim; ++k)
244
		if (bitmap1[k] & bitmap2[k])
248
		if (bitmap1[k] & bitmap2[k])
245
			return 1;
249
			return 1;
246
 
250
 
247
	if (bits % BITS_PER_LONG)
251
	if (bits % BITS_PER_LONG)
248
		if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
252
		if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
249
			return 1;
253
			return 1;
250
	return 0;
254
	return 0;
251
}
255
}
252
EXPORT_SYMBOL(__bitmap_intersects);
256
EXPORT_SYMBOL(__bitmap_intersects);
253
 
257
 
254
int __bitmap_subset(const unsigned long *bitmap1,
258
int __bitmap_subset(const unsigned long *bitmap1,
255
		    const unsigned long *bitmap2, unsigned int bits)
259
		    const unsigned long *bitmap2, unsigned int bits)
256
{
260
{
257
	unsigned int k, lim = bits/BITS_PER_LONG;
261
	unsigned int k, lim = bits/BITS_PER_LONG;
258
	for (k = 0; k < lim; ++k)
262
	for (k = 0; k < lim; ++k)
259
		if (bitmap1[k] & ~bitmap2[k])
263
		if (bitmap1[k] & ~bitmap2[k])
260
			return 0;
264
			return 0;
261
 
265
 
262
	if (bits % BITS_PER_LONG)
266
	if (bits % BITS_PER_LONG)
263
		if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
267
		if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
264
			return 0;
268
			return 0;
265
	return 1;
269
	return 1;
266
}
270
}
267
EXPORT_SYMBOL(__bitmap_subset);
271
EXPORT_SYMBOL(__bitmap_subset);
268
 
272
 
269
int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
273
int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
270
{
274
{
271
	unsigned int k, lim = bits/BITS_PER_LONG;
275
	unsigned int k, lim = bits/BITS_PER_LONG;
272
	int w = 0;
276
	int w = 0;
273
 
277
 
274
	for (k = 0; k < lim; k++)
278
	for (k = 0; k < lim; k++)
275
		w += hweight_long(bitmap[k]);
279
		w += hweight_long(bitmap[k]);
276
 
280
 
277
	if (bits % BITS_PER_LONG)
281
	if (bits % BITS_PER_LONG)
278
		w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
282
		w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
279
 
283
 
280
	return w;
284
	return w;
281
}
285
}
282
EXPORT_SYMBOL(__bitmap_weight);
286
EXPORT_SYMBOL(__bitmap_weight);
283
 
287
 
284
void bitmap_set(unsigned long *map, unsigned int start, int len)
288
void bitmap_set(unsigned long *map, unsigned int start, int len)
285
{
289
{
286
	unsigned long *p = map + BIT_WORD(start);
290
	unsigned long *p = map + BIT_WORD(start);
287
	const unsigned int size = start + len;
291
	const unsigned int size = start + len;
288
	int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
292
	int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
289
	unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
293
	unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
290
 
294
 
291
	while (len - bits_to_set >= 0) {
295
	while (len - bits_to_set >= 0) {
292
		*p |= mask_to_set;
296
		*p |= mask_to_set;
293
		len -= bits_to_set;
297
		len -= bits_to_set;
294
		bits_to_set = BITS_PER_LONG;
298
		bits_to_set = BITS_PER_LONG;
295
		mask_to_set = ~0UL;
299
		mask_to_set = ~0UL;
296
		p++;
300
		p++;
297
	}
301
	}
298
	if (len) {
302
	if (len) {
299
		mask_to_set &= BITMAP_LAST_WORD_MASK(size);
303
		mask_to_set &= BITMAP_LAST_WORD_MASK(size);
300
		*p |= mask_to_set;
304
		*p |= mask_to_set;
301
	}
305
	}
302
}
306
}
303
EXPORT_SYMBOL(bitmap_set);
307
EXPORT_SYMBOL(bitmap_set);
304
 
308
 
305
void bitmap_clear(unsigned long *map, unsigned int start, int len)
309
void bitmap_clear(unsigned long *map, unsigned int start, int len)
306
{
310
{
307
	unsigned long *p = map + BIT_WORD(start);
311
	unsigned long *p = map + BIT_WORD(start);
308
	const unsigned int size = start + len;
312
	const unsigned int size = start + len;
309
	int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
313
	int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
310
	unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
314
	unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
311
 
315
 
312
	while (len - bits_to_clear >= 0) {
316
	while (len - bits_to_clear >= 0) {
313
		*p &= ~mask_to_clear;
317
		*p &= ~mask_to_clear;
314
		len -= bits_to_clear;
318
		len -= bits_to_clear;
315
		bits_to_clear = BITS_PER_LONG;
319
		bits_to_clear = BITS_PER_LONG;
316
		mask_to_clear = ~0UL;
320
		mask_to_clear = ~0UL;
317
		p++;
321
		p++;
318
	}
322
	}
319
	if (len) {
323
	if (len) {
320
		mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
324
		mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
321
		*p &= ~mask_to_clear;
325
		*p &= ~mask_to_clear;
322
	}
326
	}
323
}
327
}
324
EXPORT_SYMBOL(bitmap_clear);
328
EXPORT_SYMBOL(bitmap_clear);
325
 
329
 
326
/*
330
/**
327
 * bitmap_find_next_zero_area - find a contiguous aligned zero area
331
 * bitmap_find_next_zero_area_off - find a contiguous aligned zero area
328
 * @map: The address to base the search on
332
 * @map: The address to base the search on
329
 * @size: The bitmap size in bits
333
 * @size: The bitmap size in bits
330
 * @start: The bitnumber to start searching at
334
 * @start: The bitnumber to start searching at
331
 * @nr: The number of zeroed bits we're looking for
335
 * @nr: The number of zeroed bits we're looking for
332
 * @align_mask: Alignment mask for zero area
336
 * @align_mask: Alignment mask for zero area
-
 
337
 * @align_offset: Alignment offset for zero area.
333
 *
338
 *
334
 * The @align_mask should be one less than a power of 2; the effect is that
339
 * The @align_mask should be one less than a power of 2; the effect is that
335
 * the bit offset of all zero areas this function finds is multiples of that
340
 * the bit offset of all zero areas this function finds plus @align_offset
336
 * power of 2. A @align_mask of 0 means no alignment is required.
341
 * is multiple of that power of 2.
337
 */
342
 */
338
unsigned long bitmap_find_next_zero_area(unsigned long *map,
343
unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
339
					 unsigned long size,
344
					 unsigned long size,
340
					 unsigned long start,
345
					 unsigned long start,
341
					 unsigned int nr,
346
					 unsigned int nr,
342
					 unsigned long align_mask)
347
					     unsigned long align_mask,
-
 
348
					     unsigned long align_offset)
343
{
349
{
344
	unsigned long index, end, i;
350
	unsigned long index, end, i;
345
again:
351
again:
346
	index = find_next_zero_bit(map, size, start);
352
	index = find_next_zero_bit(map, size, start);
347
 
353
 
348
	/* Align allocation */
354
	/* Align allocation */
349
	index = __ALIGN_MASK(index, align_mask);
355
	index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;
350
 
356
 
351
	end = index + nr;
357
	end = index + nr;
352
	if (end > size)
358
	if (end > size)
353
		return end;
359
		return end;
354
	i = find_next_bit(map, end, index);
360
	i = find_next_bit(map, end, index);
355
	if (i < end) {
361
	if (i < end) {
356
		start = i + 1;
362
		start = i + 1;
357
		goto again;
363
		goto again;
358
	}
364
	}
359
	return index;
365
	return index;
360
}
366
}
361
EXPORT_SYMBOL(bitmap_find_next_zero_area);
367
EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
362
 
368
 
363
/*
369
/*
364
 * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers,
370
 * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers,
365
 * second version by Paul Jackson, third by Joe Korty.
371
 * second version by Paul Jackson, third by Joe Korty.
366
 */
372
 */
367
 
373
 
368
#define CHUNKSZ				32
374
#define CHUNKSZ				32
369
#define nbits_to_hold_value(val)	fls(val)
375
#define nbits_to_hold_value(val)	fls(val)
370
#define BASEDEC 10		/* fancier cpuset lists input in decimal */
376
#define BASEDEC 10		/* fancier cpuset lists input in decimal */
371
 
377
 
372
 
378
 
373
 
379
 
374
 
380
 
375
 
381
 
376
/**
382
/**
377
 * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
383
 * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
378
 *	@buf: pointer to a bitmap
384
 *	@buf: pointer to a bitmap
379
 *	@pos: a bit position in @buf (0 <= @pos < @bits)
385
 *	@pos: a bit position in @buf (0 <= @pos < @bits)
380
 *	@bits: number of valid bit positions in @buf
386
 *	@bits: number of valid bit positions in @buf
381
 *
387
 *
382
 * Map the bit at position @pos in @buf (of length @bits) to the
388
 * Map the bit at position @pos in @buf (of length @bits) to the
383
 * ordinal of which set bit it is.  If it is not set or if @pos
389
 * ordinal of which set bit it is.  If it is not set or if @pos
384
 * is not a valid bit position, map to -1.
390
 * is not a valid bit position, map to -1.
385
 *
391
 *
386
 * If for example, just bits 4 through 7 are set in @buf, then @pos
392
 * If for example, just bits 4 through 7 are set in @buf, then @pos
387
 * values 4 through 7 will get mapped to 0 through 3, respectively,
393
 * values 4 through 7 will get mapped to 0 through 3, respectively,
388
 * and other @pos values will get mapped to -1.  When @pos value 7
394
 * and other @pos values will get mapped to -1.  When @pos value 7
389
 * gets mapped to (returns) @ord value 3 in this example, that means
395
 * gets mapped to (returns) @ord value 3 in this example, that means
390
 * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
396
 * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
391
 *
397
 *
392
 * The bit positions 0 through @bits are valid positions in @buf.
398
 * The bit positions 0 through @bits are valid positions in @buf.
393
 */
399
 */
394
static int bitmap_pos_to_ord(const unsigned long *buf, int pos, int bits)
400
static int bitmap_pos_to_ord(const unsigned long *buf, int pos, int bits)
395
{
401
{
396
	int i, ord;
402
	int i, ord;
397
 
403
 
398
	if (pos < 0 || pos >= bits || !test_bit(pos, buf))
404
	if (pos < 0 || pos >= bits || !test_bit(pos, buf))
399
		return -1;
405
		return -1;
400
 
406
 
401
	i = find_first_bit(buf, bits);
407
	i = find_first_bit(buf, bits);
402
	ord = 0;
408
	ord = 0;
403
	while (i < pos) {
409
	while (i < pos) {
404
		i = find_next_bit(buf, bits, i + 1);
410
		i = find_next_bit(buf, bits, i + 1);
405
	     	ord++;
411
	     	ord++;
406
	}
412
	}
407
	BUG_ON(i != pos);
413
	BUG_ON(i != pos);
408
 
414
 
409
	return ord;
415
	return ord;
410
}
416
}
411
 
417
 
412
/**
418
/**
413
 * bitmap_ord_to_pos - find position of n-th set bit in bitmap
419
 * bitmap_ord_to_pos - find position of n-th set bit in bitmap
414
 *	@buf: pointer to bitmap
420
 *	@buf: pointer to bitmap
415
 *	@ord: ordinal bit position (n-th set bit, n >= 0)
421
 *	@ord: ordinal bit position (n-th set bit, n >= 0)
416
 *	@bits: number of valid bit positions in @buf
422
 *	@bits: number of valid bit positions in @buf
417
 *
423
 *
418
 * Map the ordinal offset of bit @ord in @buf to its position in @buf.
424
 * Map the ordinal offset of bit @ord in @buf to its position in @buf.
419
 * Value of @ord should be in range 0 <= @ord < weight(buf), else
425
 * Value of @ord should be in range 0 <= @ord < weight(buf), else
420
 * results are undefined.
426
 * results are undefined.
421
 *
427
 *
422
 * If for example, just bits 4 through 7 are set in @buf, then @ord
428
 * If for example, just bits 4 through 7 are set in @buf, then @ord
423
 * values 0 through 3 will get mapped to 4 through 7, respectively,
429
 * values 0 through 3 will get mapped to 4 through 7, respectively,
424
 * and all other @ord values return undefined values.  When @ord value 3
430
 * and all other @ord values return undefined values.  When @ord value 3
425
 * gets mapped to (returns) @pos value 7 in this example, that means
431
 * gets mapped to (returns) @pos value 7 in this example, that means
426
 * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
432
 * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
427
 *
433
 *
428
 * The bit positions 0 through @bits are valid positions in @buf.
434
 * The bit positions 0 through @bits are valid positions in @buf.
429
 */
435
 */
430
int bitmap_ord_to_pos(const unsigned long *buf, int ord, int bits)
436
int bitmap_ord_to_pos(const unsigned long *buf, int ord, int bits)
431
{
437
{
432
	int pos = 0;
438
	int pos = 0;
433
 
439
 
434
	if (ord >= 0 && ord < bits) {
440
	if (ord >= 0 && ord < bits) {
435
		int i;
441
		int i;
436
 
442
 
437
		for (i = find_first_bit(buf, bits);
443
		for (i = find_first_bit(buf, bits);
438
		     i < bits && ord > 0;
444
		     i < bits && ord > 0;
439
		     i = find_next_bit(buf, bits, i + 1))
445
		     i = find_next_bit(buf, bits, i + 1))
440
	     		ord--;
446
	     		ord--;
441
		if (i < bits && ord == 0)
447
		if (i < bits && ord == 0)
442
			pos = i;
448
			pos = i;
443
	}
449
	}
444
 
450
 
445
	return pos;
451
	return pos;
446
}
452
}
447
 
453
 
448
/**
454
/**
449
 * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
455
 * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
450
 *	@dst: remapped result
456
 *	@dst: remapped result
451
 *	@src: subset to be remapped
457
 *	@src: subset to be remapped
452
 *	@old: defines domain of map
458
 *	@old: defines domain of map
453
 *	@new: defines range of map
459
 *	@new: defines range of map
454
 *	@bits: number of bits in each of these bitmaps
460
 *	@bits: number of bits in each of these bitmaps
455
 *
461
 *
456
 * Let @old and @new define a mapping of bit positions, such that
462
 * Let @old and @new define a mapping of bit positions, such that
457
 * whatever position is held by the n-th set bit in @old is mapped
463
 * whatever position is held by the n-th set bit in @old is mapped
458
 * to the n-th set bit in @new.  In the more general case, allowing
464
 * to the n-th set bit in @new.  In the more general case, allowing
459
 * for the possibility that the weight 'w' of @new is less than the
465
 * for the possibility that the weight 'w' of @new is less than the
460
 * weight of @old, map the position of the n-th set bit in @old to
466
 * weight of @old, map the position of the n-th set bit in @old to
461
 * the position of the m-th set bit in @new, where m == n % w.
467
 * the position of the m-th set bit in @new, where m == n % w.
462
 *
468
 *
463
 * If either of the @old and @new bitmaps are empty, or if @src and
469
 * If either of the @old and @new bitmaps are empty, or if @src and
464
 * @dst point to the same location, then this routine copies @src
470
 * @dst point to the same location, then this routine copies @src
465
 * to @dst.
471
 * to @dst.
466
 *
472
 *
467
 * The positions of unset bits in @old are mapped to themselves
473
 * The positions of unset bits in @old are mapped to themselves
468
 * (the identify map).
474
 * (the identify map).
469
 *
475
 *
470
 * Apply the above specified mapping to @src, placing the result in
476
 * Apply the above specified mapping to @src, placing the result in
471
 * @dst, clearing any bits previously set in @dst.
477
 * @dst, clearing any bits previously set in @dst.
472
 *
478
 *
473
 * For example, lets say that @old has bits 4 through 7 set, and
479
 * For example, lets say that @old has bits 4 through 7 set, and
474
 * @new has bits 12 through 15 set.  This defines the mapping of bit
480
 * @new has bits 12 through 15 set.  This defines the mapping of bit
475
 * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
481
 * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
476
 * bit positions unchanged.  So if say @src comes into this routine
482
 * bit positions unchanged.  So if say @src comes into this routine
477
 * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
483
 * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
478
 * 13 and 15 set.
484
 * 13 and 15 set.
479
 */
485
 */
480
void bitmap_remap(unsigned long *dst, const unsigned long *src,
486
void bitmap_remap(unsigned long *dst, const unsigned long *src,
481
		const unsigned long *old, const unsigned long *new,
487
		const unsigned long *old, const unsigned long *new,
482
		int bits)
488
		int bits)
483
{
489
{
484
	int oldbit, w;
490
	int oldbit, w;
485
 
491
 
486
	if (dst == src)		/* following doesn't handle inplace remaps */
492
	if (dst == src)		/* following doesn't handle inplace remaps */
487
		return;
493
		return;
488
	bitmap_zero(dst, bits);
494
	bitmap_zero(dst, bits);
489
 
495
 
490
	w = bitmap_weight(new, bits);
496
	w = bitmap_weight(new, bits);
491
	for_each_set_bit(oldbit, src, bits) {
497
	for_each_set_bit(oldbit, src, bits) {
492
	     	int n = bitmap_pos_to_ord(old, oldbit, bits);
498
	     	int n = bitmap_pos_to_ord(old, oldbit, bits);
493
 
499
 
494
		if (n < 0 || w == 0)
500
		if (n < 0 || w == 0)
495
			set_bit(oldbit, dst);	/* identity map */
501
			set_bit(oldbit, dst);	/* identity map */
496
		else
502
		else
497
			set_bit(bitmap_ord_to_pos(new, n % w, bits), dst);
503
			set_bit(bitmap_ord_to_pos(new, n % w, bits), dst);
498
	}
504
	}
499
}
505
}
500
EXPORT_SYMBOL(bitmap_remap);
506
EXPORT_SYMBOL(bitmap_remap);
501
 
507
 
502
/**
508
/**
503
 * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
509
 * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
504
 *	@oldbit: bit position to be mapped
510
 *	@oldbit: bit position to be mapped
505
 *	@old: defines domain of map
511
 *	@old: defines domain of map
506
 *	@new: defines range of map
512
 *	@new: defines range of map
507
 *	@bits: number of bits in each of these bitmaps
513
 *	@bits: number of bits in each of these bitmaps
508
 *
514
 *
509
 * Let @old and @new define a mapping of bit positions, such that
515
 * Let @old and @new define a mapping of bit positions, such that
510
 * whatever position is held by the n-th set bit in @old is mapped
516
 * whatever position is held by the n-th set bit in @old is mapped
511
 * to the n-th set bit in @new.  In the more general case, allowing
517
 * to the n-th set bit in @new.  In the more general case, allowing
512
 * for the possibility that the weight 'w' of @new is less than the
518
 * for the possibility that the weight 'w' of @new is less than the
513
 * weight of @old, map the position of the n-th set bit in @old to
519
 * weight of @old, map the position of the n-th set bit in @old to
514
 * the position of the m-th set bit in @new, where m == n % w.
520
 * the position of the m-th set bit in @new, where m == n % w.
515
 *
521
 *
516
 * The positions of unset bits in @old are mapped to themselves
522
 * The positions of unset bits in @old are mapped to themselves
517
 * (the identify map).
523
 * (the identify map).
518
 *
524
 *
519
 * Apply the above specified mapping to bit position @oldbit, returning
525
 * Apply the above specified mapping to bit position @oldbit, returning
520
 * the new bit position.
526
 * the new bit position.
521
 *
527
 *
522
 * For example, lets say that @old has bits 4 through 7 set, and
528
 * For example, lets say that @old has bits 4 through 7 set, and
523
 * @new has bits 12 through 15 set.  This defines the mapping of bit
529
 * @new has bits 12 through 15 set.  This defines the mapping of bit
524
 * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
530
 * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
525
 * bit positions unchanged.  So if say @oldbit is 5, then this routine
531
 * bit positions unchanged.  So if say @oldbit is 5, then this routine
526
 * returns 13.
532
 * returns 13.
527
 */
533
 */
528
int bitmap_bitremap(int oldbit, const unsigned long *old,
534
int bitmap_bitremap(int oldbit, const unsigned long *old,
529
				const unsigned long *new, int bits)
535
				const unsigned long *new, int bits)
530
{
536
{
531
	int w = bitmap_weight(new, bits);
537
	int w = bitmap_weight(new, bits);
532
	int n = bitmap_pos_to_ord(old, oldbit, bits);
538
	int n = bitmap_pos_to_ord(old, oldbit, bits);
533
	if (n < 0 || w == 0)
539
	if (n < 0 || w == 0)
534
		return oldbit;
540
		return oldbit;
535
	else
541
	else
536
		return bitmap_ord_to_pos(new, n % w, bits);
542
		return bitmap_ord_to_pos(new, n % w, bits);
537
}
543
}
538
EXPORT_SYMBOL(bitmap_bitremap);
544
EXPORT_SYMBOL(bitmap_bitremap);
539
 
545
 
540
/**
546
/**
541
 * bitmap_onto - translate one bitmap relative to another
547
 * bitmap_onto - translate one bitmap relative to another
542
 *	@dst: resulting translated bitmap
548
 *	@dst: resulting translated bitmap
543
 * 	@orig: original untranslated bitmap
549
 * 	@orig: original untranslated bitmap
544
 * 	@relmap: bitmap relative to which translated
550
 * 	@relmap: bitmap relative to which translated
545
 *	@bits: number of bits in each of these bitmaps
551
 *	@bits: number of bits in each of these bitmaps
546
 *
552
 *
547
 * Set the n-th bit of @dst iff there exists some m such that the
553
 * Set the n-th bit of @dst iff there exists some m such that the
548
 * n-th bit of @relmap is set, the m-th bit of @orig is set, and
554
 * n-th bit of @relmap is set, the m-th bit of @orig is set, and
549
 * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
555
 * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
550
 * (If you understood the previous sentence the first time your
556
 * (If you understood the previous sentence the first time your
551
 * read it, you're overqualified for your current job.)
557
 * read it, you're overqualified for your current job.)
552
 *
558
 *
553
 * In other words, @orig is mapped onto (surjectively) @dst,
559
 * In other words, @orig is mapped onto (surjectively) @dst,
554
 * using the the map {  | the n-th bit of @relmap is the
560
 * using the the map {  | the n-th bit of @relmap is the
555
 * m-th set bit of @relmap }.
561
 * m-th set bit of @relmap }.
556
 *
562
 *
557
 * Any set bits in @orig above bit number W, where W is the
563
 * Any set bits in @orig above bit number W, where W is the
558
 * weight of (number of set bits in) @relmap are mapped nowhere.
564
 * weight of (number of set bits in) @relmap are mapped nowhere.
559
 * In particular, if for all bits m set in @orig, m >= W, then
565
 * In particular, if for all bits m set in @orig, m >= W, then
560
 * @dst will end up empty.  In situations where the possibility
566
 * @dst will end up empty.  In situations where the possibility
561
 * of such an empty result is not desired, one way to avoid it is
567
 * of such an empty result is not desired, one way to avoid it is
562
 * to use the bitmap_fold() operator, below, to first fold the
568
 * to use the bitmap_fold() operator, below, to first fold the
563
 * @orig bitmap over itself so that all its set bits x are in the
569
 * @orig bitmap over itself so that all its set bits x are in the
564
 * range 0 <= x < W.  The bitmap_fold() operator does this by
570
 * range 0 <= x < W.  The bitmap_fold() operator does this by
565
 * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
571
 * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
566
 *
572
 *
567
 * Example [1] for bitmap_onto():
573
 * Example [1] for bitmap_onto():
568
 *  Let's say @relmap has bits 30-39 set, and @orig has bits
574
 *  Let's say @relmap has bits 30-39 set, and @orig has bits
569
 *  1, 3, 5, 7, 9 and 11 set.  Then on return from this routine,
575
 *  1, 3, 5, 7, 9 and 11 set.  Then on return from this routine,
570
 *  @dst will have bits 31, 33, 35, 37 and 39 set.
576
 *  @dst will have bits 31, 33, 35, 37 and 39 set.
571
 *
577
 *
572
 *  When bit 0 is set in @orig, it means turn on the bit in
578
 *  When bit 0 is set in @orig, it means turn on the bit in
573
 *  @dst corresponding to whatever is the first bit (if any)
579
 *  @dst corresponding to whatever is the first bit (if any)
574
 *  that is turned on in @relmap.  Since bit 0 was off in the
580
 *  that is turned on in @relmap.  Since bit 0 was off in the
575
 *  above example, we leave off that bit (bit 30) in @dst.
581
 *  above example, we leave off that bit (bit 30) in @dst.
576
 *
582
 *
577
 *  When bit 1 is set in @orig (as in the above example), it
583
 *  When bit 1 is set in @orig (as in the above example), it
578
 *  means turn on the bit in @dst corresponding to whatever
584
 *  means turn on the bit in @dst corresponding to whatever
579
 *  is the second bit that is turned on in @relmap.  The second
585
 *  is the second bit that is turned on in @relmap.  The second
580
 *  bit in @relmap that was turned on in the above example was
586
 *  bit in @relmap that was turned on in the above example was
581
 *  bit 31, so we turned on bit 31 in @dst.
587
 *  bit 31, so we turned on bit 31 in @dst.
582
 *
588
 *
583
 *  Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
589
 *  Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
584
 *  because they were the 4th, 6th, 8th and 10th set bits
590
 *  because they were the 4th, 6th, 8th and 10th set bits
585
 *  set in @relmap, and the 4th, 6th, 8th and 10th bits of
591
 *  set in @relmap, and the 4th, 6th, 8th and 10th bits of
586
 *  @orig (i.e. bits 3, 5, 7 and 9) were also set.
592
 *  @orig (i.e. bits 3, 5, 7 and 9) were also set.
587
 *
593
 *
588
 *  When bit 11 is set in @orig, it means turn on the bit in
594
 *  When bit 11 is set in @orig, it means turn on the bit in
589
 *  @dst corresponding to whatever is the twelfth bit that is
595
 *  @dst corresponding to whatever is the twelfth bit that is
590
 *  turned on in @relmap.  In the above example, there were
596
 *  turned on in @relmap.  In the above example, there were
591
 *  only ten bits turned on in @relmap (30..39), so that bit
597
 *  only ten bits turned on in @relmap (30..39), so that bit
592
 *  11 was set in @orig had no affect on @dst.
598
 *  11 was set in @orig had no affect on @dst.
593
 *
599
 *
594
 * Example [2] for bitmap_fold() + bitmap_onto():
600
 * Example [2] for bitmap_fold() + bitmap_onto():
595
 *  Let's say @relmap has these ten bits set:
601
 *  Let's say @relmap has these ten bits set:
596
 *		40 41 42 43 45 48 53 61 74 95
602
 *		40 41 42 43 45 48 53 61 74 95
597
 *  (for the curious, that's 40 plus the first ten terms of the
603
 *  (for the curious, that's 40 plus the first ten terms of the
598
 *  Fibonacci sequence.)
604
 *  Fibonacci sequence.)
599
 *
605
 *
600
 *  Further lets say we use the following code, invoking
606
 *  Further lets say we use the following code, invoking
601
 *  bitmap_fold() then bitmap_onto, as suggested above to
607
 *  bitmap_fold() then bitmap_onto, as suggested above to
602
 *  avoid the possitility of an empty @dst result:
608
 *  avoid the possibility of an empty @dst result:
603
 *
609
 *
604
 *	unsigned long *tmp;	// a temporary bitmap's bits
610
 *	unsigned long *tmp;	// a temporary bitmap's bits
605
 *
611
 *
606
 *	bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
612
 *	bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
607
 *	bitmap_onto(dst, tmp, relmap, bits);
613
 *	bitmap_onto(dst, tmp, relmap, bits);
608
 *
614
 *
609
 *  Then this table shows what various values of @dst would be, for
615
 *  Then this table shows what various values of @dst would be, for
610
 *  various @orig's.  I list the zero-based positions of each set bit.
616
 *  various @orig's.  I list the zero-based positions of each set bit.
611
 *  The tmp column shows the intermediate result, as computed by
617
 *  The tmp column shows the intermediate result, as computed by
612
 *  using bitmap_fold() to fold the @orig bitmap modulo ten
618
 *  using bitmap_fold() to fold the @orig bitmap modulo ten
613
 *  (the weight of @relmap).
619
 *  (the weight of @relmap).
614
 *
620
 *
615
 *      @orig           tmp            @dst
621
 *      @orig           tmp            @dst
616
 *      0                0             40
622
 *      0                0             40
617
 *      1                1             41
623
 *      1                1             41
618
 *      9                9             95
624
 *      9                9             95
619
 *      10               0             40 (*)
625
 *      10               0             40 (*)
620
 *      1 3 5 7          1 3 5 7       41 43 48 61
626
 *      1 3 5 7          1 3 5 7       41 43 48 61
621
 *      0 1 2 3 4        0 1 2 3 4     40 41 42 43 45
627
 *      0 1 2 3 4        0 1 2 3 4     40 41 42 43 45
622
 *      0 9 18 27        0 9 8 7       40 61 74 95
628
 *      0 9 18 27        0 9 8 7       40 61 74 95
623
 *      0 10 20 30       0             40
629
 *      0 10 20 30       0             40
624
 *      0 11 22 33       0 1 2 3       40 41 42 43
630
 *      0 11 22 33       0 1 2 3       40 41 42 43
625
 *      0 12 24 36       0 2 4 6       40 42 45 53
631
 *      0 12 24 36       0 2 4 6       40 42 45 53
626
 *      78 102 211       1 2 8         41 42 74 (*)
632
 *      78 102 211       1 2 8         41 42 74 (*)
627
 *
633
 *
628
 * (*) For these marked lines, if we hadn't first done bitmap_fold()
634
 * (*) For these marked lines, if we hadn't first done bitmap_fold()
629
 *     into tmp, then the @dst result would have been empty.
635
 *     into tmp, then the @dst result would have been empty.
630
 *
636
 *
631
 * If either of @orig or @relmap is empty (no set bits), then @dst
637
 * If either of @orig or @relmap is empty (no set bits), then @dst
632
 * will be returned empty.
638
 * will be returned empty.
633
 *
639
 *
634
 * If (as explained above) the only set bits in @orig are in positions
640
 * If (as explained above) the only set bits in @orig are in positions
635
 * m where m >= W, (where W is the weight of @relmap) then @dst will
641
 * m where m >= W, (where W is the weight of @relmap) then @dst will
636
 * once again be returned empty.
642
 * once again be returned empty.
637
 *
643
 *
638
 * All bits in @dst not set by the above rule are cleared.
644
 * All bits in @dst not set by the above rule are cleared.
639
 */
645
 */
640
void bitmap_onto(unsigned long *dst, const unsigned long *orig,
646
void bitmap_onto(unsigned long *dst, const unsigned long *orig,
641
			const unsigned long *relmap, int bits)
647
			const unsigned long *relmap, int bits)
642
{
648
{
643
	int n, m;       	/* same meaning as in above comment */
649
	int n, m;       	/* same meaning as in above comment */
644
 
650
 
645
	if (dst == orig)	/* following doesn't handle inplace mappings */
651
	if (dst == orig)	/* following doesn't handle inplace mappings */
646
		return;
652
		return;
647
	bitmap_zero(dst, bits);
653
	bitmap_zero(dst, bits);
648
 
654
 
649
	/*
655
	/*
650
	 * The following code is a more efficient, but less
656
	 * The following code is a more efficient, but less
651
	 * obvious, equivalent to the loop:
657
	 * obvious, equivalent to the loop:
652
	 *	for (m = 0; m < bitmap_weight(relmap, bits); m++) {
658
	 *	for (m = 0; m < bitmap_weight(relmap, bits); m++) {
653
	 *		n = bitmap_ord_to_pos(orig, m, bits);
659
	 *		n = bitmap_ord_to_pos(orig, m, bits);
654
	 *		if (test_bit(m, orig))
660
	 *		if (test_bit(m, orig))
655
	 *			set_bit(n, dst);
661
	 *			set_bit(n, dst);
656
	 *	}
662
	 *	}
657
	 */
663
	 */
658
 
664
 
659
	m = 0;
665
	m = 0;
660
	for_each_set_bit(n, relmap, bits) {
666
	for_each_set_bit(n, relmap, bits) {
661
		/* m == bitmap_pos_to_ord(relmap, n, bits) */
667
		/* m == bitmap_pos_to_ord(relmap, n, bits) */
662
		if (test_bit(m, orig))
668
		if (test_bit(m, orig))
663
			set_bit(n, dst);
669
			set_bit(n, dst);
664
		m++;
670
		m++;
665
	}
671
	}
666
}
672
}
667
EXPORT_SYMBOL(bitmap_onto);
673
EXPORT_SYMBOL(bitmap_onto);
668
 
674
 
669
/**
675
/**
670
 * bitmap_fold - fold larger bitmap into smaller, modulo specified size
676
 * bitmap_fold - fold larger bitmap into smaller, modulo specified size
671
 *	@dst: resulting smaller bitmap
677
 *	@dst: resulting smaller bitmap
672
 *	@orig: original larger bitmap
678
 *	@orig: original larger bitmap
673
 *	@sz: specified size
679
 *	@sz: specified size
674
 *	@bits: number of bits in each of these bitmaps
680
 *	@bits: number of bits in each of these bitmaps
675
 *
681
 *
676
 * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
682
 * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
677
 * Clear all other bits in @dst.  See further the comment and
683
 * Clear all other bits in @dst.  See further the comment and
678
 * Example [2] for bitmap_onto() for why and how to use this.
684
 * Example [2] for bitmap_onto() for why and how to use this.
679
 */
685
 */
680
void bitmap_fold(unsigned long *dst, const unsigned long *orig,
686
void bitmap_fold(unsigned long *dst, const unsigned long *orig,
681
			int sz, int bits)
687
			int sz, int bits)
682
{
688
{
683
	int oldbit;
689
	int oldbit;
684
 
690
 
685
	if (dst == orig)	/* following doesn't handle inplace mappings */
691
	if (dst == orig)	/* following doesn't handle inplace mappings */
686
		return;
692
		return;
687
	bitmap_zero(dst, bits);
693
	bitmap_zero(dst, bits);
688
 
694
 
689
	for_each_set_bit(oldbit, orig, bits)
695
	for_each_set_bit(oldbit, orig, bits)
690
		set_bit(oldbit % sz, dst);
696
		set_bit(oldbit % sz, dst);
691
}
697
}
692
EXPORT_SYMBOL(bitmap_fold);
698
EXPORT_SYMBOL(bitmap_fold);
693
 
699
 
694
/*
700
/*
695
 * Common code for bitmap_*_region() routines.
701
 * Common code for bitmap_*_region() routines.
696
 *	bitmap: array of unsigned longs corresponding to the bitmap
702
 *	bitmap: array of unsigned longs corresponding to the bitmap
697
 *	pos: the beginning of the region
703
 *	pos: the beginning of the region
698
 *	order: region size (log base 2 of number of bits)
704
 *	order: region size (log base 2 of number of bits)
699
 *	reg_op: operation(s) to perform on that region of bitmap
705
 *	reg_op: operation(s) to perform on that region of bitmap
700
 *
706
 *
701
 * Can set, verify and/or release a region of bits in a bitmap,
707
 * Can set, verify and/or release a region of bits in a bitmap,
702
 * depending on which combination of REG_OP_* flag bits is set.
708
 * depending on which combination of REG_OP_* flag bits is set.
703
 *
709
 *
704
 * A region of a bitmap is a sequence of bits in the bitmap, of
710
 * A region of a bitmap is a sequence of bits in the bitmap, of
705
 * some size '1 << order' (a power of two), aligned to that same
711
 * some size '1 << order' (a power of two), aligned to that same
706
 * '1 << order' power of two.
712
 * '1 << order' power of two.
707
 *
713
 *
708
 * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
714
 * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
709
 * Returns 0 in all other cases and reg_ops.
715
 * Returns 0 in all other cases and reg_ops.
710
 */
716
 */
711
 
717
 
712
enum {
718
enum {
713
	REG_OP_ISFREE,		/* true if region is all zero bits */
719
	REG_OP_ISFREE,		/* true if region is all zero bits */
714
	REG_OP_ALLOC,		/* set all bits in region */
720
	REG_OP_ALLOC,		/* set all bits in region */
715
	REG_OP_RELEASE,		/* clear all bits in region */
721
	REG_OP_RELEASE,		/* clear all bits in region */
716
};
722
};
717
 
723
 
718
static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
724
static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
719
{
725
{
720
	int nbits_reg;		/* number of bits in region */
726
	int nbits_reg;		/* number of bits in region */
721
	int index;		/* index first long of region in bitmap */
727
	int index;		/* index first long of region in bitmap */
722
	int offset;		/* bit offset region in bitmap[index] */
728
	int offset;		/* bit offset region in bitmap[index] */
723
	int nlongs_reg;		/* num longs spanned by region in bitmap */
729
	int nlongs_reg;		/* num longs spanned by region in bitmap */
724
	int nbitsinlong;	/* num bits of region in each spanned long */
730
	int nbitsinlong;	/* num bits of region in each spanned long */
725
	unsigned long mask;	/* bitmask for one long of region */
731
	unsigned long mask;	/* bitmask for one long of region */
726
	int i;			/* scans bitmap by longs */
732
	int i;			/* scans bitmap by longs */
727
	int ret = 0;		/* return value */
733
	int ret = 0;		/* return value */
728
 
734
 
729
	/*
735
	/*
730
	 * Either nlongs_reg == 1 (for small orders that fit in one long)
736
	 * Either nlongs_reg == 1 (for small orders that fit in one long)
731
	 * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
737
	 * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
732
	 */
738
	 */
733
	nbits_reg = 1 << order;
739
	nbits_reg = 1 << order;
734
	index = pos / BITS_PER_LONG;
740
	index = pos / BITS_PER_LONG;
735
	offset = pos - (index * BITS_PER_LONG);
741
	offset = pos - (index * BITS_PER_LONG);
736
	nlongs_reg = BITS_TO_LONGS(nbits_reg);
742
	nlongs_reg = BITS_TO_LONGS(nbits_reg);
737
	nbitsinlong = min(nbits_reg,  BITS_PER_LONG);
743
	nbitsinlong = min(nbits_reg,  BITS_PER_LONG);
738
 
744
 
739
	/*
745
	/*
740
	 * Can't do "mask = (1UL << nbitsinlong) - 1", as that
746
	 * Can't do "mask = (1UL << nbitsinlong) - 1", as that
741
	 * overflows if nbitsinlong == BITS_PER_LONG.
747
	 * overflows if nbitsinlong == BITS_PER_LONG.
742
	 */
748
	 */
743
	mask = (1UL << (nbitsinlong - 1));
749
	mask = (1UL << (nbitsinlong - 1));
744
	mask += mask - 1;
750
	mask += mask - 1;
745
	mask <<= offset;
751
	mask <<= offset;
746
 
752
 
747
	switch (reg_op) {
753
	switch (reg_op) {
748
	case REG_OP_ISFREE:
754
	case REG_OP_ISFREE:
749
		for (i = 0; i < nlongs_reg; i++) {
755
		for (i = 0; i < nlongs_reg; i++) {
750
			if (bitmap[index + i] & mask)
756
			if (bitmap[index + i] & mask)
751
				goto done;
757
				goto done;
752
		}
758
		}
753
		ret = 1;	/* all bits in region free (zero) */
759
		ret = 1;	/* all bits in region free (zero) */
754
		break;
760
		break;
755
 
761
 
756
	case REG_OP_ALLOC:
762
	case REG_OP_ALLOC:
757
		for (i = 0; i < nlongs_reg; i++)
763
		for (i = 0; i < nlongs_reg; i++)
758
			bitmap[index + i] |= mask;
764
			bitmap[index + i] |= mask;
759
		break;
765
		break;
760
 
766
 
761
	case REG_OP_RELEASE:
767
	case REG_OP_RELEASE:
762
		for (i = 0; i < nlongs_reg; i++)
768
		for (i = 0; i < nlongs_reg; i++)
763
			bitmap[index + i] &= ~mask;
769
			bitmap[index + i] &= ~mask;
764
		break;
770
		break;
765
	}
771
	}
766
done:
772
done:
767
	return ret;
773
	return ret;
768
}
774
}
769
 
775
 
770
/**
776
/**
771
 * bitmap_find_free_region - find a contiguous aligned mem region
777
 * bitmap_find_free_region - find a contiguous aligned mem region
772
 *	@bitmap: array of unsigned longs corresponding to the bitmap
778
 *	@bitmap: array of unsigned longs corresponding to the bitmap
773
 *	@bits: number of bits in the bitmap
779
 *	@bits: number of bits in the bitmap
774
 *	@order: region size (log base 2 of number of bits) to find
780
 *	@order: region size (log base 2 of number of bits) to find
775
 *
781
 *
776
 * Find a region of free (zero) bits in a @bitmap of @bits bits and
782
 * Find a region of free (zero) bits in a @bitmap of @bits bits and
777
 * allocate them (set them to one).  Only consider regions of length
783
 * allocate them (set them to one).  Only consider regions of length
778
 * a power (@order) of two, aligned to that power of two, which
784
 * a power (@order) of two, aligned to that power of two, which
779
 * makes the search algorithm much faster.
785
 * makes the search algorithm much faster.
780
 *
786
 *
781
 * Return the bit offset in bitmap of the allocated region,
787
 * Return the bit offset in bitmap of the allocated region,
782
 * or -errno on failure.
788
 * or -errno on failure.
783
 */
789
 */
784
int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
790
int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
785
{
791
{
786
	unsigned int pos, end;		/* scans bitmap by regions of size order */
792
	unsigned int pos, end;		/* scans bitmap by regions of size order */
787
 
793
 
788
	for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
794
	for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
789
		if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
795
		if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
790
			continue;
796
			continue;
791
		__reg_op(bitmap, pos, order, REG_OP_ALLOC);
797
		__reg_op(bitmap, pos, order, REG_OP_ALLOC);
792
		return pos;
798
		return pos;
793
	}
799
	}
794
	return -ENOMEM;
800
	return -ENOMEM;
795
}
801
}
796
EXPORT_SYMBOL(bitmap_find_free_region);
802
EXPORT_SYMBOL(bitmap_find_free_region);
797
 
803
 
798
/**
804
/**
799
 * bitmap_release_region - release allocated bitmap region
805
 * bitmap_release_region - release allocated bitmap region
800
 *	@bitmap: array of unsigned longs corresponding to the bitmap
806
 *	@bitmap: array of unsigned longs corresponding to the bitmap
801
 *	@pos: beginning of bit region to release
807
 *	@pos: beginning of bit region to release
802
 *	@order: region size (log base 2 of number of bits) to release
808
 *	@order: region size (log base 2 of number of bits) to release
803
 *
809
 *
804
 * This is the complement to __bitmap_find_free_region() and releases
810
 * This is the complement to __bitmap_find_free_region() and releases
805
 * the found region (by clearing it in the bitmap).
811
 * the found region (by clearing it in the bitmap).
806
 *
812
 *
807
 * No return value.
813
 * No return value.
808
 */
814
 */
809
void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
815
void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
810
{
816
{
811
	__reg_op(bitmap, pos, order, REG_OP_RELEASE);
817
	__reg_op(bitmap, pos, order, REG_OP_RELEASE);
812
}
818
}
813
EXPORT_SYMBOL(bitmap_release_region);
819
EXPORT_SYMBOL(bitmap_release_region);
814
 
820
 
815
/**
821
/**
816
 * bitmap_allocate_region - allocate bitmap region
822
 * bitmap_allocate_region - allocate bitmap region
817
 *	@bitmap: array of unsigned longs corresponding to the bitmap
823
 *	@bitmap: array of unsigned longs corresponding to the bitmap
818
 *	@pos: beginning of bit region to allocate
824
 *	@pos: beginning of bit region to allocate
819
 *	@order: region size (log base 2 of number of bits) to allocate
825
 *	@order: region size (log base 2 of number of bits) to allocate
820
 *
826
 *
821
 * Allocate (set bits in) a specified region of a bitmap.
827
 * Allocate (set bits in) a specified region of a bitmap.
822
 *
828
 *
823
 * Return 0 on success, or %-EBUSY if specified region wasn't
829
 * Return 0 on success, or %-EBUSY if specified region wasn't
824
 * free (not all bits were zero).
830
 * free (not all bits were zero).
825
 */
831
 */
826
int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
832
int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
827
{
833
{
828
	if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
834
	if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
829
		return -EBUSY;
835
		return -EBUSY;
830
	return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
836
	return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
831
}
837
}
832
EXPORT_SYMBOL(bitmap_allocate_region);
838
EXPORT_SYMBOL(bitmap_allocate_region);
833
 
839
 
834
/**
840
/**
835
 * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
841
 * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
836
 * @dst:   destination buffer
842
 * @dst:   destination buffer
837
 * @src:   bitmap to copy
843
 * @src:   bitmap to copy
838
 * @nbits: number of bits in the bitmap
844
 * @nbits: number of bits in the bitmap
839
 *
845
 *
840
 * Require nbits % BITS_PER_LONG == 0.
846
 * Require nbits % BITS_PER_LONG == 0.
841
 */
847
 */
842
void bitmap_copy_le(void *dst, const unsigned long *src, int nbits)
848
void bitmap_copy_le(void *dst, const unsigned long *src, int nbits)
843
{
849
{
844
	unsigned long *d = dst;
850
	unsigned long *d = dst;
845
	int i;
851
	int i;
846
 
852
 
847
	for (i = 0; i < nbits/BITS_PER_LONG; i++) {
853
	for (i = 0; i < nbits/BITS_PER_LONG; i++) {
848
		if (BITS_PER_LONG == 64)
854
		if (BITS_PER_LONG == 64)
849
			d[i] = cpu_to_le64(src[i]);
855
			d[i] = cpu_to_le64(src[i]);
850
		else
856
		else
851
			d[i] = cpu_to_le32(src[i]);
857
			d[i] = cpu_to_le32(src[i]);
852
	}
858
	}
853
}
859
}
854
EXPORT_SYMBOL(bitmap_copy_le);
860
EXPORT_SYMBOL(bitmap_copy_le);