1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __LINUX_BITMAP_H
3#define __LINUX_BITMAP_H
4
5#ifndef __ASSEMBLY__
6
7#include <linux/align.h>
8#include <linux/bitops.h>
9#include <linux/errno.h>
10#include <linux/find.h>
11#include <linux/limits.h>
12#include <linux/string.h>
13#include <linux/types.h>
14#include <linux/bitmap-str.h>
15
16struct device;
17
18/*
19 * bitmaps provide bit arrays that consume one or more unsigned
20 * longs. The bitmap interface and available operations are listed
21 * here, in bitmap.h
22 *
23 * Function implementations generic to all architectures are in
24 * lib/bitmap.c. Functions implementations that are architecture
25 * specific are in various include/asm-<arch>/bitops.h headers
26 * and other arch/<arch> specific files.
27 *
28 * See lib/bitmap.c for more details.
29 */
30
31/**
32 * DOC: bitmap overview
33 *
34 * The available bitmap operations and their rough meaning in the
35 * case that the bitmap is a single unsigned long are thus:
36 *
37 * The generated code is more efficient when nbits is known at
38 * compile-time and at most BITS_PER_LONG.
39 *
40 * ::
41 *
42 * bitmap_zero(dst, nbits) *dst = 0UL
43 * bitmap_fill(dst, nbits) *dst = ~0UL
44 * bitmap_copy(dst, src, nbits) *dst = *src
45 * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
46 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
47 * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
48 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
49 * bitmap_complement(dst, src, nbits) *dst = ~(*src)
50 * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
51 * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
52 * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2?
53 * bitmap_empty(src, nbits) Are all bits zero in *src?
54 * bitmap_full(src, nbits) Are all bits set in *src?
55 * bitmap_weight(src, nbits) Hamming Weight: number set bits
56 * bitmap_weight_and(src1, src2, nbits) Hamming Weight of and'ed bitmap
57 * bitmap_set(dst, pos, nbits) Set specified bit area
58 * bitmap_clear(dst, pos, nbits) Clear specified bit area
59 * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
60 * bitmap_find_next_zero_area_off(buf, len, pos, n, mask, mask_off) as above
61 * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
62 * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
63 * bitmap_cut(dst, src, first, n, nbits) Cut n bits from first, copy rest
64 * bitmap_replace(dst, old, new, mask, nbits) *dst = (*old & ~(*mask)) | (*new & *mask)
65 * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src)
66 * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit)
67 * bitmap_onto(dst, orig, relmap, nbits) *dst = orig relative to relmap
68 * bitmap_fold(dst, orig, sz, nbits) dst bits = orig bits mod sz
69 * bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf
70 * bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
71 * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from kernel buf
72 * bitmap_parselist_user(buf, dst, nbits) Parse bitmap dst from user buf
73 * bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region
74 * bitmap_release_region(bitmap, pos, order) Free specified bit region
75 * bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region
76 * bitmap_from_arr32(dst, buf, nbits) Copy nbits from u32[] buf to dst
77 * bitmap_from_arr64(dst, buf, nbits) Copy nbits from u64[] buf to dst
78 * bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst
79 * bitmap_to_arr64(buf, src, nbits) Copy nbits from buf to u64[] dst
80 * bitmap_get_value8(map, start) Get 8bit value from map at start
81 * bitmap_set_value8(map, value, start) Set 8bit value to map at start
82 *
83 * Note, bitmap_zero() and bitmap_fill() operate over the region of
84 * unsigned longs, that is, bits behind bitmap till the unsigned long
85 * boundary will be zeroed or filled as well. Consider to use
86 * bitmap_clear() or bitmap_set() to make explicit zeroing or filling
87 * respectively.
88 */
89
90/**
91 * DOC: bitmap bitops
92 *
93 * Also the following operations in asm/bitops.h apply to bitmaps.::
94 *
95 * set_bit(bit, addr) *addr |= bit
96 * clear_bit(bit, addr) *addr &= ~bit
97 * change_bit(bit, addr) *addr ^= bit
98 * test_bit(bit, addr) Is bit set in *addr?
99 * test_and_set_bit(bit, addr) Set bit and return old value
100 * test_and_clear_bit(bit, addr) Clear bit and return old value
101 * test_and_change_bit(bit, addr) Change bit and return old value
102 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
103 * find_first_bit(addr, nbits) Position first set bit in *addr
104 * find_next_zero_bit(addr, nbits, bit)
105 * Position next zero bit in *addr >= bit
106 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
107 * find_next_and_bit(addr1, addr2, nbits, bit)
108 * Same as find_next_bit, but in
109 * (*addr1 & *addr2)
110 *
111 */
112
113/**
114 * DOC: declare bitmap
115 * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
116 * to declare an array named 'name' of just enough unsigned longs to
117 * contain all bit positions from 0 to 'bits' - 1.
118 */
119
120/*
121 * Allocation and deallocation of bitmap.
122 * Provided in lib/bitmap.c to avoid circular dependency.
123 */
124unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
125unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
126unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
127unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
128void bitmap_free(const unsigned long *bitmap);
129
130/* Managed variants of the above. */
131unsigned long *devm_bitmap_alloc(struct device *dev,
132 unsigned int nbits, gfp_t flags);
133unsigned long *devm_bitmap_zalloc(struct device *dev,
134 unsigned int nbits, gfp_t flags);
135
136/*
137 * lib/bitmap.c provides these functions:
138 */
139
140bool __bitmap_equal(const unsigned long *bitmap1,
141 const unsigned long *bitmap2, unsigned int nbits);
142bool __pure __bitmap_or_equal(const unsigned long *src1,
143 const unsigned long *src2,
144 const unsigned long *src3,
145 unsigned int nbits);
146void __bitmap_complement(unsigned long *dst, const unsigned long *src,
147 unsigned int nbits);
148void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
149 unsigned int shift, unsigned int nbits);
150void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
151 unsigned int shift, unsigned int nbits);
152void bitmap_cut(unsigned long *dst, const unsigned long *src,
153 unsigned int first, unsigned int cut, unsigned int nbits);
154bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
155 const unsigned long *bitmap2, unsigned int nbits);
156void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
157 const unsigned long *bitmap2, unsigned int nbits);
158void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
159 const unsigned long *bitmap2, unsigned int nbits);
160bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
161 const unsigned long *bitmap2, unsigned int nbits);
162void __bitmap_replace(unsigned long *dst,
163 const unsigned long *old, const unsigned long *new,
164 const unsigned long *mask, unsigned int nbits);
165bool __bitmap_intersects(const unsigned long *bitmap1,
166 const unsigned long *bitmap2, unsigned int nbits);
167bool __bitmap_subset(const unsigned long *bitmap1,
168 const unsigned long *bitmap2, unsigned int nbits);
169unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
170unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
171 const unsigned long *bitmap2, unsigned int nbits);
172void __bitmap_set(unsigned long *map, unsigned int start, int len);
173void __bitmap_clear(unsigned long *map, unsigned int start, int len);
174
175unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
176 unsigned long size,
177 unsigned long start,
178 unsigned int nr,
179 unsigned long align_mask,
180 unsigned long align_offset);
181
182/**
183 * bitmap_find_next_zero_area - find a contiguous aligned zero area
184 * @map: The address to base the search on
185 * @size: The bitmap size in bits
186 * @start: The bitnumber to start searching at
187 * @nr: The number of zeroed bits we're looking for
188 * @align_mask: Alignment mask for zero area
189 *
190 * The @align_mask should be one less than a power of 2; the effect is that
191 * the bit offset of all zero areas this function finds is multiples of that
192 * power of 2. A @align_mask of 0 means no alignment is required.
193 */
194static inline unsigned long
195bitmap_find_next_zero_area(unsigned long *map,
196 unsigned long size,
197 unsigned long start,
198 unsigned int nr,
199 unsigned long align_mask)
200{
201 return bitmap_find_next_zero_area_off(map, size, start, nr,
202 align_mask, align_offset: 0);
203}
204
205void bitmap_remap(unsigned long *dst, const unsigned long *src,
206 const unsigned long *old, const unsigned long *new, unsigned int nbits);
207int bitmap_bitremap(int oldbit,
208 const unsigned long *old, const unsigned long *new, int bits);
209void bitmap_onto(unsigned long *dst, const unsigned long *orig,
210 const unsigned long *relmap, unsigned int bits);
211void bitmap_fold(unsigned long *dst, const unsigned long *orig,
212 unsigned int sz, unsigned int nbits);
213
214#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
215#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
216
217static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
218{
219 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
220
221 if (small_const_nbits(nbits))
222 *dst = 0;
223 else
224 memset(dst, 0, len);
225}
226
227static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
228{
229 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
230
231 if (small_const_nbits(nbits))
232 *dst = ~0UL;
233 else
234 memset(dst, 0xff, len);
235}
236
237static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
238 unsigned int nbits)
239{
240 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
241
242 if (small_const_nbits(nbits))
243 *dst = *src;
244 else
245 memcpy(dst, src, len);
246}
247
248/*
249 * Copy bitmap and clear tail bits in last word.
250 */
251static inline void bitmap_copy_clear_tail(unsigned long *dst,
252 const unsigned long *src, unsigned int nbits)
253{
254 bitmap_copy(dst, src, nbits);
255 if (nbits % BITS_PER_LONG)
256 dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
257}
258
259/*
260 * On 32-bit systems bitmaps are represented as u32 arrays internally. On LE64
261 * machines the order of hi and lo parts of numbers match the bitmap structure.
262 * In both cases conversion is not needed when copying data from/to arrays of
263 * u32. But in LE64 case, typecast in bitmap_copy_clear_tail() may lead
264 * to out-of-bound access. To avoid that, both LE and BE variants of 64-bit
265 * architectures are not using bitmap_copy_clear_tail().
266 */
267#if BITS_PER_LONG == 64
268void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
269 unsigned int nbits);
270void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
271 unsigned int nbits);
272#else
273#define bitmap_from_arr32(bitmap, buf, nbits) \
274 bitmap_copy_clear_tail((unsigned long *) (bitmap), \
275 (const unsigned long *) (buf), (nbits))
276#define bitmap_to_arr32(buf, bitmap, nbits) \
277 bitmap_copy_clear_tail((unsigned long *) (buf), \
278 (const unsigned long *) (bitmap), (nbits))
279#endif
280
281/*
282 * On 64-bit systems bitmaps are represented as u64 arrays internally. So,
283 * the conversion is not needed when copying data from/to arrays of u64.
284 */
285#if BITS_PER_LONG == 32
286void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits);
287void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits);
288#else
289#define bitmap_from_arr64(bitmap, buf, nbits) \
290 bitmap_copy_clear_tail((unsigned long *)(bitmap), (const unsigned long *)(buf), (nbits))
291#define bitmap_to_arr64(buf, bitmap, nbits) \
292 bitmap_copy_clear_tail((unsigned long *)(buf), (const unsigned long *)(bitmap), (nbits))
293#endif
294
295static inline bool bitmap_and(unsigned long *dst, const unsigned long *src1,
296 const unsigned long *src2, unsigned int nbits)
297{
298 if (small_const_nbits(nbits))
299 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
300 return __bitmap_and(dst, bitmap1: src1, bitmap2: src2, nbits);
301}
302
303static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
304 const unsigned long *src2, unsigned int nbits)
305{
306 if (small_const_nbits(nbits))
307 *dst = *src1 | *src2;
308 else
309 __bitmap_or(dst, bitmap1: src1, bitmap2: src2, nbits);
310}
311
312static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
313 const unsigned long *src2, unsigned int nbits)
314{
315 if (small_const_nbits(nbits))
316 *dst = *src1 ^ *src2;
317 else
318 __bitmap_xor(dst, bitmap1: src1, bitmap2: src2, nbits);
319}
320
321static inline bool bitmap_andnot(unsigned long *dst, const unsigned long *src1,
322 const unsigned long *src2, unsigned int nbits)
323{
324 if (small_const_nbits(nbits))
325 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
326 return __bitmap_andnot(dst, bitmap1: src1, bitmap2: src2, nbits);
327}
328
329static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
330 unsigned int nbits)
331{
332 if (small_const_nbits(nbits))
333 *dst = ~(*src);
334 else
335 __bitmap_complement(dst, src, nbits);
336}
337
338#ifdef __LITTLE_ENDIAN
339#define BITMAP_MEM_ALIGNMENT 8
340#else
341#define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
342#endif
343#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
344
345static inline bool bitmap_equal(const unsigned long *src1,
346 const unsigned long *src2, unsigned int nbits)
347{
348 if (small_const_nbits(nbits))
349 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
350 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
351 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
352 return !memcmp(p: src1, q: src2, size: nbits / 8);
353 return __bitmap_equal(bitmap1: src1, bitmap2: src2, nbits);
354}
355
356/**
357 * bitmap_or_equal - Check whether the or of two bitmaps is equal to a third
358 * @src1: Pointer to bitmap 1
359 * @src2: Pointer to bitmap 2 will be or'ed with bitmap 1
360 * @src3: Pointer to bitmap 3. Compare to the result of *@src1 | *@src2
361 * @nbits: number of bits in each of these bitmaps
362 *
363 * Returns: True if (*@src1 | *@src2) == *@src3, false otherwise
364 */
365static inline bool bitmap_or_equal(const unsigned long *src1,
366 const unsigned long *src2,
367 const unsigned long *src3,
368 unsigned int nbits)
369{
370 if (!small_const_nbits(nbits))
371 return __bitmap_or_equal(src1, src2, src3, nbits);
372
373 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
374}
375
376static inline bool bitmap_intersects(const unsigned long *src1,
377 const unsigned long *src2,
378 unsigned int nbits)
379{
380 if (small_const_nbits(nbits))
381 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
382 else
383 return __bitmap_intersects(bitmap1: src1, bitmap2: src2, nbits);
384}
385
386static inline bool bitmap_subset(const unsigned long *src1,
387 const unsigned long *src2, unsigned int nbits)
388{
389 if (small_const_nbits(nbits))
390 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
391 else
392 return __bitmap_subset(bitmap1: src1, bitmap2: src2, nbits);
393}
394
395static inline bool bitmap_empty(const unsigned long *src, unsigned nbits)
396{
397 if (small_const_nbits(nbits))
398 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
399
400 return find_first_bit(addr: src, size: nbits) == nbits;
401}
402
403static inline bool bitmap_full(const unsigned long *src, unsigned int nbits)
404{
405 if (small_const_nbits(nbits))
406 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
407
408 return find_first_zero_bit(addr: src, size: nbits) == nbits;
409}
410
411static __always_inline
412unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
413{
414 if (small_const_nbits(nbits))
415 return hweight_long(w: *src & BITMAP_LAST_WORD_MASK(nbits));
416 return __bitmap_weight(bitmap: src, nbits);
417}
418
419static __always_inline
420unsigned long bitmap_weight_and(const unsigned long *src1,
421 const unsigned long *src2, unsigned int nbits)
422{
423 if (small_const_nbits(nbits))
424 return hweight_long(w: *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits));
425 return __bitmap_weight_and(bitmap1: src1, bitmap2: src2, nbits);
426}
427
428static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
429 unsigned int nbits)
430{
431 if (__builtin_constant_p(nbits) && nbits == 1)
432 __set_bit(start, map);
433 else if (small_const_nbits(start + nbits))
434 *map |= GENMASK(start + nbits - 1, start);
435 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
436 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
437 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
438 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
439 memset((char *)map + start / 8, 0xff, nbits / 8);
440 else
441 __bitmap_set(map, start, len: nbits);
442}
443
444static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
445 unsigned int nbits)
446{
447 if (__builtin_constant_p(nbits) && nbits == 1)
448 __clear_bit(start, map);
449 else if (small_const_nbits(start + nbits))
450 *map &= ~GENMASK(start + nbits - 1, start);
451 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
452 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
453 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
454 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
455 memset((char *)map + start / 8, 0, nbits / 8);
456 else
457 __bitmap_clear(map, start, len: nbits);
458}
459
460static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
461 unsigned int shift, unsigned int nbits)
462{
463 if (small_const_nbits(nbits))
464 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
465 else
466 __bitmap_shift_right(dst, src, shift, nbits);
467}
468
469static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
470 unsigned int shift, unsigned int nbits)
471{
472 if (small_const_nbits(nbits))
473 *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
474 else
475 __bitmap_shift_left(dst, src, shift, nbits);
476}
477
478static inline void bitmap_replace(unsigned long *dst,
479 const unsigned long *old,
480 const unsigned long *new,
481 const unsigned long *mask,
482 unsigned int nbits)
483{
484 if (small_const_nbits(nbits))
485 *dst = (*old & ~(*mask)) | (*new & *mask);
486 else
487 __bitmap_replace(dst, old, new, mask, nbits);
488}
489
490static inline void bitmap_next_set_region(unsigned long *bitmap,
491 unsigned int *rs, unsigned int *re,
492 unsigned int end)
493{
494 *rs = find_next_bit(addr: bitmap, size: end, offset: *rs);
495 *re = find_next_zero_bit(addr: bitmap, size: end, offset: *rs + 1);
496}
497
498/**
499 * bitmap_release_region - release allocated bitmap region
500 * @bitmap: array of unsigned longs corresponding to the bitmap
501 * @pos: beginning of bit region to release
502 * @order: region size (log base 2 of number of bits) to release
503 *
504 * This is the complement to __bitmap_find_free_region() and releases
505 * the found region (by clearing it in the bitmap).
506 */
507static inline void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
508{
509 bitmap_clear(map: bitmap, start: pos, BIT(order));
510}
511
512/**
513 * bitmap_allocate_region - allocate bitmap region
514 * @bitmap: array of unsigned longs corresponding to the bitmap
515 * @pos: beginning of bit region to allocate
516 * @order: region size (log base 2 of number of bits) to allocate
517 *
518 * Allocate (set bits in) a specified region of a bitmap.
519 *
520 * Returns: 0 on success, or %-EBUSY if specified region wasn't
521 * free (not all bits were zero).
522 */
523static inline int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
524{
525 unsigned int len = BIT(order);
526
527 if (find_next_bit(addr: bitmap, size: pos + len, offset: pos) < pos + len)
528 return -EBUSY;
529 bitmap_set(map: bitmap, start: pos, nbits: len);
530 return 0;
531}
532
533/**
534 * bitmap_find_free_region - find a contiguous aligned mem region
535 * @bitmap: array of unsigned longs corresponding to the bitmap
536 * @bits: number of bits in the bitmap
537 * @order: region size (log base 2 of number of bits) to find
538 *
539 * Find a region of free (zero) bits in a @bitmap of @bits bits and
540 * allocate them (set them to one). Only consider regions of length
541 * a power (@order) of two, aligned to that power of two, which
542 * makes the search algorithm much faster.
543 *
544 * Returns: the bit offset in bitmap of the allocated region,
545 * or -errno on failure.
546 */
547static inline int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
548{
549 unsigned int pos, end; /* scans bitmap by regions of size order */
550
551 for (pos = 0; (end = pos + BIT(order)) <= bits; pos = end) {
552 if (!bitmap_allocate_region(bitmap, pos, order))
553 return pos;
554 }
555 return -ENOMEM;
556}
557
558/**
559 * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
560 * @n: u64 value
561 *
562 * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
563 * integers in 32-bit environment, and 64-bit integers in 64-bit one.
564 *
565 * There are four combinations of endianness and length of the word in linux
566 * ABIs: LE64, BE64, LE32 and BE32.
567 *
568 * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
569 * bitmaps and therefore don't require any special handling.
570 *
571 * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
572 * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
573 * other hand is represented as an array of 32-bit words and the position of
574 * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
575 * word. For example, bit #42 is located at 10th position of 2nd word.
576 * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
577 * values in memory as it usually does. But for BE we need to swap hi and lo
578 * words manually.
579 *
580 * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
581 * lo parts of u64. For LE32 it does nothing, and for BE environment it swaps
582 * hi and lo words, as is expected by bitmap.
583 */
584#if __BITS_PER_LONG == 64
585#define BITMAP_FROM_U64(n) (n)
586#else
587#define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
588 ((unsigned long) ((u64)(n) >> 32))
589#endif
590
591/**
592 * bitmap_from_u64 - Check and swap words within u64.
593 * @mask: source bitmap
594 * @dst: destination bitmap
595 *
596 * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
597 * to read u64 mask, we will get the wrong word.
598 * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
599 * but we expect the lower 32-bits of u64.
600 */
601static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
602{
603 bitmap_from_arr64(dst, &mask, 64);
604}
605
606/**
607 * bitmap_get_value8 - get an 8-bit value within a memory region
608 * @map: address to the bitmap memory region
609 * @start: bit offset of the 8-bit value; must be a multiple of 8
610 *
611 * Returns the 8-bit value located at the @start bit offset within the @src
612 * memory region.
613 */
614static inline unsigned long bitmap_get_value8(const unsigned long *map,
615 unsigned long start)
616{
617 const size_t index = BIT_WORD(start);
618 const unsigned long offset = start % BITS_PER_LONG;
619
620 return (map[index] >> offset) & 0xFF;
621}
622
623/**
624 * bitmap_set_value8 - set an 8-bit value within a memory region
625 * @map: address to the bitmap memory region
626 * @value: the 8-bit value; values wider than 8 bits may clobber bitmap
627 * @start: bit offset of the 8-bit value; must be a multiple of 8
628 */
629static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
630 unsigned long start)
631{
632 const size_t index = BIT_WORD(start);
633 const unsigned long offset = start % BITS_PER_LONG;
634
635 map[index] &= ~(0xFFUL << offset);
636 map[index] |= value << offset;
637}
638
639#endif /* __ASSEMBLY__ */
640
641#endif /* __LINUX_BITMAP_H */
642

source code of linux/include/linux/bitmap.h