Deleted Added
full compact
mbuf.c (26940) mbuf.c (28679)
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 *
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.7 1997/06/09 03:27:29 brian Exp $
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
32struct memmap {
33 struct mbuf *queue;
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
32struct memmap {
33 struct mbuf *queue;
34 int count;
35} MemMap[MB_MAX+2];
34 int count;
35} MemMap[MB_MAX + 2];
36
37static int totalalloced;
38
39int
36
37static int totalalloced;
38
39int
40plength(bp)
41struct mbuf *bp;
40plength(struct mbuf * bp)
42{
43 int len;
44
45 for (len = 0; bp; bp = bp->next)
46 len += bp->cnt;
41{
42 int len;
43
44 for (len = 0; bp; bp = bp->next)
45 len += bp->cnt;
47 return(len);
46 return (len);
48}
49
50struct mbuf *
47}
48
49struct mbuf *
51mballoc(cnt, type)
52int cnt;
53int type;
50mballoc(int cnt, int type)
54{
55 u_char *p;
56 struct mbuf *bp;
57
58 if (type > MB_MAX)
59 LogPrintf(LogERROR, "Bad mbuf type %d\n", 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);
60 bp = (struct mbuf *)malloc(sizeof(struct mbuf));
57 bp = (struct mbuf *) malloc(sizeof(struct mbuf));
61 if (bp == NULL) {
62 LogPrintf(LogALERT, "failed to allocate memory: %u\n", sizeof(struct mbuf));
63 ServerClose();
64 exit(1);
65 }
66 bzero(bp, 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));
67 p = (u_char *)malloc(cnt);
64 p = (u_char *) malloc(cnt);
68 if (p == NULL) {
69 LogPrintf(LogALERT, "failed to allocate memory: %d\n", cnt);
70 ServerClose();
71 exit(1);
72 }
73 MemMap[type].count += cnt;
74 totalalloced += cnt;
75 bp->base = p;
76 bp->size = bp->cnt = cnt;
77 bp->type = type;
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;
78 return(bp);
75 return (bp);
79}
80
81struct mbuf *
76}
77
78struct mbuf *
82mbfree(struct mbuf *bp)
79mbfree(struct mbuf * bp)
83{
84 struct mbuf *nbp;
85
86 if (bp) {
87 nbp = bp->next;
88 MemMap[bp->type].count -= bp->size;
89 totalalloced -= bp->size;
90 free(bp->base);
91 free(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);
92 return(nbp);
89 return (nbp);
93 }
90 }
94 return(bp);
91 return (bp);
95}
96
97void
92}
93
94void
98pfree(struct mbuf *bp)
95pfree(struct mbuf * bp)
99{
100 while (bp)
101 bp = mbfree(bp);
102}
103
104struct mbuf *
96{
97 while (bp)
98 bp = mbfree(bp);
99}
100
101struct mbuf *
105mbread(bp, ptr, len)
106struct mbuf *bp;
107u_char *ptr;
108int len;
102mbread(struct mbuf * bp, u_char * ptr, int len)
109{
110 int nb;
111
112 while (bp && len > 0) {
113 if (len > bp->cnt)
114 nb = bp->cnt;
115 else
116 nb = len;

--- 5 unchanged lines hidden (view full) ---

122 if (bp->cnt == 0) {
123#ifdef notdef
124 bp = bp->next;
125#else
126 bp = mbfree(bp);
127#endif
128 }
129 }
103{
104 int nb;
105
106 while (bp && len > 0) {
107 if (len > bp->cnt)
108 nb = bp->cnt;
109 else
110 nb = len;

--- 5 unchanged lines hidden (view full) ---

116 if (bp->cnt == 0) {
117#ifdef notdef
118 bp = bp->next;
119#else
120 bp = mbfree(bp);
121#endif
122 }
123 }
130 return(bp);
124 return (bp);
131}
132
133void
125}
126
127void
134mbwrite(bp, ptr, cnt)
135struct mbuf *bp;
136u_char *ptr;
137int cnt;
128mbwrite(struct mbuf * bp, u_char * ptr, int cnt)
138{
139 int plen;
140 int nb;
141
142 plen = plength(bp);
143 if (plen < cnt)
144 cnt = plen;
145
146 while (cnt > 0) {
129{
130 int plen;
131 int nb;
132
133 plen = plength(bp);
134 if (plen < cnt)
135 cnt = plen;
136
137 while (cnt > 0) {
147 nb = (cnt < bp->cnt)? cnt : bp->cnt;
138 nb = (cnt < bp->cnt) ? cnt : bp->cnt;
148 bcopy(ptr, MBUF_CTOP(bp), nb);
149 cnt -= bp->cnt;
150 bp = bp->next;
151 }
152}
153
154int
155ShowMemMap()
156{
157 int i;
158
159 if (!VarTerm)
160 return 1;
161
162 for (i = 0; i <= MB_MAX; i += 2)
163 fprintf(VarTerm, "%d: %d %d: %d\n",
139 bcopy(ptr, MBUF_CTOP(bp), nb);
140 cnt -= bp->cnt;
141 bp = bp->next;
142 }
143}
144
145int
146ShowMemMap()
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",
164 i, MemMap[i].count, i+1, MemMap[i+1].count);
155 i, MemMap[i].count, i + 1, MemMap[i + 1].count);
165
166 return 0;
167}
168
169void
170LogMemory()
171{
172 LogPrintf(LogDEBUG, "LogMemory: mem alloced: %d\n", totalalloced);
173 LogPrintf(LogDEBUG, "LogMemory: 1: %d 2: %d 3: %d 4: %d\n",
174 MemMap[1].count, MemMap[2].count, MemMap[3].count, MemMap[4].count);
175 LogPrintf(LogDEBUG, "LogMemory: 5: %d 6: %d 7: %d 8: %d\n",
176 MemMap[5].count, MemMap[6].count, MemMap[7].count, MemMap[8].count);
177}
156
157 return 0;
158}
159
160void
161LogMemory()
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}