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 $ 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(bp) 41 struct mbuf *bp; 42 { 43 int len; 44 45 for (len = 0; bp; bp = bp->next) 46 len += bp->cnt; 47 return(len); 48 } 49 50 struct mbuf * 51 mballoc(cnt, type) 52 int cnt; 53 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); 60 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)); 67 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; 78 return(bp); 79 } 80 81 struct mbuf * 82 mbfree(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); 92 return(nbp); 93 } 94 return(bp); 95 } 96 97 void 98 pfree(struct mbuf *bp) 99 { 100 while (bp) 101 bp = mbfree(bp); 102 } 103 104 struct mbuf * 105 mbread(bp, ptr, len) 106 struct mbuf *bp; 107 u_char *ptr; 108 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; 117 bcopy(MBUF_CTOP(bp), ptr, nb); 118 ptr += nb; 119 bp->cnt -= nb; 120 len -= nb; 121 bp->offset += nb; 122 if (bp->cnt == 0) { 123 #ifdef notdef 124 bp = bp->next; 125 #else 126 bp = mbfree(bp); 127 #endif 128 } 129 } 130 return(bp); 131 } 132 133 void 134 mbwrite(bp, ptr, cnt) 135 struct mbuf *bp; 136 u_char *ptr; 137 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) { 147 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 154 int 155 ShowMemMap() 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", 164 i, MemMap[i].count, i+1, MemMap[i+1].count); 165 166 return 0; 167 } 168 169 void 170 LogMemory() 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 } 178