⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fifo.c

📁 基于linux下C语言实现的CMPP通信协议
💻 C
字号:
/* 	file:fifo.c	first in first out(fifo) by jiniang 2005.02.04*/#include <stdlib.h>#include <string.h>#include <pthread.h>#include "fifo.h"pthread_mutex_t fifo_mutex=PTHREAD_MUTEX_INITIALIZER;	//fifo lockint fifo_init(struct fifo *p,unsigned int buf_size){	p->first=0;	p->last=0;	p->current=0;	p->max=buf_size;	return 0;}int fifo_destroy(struct fifo* p){	return p->current;}int fifo_put(struct fifo* p, void* data, unsigned int size){	int ret;	pthread_mutex_lock(&fifo_mutex);	if(p->max && p->current>=p->max)	{		ret=1;		//buf is full;	}	else	{		struct node *n=(struct node*)malloc(sizeof(struct node));		if(!n)		{			ret=1;	//can't get space		}		else		{			n->size=size;			n->data=malloc(size);			if(!n->data)			{				free(n);				ret=1;	//can't get space			}			else			{				memcpy(n->data,data,size);				n->next=0;							if(!p->first || !p->last)			//put first time				{					p->first=n;					p->last=n;				}				else				{					p->last->next=n;					p->last=n;				}				p->current++;				ret=0;			}		}	}	pthread_mutex_unlock(&fifo_mutex);	return ret;}int fifo_get(struct fifo* p, void* data, unsigned int* size){	int ret;	pthread_mutex_lock(&fifo_mutex);	if(!p->first || !p->last)	{		ret=1;	}	else	{		struct node *n=p->first;		*size=n->size;		memcpy(data,n->data,*size);		p->first=n->next;		free(n->data);		free(n);		p->current--;		ret=0;	}	pthread_mutex_unlock(&fifo_mutex);	return ret;}int fifo_isempty(struct fifo* p){		return (!p->current);}int fifo_isfull(struct fifo* p){	if(!p->max) return 0;						//if fifo=0 then return 0 It's means not full	return (p->current >= p->max);}int fifo_setmax(struct fifo* p,unsigned int size){	int ret;	pthread_mutex_unlock(&fifo_mutex);	if(size < p->current)	{		ret=1;	}	else	{		p->max=size;		ret=0;	}	pthread_mutex_unlock(&fifo_mutex);	return ret;}int fifo_getcurrent(struct fifo* p){	return p->current;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -