亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? mbuf.c

?? 這是新華龍(www.xhl.xom.xn)開發的
?? C
字號:
/* mbuf (message buffer) primitives
 */
#include <stdio.h>
#include <dos.h>	/* TEMP */
#include "global.h"
#include "mbuf.h"
#include "proc.h"

static int32 Pushdowns;		/* Total calls to pushdown() */
static int32 Pushalloc;		/* Calls to pushalloc() that call malloc */
static int32 Allocmbufs;	/* Calls to alloc_mbuf() */
static int32 Freembufs;		/* Calls to free_mbuf() that actually free */
static int32 Cachehits;		/* Hits on free mbuf cache */
static unsigned long Msizes[16];

#define	SMALL_MBUF	32
#define	MED_MBUF	128
#define	LARGE_MBUF	2048

static struct mbuf *Mbufcache[3];

/* Allocate mbuf with associated buffer of 'size' bytes */
struct mbuf *
alloc_mbuf(uint16 size)
{
	struct mbuf *bp = NULL;
	int i;
	int i_state;

	Allocmbufs++;
	/* Record the size of this request */
	if((i = ilog2(size)) >= 0)
		Msizes[i]++;

	if(size <= SMALL_MBUF){
		i = 0;
		size = SMALL_MBUF;
	} else if(size <= MED_MBUF){
		i = 1;
		size = MED_MBUF;
	} else if(size <= LARGE_MBUF){
		i = 2;
		size = LARGE_MBUF;
	} else
		i = 3;

	if(i < 3){
		i_state = dirps();
		if(Mbufcache[i] != NULL){
			bp = Mbufcache[i];
			Mbufcache[i] = bp->anext;
			Cachehits++;
		}
		restore(i_state);
	}
	if(bp == NULL)
		bp = (struct mbuf *)malloc(size + sizeof(struct mbuf));
	if(bp == NULL)
		return NULL;
	/* Clear just the header portion */
	memset(bp,0,sizeof(struct mbuf));
	if((bp->size = size) != 0)
		bp->data = (uint8 *)(bp + 1);
	bp->refcnt++;
	return bp;
}
/* Allocate mbuf, waiting if memory is unavailable */
struct mbuf *
ambufw(uint16 size)
{
	struct mbuf *bp = NULL;
	int i,i_state;

	Allocmbufs++;
	if((i = ilog2(size)) >= 0)
		Msizes[i]++;

	if(size <= SMALL_MBUF){
		i = 0;
		size = SMALL_MBUF;
	} else if(size <= MED_MBUF){
		i = 1;
		size = MED_MBUF;
	} else if(size <= LARGE_MBUF){
		i = 2;
		size = LARGE_MBUF;
	} else
		i = 3;

	if(i < 3){
		i_state = dirps();
		if(Mbufcache[i] != NULL){
			bp = Mbufcache[i];
			Mbufcache[i] = bp->anext;
			Cachehits++;
		}
		restore(i_state);
	}
	if(bp == NULL)
		bp = (struct mbuf *)mallocw(size + sizeof(struct mbuf));

	/* Clear just the header portion */
	memset(bp,0,sizeof(struct mbuf));
	if((bp->size = size) != 0)
		bp->data = (uint8 *)(bp + 1);
	bp->refcnt++;
	return bp;
}

/* Decrement the reference pointer in an mbuf. If it goes to zero,
 * free all resources associated with mbuf.
 * Return pointer to next mbuf in packet chain
 */
struct mbuf *
free_mbuf(struct mbuf **bpp)
{
	struct mbuf *bpnext;
	struct mbuf *bptmp;
	struct mbuf *bp;
	int i_state;

	if(bpp == NULL || (bp = *bpp) == NULL)
		return NULL;

	*bpp = NULL;
	bpnext = bp->next;
	if(bp->dup != NULL){
		bptmp = bp->dup;
		bp->dup = NULL;	/* Nail it before we recurse */
		free_mbuf(&bptmp);	/* Follow indirection */
	}
	/* Decrement reference count. If it has gone to zero, free it. */
	if(--bp->refcnt <= 0){
		Freembufs++;

		i_state = dirps();
		switch(bp->size){
		case SMALL_MBUF:
			bp->anext = Mbufcache[0];
			Mbufcache[0] = bp;
			break;
		case MED_MBUF:
			bp->anext = Mbufcache[1];
			Mbufcache[1] = bp;
			break;
		case LARGE_MBUF:
			bp->anext = Mbufcache[2];
			Mbufcache[2] = bp;
			break;
		default:
			free(bp);
			break;
		}
		restore(i_state);
	}
	return bpnext;
}

