xref: /freebsd/usr.sbin/ppp/mbuf.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
1 /*
2  *	      PPP Memory handling module
3  *
4  *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5  *
6  *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the Internet Initiative Japan, Inc.  The name of the
14  * IIJ may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * $Id:$
21  *
22  *      TODO:
23  */
24 #include "defs.h"
25 
26 struct memmap {
27   struct mbuf *queue;
28   int    count;
29 } MemMap[MB_MAX+2];
30 
31 static int totalalloced;
32 
33 int
34 plength(bp)
35 struct mbuf *bp;
36 {
37   int len;
38 
39   for (len = 0; bp; bp = bp->next)
40     len += bp->cnt;
41   return(len);
42 }
43 
44 struct mbuf *
45 mballoc(cnt, type)
46 int cnt;
47 int type;
48 {
49   u_char *p;
50   struct mbuf *bp;
51 
52   if (type > MB_MAX)
53     logprintf("bad type %d\n", type);
54   bp = (struct mbuf *)malloc(sizeof(struct mbuf));
55   bzero(bp, sizeof(struct mbuf));
56   p = (u_char *)malloc(cnt);
57   MemMap[type].count += cnt;
58   totalalloced += cnt;
59   bp->base = p;
60   bp->size = bp->cnt = cnt;
61   bp->type = type;
62   return(bp);
63 }
64 
65 struct mbuf *
66 mbfree(struct mbuf *bp)
67 {
68   struct mbuf *nbp;
69 
70   if (bp) {
71     nbp = bp->next;
72     MemMap[bp->type].count -= bp->size;
73     totalalloced -= bp->size;
74     free(bp->base);
75     free(bp);
76     return(nbp);
77   }
78   return(bp);
79 }
80 
81 void
82 pfree(struct mbuf *bp)
83 {
84   while (bp)
85     bp = mbfree(bp);
86 }
87 
88 struct mbuf *
89 mbread(bp, ptr, len)
90 struct mbuf *bp;
91 u_char *ptr;
92 int len;
93 {
94   int nb;
95 
96   while (bp && len > 0) {
97     if (len > bp->cnt)
98       nb = bp->cnt;
99     else
100       nb = len;
101     bcopy(MBUF_CTOP(bp), ptr, nb);
102     ptr += nb;
103     bp->cnt -= nb;
104     len -= nb;
105     bp->offset += nb;
106     if (bp->cnt == 0) {
107 #ifdef notdef
108       bp = bp->next;
109 #else
110       bp = mbfree(bp);
111 #endif
112     }
113   }
114   return(bp);
115 }
116 
117 void
118 mbwrite(bp, ptr, cnt)
119 struct mbuf *bp;
120 u_char *ptr;
121 int cnt;
122 {
123   int plen;
124   int nb;
125 
126   plen = plength(bp);
127   if (plen < cnt)
128     cnt = plen;
129 
130   while (cnt > 0) {
131     nb = (cnt < bp->cnt)? cnt : bp->cnt;
132     bcopy(ptr, MBUF_CTOP(bp), nb);
133     cnt -= bp->cnt;
134     bp = bp->next;
135   }
136 }
137 
138 void
139 DumpBp(bp)
140 struct mbuf *bp;
141 {
142   u_char *cp;
143   int cnt, loc;
144 
145   logprintf("dump bp = %x (%d)\n", bp, plength(bp));
146   loc = 0;
147   while (bp) {
148     cp = MBUF_CTOP(bp);
149     cnt = bp->cnt;
150     while (cnt > 0) {
151       logprintf("%02x", *cp++);
152       loc++;
153       if (loc == 16) {
154 	loc = 0;
155 	logprintf("\n");
156       } else
157 	logprintf(" ");
158       cnt--;
159     }
160     bp = bp->next;
161   }
162   if (loc) logprintf("\n");
163 }
164 
165 int
166 ShowMemMap()
167 {
168   int i;
169 
170   for (i = 0; i <= MB_MAX; i += 2) {
171     printf("%d: %d   %d: %d\r\n",
172 	i, MemMap[i].count, i+1, MemMap[i+1].count);
173   }
174   return(1);
175 }
176 
177 void
178 LogMemory()
179 {
180 #ifdef DEBUG
181   logprintf("mem alloced: %d\n", totalalloced);
182   logprintf(" 1: %d  2: %d   3: %d   4: %d\n",
183 	MemMap[1].count, MemMap[2].count, MemMap[3].count, MemMap[4].count);
184   logprintf(" 5: %d  6: %d   7: %d   8: %d\n",
185 	MemMap[5].count, MemMap[6].count, MemMap[7].count, MemMap[8].count);
186 #endif
187 }
188