source: rtems/cpukit/libnetworking/kern/kern_subr.c @ 39e6e65a

4.104.114.84.95
Last change on this file since 39e6e65a was 39e6e65a, checked in by Joel Sherrill <joel.sherrill@…>, on 08/19/98 at 21:32:28

Base files

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