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 #include <asm/bitops.h>
40 static inline int clear_bit(int bit
, int *mem
);
41 static inline int set_bit(int bit
, int *mem
);
42 #endif /* HAS_BITOPS */
45 #define INT_NR(bit_nr) ((bit_nr) >> INT_LOG2)
46 #define INT_COUNT(bit_count) INT_NR( bit_count + BITS_PER_INT - 1 )
47 #define BIT_IN_INT(bit_nr) ((bit_nr) & (BITS_PER_INT - 1))
49 #if !defined(HAS_BITOPS)
51 /* first_zero maps bytes value to the index of first zero bit */
52 static char first_zero
[256];
53 static int arrays_initialized
=0;
57 ** initialize static arrays used for bit operations speedup.
58 ** Currently initialized: first_zero[256]
59 ** set "arrays_initialized" to inidate that arrays where initialized
62 static void initialize_arrays()
67 for (i
=0 ; i
<256 ; i
++) {
68 /* find the first zero bit in `i' */
69 for (bit
=0 ; bit
< BITS_PER_BYTE
; bit
++)
70 /* break if the bit is zero */
71 if ( ( (1 << bit
) & i
)
80 ** Find first zero bit in the integer.
81 ** Assume there is at least one zero.
83 static inline int find_zbit_in_integer(unsigned int integer
)
87 /* find the zero bit */
88 for (i
=0 ; i
< sizeof(int) ; i
++, integer
>>=8) {
89 int byte
= integer
& 0xff;
92 return ( first_zero
[ byte
]
95 assert(0); /* never reached */
99 /* return -1 on failure */
100 static inline int find_first_zero_bit(unsigned *array
, int bits
)
102 unsigned int integer
;
104 int bytes
=INT_COUNT(bits
);
106 if (!arrays_initialized
)
109 for ( i
=bytes
; i
; i
--, array
++) {
112 /* test if integer contains a zero bit */
114 return ( find_zbit_in_integer(integer
)
115 + ((bytes
-i
) << INT_LOG2
) );
118 /* indicate failure */
122 static inline int test_bit(int pos
, unsigned *array
)
124 unsigned int integer
;
125 int bit
= BIT_IN_INT(pos
);
127 integer
= array
[ pos
>> INT_LOG2
];
129 return ( (integer
& (1 << bit
)) != 0
135 ** The following two functions are x86 specific ,
136 ** other processors will need porting
139 /* inputs: bit number and memory address (32 bit) */
140 /* output: Value of the bit before modification */
141 static inline int clear_bit(int bit
, int *mem
)
145 __asm__("xor %1,%1\n"
148 :"=m" (*mem
), "=&r" (ret
)
153 static inline int set_bit(int bit
, int *mem
)
156 __asm__("xor %1,%1\n"
159 :"=m" (*mem
), "=&r" (ret
)
164 #endif /* !deined(HAS_BITOPS) */
167 /* AssembleArray: assemble an array object using existing data */
168 bit_array
*AssembleArray(bit_array
*new_array
, unsigned int *buff
, int bits
)
170 assert(new_array
!=NULL
);
173 assert((1 << INT_LOG2
) == BITS_PER_INT
); /* if fails, redefine INT_LOG2 */
175 new_array
->bits
=bits
;
176 new_array
->array
=buff
;
180 /* ResetArray: reset the bit array to zeros */
181 int ResetArray(bit_array
*bits
)
187 assert(bits
->array
!=NULL
);
189 for(i
= INT_COUNT(bits
->bits
), p
=bits
->array
; i
; p
++, i
--)
195 /* VacantBit: find a vacant (zero) bit in the array,
196 * Return: Bit index on success, -1 on failure.
198 int VacantBit(bit_array
*bits
)
203 assert(bits
->array
!=NULL
);
205 bit
= find_first_zero_bit(bits
->array
, bits
->bits
);
207 if (bit
>= bits
->bits
) /* failed? */
213 int SampleBit(bit_array
*bits
, int i
)
215 assert(bits
!= NULL
);
216 assert(bits
->array
!= NULL
);
217 assert(i
>= 0 && i
< bits
->bits
);
219 return ( test_bit(i
,bits
->array
) != 0
227 ** Use "compare and exchange" mechanism to make sure
228 ** that bits are not modified while "integer" value
231 ** This may be the slowest technique, but it is the most portable
232 ** (Since most architectures have compare and exchange command)
234 int AssignBit(bit_array
*bits
, int bit_nr
, int val
)
238 assert(bits
!= NULL
);
239 assert(bits
->array
!= NULL
);
240 assert(val
==0 || val
==1);
241 assert(bit_nr
>= 0 && bit_nr
< bits
->bits
);
244 ret
= clear_bit(BIT_IN_INT(bit_nr
), &bits
->array
[ INT_NR(bit_nr
) ]);
246 ret
= set_bit(BIT_IN_INT(bit_nr
), &bits
->array
[ INT_NR(bit_nr
) ]);
248 return ( (ret
!=0) ? 1 : 0);
252 ** Allocate a free bit (==0) and make it used (==1).
253 ** This operation is guaranteed to resemble an atomic instruction.
255 ** Return: allocated bit index, or -1 on failure.
257 ** There is a crack between locating free bit, and allocating it.
258 ** We assign 1 to the bit, test it was not '1' before the assignment.
259 ** If it was, restart the seek and assign cycle.
263 int AllocateBit(bit_array
*bits
)
268 assert(bits
!= NULL
);
269 assert(bits
->array
!= NULL
);
272 bit_nr
= VacantBit(bits
);
274 if (bit_nr
== -1) /* No vacant bit ? */
277 orig_bit
= AssignBit(bits
, bit_nr
, 1);
278 } while (orig_bit
!= 0); /* it got assigned before we tried */
283 #endif /* CONFIG_IPC */