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