Subversion Repositories Kolibri OS

Rev

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