首页 > 代码库 > 环形缓冲区(CirBuf)

环形缓冲区(CirBuf)

cirbuf.h

#ifndef _Cirbuf_h
#define _Cirbuf_h
#include <pthread.h>
#define size_t int
struct Cirbuf
{
	unsigned char *mp_buf;
	size_t m_buf_size;
	size_t m_write, m_read;
	pthread_mutex_t mut_cirbuf;
};
typedef struct Cirbuf mCirbuf;
struct Cirbuf *util_cbuf_create (size_t size);

void util_cbuf_release (struct Cirbuf *buf);
size_t util_cbuf_space (struct Cirbuf *buf);
size_t util_cbuf_data (struct Cirbuf *buf);
size_t util_cbuf_contiguous_data (struct Cirbuf *buf);
void *util_cbuf_get_contiguous_data (struct Cirbuf *buf);
size_t util_cbuf_get_cdata (struct Cirbuf *buf, void **data);
void util_cbuf_save (struct Cirbuf *buf, const void *data, size_t len);

void util_cbuf_consume (struct Cirbuf *buf, size_t cnt);
void popdata (struct Cirbuf *cbuf, void *buf, size_t len);
#endif
</span>


cirbuf.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cirbuf.h"
#define CNT_DATA(write, read, size) (((write)-(read)) & ((size)-1))
#define CNT_SPACE(write, read, size) CNT_DATA((read), ((write)+1), (size))
//已有数据
static size_t DATA_TO_END(size_t write, size_t read, size_t size)
{
	size_t end = size - read;
	size_t n = (write + end) & (size - 1);
	return n < end ? n : end;
}
//剩余空间
static size_t SPACE_TO_END(size_t write, size_t read, size_t size)
{
	size_t end = size - 1 - write;
	size_t n = (read + end) & (size - 1);
	return n <= end ? n : end+1;
}
struct Cirbuf *util_cbuf_create (size_t size)
{
	struct Cirbuf *buf = (struct Cirbuf*)malloc(sizeof(struct Cirbuf));
	buf->mp_buf = (unsigned char*)malloc(size);
	buf->m_buf_size = size;
	buf->m_read = 0;
	buf->m_write = 0;
	pthread_mutex_init(&buf->mut_cirbuf,NULL);
	return (struct Cirbuf*)buf;
}

void util_cbuf_release (struct Cirbuf *buf)
{
	struct Cirbuf *b = (struct Cirbuf*)buf;
	free(b->mp_buf);
	free(b);
}

size_t util_cbuf_space (struct Cirbuf *buf)
{
	struct Cirbuf *b = (struct Cirbuf*)buf;
	return CNT_SPACE(b->m_write, b->m_read, b->m_buf_size);
}

size_t util_cbuf_data (struct Cirbuf *buf)
{
	struct Cirbuf *b = (struct Cirbuf*)buf;
	return CNT_DATA(b->m_write, b->m_read, b->m_buf_size);
}

size_t util_cbuf_contiguous_data (struct Cirbuf *buf)
{
	struct Cirbuf *b = (struct Cirbuf *)buf;
	return DATA_TO_END(b->m_write, b->m_read, b->m_buf_size);
}

void *util_cbuf_get_contiguous_data (struct Cirbuf *buf)
{
	struct Cirbuf *b = (struct Cirbuf*)buf;
	return b->mp_buf + b->m_read;
}

size_t util_cbuf_get_cdata (struct Cirbuf *buf, void **data)
{
	struct Cirbuf *b = (struct Cirbuf*)buf;
	*data = http://www.mamicode.com/b->mp_buf + b->m_read;>


环形缓冲区(CirBuf)