0 Star 0 Fork 1

yebigyy1997/ANet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
buffer.c 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
hurley 提交于 2016-11-10 11:56 . init
#include "buffer.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "define.h"
buffer_t *alloc_buffer()
{
buffer_t *buffer = zmalloc(sizeof(buffer_t));
if (buffer == NULL) {
goto err;
}
buffer->buff = zmalloc(DEFAULT_BUFF_SIZE);
buffer->size = DEFAULT_BUFF_SIZE;
buffer->read_idx = 0;
buffer->write_idx = 0;
return buffer;
err:
if (buffer) {
zfree(buffer->buff);
zfree(buffer);
}
return NULL;
}
void free_buffer(buffer_t *buffer)
{
if (buffer) {
zfree(buffer->buff);
zfree(buffer);
}
}
void check_buffer_size(buffer_t *buffer, size_t avlid_size)
{
if (buffer->read_idx > DEFAULT_BUFF_SIZE) {
size_t data_len = get_readable_size(buffer);
memmove(buffer->buff, buffer->buff + buffer->read_idx, data_len);
buffer->read_idx = 0;
buffer->write_idx = data_len;
}
if (get_writeable_size(buffer) < avlid_size) {
size_t new_size = buffer->size + avlid_size;
buffer->buff = zrealloc(buffer->buff, new_size);
buffer->size = new_size;
}
}
size_t get_readable_size(buffer_t *buffer)
{
assert(buffer->size >= buffer->write_idx);
assert(buffer->read_idx <= buffer->write_idx);
return buffer->write_idx - buffer->read_idx;
}
size_t get_writeable_size(buffer_t *buffer)
{
assert(buffer->size >= buffer->write_idx);
assert(buffer->read_idx <= buffer->write_idx);
return buffer->size - buffer->write_idx;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ivan_ye/ANet.git
[email protected]:ivan_ye/ANet.git
ivan_ye
ANet
ANet
master

搜索帮助