add flushing to ringbuf write

This commit is contained in:
oscar 2023-03-03 20:07:27 -05:00
parent ac02f96a31
commit b66de50303
3 changed files with 28 additions and 18 deletions

View File

@ -71,12 +71,19 @@ bsock_ringbuf_read(struct bsock_ringbuf *rb, char *buf, size_t len)
}
int
bsock_ringbuf_write(struct bsock_ringbuf *rb, char *buf, size_t len)
bsock_ringbuf_write(struct bsock_ringbuf *rb, void * ctx, struct bsock_ringbuf_io * io, char *buf, size_t len)
{
size_t free_sz = rb->max_sz - rb->sz;
if (len > free_sz) {
errno = ERANGE;
return -1;
int ret = bsock_ringbuf_flush(rb, ctx, io, rb->sz);
if (ret != 0) {
return ret;
}
assert(rb->sz == 0);
if (len > rb->max_sz) {
return io->write(ctx, buf, len);
}
}
if (rb->end > rb->start) {

View File

@ -72,9 +72,9 @@ int bsock_ringbuf_read(struct bsock_ringbuf *rb, char *buf, size_t len);
/**
* returns:
* success: 0
* failure: -1 + errno (ERANGE if not enough data present)
* failure: -1 + errno
*/
int bsock_ringbuf_write(struct bsock_ringbuf *rb, char *buf, size_t len);
int bsock_ringbuf_write(struct bsock_ringbuf *rb, void * ctx, struct bsock_ringbuf_io * io, char *buf, size_t len);
/**
* returns:

29
test.c
View File

@ -5,11 +5,11 @@
#include "ringbuf.h"
static int
ringbuf_verify(struct bsock_ringbuf *buf, char *start, size_t sz)
ringbuf_verify(struct bsock_ringbuf *buf, char * start, size_t sz)
{
int ret = 1;
ret = ret && (buf->start == start);
ret = ret && (start == buf->start);
ret = ret && (buf->sz == sz);
if (buf->start <= buf->end) {
@ -22,11 +22,11 @@ ringbuf_verify(struct bsock_ringbuf *buf, char *start, size_t sz)
}
static void
assert_write(struct bsock_ringbuf *rbuf, char *buf, size_t sz)
assert_write(struct bsock_ringbuf *rbuf, void * ctx, struct bsock_ringbuf_io * io, char *buf, size_t sz)
{
char *start = rbuf->start;
size_t ssz = rbuf->sz;
assert(bsock_ringbuf_write(rbuf, buf, sz) == 0);
assert(bsock_ringbuf_write(rbuf, ctx, io, buf, sz) == 0);
assert(ringbuf_verify(rbuf, start, ssz + sz));
}
@ -56,32 +56,28 @@ rbuf_set(struct bsock_ringbuf *rbuf, char *start, size_t sz)
}
void
ringbuf_test(void)
bsock_test(void)
{
struct bsock_ringbuf rbuf;
struct bsock_ringbuf_io io = bsock_io_posix();
char *buf = malloc(1024);
char *sbuf = malloc(1024);
for (int i = 0; i < 1024; i++) {
sbuf[i] = i % 256;
sbuf[i] = rand() % 256;
}
char *buf3 = malloc(1024);
printf("ringbuf - init\n");
bsock_ringbuf_init(&rbuf, buf, 1024);
assert(ringbuf_verify(&rbuf, buf, 0));
printf("ringbuf - read/write [128]\n");
memset(buf3, 0, 1024);
bsock_ringbuf_init(&rbuf, buf, 1024);
assert_write(&rbuf, sbuf, 128);
assert_write(&rbuf, NULL, NULL, sbuf, 128);
assert_read(&rbuf, buf3, 128);
assert(memcmp(sbuf, buf3, 128) == 0);
printf("ringbuf - read/write overflow [128]\n");
memset(buf3, 0, 1024);
bsock_ringbuf_init(&rbuf, buf, 1024);
assert_write(&rbuf, sbuf, 128);
assert_write(&rbuf, sbu3f, NULL, NULL, 128);
assert(bsock_ringbuf_read(&rbuf, buf3, 129) == -1);
assert(errno == ERANGE);
@ -112,6 +108,13 @@ ringbuf_test(void)
memset(buf3, 0, 1024);
bsock_ringbuf_init(&rbuf, buf, 1024);
rbuf_set(&rbuf, buf + 768, 0);
assert_write(&rbuf, sbuf, 2048);
assert(memcmp(sbuf, buf3, 512) == 0);
printf("ringbuf - write [overcap]\n");
memset(buf3, 0, 1024);
bsock_ringbuf_init(&rbuf, buf, 1024);
rbuf_set(&rbuf, buf + 768, 0);
assert_write(&rbuf, sbuf, 512);
assert_read(&rbuf, buf3, 512);
assert(memcmp(sbuf, buf3, 512) == 0);
@ -154,7 +157,7 @@ int
main(void)
{
printf("Starting tests...\n");
ringbuf_test();
bsock_test();
printf("Tests done!\n");
return 0;