From b0969d077818a74f2fd26b5a7bec2263a4517b07 Mon Sep 17 00:00:00 2001 From: Richard Braun Date: Fri, 31 Aug 2018 01:15:43 +0200 Subject: mbuf: new module --- Makefile.am | 6 ++ src/mbuf.c | 165 +++++++++++++++++++++++++++++++++++++++++ src/mbuf.h | 87 ++++++++++++++++++++++ test/test_mbuf.c | 222 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 480 insertions(+) create mode 100644 src/mbuf.c create mode 100644 src/mbuf.h create mode 100644 test/test_mbuf.c diff --git a/Makefile.am b/Makefile.am index aca192c..345d1fa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -34,6 +34,8 @@ librbraun_la_SOURCES = \ src/list.c \ src/list.h \ src/macros.h \ + src/mbuf.c \ + src/mbuf.h \ src/plist.c \ src/plist.h \ src/rbtree.c \ @@ -54,6 +56,7 @@ bin_PROGRAMS = \ test_fmt_sprintf \ test_fmt_sscanf \ test_hlist \ + test_mbuf \ test_plist \ test_rbtree \ test_rdxtree \ @@ -75,6 +78,9 @@ test_fmt_sscanf_LDADD = librbraun.la test_hlist_SOURCES = test/test_hlist.c test_hlist_LDADD = librbraun.la +test_mbuf_SOURCES = test/test_mbuf.c +test_mbuf_LDADD = librbraun.la + test_plist_SOURCES = test/test_plist.c test_plist_LDADD = librbraun.la diff --git a/src/mbuf.c b/src/mbuf.c new file mode 100644 index 0000000..27d6221 --- /dev/null +++ b/src/mbuf.c @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2018 Richard Braun. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + */ + +#include +#include +#include +#include +#include + +#include "mbuf.h" + +/* + * Message header. + * + * The size denotes the size of the data, without the header. + */ +struct mbuf_hdr { + uint32_t size; + char data[]; +}; + +static int +mbuf_hdr_init(struct mbuf_hdr *hdr, size_t size) +{ + hdr->size = size; + + if (hdr->size != size) { + return EMSGSIZE; + } + + return 0; +} + +static size_t +mbuf_hdr_total_size(const struct mbuf_hdr *hdr) +{ + return sizeof(*hdr) + hdr->size; +} + +static size_t +mbuf_hdr_msg_size(const struct mbuf_hdr *hdr) +{ + return hdr->size; +} + +void +mbuf_init(struct mbuf *mbuf, void *buf, size_t capacity) +{ + cbuf_init(&mbuf->cbuf, buf, capacity); +} + +void +mbuf_clear(struct mbuf *mbuf) +{ + return cbuf_clear(&mbuf->cbuf); +} + +static void +mbuf_clear_old_msgs(struct mbuf *mbuf, size_t total_size) +{ + struct mbuf_hdr hdr; + size_t size; + int error; + + do { + size = sizeof(hdr); + error = cbuf_pop(&mbuf->cbuf, &hdr, &size); + assert(!error); + + if (size == 0) { + break; + } + + size = mbuf_hdr_msg_size(&hdr); + error = cbuf_pop(&mbuf->cbuf, NULL, &size); + assert(!error && (size == mbuf_hdr_msg_size(&hdr))); + } while (cbuf_avail_size(&mbuf->cbuf) < total_size); +} + +int +mbuf_push(struct mbuf *mbuf, const void *buf, size_t size, bool erase) +{ + struct mbuf_hdr hdr; + size_t total_size; + int error; + + error = mbuf_hdr_init(&hdr, size); + + if (error) { + return error; + } + + total_size = mbuf_hdr_total_size(&hdr); + + if (total_size > cbuf_avail_size(&mbuf->cbuf)) { + if (!erase || (total_size > cbuf_capacity(&mbuf->cbuf))) { + return EMSGSIZE; + } + + mbuf_clear_old_msgs(mbuf, total_size); + } + + error = cbuf_push(&mbuf->cbuf, &hdr, sizeof(hdr), erase); + assert(!error); + error = cbuf_push(&mbuf->cbuf, buf, size, erase); + assert(!error); + + return 0; +} + +int +mbuf_pop(struct mbuf *mbuf, void *buf, size_t *sizep) +{ + struct mbuf_hdr hdr; + size_t start, size; + int error; + + start = cbuf_start(&mbuf->cbuf); + + size = sizeof(hdr); + error = cbuf_read(&mbuf->cbuf, start, &hdr, &size); + assert(!error); + + if (size == 0) { + return EAGAIN; + } + + assert(size == sizeof(hdr)); + size = mbuf_hdr_msg_size(&hdr); + + if (size > *sizep) { + error = EMSGSIZE; + goto out; + } + + cbuf_set_start(&mbuf->cbuf, start + sizeof(hdr)); + error = cbuf_pop(&mbuf->cbuf, buf, &size); + assert(!error && (size == mbuf_hdr_msg_size(&hdr))); + +out: + *sizep = size; + return error; +} diff --git a/src/mbuf.h b/src/mbuf.h new file mode 100644 index 0000000..f1464c7 --- /dev/null +++ b/src/mbuf.h @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2018 Richard Braun. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + * + * + * FIFO message buffer. + */ + +#ifndef MBUF_H +#define MBUF_H + +#include +#include + +#include "cbuf.h" + +/* + * Message buffer. + * + * Message buffers are built on top of circular byte buffers. They provide + * discrete message transfer from a producer to a consumer. + */ +struct mbuf { + struct cbuf cbuf; +}; + +/* + * Initialize a message buffer. + * + * The descriptor is set to use the given buffer for storage. Capacity + * must be a power-of-two. + */ +void mbuf_init(struct mbuf *mbuf, void *buf, size_t capacity); + +/* + * Clear a message buffer. + */ +void mbuf_clear(struct mbuf *mbuf); + +/* + * Push a message to a message buffer. + * + * If the message doesn't fit in the message buffer, either because it is + * larger than the capacity, or because the function isn't allowed to erase + * old messages and the message buffer doesn't have enough available memory + * for the new message, EMSGSIZE is returned. + */ +int mbuf_push(struct mbuf *mbuf, const void *buf, size_t size, bool erase); + +/* + * Pop a message from a message buffer. + * + * On entry, the sizep argument points to the size of the output buffer. + * On return, it is updated to the size of the message. If the message + * doesn't fit in the output buffer, it is not popped, EMSGSIZE is + * returned, but the sizep argument is updated nonetheless to let the + * user know the message size, to potentially retry with a larger buffer. + * + * If the buffer is empty, EAGAIN is returned, and the size of the output + * buffer is unmodified. + * + * The output buffer may be NULL, in which case this function acts as if + * it wasn't, but without writing output data. + */ +int mbuf_pop(struct mbuf *mbuf, void *buf, size_t *sizep); + +#endif /* MBUF_H */ diff --git a/test/test_mbuf.c b/test/test_mbuf.c new file mode 100644 index 0000000..6b3f90f --- /dev/null +++ b/test/test_mbuf.c @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2018 Richard Braun. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +static void +test_regular(void) +{ + char mbuf_buf[32], buffer[8]; + struct mbuf mbuf; + size_t size; + int error; + + mbuf_init(&mbuf, mbuf_buf, sizeof(mbuf_buf)); + +#define STRING "abcdef" + error = mbuf_push(&mbuf, STRING, STRLEN(STRING) + 1, false); + check(!error); +#undef STRING + +#define STRING "xyz" + error = mbuf_push(&mbuf, STRING, STRLEN(STRING) + 1, false); + check(!error); +#undef STRING + +#define STRING "abcdef" + size = sizeof(buffer); + error = mbuf_pop(&mbuf, buffer, &size); + check(!error); + check(size == (STRLEN(STRING) + 1)); +#undef STRING + +#define STRING "xyz" + size = sizeof(buffer); + error = mbuf_pop(&mbuf, buffer, &size); + check(!error); + check(size == (STRLEN(STRING) + 1)); +#undef STRING + + size = sizeof(buffer); + error = mbuf_pop(&mbuf, buffer, &size); + check(error == EAGAIN); +} + +static void +test_write_full(void) +{ + char mbuf_buf[16], buffer[8]; + struct mbuf mbuf; + size_t size; + int error; + + mbuf_init(&mbuf, mbuf_buf, sizeof(mbuf_buf)); + +#define STRING "abcdef" + error = mbuf_push(&mbuf, STRING, STRLEN(STRING) + 1, false); + check(!error); +#undef STRING + +#define STRING "xyz" + error = mbuf_push(&mbuf, STRING, STRLEN(STRING) + 1, false); + check(error == EMSGSIZE); +#undef STRING + +#define STRING "abcdef" + size = sizeof(buffer); + error = mbuf_pop(&mbuf, buffer, &size); + check(!error); + check(size == (STRLEN(STRING) + 1)); +#undef STRING + + size = sizeof(buffer); + error = mbuf_pop(&mbuf, buffer, &size); + check(error == EAGAIN); +} + +static void +test_overwrite(void) +{ + char mbuf_buf[16], buffer[8]; + struct mbuf mbuf; + size_t size; + int error; + + mbuf_init(&mbuf, mbuf_buf, sizeof(mbuf_buf)); + +#define STRING "abcdef" + error = mbuf_push(&mbuf, STRING, STRLEN(STRING) + 1, true); + check(!error); +#undef STRING + +#define STRING "xyz" + error = mbuf_push(&mbuf, STRING, STRLEN(STRING) + 1, true); + check(!error); +#undef STRING + +#define STRING "xyz" + size = sizeof(buffer); + error = mbuf_pop(&mbuf, buffer, &size); + check(!error); + check(size == (STRLEN(STRING) + 1)); +#undef STRING + + size = sizeof(buffer); + error = mbuf_pop(&mbuf, buffer, &size); + check(error == EAGAIN); +} + +static void +test_msg_too_big(void) +{ + char mbuf_buf[8], buffer[8]; + struct mbuf mbuf; + size_t size; + int error; + + mbuf_init(&mbuf, mbuf_buf, sizeof(mbuf_buf)); + +#define STRING "abcdef" + error = mbuf_push(&mbuf, STRING, STRLEN(STRING) + 1, true); + check(error == EMSGSIZE); +#undef STRING + + size = sizeof(buffer); + error = mbuf_pop(&mbuf, buffer, &size); + check(error == EAGAIN); +} + +static void +test_msg_far_too_big(void) +{ + char mbuf_buf[8], buffer[8]; + struct mbuf mbuf; + size_t size; + int error; + + if (sizeof(size_t) <= sizeof(uint32_t)) { + fprintf(stderr, "warning: %s disabled\n", __func__); + return; + } + + mbuf_init(&mbuf, mbuf_buf, (size_t)1 << 36); + +#define STRING "abcdef" + size = (size_t)1 << 32; + error = mbuf_push(&mbuf, STRING, size, true); + check(error == EMSGSIZE); +#undef STRING + + size = sizeof(buffer); + error = mbuf_pop(&mbuf, buffer, &size); + check(error == EAGAIN); +} + +static void +test_peak(void) +{ + char mbuf_buf[16], buffer[8]; + struct mbuf mbuf; + size_t size; + int error; + + mbuf_init(&mbuf, mbuf_buf, sizeof(mbuf_buf)); + +#define STRING "abcdef" + error = mbuf_push(&mbuf, STRING, STRLEN(STRING) + 1, true); + check(!error); +#undef STRING + +#define STRING "abcdef" + size = 0; + error = mbuf_pop(&mbuf, NULL, &size); + check((error == EMSGSIZE) && (size == (STRLEN(STRING) + 1))); + error = mbuf_pop(&mbuf, buffer, &size); + check(!error && (size == (STRLEN(STRING) + 1))); +#undef STRING + + size = sizeof(buffer); + error = mbuf_pop(&mbuf, buffer, &size); + check(error == EAGAIN); +} + +int +main(void) +{ + test_regular(); + test_write_full(); + test_overwrite(); + test_msg_too_big(); + test_msg_far_too_big(); + test_peak(); + + return EXIT_SUCCESS; +} -- cgit v1.2.3