1 /***************************************************************************
2 * Copyright 1995, Technion, Israel Institute of Technology
3 * Electrical Eng, Software Lab.
4 * Author: Michael Veksler.
5 ***************************************************************************
7 * Purpose : manipulate array of bits
8 * Portability: This is not completely portable, non CISC arcitectures
9 * Might not have atomic Clear/Set/Toggle bit. On those
10 * architectures semaphores should be used.
11 * Big Endian Concerns: This code is big endian compatible,
12 * but the byte order will be different (i.e. bit 0 will be
14 ***************************************************************************
20 ** uncoment the following line to disable assertions,
21 ** this may boost performance by up to 50%
25 #if defined(linux) && !defined(NO_ASM)
26 #include <linux/version.h>
27 #if LINUX_VERSION_CODE <= 131328 /* Linux 2.1.x doesn't return values with clear_bit and set_bit */
36 #include "bit_array.h"
38 #define inline __inline__ /* So we can compile with -ansi */
39 #include <asm/bitops.h>
41 static __inline__
int clear_bit(int bit
, int *mem
);
42 static __inline__
int set_bit(int bit
, int *mem
);
43 #endif /* HAS_BITOPS */
46 #define INT_NR(bit_nr) ((bit_nr) >> INT_LOG2)
47 #define INT_COUNT(bit_count) INT_NR( bit_count + BITS_PER_INT - 1 )
48 #define BIT_IN_INT(bit_nr) ((bit_nr) & (BITS_PER_INT - 1))
50 #if !defined(HAS_BITOPS)
52 /* first_zero maps bytes value to the index of first zero bit */
53 static char first_zero
[256];
54 static int arrays_initialized
=0;
58 ** initialize static arrays used for bit operations speedup.
59 ** Currently initialized: first_zero[256]
60 ** set "arrays_initialized" to inidate that arrays where initialized
63 static void initialize_arrays()
68 for (i
=0 ; i
<256 ; i
++) {
69 /* find the first zero bit in `i' */
70 for (bit
=0 ; bit
< BITS_PER_BYTE
; bit
++)
71 /* break if the bit is zero */
72 if ( ( (1 << bit
) & i
)
81 ** Find first zero bit in the integer.
82 ** Assume there is at least one zero.
84 static __inline__
int find_zbit_in_integer(unsigned int integer
)
88 /* find the zero bit */
89 for (i
=0 ; i
< sizeof(int) ; i
++, integer
>>=8) {
90 int byte
= integer
& 0xff;
93 return ( first_zero
[ byte
]
96 assert(0); /* never reached */
100 /* return -1 on failure */
101 static __inline__
int find_first_zero_bit(unsigned *array
, int bits
)
103 unsigned int integer
;
105 int bytes
=INT_COUNT(bits
);
107 if (!arrays_initialized
)
110 for ( i
=bytes
; i
; i
--, array
++) {
113 /* test if integer contains a zero bit */
115 return ( find_zbit_in_integer(integer
)
116 + ((bytes
-i
) << INT_LOG2
) );
119 /* indicate failure */
123 static __inline__
int test_bit(int pos
, unsigned *array
)
125 unsigned int integer
;
126 int bit
= BIT_IN_INT(pos
);
128 integer
= array
[ pos
>> INT_LOG2
];
130 return ( (integer
& (1 << bit
)) != 0
136 ** The following two functions are x86 specific ,
137 ** other processors will need porting
140 /* inputs: bit number and memory address (32 bit) */
141 /* output: Value of the bit before modification */
142 static __inline__
int clear_bit(int bit
, int *mem
)
149 :"=m" (*mem
), "=&r" (ret
)
154 static __inline__
int set_bit(int bit
, int *mem
)
160 :"=m" (*mem
), "=&r" (ret
)
165 #endif /* !deined(HAS_BITOPS) */
168 /* AssembleArray: assemble an array object using existing data */
169 bit_array
*AssembleArray(bit_array
*new_array
, unsigned int *buff
, int bits
)
171 assert(new_array
!=NULL
);
174 assert((1 << INT_LOG2
) == BITS_PER_INT
); /* if fails, redefine INT_LOG2 */
176 new_array
->bits
=bits
;
177 new_array
->array
=buff
;
181 /* ResetArray: reset the bit array to zeros */
182 int ResetArray(bit_array
*bits
)
188 assert(bits
->array
!=NULL
);
190 for(i
= INT_COUNT(bits
->bits
), p
=bits
->array
; i
; p
++, i
--)
196 /* VacantBit: find a vacant (zero) bit in the array,
197 * Return: Bit index on success, -1 on failure.
199 int VacantBit(bit_array
*bits
)
204 assert(bits
->array
!=NULL
);
206 bit
= find_first_zero_bit(bits
->array
, bits
->bits
);
208 if (bit
>= bits
->bits
) /* failed? */
214 int SampleBit(bit_array
*bits
, int i
)
216 assert(bits
!= NULL
);
217 assert(bits
->array
!= NULL
);
218 assert(i
>= 0 && i
< bits
->bits
);
220 return ( test_bit(i
,bits
->array
) != 0
228 ** Use "compare and exchange" mechanism to make sure
229 ** that bits are not modified while "integer" value
232 ** This may be the slowest technique, but it is the most portable
233 ** (Since most architectures have compare and exchange command)
235 int AssignBit(bit_array
*bits
, int bit_nr
, int val
)
239 assert(bits
!= NULL
);
240 assert(bits
->array
!= NULL
);
241 assert(val
==0 || val
==1);
242 assert(bit_nr
>= 0 && bit_nr
< bits
->bits
);
245 ret
= clear_bit(BIT_IN_INT(bit_nr
), &bits
->array
[ INT_NR(bit_nr
) ]);
247 ret
= set_bit(BIT_IN_INT(bit_nr
), &bits
->array
[ INT_NR(bit_nr
) ]);
249 return ( (ret
!=0) ? 1 : 0);
253 ** Allocate a free bit (==0) and make it used (==1).
254 ** This operation is guaranteed to resemble an atomic instruction.
256 ** Return: allocated bit index, or -1 on failure.
258 ** There is a crack between locating free bit, and allocating it.
259 ** We assign 1 to the bit, test it was not '1' before the assignment.
260 ** If it was, restart the seek and assign cycle.
264 int AllocateBit(bit_array
*bits
)
269 assert(bits
!= NULL
);
270 assert(bits
->array
!= NULL
);
273 bit_nr
= VacantBit(bits
);
275 if (bit_nr
== -1) /* No vacant bit ? */
278 orig_bit
= AssignBit(bits
, bit_nr
, 1);
279 } while (orig_bit
!= 0); /* it got assigned before we tried */
284 #endif /* CONFIG_IPC */