allow no buffering write
This commit is contained in:
parent
2afb7741d0
commit
c80c0bb6db
18
bsock.c
18
bsock.c
|
@ -41,21 +41,33 @@ bsock_io_posix()
|
|||
struct bsock *
|
||||
bsock_create(void *io_ctx, struct bsock_ringbuf_io *io, size_t rbuf_sz, size_t wbuf_sz)
|
||||
{
|
||||
if (rbuf_sz == 0) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct bsock *bsock = malloc(sizeof(struct bsock));
|
||||
if (bsock == NULL) {
|
||||
errno = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct bsock_ringbuf rbuf = { 0 }, wbuf = { 0 };
|
||||
char *buf = malloc(rbuf_sz);
|
||||
if (buf == NULL) {
|
||||
errno = ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
bsock_ringbuf_init(&rbuf, buf, rbuf_sz);
|
||||
|
||||
buf = malloc(wbuf_sz);
|
||||
if (buf == NULL) {
|
||||
goto fail;
|
||||
if (wbuf_sz > 0) {
|
||||
buf = malloc(wbuf_sz);
|
||||
if (buf == NULL) {
|
||||
errno = ENOMEM;
|
||||
goto fail;
|
||||
}
|
||||
} else {
|
||||
buf = NULL;
|
||||
}
|
||||
bsock_ringbuf_init(&wbuf, buf, wbuf_sz);
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ bsock_ringbuf_write(struct bsock_ringbuf *rb, void *ctx, struct bsock_ringbuf_io
|
|||
return ret;
|
||||
}
|
||||
|
||||
assert(rb->sz == 0);
|
||||
free_sz = rb->max_sz - rb->sz;
|
||||
if (len > rb->max_sz) {
|
||||
return io->write(ctx, buf, len);
|
||||
}
|
||||
|
|
12
test.c
12
test.c
|
@ -132,13 +132,23 @@ bsock_test(void)
|
|||
memset(buf3, 0, 1024);
|
||||
bsock_ringbuf_init(&rbuf, buf, 768);
|
||||
assert_write(&rbuf, (void *)(uintptr_t)pipes[1], &io, sbuf, 512);
|
||||
assert(bsock_ringbuf_write(&rbuf, (void *)(uintptr_t)pipes[1], &io, sbuf, 1024));
|
||||
assert(bsock_ringbuf_write(&rbuf, (void *)(uintptr_t)pipes[1], &io, sbuf, 1024) == 1024);
|
||||
assert(read(pipes[0], buf3, 512) == 512);
|
||||
assert(memcmp(sbuf, buf3, 512) == 0);
|
||||
assert(read(pipes[0], buf3, 1024) == 1024);
|
||||
assert(memcmp(sbuf, buf3, 1024) == 0);
|
||||
assert(ringbuf_verify(&rbuf, buf + 512, 0));
|
||||
|
||||
|
||||
printf("ringbuf - write no buffering\n");
|
||||
assert(pipe(pipes) == 0);
|
||||
memset(buf3, 0, 1024);
|
||||
bsock_ringbuf_init(&rbuf, NULL, 0);
|
||||
assert(bsock_ringbuf_write(&rbuf, (void *)(uintptr_t)pipes[1], &io, sbuf, 1024) == 1024);
|
||||
assert(read(pipes[0], buf3, 1024) == 1024);
|
||||
assert(memcmp(sbuf, buf3, 1024) == 0);
|
||||
assert(ringbuf_verify(&rbuf, NULL, 0));
|
||||
|
||||
printf("ringbuf - poll/flush fp\n");
|
||||
assert(pipe(pipes) == 0);
|
||||
assert(write(pipes[0], sbuf, 512) == 512);
|
||||
|
|
Loading…
Reference in New Issue