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