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 */ 23 #include "defs.h" 24 25 struct memmap { 26 struct mbuf *queue; 27 int count; 28 } MemMap[MB_MAX+2]; 29 30 static int totalalloced; 31 32 int 33 plength(bp) 34 struct mbuf *bp; 35 { 36 int len; 37 38 for (len = 0; bp; bp = bp->next) 39 len += bp->cnt; 40 return(len); 41 } 42 43 struct mbuf * 44 mballoc(cnt, type) 45 int cnt; 46 int type; 47 { 48 u_char *p; 49 struct mbuf *bp; 50 51 if (type > MB_MAX) 52 logprintf("bad type %d\n", type); 53 bp = (struct mbuf *)malloc(sizeof(struct mbuf)); 54 bzero(bp, sizeof(struct mbuf)); 55 p = (u_char *)malloc(cnt); 56 MemMap[type].count += cnt; 57 totalalloced += cnt; 58 bp->base = p; 59 bp->size = bp->cnt = cnt; 60 bp->type = type; 61 return(bp); 62 } 63 64 struct mbuf * 65 mbfree(struct mbuf *bp) 66 { 67 struct mbuf *nbp; 68 69 if (bp) { 70 nbp = bp->next; 71 MemMap[bp->type].count -= bp->size; 72 totalalloced -= bp->size; 73 free(bp->base); 74 free(bp); 75 return(nbp); 76 } 77 return(bp); 78 } 79 80 void 81 pfree(struct mbuf *bp) 82 { 83 while (bp) 84 bp = mbfree(bp); 85 } 86 87 struct mbuf * 88 mbread(bp, ptr, len) 89 struct mbuf *bp; 90 u_char *ptr; 91 int len; 92 { 93 int nb; 94 95 while (bp && len > 0) { 96 if (len > bp->cnt) 97 nb = bp->cnt; 98 else 99 nb = len; 100 bcopy(MBUF_CTOP(bp), ptr, nb); 101 ptr += nb; 102 bp->cnt -= nb; 103 len -= nb; 104 bp->offset += nb; 105 if (bp->cnt == 0) { 106 #ifdef notdef 107 bp = bp->next; 108 #else 109 bp = mbfree(bp); 110 #endif 111 } 112 } 113 return(bp); 114 } 115 116 void 117 mbwrite(bp, ptr, cnt) 118 struct mbuf *bp; 119 u_char *ptr; 120 int cnt; 121 { 122 int plen; 123 int nb; 124 125 plen = plength(bp); 126 if (plen < cnt) 127 cnt = plen; 128 129 while (cnt > 0) { 130 nb = (cnt < bp->cnt)? cnt : bp->cnt; 131 bcopy(ptr, MBUF_CTOP(bp), nb); 132 cnt -= bp->cnt; 133 bp = bp->next; 134 } 135 } 136 137 void 138 DumpBp(bp) 139 struct mbuf *bp; 140 { 141 u_char *cp; 142 int cnt, loc; 143 144 logprintf("dump bp = %x (%d)\n", bp, plength(bp)); 145 loc = 0; 146 while (bp) { 147 cp = MBUF_CTOP(bp); 148 cnt = bp->cnt; 149 while (cnt > 0) { 150 logprintf("%02x", *cp++); 151 loc++; 152 if (loc == 16) { 153 loc = 0; 154 logprintf("\n"); 155 } else 156 logprintf(" "); 157 cnt--; 158 } 159 bp = bp->next; 160 } 161 if (loc) logprintf("\n"); 162 } 163 164 int 165 ShowMemMap() 166 { 167 int i; 168 169 for (i = 0; i <= MB_MAX; i += 2) { 170 printf("%d: %d %d: %d\r\n", 171 i, MemMap[i].count, i+1, MemMap[i+1].count); 172 } 173 return(1); 174 } 175 176 void 177 LogMemory() 178 { 179 #ifdef DEBUG 180 logprintf("mem alloced: %d\n", totalalloced); 181 logprintf(" 1: %d 2: %d 3: %d 4: %d\n", 182 MemMap[1].count, MemMap[2].count, MemMap[3].count, MemMap[4].count); 183 logprintf(" 5: %d 6: %d 7: %d 8: %d\n", 184 MemMap[5].count, MemMap[6].count, MemMap[7].count, MemMap[8].count); 185 #endif 186 } 187