/* Free packet (a chain of mbufs). Return pointer to next packet on queue,
 * if any
 */
struct mbuf *
free_p(struct mbuf **bpp)
{
	struct mbuf *bp;   
	register struct mbuf *abp;

	if(bpp == NULL || (bp = *bpp) == NULL)
		return NULL;
	abp = bp->anext;
	while(bp != NULL)
		bp = free_mbuf(&bp);
	*bpp = NULL;
	return abp;
}		
/* Free entire queue of packets (of mbufs) */
void
free_q(struct mbuf **q)
{
	struct mbuf *bp;

	while((bp = dequeue(q)) != NULL)
		free_p(&bp);
}

/* Count up the total number of bytes in a packet */
uint16
len_p(struct mbuf *bp)
{
	register uint16 cnt = 0;

	while(bp != NULL){
		cnt += bp->cnt;
		bp = bp->next;
	}
	return cnt;
}
/* Count up the number of packets in a queue */
uint16
len_q(struct mbuf *bp)
{
	register uint16 cnt;

	for(cnt=0;bp != NULL;cnt++,bp = bp->anext)
		;
	return cnt;
}
/* Trim mbuf to specified length by lopping off end */
void
trim_mbuf(struct mbuf **bpp,uint16 length)
{
	register uint16 tot = 0;
	register struct mbuf *bp;

	if(bpp == NULL || *bpp == NULL)
		return;	/* Nothing to trim */

	if(length == 0){
		/* Toss the whole thing */
		free_p(bpp);
		return;
	}
	/* Find the point at which to trim. If length is greater than
	 * the packet, we'll just fall through without doing anything
	 */
	for( bp = *bpp; bp != NULL; bp = bp->next){
		if(tot + bp->cnt < length){
			tot += bp->cnt;
		} else {
			/* Cut here */
			bp->cnt = length - tot;
			free_p(&bp->next);
			bp->next = NULL;
			break;
		}
	}
}
/* Duplicate/enqueue/dequeue operations based on mbufs */

/* Duplicate first 'cnt' bytes of packet starting at 'offset'.
 * This is done without copying data; only the headers are duplicated,
 * but without data segments of their own. The pointers are set up to
 * share the data segments of the original copy. The return pointer is
 * passed back through the first argument, and the return value is the
 * number of bytes actually duplicated.
 */
uint16
dup_p(
struct mbuf **hp,
register struct mbuf *bp,
register uint16 offset,
register uint16 cnt
){
	struct mbuf *cp;
	uint16 tot;

	if(cnt == 0 || bp == NULL || hp == NULL){
		if(hp != NULL)
			*hp = NULL;
		return 0;
	}
	if((*hp = cp = alloc_mbuf(0)) == NULL){
		return 0;
	}
	/* Skip over leading mbufs that are smaller than the offset */
	while(bp != NULL && bp->cnt <= offset){
		offset -= bp->cnt;
		bp = bp->next;
	}
	if(bp == NULL){
		free_mbuf(&cp);
		*hp = NULL;
		return 0;	/* Offset was too big */
	}
	tot = 0;
	for(;;){
		/* Make sure we get the original, "real" buffer (i.e. handle the
		 * case of duping a dupe)
		 */
		if(bp->dup != NULL)
			cp->dup = bp->dup;
		else
			cp->dup = bp;

		/* Increment the duplicated buffer's reference count */
		cp->dup->refcnt++;

		cp->data = bp->data + offset;
		cp->cnt = min(cnt,bp->cnt - offset);
		offset = 0;
		cnt -= cp->cnt;
		tot += cp->cnt;
		bp = bp->next;
		if(cnt == 0 || bp == NULL || (cp->next = alloc_mbuf(0)) == NULL)
			break;
		cp = cp->next;
	}
	return tot;
}
/* Copy first 'cnt' bytes of packet into a new, single mbuf */
struct mbuf *
copy_p(
register struct mbuf *bp,
register uint16 cnt
){
	register struct mbuf *cp;
	register uint8 *wp;
	register uint16 n;

	if(bp == NULL || cnt == 0 || (cp = alloc_mbuf(cnt)) == NULL)
		return NULL;
	wp = cp->data;
	while(cnt != 0 && bp != NULL){
		n = min(cnt,bp->cnt);
		memcpy(wp,bp->data,n);
		wp += n;
		cp->cnt += n;
		cnt -= n;
		bp = bp->next;
	}
	return cp;
}
/* Copy and delete "cnt" bytes from beginning of packet. Return number of
 * bytes actually pulled off
 */
