1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// src/sys/kern/kern_subr.c
|
4 |
|
|
//
|
5 |
|
|
//==========================================================================
|
6 |
|
|
//####BSDCOPYRIGHTBEGIN####
|
7 |
|
|
//
|
8 |
|
|
// -------------------------------------------
|
9 |
|
|
//
|
10 |
|
|
// Portions of this software may have been derived from OpenBSD,
|
11 |
|
|
// FreeBSD or other sources, and are covered by the appropriate
|
12 |
|
|
// copyright disclaimers included herein.
|
13 |
|
|
//
|
14 |
|
|
// Portions created by Red Hat are
|
15 |
|
|
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
|
16 |
|
|
//
|
17 |
|
|
// -------------------------------------------
|
18 |
|
|
//
|
19 |
|
|
//####BSDCOPYRIGHTEND####
|
20 |
|
|
//==========================================================================
|
21 |
|
|
|
22 |
|
|
/*
|
23 |
|
|
* Copyright (c) 1982, 1986, 1991, 1993
|
24 |
|
|
* The Regents of the University of California. All rights reserved.
|
25 |
|
|
* (c) UNIX System Laboratories, Inc.
|
26 |
|
|
* All or some portions of this file are derived from material licensed
|
27 |
|
|
* to the University of California by American Telephone and Telegraph
|
28 |
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
29 |
|
|
* the permission of UNIX System Laboratories, Inc.
|
30 |
|
|
*
|
31 |
|
|
* Redistribution and use in source and binary forms, with or without
|
32 |
|
|
* modification, are permitted provided that the following conditions
|
33 |
|
|
* are met:
|
34 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
35 |
|
|
* notice, this list of conditions and the following disclaimer.
|
36 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
37 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
38 |
|
|
* documentation and/or other materials provided with the distribution.
|
39 |
|
|
* 3. All advertising materials mentioning features or use of this software
|
40 |
|
|
* must display the following acknowledgement:
|
41 |
|
|
* This product includes software developed by the University of
|
42 |
|
|
* California, Berkeley and its contributors.
|
43 |
|
|
* 4. Neither the name of the University nor the names of its contributors
|
44 |
|
|
* may be used to endorse or promote products derived from this software
|
45 |
|
|
* without specific prior written permission.
|
46 |
|
|
*
|
47 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
48 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
49 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
50 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
51 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
52 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
53 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
54 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
55 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
56 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
57 |
|
|
* SUCH DAMAGE.
|
58 |
|
|
*
|
59 |
|
|
* @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
|
60 |
|
|
* $FreeBSD: src/sys/kern/kern_subr.c,v 1.31 1999/10/29 18:08:51 phk Exp $
|
61 |
|
|
*/
|
62 |
|
|
|
63 |
|
|
#include <sys/param.h>
|
64 |
|
|
#include <sys/malloc.h>
|
65 |
|
|
#include <sys/queue.h>
|
66 |
|
|
#include <cyg/io/file.h>
|
67 |
|
|
|
68 |
|
|
int
|
69 |
|
|
uiomove(cp, n, uio)
|
70 |
|
|
register caddr_t cp;
|
71 |
|
|
register int n;
|
72 |
|
|
register struct uio *uio;
|
73 |
|
|
{
|
74 |
|
|
register struct iovec *iov;
|
75 |
|
|
u_int cnt;
|
76 |
|
|
int error = 0;
|
77 |
|
|
|
78 |
|
|
while (n > 0 && uio->uio_resid) {
|
79 |
|
|
iov = uio->uio_iov;
|
80 |
|
|
cnt = iov->iov_len;
|
81 |
|
|
if (cnt == 0) {
|
82 |
|
|
uio->uio_iov++;
|
83 |
|
|
uio->uio_iovcnt--;
|
84 |
|
|
continue;
|
85 |
|
|
}
|
86 |
|
|
if (cnt > n)
|
87 |
|
|
cnt = n;
|
88 |
|
|
|
89 |
|
|
switch (uio->uio_segflg) {
|
90 |
|
|
|
91 |
|
|
case UIO_USERSPACE:
|
92 |
|
|
if (uio->uio_rw == UIO_READ)
|
93 |
|
|
error = copyout(cp, iov->iov_base, cnt);
|
94 |
|
|
else
|
95 |
|
|
error = copyin(iov->iov_base, cp, cnt);
|
96 |
|
|
if (error)
|
97 |
|
|
break;
|
98 |
|
|
break;
|
99 |
|
|
|
100 |
|
|
case UIO_SYSSPACE:
|
101 |
|
|
if (uio->uio_rw == UIO_READ)
|
102 |
|
|
bcopy((caddr_t)cp, iov->iov_base, cnt);
|
103 |
|
|
else
|
104 |
|
|
bcopy(iov->iov_base, (caddr_t)cp, cnt);
|
105 |
|
|
break;
|
106 |
|
|
}
|
107 |
|
|
(char *)(iov->iov_base) += cnt;
|
108 |
|
|
iov->iov_len -= cnt;
|
109 |
|
|
uio->uio_resid -= cnt;
|
110 |
|
|
uio->uio_offset += cnt;
|
111 |
|
|
cp += cnt;
|
112 |
|
|
n -= cnt;
|
113 |
|
|
}
|
114 |
|
|
return (error);
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
/*
|
118 |
|
|
* General routine to allocate a hash table.
|
119 |
|
|
*/
|
120 |
|
|
void *
|
121 |
|
|
hashinit(int elements, void *type, u_long *hashmask)
|
122 |
|
|
{
|
123 |
|
|
long hashsize;
|
124 |
|
|
LIST_HEAD(generic, generic) *hashtbl;
|
125 |
|
|
int i;
|
126 |
|
|
|
127 |
|
|
if (elements <= 0)
|
128 |
|
|
panic("hashinit: bad elements");
|
129 |
|
|
for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
|
130 |
|
|
continue;
|
131 |
|
|
hashsize >>= 1;
|
132 |
|
|
hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
|
133 |
|
|
for (i = 0; i < hashsize; i++)
|
134 |
|
|
LIST_INIT(&hashtbl[i]);
|
135 |
|
|
*hashmask = hashsize - 1;
|
136 |
|
|
return (hashtbl);
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
|
140 |
|
|
2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
|
141 |
|
|
7159, 7673, 8191, 12281, 16381, 24571, 32749 };
|
142 |
|
|
#define NPRIMES (sizeof(primes) / sizeof(primes[0]))
|
143 |
|
|
|
144 |
|
|
/*
|
145 |
|
|
* General routine to allocate a prime number sized hash table.
|
146 |
|
|
*/
|
147 |
|
|
void *
|
148 |
|
|
phashinit(int elements, void *type, u_long *nentries)
|
149 |
|
|
{
|
150 |
|
|
long hashsize;
|
151 |
|
|
LIST_HEAD(generic, generic) *hashtbl;
|
152 |
|
|
int i;
|
153 |
|
|
|
154 |
|
|
if (elements <= 0)
|
155 |
|
|
panic("phashinit: bad elements");
|
156 |
|
|
for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
|
157 |
|
|
i++;
|
158 |
|
|
if (i == NPRIMES)
|
159 |
|
|
break;
|
160 |
|
|
hashsize = primes[i];
|
161 |
|
|
}
|
162 |
|
|
hashsize = primes[i - 1];
|
163 |
|
|
hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
|
164 |
|
|
for (i = 0; i < hashsize; i++)
|
165 |
|
|
LIST_INIT(&hashtbl[i]);
|
166 |
|
|
*nentries = hashsize;
|
167 |
|
|
return (hashtbl);
|
168 |
|
|
}
|