xref: /f-stack/tools/compat/include/sys/_bitset.h (revision 1eaf0ac3)
1*1eaf0ac3Slogwang /*-
2*1eaf0ac3Slogwang  * Copyright (c) 2008, Jeffrey Roberson <[email protected]>
3*1eaf0ac3Slogwang  * All rights reserved.
4*1eaf0ac3Slogwang  *
5*1eaf0ac3Slogwang  * Copyright (c) 2008 Nokia Corporation
6*1eaf0ac3Slogwang  * All rights reserved.
7*1eaf0ac3Slogwang  *
8*1eaf0ac3Slogwang  * Redistribution and use in source and binary forms, with or without
9*1eaf0ac3Slogwang  * modification, are permitted provided that the following conditions
10*1eaf0ac3Slogwang  * are met:
11*1eaf0ac3Slogwang  * 1. Redistributions of source code must retain the above copyright
12*1eaf0ac3Slogwang  *    notice unmodified, this list of conditions, and the following
13*1eaf0ac3Slogwang  *    disclaimer.
14*1eaf0ac3Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
15*1eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer in the
16*1eaf0ac3Slogwang  *    documentation and/or other materials provided with the distribution.
17*1eaf0ac3Slogwang  *
18*1eaf0ac3Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*1eaf0ac3Slogwang  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*1eaf0ac3Slogwang  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*1eaf0ac3Slogwang  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*1eaf0ac3Slogwang  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*1eaf0ac3Slogwang  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*1eaf0ac3Slogwang  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*1eaf0ac3Slogwang  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*1eaf0ac3Slogwang  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*1eaf0ac3Slogwang  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*1eaf0ac3Slogwang  *
29*1eaf0ac3Slogwang  * $FreeBSD$
30*1eaf0ac3Slogwang  */
31*1eaf0ac3Slogwang 
32*1eaf0ac3Slogwang #ifndef _SYS__BITSET_H_
33*1eaf0ac3Slogwang #define	_SYS__BITSET_H_
34*1eaf0ac3Slogwang 
35*1eaf0ac3Slogwang /*
36*1eaf0ac3Slogwang  * Macros addressing word and bit within it, tuned to make compiler
37*1eaf0ac3Slogwang  * optimize cases when SETSIZE fits into single machine word.
38*1eaf0ac3Slogwang  */
39*1eaf0ac3Slogwang #define	_BITSET_BITS		(sizeof(long) * 8)
40*1eaf0ac3Slogwang 
41*1eaf0ac3Slogwang #define	__howmany(x, y)	(((x) + ((y) - 1)) / (y))
42*1eaf0ac3Slogwang 
43*1eaf0ac3Slogwang #define	__bitset_words(_s)	(__howmany(_s, _BITSET_BITS))
44*1eaf0ac3Slogwang 
45*1eaf0ac3Slogwang #define	BITSET_DEFINE(t, _s)						\
46*1eaf0ac3Slogwang struct t {								\
47*1eaf0ac3Slogwang         long    __bits[__bitset_words((_s))];				\
48*1eaf0ac3Slogwang }
49*1eaf0ac3Slogwang 
50*1eaf0ac3Slogwang /*
51*1eaf0ac3Slogwang  * Helper to declare a bitset without it's size being a constant.
52*1eaf0ac3Slogwang  *
53*1eaf0ac3Slogwang  * Sadly we cannot declare a bitset struct with '__bits[]', because it's
54*1eaf0ac3Slogwang  * the only member of the struct and the compiler complains.
55*1eaf0ac3Slogwang  */
56*1eaf0ac3Slogwang #define BITSET_DEFINE_VAR(t)	BITSET_DEFINE(t, 1)
57*1eaf0ac3Slogwang 
58*1eaf0ac3Slogwang #endif /* !_SYS__BITSET_H_ */
59