Annotation of kernel/bsd/kern/kern_subr.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
                      3:  *
                      4:  * @APPLE_LICENSE_HEADER_START@
                      5:  * 
                      6:  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
                      7:  * Reserved.  This file contains Original Code and/or Modifications of
                      8:  * Original Code as defined in and that are subject to the Apple Public
                      9:  * Source License Version 1.1 (the "License").  You may not use this file
                     10:  * except in compliance with the License.  Please obtain a copy of the
                     11:  * License at http://www.apple.com/publicsource and read it before using
                     12:  * this file.
                     13:  * 
                     14:  * The Original Code and all software distributed under the License are
                     15:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
                     16:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
                     17:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
                     18:  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
                     19:  * License for the specific language governing rights and limitations
                     20:  * under the License.
                     21:  * 
                     22:  * @APPLE_LICENSE_HEADER_END@
                     23:  */
                     24: 
                     25: /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
                     26: /*
                     27:  * Copyright (c) 1982, 1986, 1991, 1993
                     28:  *     The Regents of the University of California.  All rights reserved.
                     29:  * (c) UNIX System Laboratories, Inc.
                     30:  * All or some portions of this file are derived from material licensed
                     31:  * to the University of California by American Telephone and Telegraph
                     32:  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
                     33:  * the permission of UNIX System Laboratories, Inc.
                     34:  *
                     35:  * Redistribution and use in source and binary forms, with or without
                     36:  * modification, are permitted provided that the following conditions
                     37:  * are met:
                     38:  * 1. Redistributions of source code must retain the above copyright
                     39:  *    notice, this list of conditions and the following disclaimer.
                     40:  * 2. Redistributions in binary form must reproduce the above copyright
                     41:  *    notice, this list of conditions and the following disclaimer in the
                     42:  *    documentation and/or other materials provided with the distribution.
                     43:  * 3. All advertising materials mentioning features or use of this software
                     44:  *    must display the following acknowledgement:
                     45:  *     This product includes software developed by the University of
                     46:  *     California, Berkeley and its contributors.
                     47:  * 4. Neither the name of the University nor the names of its contributors
                     48:  *    may be used to endorse or promote products derived from this software
                     49:  *    without specific prior written permission.
                     50:  *
                     51:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     52:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     53:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     54:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     55:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     56:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     57:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     58:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     59:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     60:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     61:  * SUCH DAMAGE.
                     62:  *
                     63:  *     @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
                     64:  */
                     65: 
                     66: #include <sys/param.h>
                     67: #include <sys/systm.h>
                     68: #include <sys/proc.h>
                     69: #include <sys/malloc.h>
                     70: #include <sys/queue.h>
                     71: 
                     72: int
                     73: uiomove(cp, n, uio)
                     74:        register caddr_t cp;
                     75:        register int n;
                     76:        register struct uio *uio;
                     77: {
                     78:        register struct iovec *iov;
                     79:        u_int cnt;
                     80:        int error = 0;
                     81: 
                     82: #if DIAGNOSTIC
                     83:        if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
                     84:                panic("uiomove: mode");
                     85:        if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != current_proc())
                     86:                panic("uiomove proc");
                     87: #endif
                     88: 
                     89:        while (n > 0 && uio->uio_resid) {
                     90:                iov = uio->uio_iov;
                     91:                cnt = iov->iov_len;
                     92:                if (cnt == 0) {
                     93:                        uio->uio_iov++;
                     94:                        uio->uio_iovcnt--;
                     95:                        continue;
                     96:                }
                     97:                if (cnt > n)
                     98:                        cnt = n;
                     99:                switch (uio->uio_segflg) {
                    100: 
                    101:                case UIO_USERSPACE:
                    102:                case UIO_USERISPACE:
                    103:                        if (uio->uio_rw == UIO_READ)
                    104:                                error = copyout(cp, iov->iov_base, cnt);
                    105:                        else
                    106:                                error = copyin(iov->iov_base, cp, cnt);
                    107:                        if (error)
                    108:                                return (error);
                    109:                        break;
                    110: 
                    111:                case UIO_SYSSPACE:
                    112:                        if (uio->uio_rw == UIO_READ)
                    113:                                error = copywithin((caddr_t)cp, iov->iov_base,
                    114:                                                   cnt);
                    115:                        else
                    116:                                error = copywithin(iov->iov_base, (caddr_t)cp,
                    117:                                                   cnt);
                    118:                        break;
                    119:                }
                    120:                iov->iov_base += cnt;
                    121:                iov->iov_len -= cnt;
                    122:                uio->uio_resid -= cnt;
                    123:                uio->uio_offset += cnt;
                    124:                cp += cnt;
                    125:                n -= cnt;
                    126:        }
                    127:        return (error);
                    128: }
                    129: 
                    130: /*
                    131:  * Give next character to user as result of read.
                    132:  */
                    133: int
                    134: ureadc(c, uio)
                    135:        register int c;
                    136:        register struct uio *uio;
                    137: {
                    138:        register struct iovec *iov;
                    139: 
                    140:        if (uio->uio_resid <= 0)
                    141:                panic("ureadc: non-positive resid");
                    142: again:
                    143:        if (uio->uio_iovcnt == 0)
                    144:                panic("ureadc: non-positive iovcnt");
                    145:        iov = uio->uio_iov;
                    146:        if (iov->iov_len <= 0) {
                    147:                uio->uio_iovcnt--;
                    148:                uio->uio_iov++;
                    149:                goto again;
                    150:        }
                    151:        switch (uio->uio_segflg) {
                    152: 
                    153:        case UIO_USERSPACE:
                    154:                if (subyte(iov->iov_base, c) < 0)
                    155:                        return (EFAULT);
                    156:                break;
                    157: 
                    158:        case UIO_SYSSPACE:
                    159:                *iov->iov_base = c;
                    160:                break;
                    161: 
                    162:        case UIO_USERISPACE:
                    163:                if (suibyte(iov->iov_base, c) < 0)
                    164:                        return (EFAULT);
                    165:                break;
                    166:        }
                    167:        iov->iov_base++;
                    168:        iov->iov_len--;
                    169:        uio->uio_resid--;
                    170:        uio->uio_offset++;
                    171:        return (0);
                    172: }
                    173: 
                    174: #if defined(vax) || defined(ppc)
                    175: /* unused except by ct.c, other oddities XXX */
                    176: /*
                    177:  * Get next character written in by user from uio.
                    178:  */
                    179: uwritec(uio)
                    180:        struct uio *uio;
                    181: {
                    182:        register struct iovec *iov;
                    183:        register int c;
                    184: 
                    185:        if (uio->uio_resid <= 0)
                    186:                return (-1);
                    187: again:
                    188:        if (uio->uio_iovcnt <= 0)
                    189:                panic("uwritec: non-positive iovcnt");
                    190:        iov = uio->uio_iov;
                    191:        if (iov->iov_len == 0) {
                    192:                uio->uio_iov++;
                    193:                if (--uio->uio_iovcnt == 0)
                    194:                        return (-1);
                    195:                goto again;
                    196:        }
                    197:        switch (uio->uio_segflg) {
                    198: 
                    199:        case UIO_USERSPACE:
                    200:                c = fubyte(iov->iov_base);
                    201:                break;
                    202: 
                    203:        case UIO_SYSSPACE:
                    204:                c = *iov->iov_base & 0377;
                    205:                break;
                    206: 
                    207:        case UIO_USERISPACE:
                    208:                c = fuibyte(iov->iov_base);
                    209:                break;
                    210: 
                    211:        default:
                    212:                c = 0;  /* avoid uninitialized variable warning */
                    213:                panic("uwritec: bogus uio_segflg");
                    214:                break;
                    215:        }
                    216:        if (c < 0)
                    217:                return (-1);
                    218:        iov->iov_base++;
                    219:        iov->iov_len--;
                    220:        uio->uio_resid--;
                    221:        uio->uio_offset++;
                    222:        return (c);
                    223: }
                    224: #endif /* vax || ppc */
                    225: 
                    226: /*
                    227:  * General routine to allocate a hash table.
                    228:  */
                    229: void *
                    230: hashinit(elements, type, hashmask)
                    231:        int elements, type;
                    232:        u_long *hashmask;
                    233: {
                    234:        long hashsize;
                    235:        LIST_HEAD(generic, generic) *hashtbl;
                    236:        int i;
                    237: 
                    238:        if (elements <= 0)
                    239:                panic("hashinit: bad cnt");
                    240:        for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
                    241:                continue;
                    242:        hashsize >>= 1;
                    243:        MALLOC(hashtbl, struct generic *, 
                    244:                        (u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
                    245:        bzero(hashtbl, (u_long)hashsize * sizeof(*hashtbl));
                    246:        for (i = 0; i < hashsize; i++)
                    247:                LIST_INIT(&hashtbl[i]);
                    248:        *hashmask = hashsize - 1;
                    249:        return (hashtbl);
                    250: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.