source: rtems/cpukit/libnetworking/kern/kern_subr.c @ 0e16fa45

5
Last change on this file since 0e16fa45 was cb68253, checked in by Sebastian Huber <sebastian.huber@…>, on 09/07/18 at 04:19:02

network: Use kernel/user space header files

Add and use <machine/rtems-bsd-kernel-space.h> and
<machine/rtems-bsd-user-space.h> similar to the libbsd to avoid command
line defines and defines scattered throught the code base.

Simplify cpukit/libnetworking/Makefile.am.

Update #3375.

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