uint16
pullup(
struct mbuf **bph,
void *buf,
uint16 cnt
){
	struct mbuf *bp;
	uint16 n,tot;
	uint8 *obp = buf;

	tot = 0;
	if(bph == NULL)
		return 0;
	while(cnt != 0 && (bp = *bph) != NULL){
		n = min(cnt,bp->cnt);
		if(obp != NULL){
			if(n == 1){	/* Common case optimization */
				*obp++ = *bp->data;
			} else if(n > 1){
				memcpy(obp,bp->data,n);
				obp += n;
			}
		}
		tot += n;
		cnt -= n;
		bp->data += n;
		bp->cnt -= n;		
		if(bp->cnt == 0){
			/* If this is the last mbuf of a packet but there
			 * are others on the queue, return a pointer to
			 * the next on the queue. This allows pullups to
			 * to work on a packet queue
			 */
			if(bp->next == NULL && bp->anext != NULL){
				*bph = bp->anext;
				free_mbuf(&bp);
			} else
				*bph = free_mbuf(&bp);
		}
	}
	return tot;
}
/* Copy data from within mbuf to user-provided buffer, starting at
 * 'offset' bytes from start of mbuf and copying no more than 'len'
 * bytes. Return actual number of bytes copied
 */
uint16
extract(
struct mbuf *bp,
uint16 offset,
void *buf,
uint16 len
){
	uint8 *obp = buf;
	uint16 copied = 0;
	uint16 n;

	/* Skip over offset if greater than first mbuf(s) */
	while(bp != NULL && offset >= bp->cnt){
		offset -= bp->cnt;
		bp = bp->next;
	}
	while(bp != NULL && len != 0){
		n = min(len,bp->cnt - offset);	/* offset must be < bp->cnt */
		memcpy(obp,bp->data+offset,n);
		copied += n;
		obp += n;
		len -= n;
		if(n + offset == bp->cnt)
			bp = bp->next;	/* Buffer exhausted, get next */
		offset = 0;		/* No more offset after first */
	}
	return copied;
}
/* Append mbuf to end of mbuf chain */
void
append(
struct mbuf **bph,
struct mbuf **bpp
){
	register struct mbuf *p;

	if(bph == NULL || bpp == NULL || *bpp == NULL)
		return;

	if(*bph == NULL){
		/* First one on chain */
		*bph = *bpp;
	} else {
		for(p = *bph ; p->next != NULL ; p = p->next)
			;
		p->next = *bpp;
	}
	*bpp = NULL;	/* We've consumed it */
}
/* Insert specified amount of contiguous new space at the beginning of an
 * mbuf chain. If enough space is available in the first mbuf, no new space
 * is allocated. Otherwise a mbuf of the appropriate size is allocated and
 * tacked on the front of the chain.
 *
 * This operation is the logical inverse of pullup(), hence the name.
 */
