2 * $Id: atp_bufs.c,v 1.5 2009-10-13 22:55:37 didg Exp $
4 * Copyright (c) 1990,1991 Regents of The University of Michigan.
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.
17 * Research Systems Unix Group
18 * The University of Michigan
20 * 535 W. William Street
23 * netatalk@itd.umich.edu
27 * Our own memory maintenance for atp
32 #endif /* HAVE_CONFIG_H */
38 #include <sys/types.h>
40 #include <sys/socket.h>
42 #include <netatalk/at.h>
43 #include <atalk/atp.h>
44 #include "atp_internals.h"
46 #define N_MORE_BUFS 10
48 static struct atpbuf *free_list = NULL; /* free buffers */
51 static int numbufs = 0;
54 /* only call this when the free_list is empty...
55 * N_MORE_BUFS must be >= one
57 static int more_bufs(void)
63 /* get the whole chunk in one malloc call
65 if (( mem = malloc( N_MORE_BUFS * sizeof( struct atpbuf ))) == NULL ) {
69 /* now split into separate bufs
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 ));
76 bp->atpbuf_next = NULL;
83 void atp_print_bufuse(ATP ah, char *s)
86 int i, sentcount, incount, respcount;
89 for ( bp = ah->atph_sent; bp != NULL; bp = bp->atpbuf_next ) {
91 for ( i = 0; i < 8; ++i ) {
92 if ( bp->atpbuf_info.atpbuf_xo.atpxo_packet[ i ] != NULL ) {
98 if ( ah->atph_reqpkt != NULL ) {
104 for ( bp = ah->atph_queue; bp != NULL; bp = bp->atpbuf_next, ++incount );
107 for ( i = 0; i < 8; ++i ) {
108 if ( ah->atph_resppkt[ i ] != NULL ) {
113 printf( "<%d> %s: bufs total %d sent %d incoming %d req %d resp %d\n",
114 getpid(), s, numbufs, sentcount, incount,
115 ( ah->atph_reqpkt != NULL ) ? 1: 0, respcount );
120 struct atpbuf *atp_alloc_buf(void)
124 if ( free_list == NULL && more_bufs() ) return NULL;
127 free_list = free_list->atpbuf_next;
135 int atp_free_buf(struct atpbuf *bp)
140 bp->atpbuf_next = free_list;