1
2#ifndef __LINUX_BITMAP_H
3#define __LINUX_BITMAP_H
4
5#ifndef __ASSEMBLY__
6
7#include <linux/types.h>
8#include <linux/bitops.h>
9#include <linux/string.h>
10#include <linux/kernel.h>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114extern unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
115extern unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
116extern void bitmap_free(const unsigned long *bitmap);
117
118
119
120
121
122extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
123extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
124extern int __bitmap_equal(const unsigned long *bitmap1,
125 const unsigned long *bitmap2, unsigned int nbits);
126extern bool __pure __bitmap_or_equal(const unsigned long *src1,
127 const unsigned long *src2,
128 const unsigned long *src3,
129 unsigned int nbits);
130extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
131 unsigned int nbits);
132extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
133 unsigned int shift, unsigned int nbits);
134extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
135 unsigned int shift, unsigned int nbits);
136extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
137 const unsigned long *bitmap2, unsigned int nbits);
138extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
139 const unsigned long *bitmap2, unsigned int nbits);
140extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
141 const unsigned long *bitmap2, unsigned int nbits);
142extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
143 const unsigned long *bitmap2, unsigned int nbits);
144extern void __bitmap_replace(unsigned long *dst,
145 const unsigned long *old, const unsigned long *new,
146 const unsigned long *mask, unsigned int nbits);
147extern int __bitmap_intersects(const unsigned long *bitmap1,
148 const unsigned long *bitmap2, unsigned int nbits);
149extern int __bitmap_subset(const unsigned long *bitmap1,
150 const unsigned long *bitmap2, unsigned int nbits);
151extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
152extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
153extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
154
155extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
156 unsigned long size,
157 unsigned long start,
158 unsigned int nr,
159 unsigned long align_mask,
160 unsigned long align_offset);
161
162
163
164
165
166
167
168
169
170
171
172
173
174static inline unsigned long
175bitmap_find_next_zero_area(unsigned long *map,
176 unsigned long size,
177 unsigned long start,
178 unsigned int nr,
179 unsigned long align_mask)
180{
181 return bitmap_find_next_zero_area_off(map, size, start, nr,
182 align_mask, 0);
183}
184
185extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
186 unsigned long *dst, int nbits);
187extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
188 unsigned long *dst, int nbits);
189extern int bitmap_parselist(const char *buf, unsigned long *maskp,
190 int nmaskbits);
191extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
192 unsigned long *dst, int nbits);
193extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
194 const unsigned long *old, const unsigned long *new, unsigned int nbits);
195extern int bitmap_bitremap(int oldbit,
196 const unsigned long *old, const unsigned long *new, int bits);
197extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
198 const unsigned long *relmap, unsigned int bits);
199extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
200 unsigned int sz, unsigned int nbits);
201extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
202extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
203extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
204
205#ifdef __BIG_ENDIAN
206extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
207#else
208#define bitmap_copy_le bitmap_copy
209#endif
210extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
211extern int bitmap_print_to_pagebuf(bool list, char *buf,
212 const unsigned long *maskp, int nmaskbits);
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
217
218
219
220
221
222#define small_const_nbits(nbits) \
223 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG && (nbits) > 0)
224
225static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
226{
227 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
228 memset(dst, 0, len);
229}
230
231static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
232{
233 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
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 memcpy(dst, src, len);
242}
243
244
245
246
247static inline void bitmap_copy_clear_tail(unsigned long *dst,
248 const unsigned long *src, unsigned int nbits)
249{
250 bitmap_copy(dst, src, nbits);
251 if (nbits % BITS_PER_LONG)
252 dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
253}
254
255
256
257
258
259#if BITS_PER_LONG == 64
260extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
261 unsigned int nbits);
262extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
263 unsigned int nbits);
264#else
265#define bitmap_from_arr32(bitmap, buf, nbits) \
266 bitmap_copy_clear_tail((unsigned long *) (bitmap), \
267 (const unsigned long *) (buf), (nbits))
268#define bitmap_to_arr32(buf, bitmap, nbits) \
269 bitmap_copy_clear_tail((unsigned long *) (buf), \
270 (const unsigned long *) (bitmap), (nbits))
271#endif
272
273static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
274 const unsigned long *src2, unsigned int nbits)
275{
276 if (small_const_nbits(nbits))
277 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
278 return __bitmap_and(dst, src1, src2, nbits);
279}
280
281static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
282 const unsigned long *src2, unsigned int nbits)
283{
284 if (small_const_nbits(nbits))
285 *dst = *src1 | *src2;
286 else
287 __bitmap_or(dst, src1, src2, nbits);
288}
289
290static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
291 const unsigned long *src2, unsigned int nbits)
292{
293 if (small_const_nbits(nbits))
294 *dst = *src1 ^ *src2;
295 else
296 __bitmap_xor(dst, src1, src2, nbits);
297}
298
299static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
300 const unsigned long *src2, unsigned int nbits)
301{
302 if (small_const_nbits(nbits))
303 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
304 return __bitmap_andnot(dst, src1, src2, nbits);
305}
306
307static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
308 unsigned int nbits)
309{
310 if (small_const_nbits(nbits))
311 *dst = ~(*src);
312 else
313 __bitmap_complement(dst, src, nbits);
314}
315
316#ifdef __LITTLE_ENDIAN
317#define BITMAP_MEM_ALIGNMENT 8
318#else
319#define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
320#endif
321#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
322
323static inline int bitmap_equal(const unsigned long *src1,
324 const unsigned long *src2, unsigned int nbits)
325{
326 if (small_const_nbits(nbits))
327 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
328 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
329 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
330 return !memcmp(src1, src2, nbits / 8);
331 return __bitmap_equal(src1, src2, nbits);
332}
333
334
335
336
337
338
339
340
341
342
343static inline bool bitmap_or_equal(const unsigned long *src1,
344 const unsigned long *src2,
345 const unsigned long *src3,
346 unsigned int nbits)
347{
348 if (!small_const_nbits(nbits))
349 return __bitmap_or_equal(src1, src2, src3, nbits);
350
351 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
352}
353
354static inline int bitmap_intersects(const unsigned long *src1,
355 const unsigned long *src2, unsigned int nbits)
356{
357 if (small_const_nbits(nbits))
358 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
359 else
360 return __bitmap_intersects(src1, src2, nbits);
361}
362
363static inline int bitmap_subset(const unsigned long *src1,
364 const unsigned long *src2, unsigned int nbits)
365{
366 if (small_const_nbits(nbits))
367 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
368 else
369 return __bitmap_subset(src1, src2, nbits);
370}
371
372static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
373{
374 if (small_const_nbits(nbits))
375 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
376
377 return find_first_bit(src, nbits) == nbits;
378}
379
380static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
381{
382 if (small_const_nbits(nbits))
383 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
384
385 return find_first_zero_bit(src, nbits) == nbits;
386}
387
388static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
389{
390 if (small_const_nbits(nbits))
391 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
392 return __bitmap_weight(src, nbits);
393}
394
395static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
396 unsigned int nbits)
397{
398 if (__builtin_constant_p(nbits) && nbits == 1)
399 __set_bit(start, map);
400 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
401 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
402 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
403 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
404 memset((char *)map + start / 8, 0xff, nbits / 8);
405 else
406 __bitmap_set(map, start, nbits);
407}
408
409static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
410 unsigned int nbits)
411{
412 if (__builtin_constant_p(nbits) && nbits == 1)
413 __clear_bit(start, map);
414 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
415 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
416 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
417 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
418 memset((char *)map + start / 8, 0, nbits / 8);
419 else
420 __bitmap_clear(map, start, nbits);
421}
422
423static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
424 unsigned int shift, unsigned int nbits)
425{
426 if (small_const_nbits(nbits))
427 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
428 else
429 __bitmap_shift_right(dst, src, shift, nbits);
430}
431
432static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
433 unsigned int shift, unsigned int nbits)
434{
435 if (small_const_nbits(nbits))
436 *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
437 else
438 __bitmap_shift_left(dst, src, shift, nbits);
439}
440
441static inline void bitmap_replace(unsigned long *dst,
442 const unsigned long *old,
443 const unsigned long *new,
444 const unsigned long *mask,
445 unsigned int nbits)
446{
447 if (small_const_nbits(nbits))
448 *dst = (*old & ~(*mask)) | (*new & *mask);
449 else
450 __bitmap_replace(dst, old, new, mask, nbits);
451}
452
453static inline int bitmap_parse(const char *buf, unsigned int buflen,
454 unsigned long *maskp, int nmaskbits)
455{
456 return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
457}
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485#if __BITS_PER_LONG == 64
486#define BITMAP_FROM_U64(n) (n)
487#else
488#define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
489 ((unsigned long) ((u64)(n) >> 32))
490#endif
491
492
493
494
495
496
497
498
499
500
501
502static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
503{
504 dst[0] = mask & ULONG_MAX;
505
506 if (sizeof(mask) > sizeof(unsigned long))
507 dst[1] = mask >> 32;
508}
509
510
511
512
513
514
515
516
517
518static inline unsigned long bitmap_get_value8(const unsigned long *map,
519 unsigned long start)
520{
521 const size_t index = BIT_WORD(start);
522 const unsigned long offset = start % BITS_PER_LONG;
523
524 return (map[index] >> offset) & 0xFF;
525}
526
527
528
529
530
531
532
533static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
534 unsigned long start)
535{
536 const size_t index = BIT_WORD(start);
537 const unsigned long offset = start % BITS_PER_LONG;
538
539 map[index] &= ~(0xFFUL << offset);
540 map[index] |= value << offset;
541}
542
543#endif
544
545#endif
546