Annotation of driverkit/tests/netbuf.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:  * netbuf.m - netbuf stub for testing NetDriver objects.
                     26:  *
                     27:  * HISTORY
                     28:  * 24-Jul-91    Doug Mitchell at NeXT
                     29:  *      Created. 
                     30:  */
                     31: 
                     32: /*
                     33:  * Hmmm...we need KERNEL defined to get netif.h...
                     34:  */
                     35: #import <bsd/sys/socket.h>             
                     36: #define KERNEL 1
                     37: #import <net/netif.h>
                     38: #undef KERNEL
                     39: #import <driverkit/generalFuncs.h>
                     40: #import <libc.h>
                     41: 
                     42: /*
                     43:  * Our private mbuf equivalent.
                     44:  */
                     45: typedef struct {
                     46:        void            *start_data;            // starting DMA data at
                     47:                                                // nb_alloc time
                     48:        int             start_length;           // starting size at 
                     49:                                                // nb_alloc time
                     50:        void            *data;                  // current DMA data
                     51:        int             length;                 // amount of data in *data
                     52:        void            (*free_fcn)(void *);    // "free" function
                     53:        void            *free_arg;              // "free argument
                     54: } stub_netbuf_t;
                     55: 
                     56: /*
                     57:  * Private functions.
                     58:  */
                     59: static stub_netbuf_t *stub_netbuf_alloc();
                     60: static void stub_netbuf_free(stub_netbuf_t *snb);
                     61: 
                     62: 
                     63: char *nb_map(netbuf_t nb)
                     64: {
                     65:        stub_netbuf_t *snb = (stub_netbuf_t *)nb;
                     66: 
                     67:        return (char *)snb->data;
                     68: }
                     69: 
                     70: /*
                     71:  * Alloc some data as well as a stub_netbuf_t.
                     72:  */
                     73: netbuf_t nb_alloc(unsigned size)
                     74: {
                     75:        char *data;
                     76:        stub_netbuf_t *snb;
                     77: 
                     78:        data = IOMalloc(size);
                     79:        snb = stub_netbuf_alloc();
                     80:        snb->data = snb->start_data = data;
                     81:        snb->length = snb->start_length = size;
                     82:        snb->free_fcn = NULL;
                     83:        return (netbuf_t)snb;
                     84: }
                     85: 
                     86: netbuf_t nb_alloc_wrapper(void *data, 
                     87:        unsigned size,
                     88:        void freefunc(void *), void *freefunc_arg)
                     89: {
                     90:        stub_netbuf_t *snb;
                     91: 
                     92:        snb = stub_netbuf_alloc();
                     93:        snb->data = snb->start_data = data;
                     94:        snb->length = snb->start_length = size;
                     95:        snb->free_fcn = freefunc;
                     96:        snb->free_arg = freefunc_arg;
                     97:        return (netbuf_t)snb;
                     98: }
                     99: 
                    100: void nb_free(netbuf_t nb)
                    101: {
                    102:        stub_netbuf_t *snb = (stub_netbuf_t *)nb;
                    103:        
                    104:        /*
                    105:         * First free the data, whether we allocated it or the driver did.
                    106:         */
                    107:        if(snb->free_fcn) {
                    108:                (*snb->free_fcn)(snb->free_arg);
                    109:        }
                    110:        else {
                    111:                IOFree(snb->data, snb->length);
                    112:        }
                    113:        
                    114:        /*
                    115:         * now free the wrapper.
                    116:         */
                    117:        stub_netbuf_free(snb);
                    118: }
                    119: 
                    120: void nb_free_wrapper(netbuf_t nb)
                    121: {
                    122:        stub_netbuf_t *snb = (stub_netbuf_t *)nb;
                    123: 
                    124:        stub_netbuf_free(snb);
                    125: }
                    126: 
                    127: unsigned nb_size(netbuf_t nb)
                    128: {
                    129:        stub_netbuf_t *snb = (stub_netbuf_t *)nb;
                    130:        
                    131:        return (unsigned)snb->length;
                    132: }
                    133: 
                    134: int nb_shrink_top(netbuf_t nb, unsigned size)
                    135: {
                    136:        stub_netbuf_t *snb = (stub_netbuf_t *)nb;
                    137:        
                    138:        if((snb->length - (int)size) < 0) {
                    139:                IOLog("Illegal nb_shrink_top\n");
                    140:                return -1;
                    141:        }
                    142:        snb->length -= size;
                    143:        snb->data   += size;
                    144:        return 0;
                    145: }
                    146: 
                    147: int
                    148: nb_grow_top(netbuf_t nb, unsigned size)
                    149: {
                    150:        stub_netbuf_t *snb = (stub_netbuf_t *)nb;
                    151:        
                    152:        if(((char *)snb->data - size) < (char *)snb->start_data) {
                    153:                IOLog("Illegal nb_grow_top (buf underflow)\n");
                    154:                return -1;
                    155:        }
                    156:        if((snb->length + size) > snb->start_length) {
                    157:                IOLog("Illegal nb_grow_top (buf overflow)\n");
                    158:                return -1;
                    159:        }
                    160:        snb->length += size;
                    161:        snb->data   -= size;
                    162:        return 0;
                    163: }
                    164: 
                    165: int
                    166: nb_shrink_bot(netbuf_t nb, unsigned size)
                    167: {
                    168:        stub_netbuf_t *snb = (stub_netbuf_t *)nb;
                    169:        
                    170:        if((snb->length - (int)size) < 0) {
                    171:                IOLog("Illegal nb_shrink_bot\n");
                    172:                return -1;
                    173:        }
                    174:        snb->length -= size;
                    175:        return 0;
                    176: }
                    177: 
                    178: 
                    179: int
                    180: nb_grow_bot(netbuf_t nb, unsigned size)
                    181: {
                    182:        stub_netbuf_t *snb = (stub_netbuf_t *)nb;
                    183:        
                    184:        if((snb->length + size) > snb->start_length) {
                    185:                IOLog("Illegal nb_grow_bot (buf overflow)\n");
                    186:                return -1;
                    187:        }
                    188:        snb->length += size;
                    189:        return 0;
                    190: }
                    191: 
                    192: /*
                    193:  * Private functions.
                    194:  */
                    195: static stub_netbuf_t *stub_netbuf_alloc()
                    196: {
                    197:        stub_netbuf_t *snb;
                    198:        
                    199:        snb = (stub_netbuf_t *)IOMalloc(sizeof(*snb));
                    200:        bzero(snb, sizeof(*snb));
                    201:        return snb;
                    202: }
                    203: 
                    204: static void stub_netbuf_free(stub_netbuf_t *snb)
                    205: {
                    206:        IOFree(snb, sizeof(*snb));
                    207: }

unix.superglobalmegacorp.com

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