xref: /freebsd/usr.sbin/ppp/mbuf.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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: mbuf.c,v 1.8 1997/06/25 19:30:02 brian Exp $
21  *
22  */
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/param.h>
26 #include <netinet/in.h>
27 #include "defs.h"
28 #include "loadalias.h"
29 #include "vars.h"
30 #include "server.h"
31 
32 struct memmap {
33   struct mbuf *queue;
34   int count;
35 }      MemMap[MB_MAX + 2];
36 
37 static int totalalloced;
38 
39 int
40 plength(struct mbuf * bp)
41 {
42   int len;
43 
44   for (len = 0; bp; bp = bp->next)
45     len += bp->cnt;
46   return (len);
47 }
48 
49 struct mbuf *
50 mballoc(int cnt, int type)
51 {
52   u_char *p;
53   struct mbuf *bp;
54 
55   if (type > MB_MAX)
56     LogPrintf(LogERROR, "Bad mbuf type %d\n", type);
57   bp = (struct mbuf *) malloc(sizeof(struct mbuf));
58   if (bp == NULL) {
59     LogPrintf(LogALERT, "failed to allocate memory: %u\n", sizeof(struct mbuf));
60     ServerClose();
61     exit(1);
62   }
63   bzero(bp, sizeof(struct mbuf));
64   p = (u_char *) malloc(cnt);
65   if (p == NULL) {
66     LogPrintf(LogALERT, "failed to allocate memory: %d\n", cnt);
67     ServerClose();
68     exit(1);
69   }
70   MemMap[type].count += cnt;
71   totalalloced += cnt;
72   bp->base = p;
73   bp->size = bp->cnt = cnt;
74   bp->type = type;
75   return (bp);
76 }
77 
78 struct mbuf *
79 mbfree(struct mbuf * bp)
80 {
81   struct mbuf *nbp;
82 
83   if (bp) {
84     nbp = bp->next;
85     MemMap[bp->type].count -= bp->size;
86     totalalloced -= bp->size;
87     free(bp->base);
88     free(bp);
89     return (nbp);
90   }
91   return (bp);
92 }
93 
94 void
95 pfree(struct mbuf * bp)
96 {
97   while (bp)
98     bp = mbfree(bp);
99 }
100 
101 struct mbuf *
102 mbread(struct mbuf * bp, u_char * ptr, int len)
103 {
104   int nb;
105 
106   while (bp && len > 0) {
107     if (len > bp->cnt)
108       nb = bp->cnt;
109     else
110       nb = len;
111     bcopy(MBUF_CTOP(bp), ptr, nb);
112     ptr += nb;
113     bp->cnt -= nb;
114     len -= nb;
115     bp->offset += nb;
116     if (bp->cnt == 0) {
117 #ifdef notdef
118       bp = bp->next;
119 #else
120       bp = mbfree(bp);
121 #endif
122     }
123   }
124   return (bp);
125 }
126 
127 void
128 mbwrite(struct mbuf * bp, u_char * ptr, int cnt)
129 {
130   int plen;
131   int nb;
132 
133   plen = plength(bp);
134   if (plen < cnt)
135     cnt = plen;
136 
137   while (cnt > 0) {
138     nb = (cnt < bp->cnt) ? cnt : bp->cnt;
139     bcopy(ptr, MBUF_CTOP(bp), nb);
140     cnt -= bp->cnt;
141     bp = bp->next;
142   }
143 }
144 
145 int
146 ShowMemMap()
147 {
148   int i;
149 
150   if (!VarTerm)
151     return 1;
152 
153   for (i = 0; i <= MB_MAX; i += 2)
154     fprintf(VarTerm, "%d: %d   %d: %d\n",
155 	    i, MemMap[i].count, i + 1, MemMap[i + 1].count);
156 
157   return 0;
158 }
159 
160 void
161 LogMemory()
162 {
163   LogPrintf(LogDEBUG, "LogMemory: mem alloced: %d\n", totalalloced);
164   LogPrintf(LogDEBUG, "LogMemory:  1: %d  2: %d   3: %d   4: %d\n",
165 	MemMap[1].count, MemMap[2].count, MemMap[3].count, MemMap[4].count);
166   LogPrintf(LogDEBUG, "LogMemory:  5: %d  6: %d   7: %d   8: %d\n",
167 	MemMap[5].count, MemMap[6].count, MemMap[7].count, MemMap[8].count);
168 }
169