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

📄 bufpool.c

📁 linux环境下用纯c写的RTP协议的通用开发库
💻 C
字号:
/*------------------------------------------------------------------------- * bufpool.c - bufpoolinit, bufpoolgetbuf, bufpoolfreebuf, bufpooldestroy * * Bufferpool design is adapted from: *  * Comer, D. E., Operating System Design: The XINU Approach, Prentice-Hall, * 1984. *------------------------------------------------------------------------- */#include <rtplibcommon.h>#include <stdlib.h>#include <bufpool.h>/*------------------------------------------------------------------------ * bufpoolinit - allocate memory for a pool of fixed-size buffers * and link together *------------------------------------------------------------------------ */intbufpoolinit(struct bufpool *pbp, int bufsize, int numbufs){	char *where;	if (bufsize < BPMINB)		return ERROR;  	where = pbp->bp_base = (char *) malloc((bufsize + sizeof(struct bufpool *)) * numbufs);	if (where == NULL)		return ERROR;	pbp->bp_total = numbufs;	pbp->bp_next = where;	pbp->bp_size = bufsize;	sem_init(&pbp->bp_sem, FALSE, numbufs);	pthread_mutex_init(&pbp->bp_mutex, NULL);	bufsize += sizeof(struct bufpool *);	for (numbufs-- ; numbufs > 0 ; numbufs--, where += bufsize)		*((int *) where ) = (int) (where + bufsize);	*((int *) where) = (int) NULL;	return OK;}/*------------------------------------------------------------------------ * bufpoolgetbuf - get a fixed size buffer *------------------------------------------------------------------------ */int *bufpoolgetbuf(struct bufpool *pbp){	int	*buf;	if (sem_trywait(&pbp->bp_sem) != 0)		return NULL;  	if (pthread_mutex_lock(&pbp->bp_mutex) != 0) {		sem_post(&pbp->bp_sem);		return NULL;	}  	buf = (int *) pbp->bp_next;	pbp->bp_next = (char *) *buf;	pthread_mutex_unlock(&pbp->bp_mutex);  	*buf = (int) pbp;	return buf + 1;}/*------------------------------------------------------------------------ *  bufpoolfreebuf - free a buffer that was allocated from a pool by getbuf *------------------------------------------------------------------------ */voidbufpoolfreebuf(void *vbuf){  	int			*buf = (int *) vbuf;	struct bufpool	*pbp;	pbp = (struct bufpool *) *(--buf);	pthread_mutex_lock(&pbp->bp_mutex);  	*buf = (int) pbp->bp_next;	pbp->bp_next = (char *) buf;  	pthread_mutex_unlock(&pbp->bp_mutex);	sem_post(&pbp->bp_sem);}/*------------------------------------------------------------------------ *  bufpooldestroy - destroy a pool of buffers *------------------------------------------------------------------------ */intbufpooldestroy(struct bufpool *pbp){  	int i;  	if (pbp == NULL)		return ERROR;	for (i = 0; i < pbp->bp_total; i++)		sem_wait(&pbp->bp_sem);	free(pbp->bp_base);	sem_destroy(&pbp->bp_sem);	pthread_mutex_destroy(&pbp->bp_mutex);	return OK;}

⌨️ 快捷键说明

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