void
pushdown(struct mbuf **bpp,void *buf,uint16 size)
{
	struct mbuf *bp;

	Pushdowns++;
	if(bpp == NULL)
		return;
	/* Check that bp is real, that it hasn't been duplicated, and
	 * that it itself isn't a duplicate before checking to see if
	 * there's enough space at its front.
	 */
	if((bp = *bpp) != NULL && bp->refcnt == 1 && bp->dup == NULL
	 && bp->data - (uint8 *)(bp+1) >= size){
		/* No need to alloc new mbuf, just adjust this one */
		bp->data -= size;
		bp->cnt += size;
	} else {
		(*bpp) = ambufw(size);
		(*bpp)->next = bp;
		bp = *bpp;
		bp->cnt = size;
		Pushalloc++;
	}
	if(buf != NULL)
		memcpy(bp->data,buf,size);
}
/* Append packet to end of packet queue */
void
enqueue(
struct mbuf **q,
struct mbuf **bpp
){
	register struct mbuf *p;
	uint8 i_state;

	if(q == NULL || bpp == NULL || *bpp == NULL)
		return;
	i_state = dirps();
	if(*q == NULL){
		/* List is empty, stick at front */
		*q = *bpp;
	} else {
		for(p = *q ; p->anext != NULL ; p = p->anext)
			;
		p->anext = *bpp;
	}
	*bpp = NULL;	/* We've consumed it */
	restore(i_state);
	ksignal(q,1);
}
/* Unlink a packet from the head of the queue */
struct mbuf *
dequeue(struct mbuf **q)
{
	register struct mbuf *bp;
	uint8 i_state;

	if(q == NULL)
		return NULL;
	i_state = dirps();
	if((bp = *q) != NULL){
		*q = bp->anext;
		bp->anext = NULL;
	}
	restore(i_state);
	return bp;
}	

/* Copy user data into an mbuf */
struct mbuf *
qdata(void *data,uint16 cnt)
{
	register struct mbuf *bp;

	bp = ambufw(cnt);
	memcpy(bp->data,data,cnt);
	bp->cnt = cnt;
	return bp;
}
/* Pull a 32-bit integer in host order from buffer in network byte order.
 * On error, return 0. Note that this is indistinguishable from a normal
 * return.
 */
int32
pull32(struct mbuf **bpp)
{
	uint8 buf[4];

	if(pullup(bpp,buf,4) != 4){
		/* Return zero if insufficient buffer */
		return 0;
	}
	return get32(buf);
}
/* Pull a 16-bit integer in host order from buffer in network byte order.
 * Return -1 on error
 */
long
pull16(struct mbuf **bpp)
{
	uint8 buf[2];

	if(pullup(bpp,buf,2) != 2){
		return -1;		/* Nothing left */
	}
	return get16(buf);
}
/* Pull single byte from mbuf */
int
pull8(struct mbuf **bpp)
{
	uint8 c;

	if(pullup(bpp,&c,1) != 1)
		return -1;		/* Nothing left */
	return c;
}
int
write_p(FILE *fp,struct mbuf *bp)
{
	while(bp != NULL){
		if(fwrite(bp->data,1,bp->cnt,fp) != bp->cnt)
			return -1;
		bp = bp->next;
	}
	return 0;
}
/* Reclaim unused space in a mbuf chain. If the argument is a chain of mbufs
 * and/or it appears to have wasted space, copy it to a single new mbuf and
 * free the old mbuf(s). But refuse to move mbufs that merely
 * reference other mbufs, or that have other headers referencing them.
 *
 * Be extremely careful that there aren't any other pointers to
 * (or into) this mbuf, since we have no way of detecting them here.
 * This function is meant to be called only when free memory is in
 * short supply.
 */
