2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This file is part of the device-mapper userspace tools.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 /* FIXME: calculate this. */
21 dm_bitset_t dm_bitset_create(struct dm_pool *mem, unsigned num_bits)
23 unsigned n = (num_bits / DM_BITS_PER_INT) + 2;
24 size_t size = sizeof(int) * n;
28 bs = dm_pool_zalloc(mem, size);
40 void dm_bitset_destroy(dm_bitset_t bs)
45 int dm_bitset_equal(dm_bitset_t in1, dm_bitset_t in2)
49 for (i = (in1[0] / DM_BITS_PER_INT) + 1; i; i--)
56 void dm_bit_and(dm_bitset_t out, dm_bitset_t in1, dm_bitset_t in2)
60 for (i = (in1[0] / DM_BITS_PER_INT) + 1; i; i--)
61 out[i] = in1[i] & in2[i];
63 void dm_bit_union(dm_bitset_t out, dm_bitset_t in1, dm_bitset_t in2)
66 for (i = (in1[0] / DM_BITS_PER_INT) + 1; i; i--)
67 out[i] = in1[i] | in2[i];
70 static int _test_word(uint32_t test, int bit)
72 uint32_t tb = test >> bit;
74 return (tb ? ffs(tb) + bit - 1 : -1);
77 int dm_bit_get_next(dm_bitset_t bs, int last_bit)
82 last_bit++; /* otherwise we'll return the same bit again */
85 * bs[0] holds number of bits
87 while (last_bit < (int) bs[0]) {
88 word = last_bit >> INT_SHIFT;
90 bit = last_bit & (DM_BITS_PER_INT - 1);
92 if ((bit = _test_word(test, bit)) >= 0)
93 return (word * DM_BITS_PER_INT) + bit;
95 last_bit = last_bit - (last_bit & (DM_BITS_PER_INT - 1)) +
102 int dm_bit_get_first(dm_bitset_t bs)
104 return dm_bit_get_next(bs, -1);