1
2
3
4
5
6#include <linux/export.h>
7#include <linux/thread_info.h>
8#include <linux/ctype.h>
9#include <linux/errno.h>
10#include <linux/bitmap.h>
11#include <linux/bitops.h>
12#include <linux/bug.h>
13#include <linux/kernel.h>
14#include <linux/mm.h>
15#include <linux/slab.h>
16#include <linux/string.h>
17#include <linux/uaccess.h>
18
19#include <asm/page.h>
20
21#include "kstrtox.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46int __bitmap_equal(const unsigned long *bitmap1,
47 const unsigned long *bitmap2, unsigned int bits)
48{
49 unsigned int k, lim = bits/BITS_PER_LONG;
50 for (k = 0; k < lim; ++k)
51 if (bitmap1[k] != bitmap2[k])
52 return 0;
53
54 if (bits % BITS_PER_LONG)
55 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
56 return 0;
57
58 return 1;
59}
60EXPORT_SYMBOL(__bitmap_equal);
61
62void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
63{
64 unsigned int k, lim = BITS_TO_LONGS(bits);
65 for (k = 0; k < lim; ++k)
66 dst[k] = ~src[k];
67}
68EXPORT_SYMBOL(__bitmap_complement);
69
70
71
72
73
74
75
76
77
78
79
80
81void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
82 unsigned shift, unsigned nbits)
83{
84 unsigned k, lim = BITS_TO_LONGS(nbits);
85 unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
86 unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
87 for (k = 0; off + k < lim; ++k) {
88 unsigned long upper, lower;
89
90
91
92
93
94 if (!rem || off + k + 1 >= lim)
95 upper = 0;
96 else {
97 upper = src[off + k + 1];
98 if (off + k + 1 == lim - 1)
99 upper &= mask;
100 upper <<= (BITS_PER_LONG - rem);
101 }
102 lower = src[off + k];
103 if (off + k == lim - 1)
104 lower &= mask;
105 lower >>= rem;
106 dst[k] = lower | upper;
107 }
108 if (off)
109 memset(&dst[lim - off], 0, off*sizeof(unsigned long));
110}
111EXPORT_SYMBOL(__bitmap_shift_right);
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
127 unsigned int shift, unsigned int nbits)
128{
129 int k;
130 unsigned int lim = BITS_TO_LONGS(nbits);
131 unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
132 for (k = lim - off - 1; k >= 0; --k) {
133 unsigned long upper, lower;
134
135
136
137
138
139 if (rem && k > 0)
140 lower = src[k - 1] >> (BITS_PER_LONG - rem);
141 else
142 lower = 0;
143 upper = src[k] << rem;
144 dst[k + off] = lower | upper;
145 }
146 if (off)
147 memset(dst, 0, off*sizeof(unsigned long));
148}
149EXPORT_SYMBOL(__bitmap_shift_left);
150
151int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
152 const unsigned long *bitmap2, unsigned int bits)
153{
154 unsigned int k;
155 unsigned int lim = bits/BITS_PER_LONG;
156 unsigned long result = 0;
157
158 for (k = 0; k < lim; k++)
159 result |= (dst[k] = bitmap1[k] & bitmap2[k]);
160 if (bits % BITS_PER_LONG)
161 result |= (dst[k] = bitmap1[k] & bitmap2[k] &
162 BITMAP_LAST_WORD_MASK(bits));
163 return result != 0;
164}
165EXPORT_SYMBOL(__bitmap_and);
166
167void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
168 const unsigned long *bitmap2, unsigned int bits)
169{
170 unsigned int k;
171 unsigned int nr = BITS_TO_LONGS(bits);
172
173 for (k = 0; k < nr; k++)
174 dst[k] = bitmap1[k] | bitmap2[k];
175}
176EXPORT_SYMBOL(__bitmap_or);
177
178void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
179 const unsigned long *bitmap2, unsigned int bits)
180{
181 unsigned int k;
182 unsigned int nr = BITS_TO_LONGS(bits);
183
184 for (k = 0; k < nr; k++)
185 dst[k] = bitmap1[k] ^ bitmap2[k];
186}
187EXPORT_SYMBOL(__bitmap_xor);
188
189int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
190 const unsigned long *bitmap2, unsigned int bits)
191{
192 unsigned int k;
193 unsigned int lim = bits/BITS_PER_LONG;
194 unsigned long result = 0;
195
196 for (k = 0; k < lim; k++)
197 result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
198 if (bits % BITS_PER_LONG)
199 result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
200 BITMAP_LAST_WORD_MASK(bits));
201 return result != 0;
202}
203EXPORT_SYMBOL(__bitmap_andnot);
204
205int __bitmap_intersects(const unsigned long *bitmap1,
206 const unsigned long *bitmap2, unsigned int bits)
207{
208 unsigned int k, lim = bits/BITS_PER_LONG;
209 for (k = 0; k < lim; ++k)
210 if (bitmap1[k] & bitmap2[k])
211 return 1;
212
213 if (bits % BITS_PER_LONG)
214 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
215 return 1;
216 return 0;
217}
218EXPORT_SYMBOL(__bitmap_intersects);
219
220int __bitmap_subset(const unsigned long *bitmap1,
221 const unsigned long *bitmap2, unsigned int bits)
222{
223 unsigned int k, lim = bits/BITS_PER_LONG;
224 for (k = 0; k < lim; ++k)
225 if (bitmap1[k] & ~bitmap2[k])
226 return 0;
227
228 if (bits % BITS_PER_LONG)
229 if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
230 return 0;
231 return 1;
232}
233EXPORT_SYMBOL(__bitmap_subset);
234
235int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
236{
237 unsigned int k, lim = bits/BITS_PER_LONG;
238 int w = 0;
239
240 for (k = 0; k < lim; k++)
241 w += hweight_long(bitmap[k]);
242
243 if (bits % BITS_PER_LONG)
244 w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
245
246 return w;
247}
248EXPORT_SYMBOL(__bitmap_weight);
249
250void __bitmap_set(unsigned long *map, unsigned int start, int len)
251{
252 unsigned long *p = map + BIT_WORD(start);
253 const unsigned int size = start + len;
254 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
255 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
256
257 while (len - bits_to_set >= 0) {
258 *p |= mask_to_set;
259 len -= bits_to_set;
260 bits_to_set = BITS_PER_LONG;
261 mask_to_set = ~0UL;
262 p++;
263 }
264 if (len) {
265 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
266 *p |= mask_to_set;
267 }
268}
269EXPORT_SYMBOL(__bitmap_set);
270
271void __bitmap_clear(unsigned long *map, unsigned int start, int len)
272{
273 unsigned long *p = map + BIT_WORD(start);
274 const unsigned int size = start + len;
275 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
276 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
277
278 while (len - bits_to_clear >= 0) {
279 *p &= ~mask_to_clear;
280 len -= bits_to_clear;
281 bits_to_clear = BITS_PER_LONG;
282 mask_to_clear = ~0UL;
283 p++;
284 }
285 if (len) {
286 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
287 *p &= ~mask_to_clear;
288 }
289}
290EXPORT_SYMBOL(__bitmap_clear);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
306 unsigned long size,
307 unsigned long start,
308 unsigned int nr,
309 unsigned long align_mask,
310 unsigned long align_offset)
311{
312 unsigned long index, end, i;
313again:
314 index = find_next_zero_bit(map, size, start);
315
316
317 index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;
318
319 end = index + nr;
320 if (end > size)
321 return end;
322 i = find_next_bit(map, end, index);
323 if (i < end) {
324 start = i + 1;
325 goto again;
326 }
327 return index;
328}
329EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
330
331
332
333
334
335
336#define CHUNKSZ 32
337#define nbits_to_hold_value(val) fls(val)
338#define BASEDEC 10
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356int __bitmap_parse(const char *buf, unsigned int buflen,
357 int is_user, unsigned long *maskp,
358 int nmaskbits)
359{
360 int c, old_c, totaldigits, ndigits, nchunks, nbits;
361 u32 chunk;
362 const char __user __force *ubuf = (const char __user __force *)buf;
363
364 bitmap_zero(maskp, nmaskbits);
365
366 nchunks = nbits = totaldigits = c = 0;
367 do {
368 chunk = 0;
369 ndigits = totaldigits;
370
371
372 while (buflen) {
373 old_c = c;
374 if (is_user) {
375 if (__get_user(c, ubuf++))
376 return -EFAULT;
377 }
378 else
379 c = *buf++;
380 buflen--;
381 if (isspace(c))
382 continue;
383
384
385
386
387
388
389 if (totaldigits && c && isspace(old_c))
390 return -EINVAL;
391
392
393 if (c == '\0' || c == ',')
394 break;
395
396 if (!isxdigit(c))
397 return -EINVAL;
398
399
400
401
402
403
404 if (chunk & ~((1UL << (CHUNKSZ - 4)) - 1))
405 return -EOVERFLOW;
406
407 chunk = (chunk << 4) | hex_to_bin(c);
408 totaldigits++;
409 }
410 if (ndigits == totaldigits)
411 return -EINVAL;
412 if (nchunks == 0 && chunk == 0)
413 continue;
414
415 __bitmap_shift_left(maskp, maskp, CHUNKSZ, nmaskbits);
416 *maskp |= chunk;
417 nchunks++;
418 nbits += (nchunks == 1) ? nbits_to_hold_value(chunk) : CHUNKSZ;
419 if (nbits > nmaskbits)
420 return -EOVERFLOW;
421 } while (buflen && c == ',');
422
423 return 0;
424}
425EXPORT_SYMBOL(__bitmap_parse);
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442int bitmap_parse_user(const char __user *ubuf,
443 unsigned int ulen, unsigned long *maskp,
444 int nmaskbits)
445{
446 if (!access_ok(ubuf, ulen))
447 return -EFAULT;
448 return __bitmap_parse((const char __force *)ubuf,
449 ulen, 1, maskp, nmaskbits);
450
451}
452EXPORT_SYMBOL(bitmap_parse_user);
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
471 int nmaskbits)
472{
473 ptrdiff_t len = PAGE_SIZE - offset_in_page(buf);
474
475 return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
476 scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
477}
478EXPORT_SYMBOL(bitmap_print_to_pagebuf);
479
480
481
482
483
484
485
486
487struct region {
488 unsigned int start;
489 unsigned int off;
490 unsigned int group_len;
491 unsigned int end;
492};
493
494static int bitmap_set_region(const struct region *r,
495 unsigned long *bitmap, int nbits)
496{
497 unsigned int start;
498
499 if (r->end >= nbits)
500 return -ERANGE;
501
502 for (start = r->start; start <= r->end; start += r->group_len)
503 bitmap_set(bitmap, start, min(r->end - start + 1, r->off));
504
505 return 0;
506}
507
508static int bitmap_check_region(const struct region *r)
509{
510 if (r->start > r->end || r->group_len == 0 || r->off > r->group_len)
511 return -EINVAL;
512
513 return 0;
514}
515
516static const char *bitmap_getnum(const char *str, unsigned int *num)
517{
518 unsigned long long n;
519 unsigned int len;
520
521 len = _parse_integer(str, 10, &n);
522 if (!len)
523 return ERR_PTR(-EINVAL);
524 if (len & KSTRTOX_OVERFLOW || n != (unsigned int)n)
525 return ERR_PTR(-EOVERFLOW);
526
527 *num = n;
528 return str + len;
529}
530
531static inline bool end_of_str(char c)
532{
533 return c == '\0' || c == '\n';
534}
535
536static inline bool __end_of_region(char c)
537{
538 return isspace(c) || c == ',';
539}
540
541static inline bool end_of_region(char c)
542{
543 return __end_of_region(c) || end_of_str(c);
544}
545
546
547
548
549
550static const char *bitmap_find_region(const char *str)
551{
552 while (__end_of_region(*str))
553 str++;
554
555 return end_of_str(*str) ? NULL : str;
556}
557
558static const char *bitmap_parse_region(const char *str, struct region *r)
559{
560 str = bitmap_getnum(str, &r->start);
561 if (IS_ERR(str))
562 return str;
563
564 if (end_of_region(*str))
565 goto no_end;
566
567 if (*str != '-')
568 return ERR_PTR(-EINVAL);
569
570 str = bitmap_getnum(str + 1, &r->end);
571 if (IS_ERR(str))
572 return str;
573
574 if (end_of_region(*str))
575 goto no_pattern;
576
577 if (*str != ':')
578 return ERR_PTR(-EINVAL);
579
580 str = bitmap_getnum(str + 1, &r->off);
581 if (IS_ERR(str))
582 return str;
583
584 if (*str != '/')
585 return ERR_PTR(-EINVAL);
586
587 return bitmap_getnum(str + 1, &r->group_len);
588
589no_end:
590 r->end = r->start;
591no_pattern:
592 r->off = r->end + 1;
593 r->group_len = r->end + 1;
594
595 return end_of_str(*str) ? NULL : str;
596}
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits)
623{
624 struct region r;
625 long ret;
626
627 bitmap_zero(maskp, nmaskbits);
628
629 while (buf) {
630 buf = bitmap_find_region(buf);
631 if (buf == NULL)
632 return 0;
633
634 buf = bitmap_parse_region(buf, &r);
635 if (IS_ERR(buf))
636 return PTR_ERR(buf);
637
638 ret = bitmap_check_region(&r);
639 if (ret)
640 return ret;
641
642 ret = bitmap_set_region(&r, maskp, nmaskbits);
643 if (ret)
644 return ret;
645 }
646
647 return 0;
648}
649EXPORT_SYMBOL(bitmap_parselist);
650
651
652
653
654
655
656
657
658
659
660
661
662
663int bitmap_parselist_user(const char __user *ubuf,
664 unsigned int ulen, unsigned long *maskp,
665 int nmaskbits)
666{
667 char *buf;
668 int ret;
669
670 buf = memdup_user_nul(ubuf, ulen);
671 if (IS_ERR(buf))
672 return PTR_ERR(buf);
673
674 ret = bitmap_parselist(buf, maskp, nmaskbits);
675
676 kfree(buf);
677 return ret;
678}
679EXPORT_SYMBOL(bitmap_parselist_user);
680
681
682#ifdef CONFIG_NUMA
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
702{
703 if (pos >= nbits || !test_bit(pos, buf))
704 return -1;
705
706 return __bitmap_weight(buf, pos);
707}
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
728{
729 unsigned int pos;
730
731 for (pos = find_first_bit(buf, nbits);
732 pos < nbits && ord;
733 pos = find_next_bit(buf, nbits, pos + 1))
734 ord--;
735
736 return pos;
737}
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771void bitmap_remap(unsigned long *dst, const unsigned long *src,
772 const unsigned long *old, const unsigned long *new,
773 unsigned int nbits)
774{
775 unsigned int oldbit, w;
776
777 if (dst == src)
778 return;
779 bitmap_zero(dst, nbits);
780
781 w = bitmap_weight(new, nbits);
782 for_each_set_bit(oldbit, src, nbits) {
783 int n = bitmap_pos_to_ord(old, oldbit, nbits);
784
785 if (n < 0 || w == 0)
786 set_bit(oldbit, dst);
787 else
788 set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
789 }
790}
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818int bitmap_bitremap(int oldbit, const unsigned long *old,
819 const unsigned long *new, int bits)
820{
821 int w = bitmap_weight(new, bits);
822 int n = bitmap_pos_to_ord(old, oldbit, bits);
823 if (n < 0 || w == 0)
824 return oldbit;
825 else
826 return bitmap_ord_to_pos(new, n % w, bits);
827}
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935void bitmap_onto(unsigned long *dst, const unsigned long *orig,
936 const unsigned long *relmap, unsigned int bits)
937{
938 unsigned int n, m;
939
940 if (dst == orig)
941 return;
942 bitmap_zero(dst, bits);
943
944
945
946
947
948
949
950
951
952
953
954 m = 0;
955 for_each_set_bit(n, relmap, bits) {
956
957 if (test_bit(m, orig))
958 set_bit(n, dst);
959 m++;
960 }
961}
962
963
964
965
966
967
968
969
970
971
972
973
974void bitmap_fold(unsigned long *dst, const unsigned long *orig,
975 unsigned int sz, unsigned int nbits)
976{
977 unsigned int oldbit;
978
979 if (dst == orig)
980 return;
981 bitmap_zero(dst, nbits);
982
983 for_each_set_bit(oldbit, orig, nbits)
984 set_bit(oldbit % sz, dst);
985}
986#endif
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006enum {
1007 REG_OP_ISFREE,
1008 REG_OP_ALLOC,
1009 REG_OP_RELEASE,
1010};
1011
1012static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
1013{
1014 int nbits_reg;
1015 int index;
1016 int offset;
1017 int nlongs_reg;
1018 int nbitsinlong;
1019 unsigned long mask;
1020 int i;
1021 int ret = 0;
1022
1023
1024
1025
1026
1027 nbits_reg = 1 << order;
1028 index = pos / BITS_PER_LONG;
1029 offset = pos - (index * BITS_PER_LONG);
1030 nlongs_reg = BITS_TO_LONGS(nbits_reg);
1031 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
1032
1033
1034
1035
1036
1037 mask = (1UL << (nbitsinlong - 1));
1038 mask += mask - 1;
1039 mask <<= offset;
1040
1041 switch (reg_op) {
1042 case REG_OP_ISFREE:
1043 for (i = 0; i < nlongs_reg; i++) {
1044 if (bitmap[index + i] & mask)
1045 goto done;
1046 }
1047 ret = 1;
1048 break;
1049
1050 case REG_OP_ALLOC:
1051 for (i = 0; i < nlongs_reg; i++)
1052 bitmap[index + i] |= mask;
1053 break;
1054
1055 case REG_OP_RELEASE:
1056 for (i = 0; i < nlongs_reg; i++)
1057 bitmap[index + i] &= ~mask;
1058 break;
1059 }
1060done:
1061 return ret;
1062}
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
1079{
1080 unsigned int pos, end;
1081
1082 for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
1083 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
1084 continue;
1085 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
1086 return pos;
1087 }
1088 return -ENOMEM;
1089}
1090EXPORT_SYMBOL(bitmap_find_free_region);
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
1104{
1105 __reg_op(bitmap, pos, order, REG_OP_RELEASE);
1106}
1107EXPORT_SYMBOL(bitmap_release_region);
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
1121{
1122 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
1123 return -EBUSY;
1124 return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
1125}
1126EXPORT_SYMBOL(bitmap_allocate_region);
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136#ifdef __BIG_ENDIAN
1137void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits)
1138{
1139 unsigned int i;
1140
1141 for (i = 0; i < nbits/BITS_PER_LONG; i++) {
1142 if (BITS_PER_LONG == 64)
1143 dst[i] = cpu_to_le64(src[i]);
1144 else
1145 dst[i] = cpu_to_le32(src[i]);
1146 }
1147}
1148EXPORT_SYMBOL(bitmap_copy_le);
1149#endif
1150
1151unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
1152{
1153 return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
1154 flags);
1155}
1156EXPORT_SYMBOL(bitmap_alloc);
1157
1158unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
1159{
1160 return bitmap_alloc(nbits, flags | __GFP_ZERO);
1161}
1162EXPORT_SYMBOL(bitmap_zalloc);
1163
1164void bitmap_free(const unsigned long *bitmap)
1165{
1166 kfree(bitmap);
1167}
1168EXPORT_SYMBOL(bitmap_free);
1169
1170#if BITS_PER_LONG == 64
1171
1172
1173
1174
1175
1176
1177void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)
1178{
1179 unsigned int i, halfwords;
1180
1181 halfwords = DIV_ROUND_UP(nbits, 32);
1182 for (i = 0; i < halfwords; i++) {
1183 bitmap[i/2] = (unsigned long) buf[i];
1184 if (++i < halfwords)
1185 bitmap[i/2] |= ((unsigned long) buf[i]) << 32;
1186 }
1187
1188
1189 if (nbits % BITS_PER_LONG)
1190 bitmap[(halfwords - 1) / 2] &= BITMAP_LAST_WORD_MASK(nbits);
1191}
1192EXPORT_SYMBOL(bitmap_from_arr32);
1193
1194
1195
1196
1197
1198
1199
1200void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
1201{
1202 unsigned int i, halfwords;
1203
1204 halfwords = DIV_ROUND_UP(nbits, 32);
1205 for (i = 0; i < halfwords; i++) {
1206 buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
1207 if (++i < halfwords)
1208 buf[i] = (u32) (bitmap[i/2] >> 32);
1209 }
1210
1211
1212 if (nbits % BITS_PER_LONG)
1213 buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
1214}
1215EXPORT_SYMBOL(bitmap_to_arr32);
1216
1217#endif
1218