2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/queue.h>
52 register struct uio
*uio
;
54 register struct iovec
*iov
;
59 if (uio
->uio_rw
!= UIO_READ
&& uio
->uio_rw
!= UIO_WRITE
)
60 panic("uiomove: mode");
61 if (uio
->uio_segflg
== UIO_USERSPACE
&& uio
->uio_procp
!= curproc
)
62 panic("uiomove proc");
64 while (n
> 0 && uio
->uio_resid
) {
75 switch (uio
->uio_segflg
) {
79 if (uio
->uio_rw
== UIO_READ
)
80 error
= copyout(cp
, iov
->iov_base
, cnt
);
82 error
= copyin(iov
->iov_base
, cp
, cnt
);
88 if (uio
->uio_rw
== UIO_READ
)
89 bcopy((caddr_t
)cp
, iov
->iov_base
, cnt
);
91 bcopy(iov
->iov_base
, (caddr_t
)cp
, cnt
);
98 uio
->uio_resid
-= cnt
;
99 uio
->uio_offset
+= cnt
;
107 * Give next character to user as result of read.
112 register struct uio
*uio
;
114 register struct iovec
*iov
;
117 if (uio
->uio_iovcnt
== 0 || uio
->uio_resid
== 0)
120 if (iov
->iov_len
== 0) {
125 switch (uio
->uio_segflg
) {
128 if (subyte(iov
->iov_base
, c
) < 0)
137 if (suibyte(iov
->iov_base
, c
) < 0)
150 #ifdef vax /* unused except by ct.c, other oddities XXX */
152 * Get next character written in by user from uio.
158 register struct iovec
*iov
;
161 if (uio
->uio_resid
<= 0)
164 if (uio
->uio_iovcnt
<= 0)
167 if (iov
->iov_len
== 0) {
169 if (--uio
->uio_iovcnt
== 0)
173 switch (uio
->uio_segflg
) {
176 c
= fubyte(iov
->iov_base
);
180 c
= *(u_char
*) iov
->iov_base
;
184 c
= fuibyte(iov
->iov_base
);
198 * General routine to allocate a hash table.
201 hashinit(elements
, type
, hashmask
)
206 LIST_HEAD(generic
, generic
) *hashtbl
;
210 panic("hashinit: bad elements");
211 for (hashsize
= 1; hashsize
<= elements
; hashsize
<<= 1)
214 hashtbl
= malloc((u_long
)hashsize
* sizeof(*hashtbl
), type
, M_WAITOK
);
215 for (i
= 0; i
< hashsize
; i
++)
216 LIST_INIT(&hashtbl
[i
]);
217 *hashmask
= hashsize
- 1;
222 static int primes
[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
223 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
224 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
227 * General routine to allocate a prime number sized hash table.
230 phashinit(elements
, type
, nentries
)
235 LIST_HEAD(generic
, generic
) *hashtbl
;
239 panic("phashinit: bad elements");
240 for (i
= 1, hashsize
= primes
[1]; hashsize
<= elements
;) {
244 hashsize
= primes
[i
];
246 hashsize
= primes
[i
- 1];
247 hashtbl
= malloc((u_long
)hashsize
* sizeof(*hashtbl
), type
, M_WAITOK
);
248 for (i
= 0; i
< hashsize
; i
++)
249 LIST_INIT(&hashtbl
[i
]);
250 *nentries
= hashsize
;