2018-06-06 09:42:14 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 05:20:36 +07:00
|
|
|
/*
|
2005-11-02 10:58:39 +07:00
|
|
|
* Copyright (c) 2000-2005 Silicon Graphics, Inc.
|
|
|
|
* All Rights Reserved.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
|
|
|
#include "xfs.h"
|
2013-10-23 06:50:10 +07:00
|
|
|
#include "xfs_log_format.h"
|
2013-10-29 18:11:58 +07:00
|
|
|
#include "xfs_bit.h"
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2005-11-02 10:58:39 +07:00
|
|
|
/*
|
|
|
|
* XFS bit manipulation routines, used in non-realtime code.
|
|
|
|
*/
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/*
|
2007-06-28 13:43:30 +07:00
|
|
|
* Return whether bitmap is empty.
|
|
|
|
* Size is number of words in the bitmap, which is padded to word boundary
|
|
|
|
* Returns 1 for empty, 0 for non-empty.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
|
|
|
int
|
2007-06-28 13:43:30 +07:00
|
|
|
xfs_bitmap_empty(uint *map, uint size)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2007-06-28 13:43:30 +07:00
|
|
|
uint i;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2007-06-28 13:43:30 +07:00
|
|
|
for (i = 0; i < size; i++) {
|
2016-01-04 12:10:19 +07:00
|
|
|
if (map[i] != 0)
|
|
|
|
return 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2016-01-04 12:10:19 +07:00
|
|
|
return 1;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Count the number of contiguous bits set in the bitmap starting with bit
|
|
|
|
* start_bit. Size is the size of the bitmap in words.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
xfs_contig_bits(uint *map, uint size, uint start_bit)
|
|
|
|
{
|
|
|
|
uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
|
|
|
|
uint result = 0;
|
|
|
|
uint tmp;
|
|
|
|
|
|
|
|
size <<= BIT_TO_WORD_SHIFT;
|
|
|
|
|
|
|
|
ASSERT(start_bit < size);
|
|
|
|
size -= start_bit & ~(NBWORD - 1);
|
|
|
|
start_bit &= (NBWORD - 1);
|
|
|
|
if (start_bit) {
|
|
|
|
tmp = *p++;
|
|
|
|
/* set to one first offset bits prior to start */
|
|
|
|
tmp |= (~0U >> (NBWORD-start_bit));
|
|
|
|
if (tmp != ~0U)
|
|
|
|
goto found;
|
|
|
|
result += NBWORD;
|
|
|
|
size -= NBWORD;
|
|
|
|
}
|
|
|
|
while (size) {
|
|
|
|
if ((tmp = *p++) != ~0U)
|
|
|
|
goto found;
|
|
|
|
result += NBWORD;
|
|
|
|
size -= NBWORD;
|
|
|
|
}
|
|
|
|
return result - start_bit;
|
|
|
|
found:
|
|
|
|
return result + ffz(tmp) - start_bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This takes the bit number to start looking from and
|
|
|
|
* returns the next set bit from there. It returns -1
|
|
|
|
* if there are no more bits set or the start bit is
|
|
|
|
* beyond the end of the bitmap.
|
|
|
|
*
|
|
|
|
* Size is the number of words, not bytes, in the bitmap.
|
|
|
|
*/
|
|
|
|
int xfs_next_bit(uint *map, uint size, uint start_bit)
|
|
|
|
{
|
|
|
|
uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
|
|
|
|
uint result = start_bit & ~(NBWORD - 1);
|
|
|
|
uint tmp;
|
|
|
|
|
|
|
|
size <<= BIT_TO_WORD_SHIFT;
|
|
|
|
|
|
|
|
if (start_bit >= size)
|
|
|
|
return -1;
|
|
|
|
size -= result;
|
|
|
|
start_bit &= (NBWORD - 1);
|
|
|
|
if (start_bit) {
|
|
|
|
tmp = *p++;
|
|
|
|
/* set to zero first offset bits prior to start */
|
|
|
|
tmp &= (~0U << start_bit);
|
|
|
|
if (tmp != 0U)
|
|
|
|
goto found;
|
|
|
|
result += NBWORD;
|
|
|
|
size -= NBWORD;
|
|
|
|
}
|
|
|
|
while (size) {
|
|
|
|
if ((tmp = *p++) != 0U)
|
|
|
|
goto found;
|
|
|
|
result += NBWORD;
|
|
|
|
size -= NBWORD;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
found:
|
|
|
|
return result + ffs(tmp) - 1;
|
|
|
|
}
|