void
mbuf_crunch(struct mbuf **bpp)
{
	struct mbuf *bp = *bpp;
	struct mbuf *nbp;

	if(bp->refcnt > 1 || bp->dup != NULL){
		/* Can't crunch, there are other refs */
		return;
	}
	if(bp->next == NULL && bp->cnt == bp->size){
		/* Nothing to be gained by crunching */
		return;
	}
	if((nbp = copy_p(bp,len_p(bp))) == NULL){
		/* Copy failed due to lack of (contiguous) space */
		return;
	}
	nbp->anext = bp->anext;
	free_p(&bp);
	*bpp = nbp;
}
void
mbufstat(void)
{
	printf("mbuf allocs %lu free cache hits %lu (%lu%%) mbuf frees %lu\n",
	 Allocmbufs,Cachehits,100*Cachehits/Allocmbufs,Freembufs);
	printf("pushdown calls %lu pushdown calls to alloc_mbuf %lu\n",
	 Pushdowns,Pushalloc);
	printf("Free cache: small %u medium %u large %u\n",
	 len_q(Mbufcache[0]),len_q(Mbufcache[1]),len_q(Mbufcache[2]));
}
void
mbufsizes(void)
{
	int i;

	printf("Mbuf sizes:\n");
	for(i=0;i<16;i += 4){
		printf("N>=%5u:%7ld| N>=%5u:%7ld| N>=%5u:%7ld| N>=%5u:%7ld\n",
		 1<<i,Msizes[i],2<<i,Msizes[i+1],
		 4<<i,Msizes[i+2],8<<i,Msizes[i+3]);
	}
}
/* Mbuf garbage collection - return all mbufs on free cache to heap */
void
mbuf_garbage(int red)
{
	int i_state;
	int i;
	struct mbuf *bp;

	/* Blow entire cache */
	for(i=0;i<3;i++){
		i_state = dirps();		
		while((bp = Mbufcache[i]) != NULL){
			Mbufcache[i] = bp->anext;
			free(bp);
		}
		restore(i_state);
	}
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美日韩一区二区三区高清| 亚洲精品乱码久久久久久黑人| 亚洲一区二区在线免费看| 成人一区二区三区| 91精品国产综合久久精品| 亚洲精品欧美专区| 99国产精品国产精品毛片| 中文字幕 久热精品 视频在线| 久久成人18免费观看| 欧美一区二区三区精品| 亚洲国产aⅴ成人精品无吗| 成人精品小蝌蚪| 国产日韩av一区| 国产一区二区三区久久久 | 国产999精品久久久久久绿帽| 欧美日韩极品在线观看一区| 一二三四社区欧美黄| 日本电影亚洲天堂一区| 亚洲欧美日韩国产综合| 色94色欧美sute亚洲线路一久| 自拍偷拍亚洲激情| 欧美亚州韩日在线看免费版国语版| 国产一区二区看久久| 欧美激情在线看| 97精品电影院| 一区二区三区在线看| 欧美精品在线一区二区三区| 日韩中文字幕1| 精品三级在线观看| 国产91清纯白嫩初高中在线观看| 国产视频一区二区在线| av在线一区二区三区| 亚洲成人在线观看视频| 日韩欧美123| 成人丝袜高跟foot| 成人免费一区二区三区视频 | 国产精品久久久久久久久动漫| 九一九一国产精品| 日韩一级欧美一级| 国产ts人妖一区二区| 国产精品久久久久三级| 91亚洲精品久久久蜜桃| 一区二区三区不卡视频在线观看| 国产**成人网毛片九色| 精品国产电影一区二区| 国产成人在线视频网址| 亚洲男同性视频| 欧美日韩www| 国产不卡免费视频| 亚洲大片免费看| 国产精品污污网站在线观看| 欧美性三三影院| 免费不卡在线观看| 精品国产一区二区三区忘忧草 | 国产综合久久久久久鬼色 | 一区二区三区四区乱视频| 欧美性生活大片视频| 国产精品一区久久久久| 欧美极品aⅴ影院| 欧美午夜宅男影院| 国产福利精品导航| 日本伊人色综合网| 亚洲欧美日韩在线播放| 日韩欧美不卡一区| 欧美日韩中字一区| 成人永久看片免费视频天堂| 五月天网站亚洲| 综合久久给合久久狠狠狠97色| 欧美一区二区视频在线观看 | 日韩视频免费直播| 色偷偷久久一区二区三区| 久久精品国产秦先生| 一区二区三区色| 国产亚洲美州欧州综合国| 欧美日韩国产另类一区| av在线这里只有精品| 国产不卡视频一区二区三区| 美女视频一区在线观看| 亚洲二区在线视频| 1000部国产精品成人观看| 久久九九国产精品| 欧美一激情一区二区三区| 91在线码无精品| 成人午夜在线视频| 国产成人精品午夜视频免费| 强制捆绑调教一区二区| 日本aⅴ精品一区二区三区 | 日韩av一区二区三区四区| 亚洲男同性恋视频| 亚洲三级在线观看| 亚洲麻豆国产自偷在线| 精品99999| 91精品国产综合久久久久久久久久 | 看片网站欧美日韩| 日日骚欧美日韩| 亚洲18女电影在线观看| 亚洲国产精品天堂| 亚洲国产人成综合网站| 亚洲精品高清在线| 一区二区三区在线视频播放| 亚洲猫色日本管| 亚洲在线成人精品| 亚洲一区二区三区中文字幕 | 久久精品一区二区三区不卡 | 欧美国产日韩一二三区| 欧美激情在线观看视频免费| 久久久久久毛片| 欧美激情一区二区三区全黄| 国产午夜精品久久久久久免费视| 国产性色一区二区| 久久精品亚洲乱码伦伦中文| 久久精品夜色噜噜亚洲aⅴ| 久久久久久久久久久黄色| 国产日韩影视精品| 中文字幕一区av| 亚洲精品你懂的| 亚洲va在线va天堂| 奇米777欧美一区二区| 韩国理伦片一区二区三区在线播放| 久久99国内精品| 国产91精品欧美| 99re这里只有精品首页| 日本韩国欧美在线| 91香蕉视频mp4| 欧美日韩综合在线免费观看| 555夜色666亚洲国产免| 337p日本欧洲亚洲大胆色噜噜| 国产亚洲成年网址在线观看| 亚洲欧美国产毛片在线| 免费成人性网站| 成人一区二区三区中文字幕| 日本久久电影网| 日韩欧美一区在线观看| 欧美国产视频在线| 亚洲国产精品综合小说图片区| 久久国产生活片100| 91影院在线免费观看| 欧美丰满高潮xxxx喷水动漫| 国产色91在线| 亚洲与欧洲av电影| 国产一区二区h| 99久久久国产精品| 欧美一区二区三区在线看| 国产欧美一区视频| 亚洲va欧美va国产va天堂影院| 激情六月婷婷综合| 色妹子一区二区| 精品久久久久香蕉网| 一区二区三区国产精品| 国产精品亚洲成人| 欧美男人的天堂一二区| 国产精品女同一区二区三区| 奇米综合一区二区三区精品视频| av电影在线观看完整版一区二区| 91精品免费观看| 亚洲一区二区三区视频在线播放| 国产91高潮流白浆在线麻豆 | 欧美电视剧免费全集观看| 亚洲欧洲在线观看av| 丝袜诱惑制服诱惑色一区在线观看| 国产一区二区免费在线| 欧美高清视频不卡网| 亚洲精品国产视频| 成人黄色av网站在线| 欧美成人一区二区三区在线观看 | 欧美一区二区啪啪| 亚洲最色的网站| 国产老妇另类xxxxx| 成人app软件下载大全免费| 91小视频在线观看| 亚洲视频在线观看三级| 色香蕉久久蜜桃| 亚洲综合色丁香婷婷六月图片| 一本到一区二区三区| 亚洲国产va精品久久久不卡综合| 欧美中文字幕不卡| 香蕉av福利精品导航| 日韩一级黄色片| 国产精品一区在线| 自拍偷拍欧美激情| 欧美视频一区二区三区四区| 天堂蜜桃一区二区三区| 精品国产免费久久| 国产宾馆实践打屁股91| 亚洲精品成人在线| 欧美丰满高潮xxxx喷水动漫| 国模冰冰炮一区二区| 国产精品久久久99| 欧美在线观看一区二区| 日本女优在线视频一区二区| 久久亚区不卡日本| 色综合久久九月婷婷色综合| 香蕉久久夜色精品国产使用方法| 欧美成人午夜电影| 成人av免费观看| 亚洲图片欧美一区| 久久综合久久综合久久综合| 91在线免费看| 看国产成人h片视频| 国产精品女主播在线观看|