Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

环形buffer有问题 #49

Closed
weixuechao2018 opened this issue Dec 12, 2019 · 2 comments
Closed

环形buffer有问题 #49

weixuechao2018 opened this issue Dec 12, 2019 · 2 comments

Comments

@weixuechao2018
Copy link

当数据写到buffer尾部时,如果队尾如果在写数据有问题

@weixuechao2018
Copy link
Author

ssize_t rb_write(struct ringbuffer *rb, const void *buf, size_t len)
{
if (!rb) {
return -1;
}
int left = rb_get_space_free(rb);
if ((int)len > left) {
printf("Not enough space: %zu request, %d available\n", len, left);
return -1;
}
// 这里修改过
#if 1
if((rb->length - rb->end) < len) {
int firstLen = rb->length - rb->end;
memcpy(rb_end_ptr(rb), buf, firstLen);
rb->end = (rb->end + firstLen) % rb->length;

    int secondLen = len - firstLen;
    memcpy(rb_end_ptr(rb), buf+firstLen, secondLen);
    rb->end = (rb->end + secondLen) % rb->length;
} else {
    memcpy(rb_end_ptr(rb), buf, len);
    rb->end = (rb->end + len) % rb->length;
}

#else
memcpy(rb_end_ptr(rb), buf, len);
rb->end = (rb->end + len) % rb->length;
#endif

return len;

}
这样改的,我测试了可以循环写,不知道对不对

@gozfree
Copy link
Owner

gozfree commented Dec 14, 2019

非常感谢,你是对的,已经fix了
b916cf6

@gozfree gozfree closed this as completed Dec 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants