]> arthur.barton.de Git - netatalk.git/blob - libatalk/atp/atp_bufs.c
massive commenting/autoconf changes
[netatalk.git] / libatalk / atp / atp_bufs.c
1 /*
2  * $Id: atp_bufs.c,v 1.4 2001-06-29 14:14:46 rufustfirefly Exp $
3  *
4  * Copyright (c) 1990,1991 Regents of The University of Michigan.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and
8  * its documentation for any purpose and without fee is hereby granted,
9  * provided that the above copyright notice appears in all copies and
10  * that both that copyright notice and this permission notice appear
11  * in supporting documentation, and that the name of The University
12  * of Michigan not be used in advertising or publicity pertaining to
13  * distribution of the software without specific, written prior
14  * permission. This software is supplied as is without expressed or
15  * implied warranties of any kind.
16  *
17  *      Research Systems Unix Group
18  *      The University of Michigan
19  *      c/o Mike Clark
20  *      535 W. William Street
21  *      Ann Arbor, Michigan
22  *      +1-313-763-0525
23  *      netatalk@itd.umich.edu
24  */
25
26 /* 
27  * Our own memory maintenance for atp
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif /* HAVE_CONFIG_H */
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/socket.h>
41
42 #include <netatalk/at.h>
43 #include <atalk/atp.h>
44 #include "atp_internals.h"
45
46 #define                 N_MORE_BUFS             10
47
48 static struct atpbuf    *free_list = NULL;      /* free buffers */
49
50 #ifdef EBUG
51 static int              numbufs = 0;
52 #endif /* EBUG */
53
54 /* only call this when the free_list is empty...
55  * N_MORE_BUFS must be >= one
56 */
57 static int more_bufs(void)
58 {
59     int                 i;
60     char                *mem;
61     struct atpbuf       *bp;
62
63     /* get the whole chunk in one malloc call
64     */
65     if (( mem = malloc( N_MORE_BUFS * sizeof( struct atpbuf ))) == NULL ) {
66         errno = ENOBUFS;
67         return -1;
68     }
69     /* now split into separate bufs
70     */
71     bp = free_list = (struct atpbuf *) mem;
72     for ( i = 1; i < N_MORE_BUFS; ++i ) {
73         bp->atpbuf_next = (struct atpbuf *) ( mem += sizeof( struct atpbuf ));
74         bp = bp->atpbuf_next;
75     }
76     bp->atpbuf_next = NULL;
77
78     return 0;
79 }
80
81
82 #ifdef EBUG
83 void atp_print_bufuse( ah, s )
84     ATP         ah;
85     char        *s;
86 {
87     struct atpbuf       *bp;
88     int                 i, sentcount, incount, respcount;
89
90     sentcount = 0;
91     for ( bp = ah->atph_sent; bp != NULL; bp = bp->atpbuf_next ) {
92         ++sentcount;
93         for ( i = 0; i < 8; ++i ) {
94             if ( bp->atpbuf_info.atpbuf_xo.atpxo_packet[ i ] != NULL ) {
95                 ++sentcount;
96             }
97         }
98     }
99
100     if ( ah->atph_reqpkt != NULL ) {
101         ++sentcount;
102     }
103
104
105     incount = 0;
106     for ( bp = ah->atph_queue; bp != NULL; bp = bp->atpbuf_next, ++incount );
107
108     respcount = 0;
109     for ( i = 0; i < 8; ++i ) {
110         if ( ah->atph_resppkt[ i ] != NULL ) {
111             ++respcount;
112         }
113     }
114
115     printf( "<%d> %s: bufs total %d  sent %d  incoming %d  req %d  resp %d\n",
116         getpid(), s, numbufs, sentcount, incount,
117         ( ah->atph_reqpkt != NULL ) ? 1: 0, respcount );
118 }
119 #endif /* EBUG */
120
121
122 struct atpbuf *atp_alloc_buf(void)
123 {
124     struct atpbuf *bp;
125
126     if ( free_list == NULL && more_bufs() ) return NULL;
127
128     bp = free_list;
129     free_list = free_list->atpbuf_next;
130 #ifdef EBUG
131     ++numbufs;
132 #endif /* EBUG */
133     return bp;
134 }
135
136
137 int atp_free_buf( bp )
138     struct atpbuf       *bp;
139 {
140     if ( bp == NULL ) {
141         return -1;
142     }
143     bp->atpbuf_next = free_list;
144     free_list = bp;
145 #ifdef EBUG
146     --numbufs;
147 #endif /* EBUG */
148     return 0;
149 }
150
151