1 | /* |
---|
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. |
---|
9 | * |
---|
10 | * Redistribution and use in source and binary forms, with or without |
---|
11 | * modification, are permitted provided that the following conditions |
---|
12 | * are met: |
---|
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. |
---|
25 | * |
---|
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 |
---|
36 | * SUCH DAMAGE. |
---|
37 | * |
---|
38 | * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94 |
---|
39 | * $Id$ |
---|
40 | */ |
---|
41 | |
---|
42 | #include <sys/param.h> |
---|
43 | #include <sys/systm.h> |
---|
44 | #include <sys/proc.h> |
---|
45 | #include <sys/malloc.h> |
---|
46 | #include <sys/queue.h> |
---|
47 | |
---|
48 | int |
---|
49 | uiomove(void *cp, int n, struct uio *uio) |
---|
50 | { |
---|
51 | register struct iovec *iov; |
---|
52 | u_int cnt; |
---|
53 | int error; |
---|
54 | |
---|
55 | #ifdef DIAGNOSTIC |
---|
56 | if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE) |
---|
57 | panic("uiomove: mode"); |
---|
58 | #endif |
---|
59 | while (n > 0 && uio->uio_resid) { |
---|
60 | iov = uio->uio_iov; |
---|
61 | cnt = iov->iov_len; |
---|
62 | if (cnt == 0) { |
---|
63 | uio->uio_iov++; |
---|
64 | uio->uio_iovcnt--; |
---|
65 | continue; |
---|
66 | } |
---|
67 | if (cnt > n) |
---|
68 | cnt = n; |
---|
69 | |
---|
70 | switch (uio->uio_segflg) { |
---|
71 | |
---|
72 | case UIO_USERSPACE: |
---|
73 | case UIO_USERISPACE: |
---|
74 | if (uio->uio_rw == UIO_READ) |
---|
75 | error = copyout(cp, iov->iov_base, cnt); |
---|
76 | else |
---|
77 | error = copyin(iov->iov_base, cp, cnt); |
---|
78 | if (error) |
---|
79 | return (error); |
---|
80 | break; |
---|
81 | |
---|
82 | case UIO_SYSSPACE: |
---|
83 | if (uio->uio_rw == UIO_READ) |
---|
84 | bcopy((caddr_t)cp, iov->iov_base, cnt); |
---|
85 | else |
---|
86 | bcopy(iov->iov_base, (caddr_t)cp, cnt); |
---|
87 | break; |
---|
88 | case UIO_NOCOPY: |
---|
89 | break; |
---|
90 | } |
---|
91 | iov->iov_base += cnt; |
---|
92 | iov->iov_len -= cnt; |
---|
93 | uio->uio_resid -= cnt; |
---|
94 | uio->uio_offset += cnt; |
---|
95 | cp += cnt; |
---|
96 | n -= cnt; |
---|
97 | } |
---|
98 | return (0); |
---|
99 | } |
---|
100 | |
---|
101 | #ifdef vax /* unused except by ct.c, other oddities XXX */ |
---|
102 | /* |
---|
103 | * Get next character written in by user from uio. |
---|
104 | */ |
---|
105 | int |
---|
106 | uwritec(uio) |
---|
107 | struct uio *uio; |
---|
108 | { |
---|
109 | register struct iovec *iov; |
---|
110 | register int c; |
---|
111 | |
---|
112 | if (uio->uio_resid <= 0) |
---|
113 | return (-1); |
---|
114 | again: |
---|
115 | if (uio->uio_iovcnt <= 0) |
---|
116 | panic("uwritec"); |
---|
117 | iov = uio->uio_iov; |
---|
118 | if (iov->iov_len == 0) { |
---|
119 | uio->uio_iov++; |
---|
120 | if (--uio->uio_iovcnt == 0) |
---|
121 | return (-1); |
---|
122 | goto again; |
---|
123 | } |
---|
124 | switch (uio->uio_segflg) { |
---|
125 | |
---|
126 | case UIO_USERSPACE: |
---|
127 | c = fubyte(iov->iov_base); |
---|
128 | break; |
---|
129 | |
---|
130 | case UIO_SYSSPACE: |
---|
131 | c = *(u_char *) iov->iov_base; |
---|
132 | break; |
---|
133 | |
---|
134 | case UIO_USERISPACE: |
---|
135 | c = fuibyte(iov->iov_base); |
---|
136 | break; |
---|
137 | } |
---|
138 | if (c < 0) |
---|
139 | return (-1); |
---|
140 | iov->iov_base++; |
---|
141 | iov->iov_len--; |
---|
142 | uio->uio_resid--; |
---|
143 | uio->uio_offset++; |
---|
144 | return (c); |
---|
145 | } |
---|
146 | #endif /* vax */ |
---|
147 | |
---|
148 | /* |
---|
149 | * General routine to allocate a hash table. |
---|
150 | */ |
---|
151 | void * |
---|
152 | hashinit(elements, type, hashmask) |
---|
153 | int elements, type; |
---|
154 | u_long *hashmask; |
---|
155 | { |
---|
156 | long hashsize; |
---|
157 | LIST_HEAD(generic, generic) *hashtbl; |
---|
158 | int i; |
---|
159 | |
---|
160 | if (elements <= 0) |
---|
161 | panic("hashinit: bad elements"); |
---|
162 | for (hashsize = 1; hashsize <= elements; hashsize <<= 1) |
---|
163 | continue; |
---|
164 | hashsize >>= 1; |
---|
165 | hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); |
---|
166 | for (i = 0; i < hashsize; i++) |
---|
167 | LIST_INIT(&hashtbl[i]); |
---|
168 | *hashmask = hashsize - 1; |
---|
169 | return (hashtbl); |
---|
170 | } |
---|
171 | |
---|
172 | #define NPRIMES 27 |
---|
173 | static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039, |
---|
174 | 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653, |
---|
175 | 7159, 7673, 8191, 12281, 16381, 24571, 32749 }; |
---|
176 | |
---|
177 | /* |
---|
178 | * General routine to allocate a prime number sized hash table. |
---|
179 | */ |
---|
180 | void * |
---|
181 | phashinit(elements, type, nentries) |
---|
182 | int elements, type; |
---|
183 | u_long *nentries; |
---|
184 | { |
---|
185 | long hashsize; |
---|
186 | LIST_HEAD(generic, generic) *hashtbl; |
---|
187 | int i; |
---|
188 | |
---|
189 | if (elements <= 0) |
---|
190 | panic("phashinit: bad elements"); |
---|
191 | for (i = 1, hashsize = primes[1]; hashsize <= elements;) { |
---|
192 | i++; |
---|
193 | if (i == NPRIMES) |
---|
194 | break; |
---|
195 | hashsize = primes[i]; |
---|
196 | } |
---|
197 | hashsize = primes[i - 1]; |
---|
198 | hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); |
---|
199 | for (i = 0; i < hashsize; i++) |
---|
200 | LIST_INIT(&hashtbl[i]); |
---|
201 | *nentries = hashsize; |
---|
202 | return (hashtbl); |
---|
203 | } |
---|