2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * bit.c: Allocate and search select bitfields
30 * Known contributors to this file:
36 #include "gen.h" /* getfdtablesize etc. */
38 static int bit_nbytes;
46 if (bit_nbytes == 0) {
47 nfile = getfdtablesize();
48 bit_nbytes = (nfile + (BIT_BITSPERMASK-1)) / BIT_NBBY;
50 mask = (bit_fdmask) malloc(bit_nbytes);
51 (void) bit_zero(mask);
59 bit_zero(bit_fdmask bitp)
66 nwords = bit_nbytes / sizeof(*mask);
67 for (i=0; i<nwords; i++)
75 bit_not(bit_fdmask bitp)
77 register bit_mask *mask;
82 nwords = bit_nbytes / sizeof(*mask);
83 for (i=0; i<nwords; i++,mask++)
91 bit_copy(bit_fdmask bitsrc, bit_fdmask bitdst)
93 register bit_mask *src;
94 register bit_mask *dst;
100 nwords = bit_nbytes / sizeof(*dst);
101 for (i=0; i<nwords; i++)
109 bit_or(bit_fdmask bitsrc, bit_fdmask bitdst)
111 register bit_mask *src;
112 register bit_mask *dst;
116 nwords = bit_nbytes / sizeof(*dst);
119 for (i=0; i<nwords; i++)
127 bit_or3(bit_fdmask bitsrc1, bit_fdmask bitsrc2, bit_fdmask bitdst)
129 register bit_mask *src1;
130 register bit_mask *src2;
131 register bit_mask *dst;
138 nwords = bit_nbytes / sizeof(*dst);
139 for (i=0; i<nwords; i++)
140 *dst++ = *src1++ | *src2++;
147 bit_and(bit_fdmask bitsrc, bit_fdmask bitdst)
149 register bit_mask *src;
150 register bit_mask *dst;
154 nwords = bit_nbytes / sizeof(*src);
157 for (i=0; i<nwords; i++)
165 bit_and3(bit_fdmask bitsrc1, bit_fdmask bitsrc2, bit_fdmask bitdst)
167 register bit_mask *src1;
168 register bit_mask *src2;
169 register bit_mask *dst;
176 nwords = bit_nbytes / sizeof(*dst);
177 for (i=0; i<nwords; i++)
178 *dst++ = *src1++ & *src2++;
182 * Return first bit set in fd mask.
183 * speedy version, not using BIT_ISSETB()
186 bit_fd(bit_fdmask bitp)
188 register bit_mask *mask;
189 register unsigned int j;
195 nwords = bit_nbytes / sizeof(m);
196 for (i=0; i<nwords; i++,mask++) {
197 if ((m = *mask) == 0)
199 for (j=0; j<BIT_BITSPERMASK; j++) {
201 return i*BIT_BITSPERMASK + j;