]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/xfs/xfs_bit.c
Merge remote-tracking branch 'md/for-next'
[karo-tx-linux.git] / fs / xfs / xfs_bit.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_log_format.h"
20
21 /*
22  * XFS bit manipulation routines, used in non-realtime code.
23  */
24
25 /*
26  * Return whether bitmap is empty.
27  * Size is number of words in the bitmap, which is padded to word boundary
28  * Returns 1 for empty, 0 for non-empty.
29  */
30 int
31 xfs_bitmap_empty(uint *map, uint size)
32 {
33         uint i;
34         uint ret = 0;
35
36         for (i = 0; i < size; i++) {
37                 ret |= map[i];
38         }
39
40         return (ret == 0);
41 }
42
43 /*
44  * Count the number of contiguous bits set in the bitmap starting with bit
45  * start_bit.  Size is the size of the bitmap in words.
46  */
47 int
48 xfs_contig_bits(uint *map, uint size, uint start_bit)
49 {
50         uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
51         uint result = 0;
52         uint tmp;
53
54         size <<= BIT_TO_WORD_SHIFT;
55
56         ASSERT(start_bit < size);
57         size -= start_bit & ~(NBWORD - 1);
58         start_bit &= (NBWORD - 1);
59         if (start_bit) {
60                 tmp = *p++;
61                 /* set to one first offset bits prior to start */
62                 tmp |= (~0U >> (NBWORD-start_bit));
63                 if (tmp != ~0U)
64                         goto found;
65                 result += NBWORD;
66                 size -= NBWORD;
67         }
68         while (size) {
69                 if ((tmp = *p++) != ~0U)
70                         goto found;
71                 result += NBWORD;
72                 size -= NBWORD;
73         }
74         return result - start_bit;
75 found:
76         return result + ffz(tmp) - start_bit;
77 }
78
79 /*
80  * This takes the bit number to start looking from and
81  * returns the next set bit from there.  It returns -1
82  * if there are no more bits set or the start bit is
83  * beyond the end of the bitmap.
84  *
85  * Size is the number of words, not bytes, in the bitmap.
86  */
87 int xfs_next_bit(uint *map, uint size, uint start_bit)
88 {
89         uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
90         uint result = start_bit & ~(NBWORD - 1);
91         uint tmp;
92
93         size <<= BIT_TO_WORD_SHIFT;
94
95         if (start_bit >= size)
96                 return -1;
97         size -= result;
98         start_bit &= (NBWORD - 1);
99         if (start_bit) {
100                 tmp = *p++;
101                 /* set to zero first offset bits prior to start */
102                 tmp &= (~0U << start_bit);
103                 if (tmp != 0U)
104                         goto found;
105                 result += NBWORD;
106                 size -= NBWORD;
107         }
108         while (size) {
109                 if ((tmp = *p++) != 0U)
110                         goto found;
111                 result += NBWORD;
112                 size -= NBWORD;
113         }
114         return -1;
115 found:
116         return result + ffs(tmp) - 1;
117 }