subr_hash.c revision 32286
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: kern_subr.c,v 1.14 1997/12/19 09:03:23 dyson Exp $
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/lock.h>
47
48#include <vm/vm.h>
49#include <vm/vm_prot.h>
50#include <vm/vm_page.h>
51#include <vm/vm_map.h>
52
53int
54uiomove(cp, n, uio)
55	register caddr_t cp;
56	register int n;
57	register struct uio *uio;
58{
59	register struct iovec *iov;
60	u_int cnt;
61	int error;
62
63#ifdef DIAGNOSTIC
64	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
65		panic("uiomove: mode");
66	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
67		panic("uiomove proc");
68#endif
69	while (n > 0 && uio->uio_resid) {
70		iov = uio->uio_iov;
71		cnt = iov->iov_len;
72		if (cnt == 0) {
73			uio->uio_iov++;
74			uio->uio_iovcnt--;
75			continue;
76		}
77		if (cnt > n)
78			cnt = n;
79
80		switch (uio->uio_segflg) {
81
82		case UIO_USERSPACE:
83		case UIO_USERISPACE:
84			if (uio->uio_rw == UIO_READ)
85				error = copyout(cp, iov->iov_base, cnt);
86			else
87				error = copyin(iov->iov_base, cp, cnt);
88			if (error)
89				return (error);
90			break;
91
92		case UIO_SYSSPACE:
93			if (uio->uio_rw == UIO_READ)
94				bcopy((caddr_t)cp, iov->iov_base, cnt);
95			else
96				bcopy(iov->iov_base, (caddr_t)cp, cnt);
97			break;
98		case UIO_NOCOPY:
99			break;
100		}
101		iov->iov_base += cnt;
102		iov->iov_len -= cnt;
103		uio->uio_resid -= cnt;
104		uio->uio_offset += cnt;
105		cp += cnt;
106		n -= cnt;
107	}
108	return (0);
109}
110
111int
112uiomoveco(cp, n, uio, obj)
113	caddr_t cp;
114	int n;
115	struct uio *uio;
116	struct vm_object *obj;
117{
118	struct iovec *iov;
119	u_int cnt;
120	int error;
121
122#ifdef DIAGNOSTIC
123	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
124		panic("uiomove: mode");
125	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
126		panic("uiomove proc");
127#endif
128	while (n > 0 && uio->uio_resid) {
129		iov = uio->uio_iov;
130		cnt = iov->iov_len;
131		if (cnt == 0) {
132			uio->uio_iov++;
133			uio->uio_iovcnt--;
134			continue;
135		}
136		if (cnt > n)
137			cnt = n;
138
139		switch (uio->uio_segflg) {
140
141		case UIO_USERSPACE:
142		case UIO_USERISPACE:
143			if (uio->uio_rw == UIO_READ) {
144				if (((cnt & PAGE_MASK) == 0) &&
145					((((int) iov->iov_base) & PAGE_MASK) == 0) &&
146					((uio->uio_offset & PAGE_MASK) == 0) &&
147					((((int) cp) & PAGE_MASK) == 0)) {
148						error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
149								uio->uio_offset, cnt,
150								(vm_offset_t) iov->iov_base, NULL);
151				} else {
152					error = copyout(cp, iov->iov_base, cnt);
153				}
154			} else {
155				error = copyin(iov->iov_base, cp, cnt);
156			}
157			if (error)
158				return (error);
159			break;
160
161		case UIO_SYSSPACE:
162			if (uio->uio_rw == UIO_READ)
163				bcopy((caddr_t)cp, iov->iov_base, cnt);
164			else
165				bcopy(iov->iov_base, (caddr_t)cp, cnt);
166			break;
167		case UIO_NOCOPY:
168			break;
169		}
170		iov->iov_base += cnt;
171		iov->iov_len -= cnt;
172		uio->uio_resid -= cnt;
173		uio->uio_offset += cnt;
174		cp += cnt;
175		n -= cnt;
176	}
177	return (0);
178}
179
180int
181uioread(n, uio, obj, nread)
182	int n;
183	struct uio *uio;
184	struct vm_object *obj;
185	int *nread;
186{
187	int npagesmoved;
188	struct iovec *iov;
189	u_int cnt, tcnt;
190	int error;
191
192	*nread = 0;
193	error = 0;
194
195	while (n > 0 && uio->uio_resid) {
196		iov = uio->uio_iov;
197		cnt = iov->iov_len;
198		if (cnt == 0) {
199			uio->uio_iov++;
200			uio->uio_iovcnt--;
201			continue;
202		}
203		if (cnt > n)
204			cnt = n;
205
206		if ((uio->uio_segflg == UIO_USERSPACE) &&
207			((((int) iov->iov_base) & PAGE_MASK) == 0) &&
208				 ((uio->uio_offset & PAGE_MASK) == 0) ) {
209
210			if (cnt < PAGE_SIZE)
211				break;
212
213			cnt &= ~PAGE_MASK;
214
215			error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
216						uio->uio_offset, cnt,
217						(vm_offset_t) iov->iov_base, &npagesmoved);
218
219			if (npagesmoved == 0)
220				break;
221
222			tcnt = npagesmoved * PAGE_SIZE;
223			if (tcnt != cnt) {
224				cnt = tcnt;
225			}
226
227			if (error)
228				break;
229
230			iov->iov_base += cnt;
231			iov->iov_len -= cnt;
232			uio->uio_resid -= cnt;
233			uio->uio_offset += cnt;
234			*nread += cnt;
235			n -= cnt;
236		} else {
237			break;
238		}
239	}
240	return error;
241}
242
243/*
244 * Give next character to user as result of read.
245 */
246int
247ureadc(c, uio)
248	register int c;
249	register struct uio *uio;
250{
251	register struct iovec *iov;
252
253again:
254	if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
255		panic("ureadc");
256	iov = uio->uio_iov;
257	if (iov->iov_len == 0) {
258		uio->uio_iovcnt--;
259		uio->uio_iov++;
260		goto again;
261	}
262	switch (uio->uio_segflg) {
263
264	case UIO_USERSPACE:
265		if (subyte(iov->iov_base, c) < 0)
266			return (EFAULT);
267		break;
268
269	case UIO_SYSSPACE:
270		*iov->iov_base = c;
271		break;
272
273	case UIO_USERISPACE:
274		if (suibyte(iov->iov_base, c) < 0)
275			return (EFAULT);
276		break;
277	case UIO_NOCOPY:
278		break;
279	}
280	iov->iov_base++;
281	iov->iov_len--;
282	uio->uio_resid--;
283	uio->uio_offset++;
284	return (0);
285}
286
287#ifdef vax	/* unused except by ct.c, other oddities XXX */
288/*
289 * Get next character written in by user from uio.
290 */
291int
292uwritec(uio)
293	struct uio *uio;
294{
295	register struct iovec *iov;
296	register int c;
297
298	if (uio->uio_resid <= 0)
299		return (-1);
300again:
301	if (uio->uio_iovcnt <= 0)
302		panic("uwritec");
303	iov = uio->uio_iov;
304	if (iov->iov_len == 0) {
305		uio->uio_iov++;
306		if (--uio->uio_iovcnt == 0)
307			return (-1);
308		goto again;
309	}
310	switch (uio->uio_segflg) {
311
312	case UIO_USERSPACE:
313		c = fubyte(iov->iov_base);
314		break;
315
316	case UIO_SYSSPACE:
317		c = *(u_char *) iov->iov_base;
318		break;
319
320	case UIO_USERISPACE:
321		c = fuibyte(iov->iov_base);
322		break;
323	}
324	if (c < 0)
325		return (-1);
326	iov->iov_base++;
327	iov->iov_len--;
328	uio->uio_resid--;
329	uio->uio_offset++;
330	return (c);
331}
332#endif /* vax */
333
334/*
335 * General routine to allocate a hash table.
336 */
337void *
338hashinit(elements, type, hashmask)
339	int elements;
340	struct malloc_type *type;
341	u_long *hashmask;
342{
343	long hashsize;
344	LIST_HEAD(generic, generic) *hashtbl;
345	int i;
346
347	if (elements <= 0)
348		panic("hashinit: bad elements");
349	for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
350		continue;
351	hashsize >>= 1;
352	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
353	for (i = 0; i < hashsize; i++)
354		LIST_INIT(&hashtbl[i]);
355	*hashmask = hashsize - 1;
356	return (hashtbl);
357}
358
359static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
360			2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
361			7159, 7673, 8191, 12281, 16381, 24571, 32749 };
362#define NPRIMES (sizeof(primes) / sizeof(primes[0]))
363
364/*
365 * General routine to allocate a prime number sized hash table.
366 */
367void *
368phashinit(elements, type, nentries)
369	int elements;
370	struct malloc_type *type;
371	u_long *nentries;
372{
373	long hashsize;
374	LIST_HEAD(generic, generic) *hashtbl;
375	int i;
376
377	if (elements <= 0)
378		panic("phashinit: bad elements");
379	for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
380		i++;
381		if (i == NPRIMES)
382			break;
383		hashsize = primes[i];
384	}
385	hashsize = primes[i - 1];
386	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
387	for (i = 0; i < hashsize; i++)
388		LIST_INIT(&hashtbl[i]);
389	*nentries = hashsize;
390	return (hashtbl);
391}
392