Subversion Repositories Kolibri OS

Rev

Rev 5270 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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