subr_hash.c revision 6324
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.3 1994/08/02 07:42:14 davidg 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/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 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc) 62 panic("uiomove proc"); 63#endif 64 while (n > 0 && uio->uio_resid) { 65 iov = uio->uio_iov; 66 cnt = iov->iov_len; 67 if (cnt == 0) { 68 uio->uio_iov++; 69 uio->uio_iovcnt--; 70 continue; 71 } 72 if (cnt > n) 73 cnt = n; 74 75 switch (uio->uio_segflg) { 76 77 case UIO_USERSPACE: 78 case UIO_USERISPACE: 79 if (uio->uio_rw == UIO_READ) 80 error = copyout(cp, iov->iov_base, cnt); 81 else 82 error = copyin(iov->iov_base, cp, cnt); 83 if (error) 84 return (error); 85 break; 86 87 case UIO_SYSSPACE: 88 if (uio->uio_rw == UIO_READ) 89 bcopy((caddr_t)cp, iov->iov_base, cnt); 90 else 91 bcopy(iov->iov_base, (caddr_t)cp, cnt); 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/* 105 * Give next character to user as result of read. 106 */ 107int 108ureadc(c, uio) 109 register int c; 110 register struct uio *uio; 111{ 112 register struct iovec *iov; 113 114again: 115 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0) 116 panic("ureadc"); 117 iov = uio->uio_iov; 118 if (iov->iov_len == 0) { 119 uio->uio_iovcnt--; 120 uio->uio_iov++; 121 goto again; 122 } 123 switch (uio->uio_segflg) { 124 125 case UIO_USERSPACE: 126 if (subyte(iov->iov_base, c) < 0) 127 return (EFAULT); 128 break; 129 130 case UIO_SYSSPACE: 131 *iov->iov_base = c; 132 break; 133 134 case UIO_USERISPACE: 135 if (suibyte(iov->iov_base, c) < 0) 136 return (EFAULT); 137 break; 138 } 139 iov->iov_base++; 140 iov->iov_len--; 141 uio->uio_resid--; 142 uio->uio_offset++; 143 return (0); 144} 145 146#ifdef vax /* unused except by ct.c, other oddities XXX */ 147/* 148 * Get next character written in by user from uio. 149 */ 150int 151uwritec(uio) 152 struct uio *uio; 153{ 154 register struct iovec *iov; 155 register int c; 156 157 if (uio->uio_resid <= 0) 158 return (-1); 159again: 160 if (uio->uio_iovcnt <= 0) 161 panic("uwritec"); 162 iov = uio->uio_iov; 163 if (iov->iov_len == 0) { 164 uio->uio_iov++; 165 if (--uio->uio_iovcnt == 0) 166 return (-1); 167 goto again; 168 } 169 switch (uio->uio_segflg) { 170 171 case UIO_USERSPACE: 172 c = fubyte(iov->iov_base); 173 break; 174 175 case UIO_SYSSPACE: 176 c = *(u_char *) iov->iov_base; 177 break; 178 179 case UIO_USERISPACE: 180 c = fuibyte(iov->iov_base); 181 break; 182 } 183 if (c < 0) 184 return (-1); 185 iov->iov_base++; 186 iov->iov_len--; 187 uio->uio_resid--; 188 uio->uio_offset++; 189 return (c); 190} 191#endif /* vax */ 192 193/* 194 * General routine to allocate a hash table. 195 */ 196void * 197hashinit(elements, type, hashmask) 198 int elements, type; 199 u_long *hashmask; 200{ 201 long hashsize; 202 LIST_HEAD(generic, generic) *hashtbl; 203 int i; 204 205 if (elements <= 0) 206 panic("hashinit: bad cnt"); 207 for (hashsize = 1; hashsize <= elements; hashsize <<= 1) 208 continue; 209 hashsize >>= 1; 210 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); 211 for (i = 0; i < hashsize; i++) 212 LIST_INIT(&hashtbl[i]); 213 *hashmask = hashsize - 1; 214 return (hashtbl); 215} 216