]> arthur.barton.de Git - netatalk.git/blob - libatalk/atp/atp_bufs.c
Initial revision
[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 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #include <sys/types.h>
33 #include <sys/time.h>
34
35 #include <netatalk/at.h>
36 #include <atalk/atp.h>
37 #include "atp_internals.h"
38
39 #define                 N_MORE_BUFS             10
40
41 static struct atpbuf    *free_list = NULL;      /* free buffers */
42
43 #ifdef EBUG
44 static int              numbufs = 0;
45 #endif EBUG
46
47 /* only call this when the free_list is empty...
48  * N_MORE_BUFS must be >= one
49 */
50 static int more_bufs(void)
51 {
52     int                 i;
53     char                *mem;
54     struct atpbuf       *bp;
55
56     /* get the whole chunk in one malloc call
57     */
58     if (( mem = malloc( N_MORE_BUFS * sizeof( struct atpbuf ))) == NULL ) {
59         errno = ENOBUFS;
60         return -1;
61     }
62     /* now split into separate bufs
63     */
64     bp = free_list = (struct atpbuf *) mem;
65     for ( i = 1; i < N_MORE_BUFS; ++i ) {
66         bp->atpbuf_next = (struct atpbuf *) ( mem += sizeof( struct atpbuf ));
67         bp = bp->atpbuf_next;
68     }
69     bp->atpbuf_next = NULL;
70
71     return 0;
72 }
73
74
75 #ifdef EBUG
76 void atp_print_bufuse( ah, s )
77     ATP         ah;
78     char        *s;
79 {
80     struct atpbuf       *bp;
81     int                 i, sentcount, incount, respcount;
82
83     sentcount = 0;
84     for ( bp = ah->atph_sent; bp != NULL; bp = bp->atpbuf_next ) {
85         ++sentcount;
86         for ( i = 0; i < 8; ++i ) {
87             if ( bp->atpbuf_info.atpbuf_xo.atpxo_packet[ i ] != NULL ) {
88                 ++sentcount;
89             }
90         }
91     }
92
93     if ( ah->atph_reqpkt != NULL ) {
94         ++sentcount;
95     }
96
97
98     incount = 0;
99     for ( bp = ah->atph_queue; bp != NULL; bp = bp->atpbuf_next, ++incount );
100
101     respcount = 0;
102     for ( i = 0; i < 8; ++i ) {
103         if ( ah->atph_resppkt[ i ] != NULL ) {
104             ++respcount;
105         }
106     }
107
108     printf( "<%d> %s: bufs total %d  sent %d  incoming %d  req %d  resp %d\n",
109         getpid(), s, numbufs, sentcount, incount,
110         ( ah->atph_reqpkt != NULL ) ? 1: 0, respcount );
111 }
112 #endif EBUG
113
114
115 struct atpbuf *atp_alloc_buf(void)
116 {
117     struct atpbuf *bp;
118
119     if ( free_list == NULL && more_bufs() ) return NULL;
120
121     bp = free_list;
122     free_list = free_list->atpbuf_next;
123 #ifdef EBUG
124     ++numbufs;
125 #endif EBUG
126     return bp;
127 }
128
129
130 int atp_free_buf( bp )
131     struct atpbuf       *bp;
132 {
133     if ( bp == NULL ) {
134         return -1;
135     }
136     bp->atpbuf_next = free_list;
137     free_list = bp;
138 #ifdef EBUG
139     --numbufs;
140 #endif EBUG
141     return 0;
142 }
143
144