mbuf.c revision 31343
1213498Scognet/* 2213498Scognet * PPP Memory handling module 3213498Scognet * 4213498Scognet * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5213498Scognet * 6213498Scognet * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 7213498Scognet * 8213498Scognet * Redistribution and use in source and binary forms are permitted 9213498Scognet * provided that the above copyright notice and this paragraph are 10213498Scognet * duplicated in all such forms and that any documentation, 11213498Scognet * advertising materials, and other materials related to such 12213498Scognet * distribution and use acknowledge that the software was developed 13213498Scognet * by the Internet Initiative Japan, Inc. The name of the 14213498Scognet * IIJ may not be used to endorse or promote products derived 15213498Scognet * from this software without specific prior written permission. 16213498Scognet * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17213498Scognet * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18213498Scognet * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19213498Scognet * 20213498Scognet * $Id: mbuf.c,v 1.10 1997/10/26 01:03:16 brian Exp $ 21213498Scognet * 22213498Scognet */ 23213498Scognet#include <sys/param.h> 24213498Scognet#include <sys/socket.h> 25213498Scognet#include <netinet/in.h> 26213498Scognet 27236989Simp#include <stdio.h> 28213498Scognet#include <stdlib.h> 29213498Scognet#include <string.h> 30213498Scognet 31213498Scognet#include "command.h" 32213498Scognet#include "mbuf.h" 33213498Scognet#include "log.h" 34213498Scognet#include "defs.h" 35238189Simp#include "loadalias.h" 36213498Scognet#include "vars.h" 37213498Scognet#include "server.h" 38213498Scognet 39213498Scognetstruct memmap { 40213498Scognet struct mbuf *queue; 41213498Scognet int count; 42213498Scognet} MemMap[MB_MAX + 2]; 43213498Scognet 44213498Scognetstatic int totalalloced; 45213498Scognet 46213498Scognetint 47213498Scognetplength(struct mbuf * bp) 48238189Simp{ 49213498Scognet int len; 50213498Scognet 51213498Scognet for (len = 0; bp; bp = bp->next) 52213498Scognet len += bp->cnt; 53213498Scognet return (len); 54213498Scognet} 55213498Scognet 56213498Scognetstruct mbuf * 57213498Scognetmballoc(int cnt, int type) 58213498Scognet{ 59213498Scognet u_char *p; 60213498Scognet struct mbuf *bp; 61213498Scognet 62213498Scognet if (type > MB_MAX) 63213498Scognet LogPrintf(LogERROR, "Bad mbuf type %d\n", type); 64213498Scognet bp = (struct mbuf *) malloc(sizeof(struct mbuf)); 65213498Scognet if (bp == NULL) { 66213498Scognet LogPrintf(LogALERT, "failed to allocate memory: %u\n", sizeof(struct mbuf)); 67213498Scognet ServerClose(); 68213498Scognet exit(1); 69213498Scognet } 70213498Scognet memset(bp, '\0', sizeof(struct mbuf)); 71213498Scognet p = (u_char *) malloc(cnt); 72213498Scognet if (p == NULL) { 73213498Scognet LogPrintf(LogALERT, "failed to allocate memory: %d\n", cnt); 74213498Scognet ServerClose(); 75213498Scognet exit(1); 76213498Scognet } 77213498Scognet MemMap[type].count += cnt; 78213498Scognet totalalloced += cnt; 79213498Scognet bp->base = p; 80213498Scognet bp->size = bp->cnt = cnt; 81213498Scognet bp->type = type; 82213498Scognet return (bp); 83213498Scognet} 84213498Scognet 85213498Scognetstruct mbuf * 86213498Scognetmbfree(struct mbuf * bp) 87213498Scognet{ 88213498Scognet struct mbuf *nbp; 89213498Scognet 90213498Scognet if (bp) { 91213498Scognet nbp = bp->next; 92213498Scognet MemMap[bp->type].count -= bp->size; 93213498Scognet totalalloced -= bp->size; 94213498Scognet free(bp->base); 95213498Scognet free(bp); 96213498Scognet return (nbp); 97213498Scognet } 98213498Scognet return (bp); 99213498Scognet} 100213498Scognet 101213498Scognetvoid 102213498Scognetpfree(struct mbuf * bp) 103213498Scognet{ 104213498Scognet while (bp) 105213498Scognet bp = mbfree(bp); 106213498Scognet} 107238189Simp 108238189Simpstruct mbuf * 109mbread(struct mbuf * bp, u_char * ptr, int len) 110{ 111 int nb; 112 113 while (bp && len > 0) { 114 if (len > bp->cnt) 115 nb = bp->cnt; 116 else 117 nb = len; 118 memcpy(ptr, MBUF_CTOP(bp), nb); 119 ptr += nb; 120 bp->cnt -= nb; 121 len -= nb; 122 bp->offset += nb; 123 if (bp->cnt == 0) { 124#ifdef notdef 125 bp = bp->next; 126#else 127 bp = mbfree(bp); 128#endif 129 } 130 } 131 return (bp); 132} 133 134void 135mbwrite(struct mbuf * bp, u_char * ptr, int cnt) 136{ 137 int plen; 138 int nb; 139 140 plen = plength(bp); 141 if (plen < cnt) 142 cnt = plen; 143 144 while (cnt > 0) { 145 nb = (cnt < bp->cnt) ? cnt : bp->cnt; 146 memcpy(MBUF_CTOP(bp), ptr, nb); 147 cnt -= bp->cnt; 148 bp = bp->next; 149 } 150} 151 152int 153ShowMemMap(struct cmdargs const *arg) 154{ 155 int i; 156 157 if (!VarTerm) 158 return 1; 159 160 for (i = 0; i <= MB_MAX; i += 2) 161 fprintf(VarTerm, "%d: %d %d: %d\n", 162 i, MemMap[i].count, i + 1, MemMap[i + 1].count); 163 164 return 0; 165} 166 167void 168LogMemory() 169{ 170 LogPrintf(LogDEBUG, "LogMemory: mem alloced: %d\n", totalalloced); 171 LogPrintf(LogDEBUG, "LogMemory: 1: %d 2: %d 3: %d 4: %d\n", 172 MemMap[1].count, MemMap[2].count, MemMap[3].count, MemMap[4].count); 173 LogPrintf(LogDEBUG, "LogMemory: 5: %d 6: %d 7: %d 8: %d\n", 174 MemMap[5].count, MemMap[6].count, MemMap[7].count, MemMap[8].count); 175} 176