4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
7 * This file is part of the device-mapper userspace tools.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* FIXME: calculate this. */
23 dm_bitset_t
dm_bitset_create(struct dm_pool
*mem
, unsigned num_bits
)
25 unsigned n
= (num_bits
/ DM_BITS_PER_INT
) + 2;
26 size_t size
= sizeof(int) * n
;
30 bs
= dm_pool_zalloc(mem
, size
);
45 void dm_bitset_destroy(dm_bitset_t bs
)
50 void dm_bit_union(dm_bitset_t out
, dm_bitset_t in1
, dm_bitset_t in2
)
53 for (i
= (in1
[0] / DM_BITS_PER_INT
) + 1; i
; i
--)
54 out
[i
] = in1
[i
] | in2
[i
];
60 static inline int _test_word(uint32_t test
, int bit
)
62 while (bit
< (int) DM_BITS_PER_INT
) {
63 if (test
& (0x1 << bit
))
71 int dm_bit_get_next(dm_bitset_t bs
, int last_bit
)
76 last_bit
++; /* otherwise we'll return the same bit again */
79 * bs[0] holds number of bits
81 while (last_bit
< (int) bs
[0]) {
82 word
= last_bit
>> INT_SHIFT
;
84 bit
= last_bit
& (DM_BITS_PER_INT
- 1);
86 if ((bit
= _test_word(test
, bit
)) >= 0)
87 return (word
* DM_BITS_PER_INT
) + bit
;
89 last_bit
= last_bit
- (last_bit
& (DM_BITS_PER_INT
- 1)) +
96 int dm_bit_get_first(dm_bitset_t bs
)
98 return dm_bit_get_next(bs
, -1);