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