diff --git a/include/neo.h b/include/neo.h index 8b70449..3ed314d 100644 --- a/include/neo.h +++ b/include/neo.h @@ -11,6 +11,7 @@ extern "C" { #endif #include "neo/_error.h" +#include "neo/_nbuf.h" #include "neo/_nref.h" #include "neo/_types.h" #include "neo/_stddef.h" diff --git a/include/neo/_nbuf.h b/include/neo/_nbuf.h new file mode 100644 index 0000000..a1994df --- /dev/null +++ b/include/neo/_nbuf.h @@ -0,0 +1,100 @@ +/** See the end of this file for copyright and license terms. */ + +#pragma once + +#include "neo/_toolchain.h" +#include "neo/_types.h" + +/** + * Create a new buffer of fixed size. + * + * If allocation fails or `size` is 0, an error is yeeted. + * + * @param size: Size in bytes + * @param err: Error pointer + * @returns The buffer, except if an error occurred + */ +nbuf_t *nbuf_create(usize size, error *err); + +/** + * Create a new buffer of fixed size and copy `data` into it. + * + * The original pointer is neither modified nor deallocated. + * If allocation fails, `data` is `nil`, or `size` is 0, an error is yeeted. + * + * @param data: Raw data to fill the buffer with + * @param size: How many bytes are read from `data`, and the buffer size + * @param err: Error pointer + * @returns The buffer, except if an error occurred + */ +nbuf_t *nbuf_from(const void *restrict data, usize size, error *err); + +/** + * Return a new copy of `buf`. + * + * If `buf` is `nil` or allocation fails, an error is yeeted. + * + * @param buf: Buffer to create a copy of + * @param err: Error pointer + * @returns A copy of `buf`, unless an error occurred + */ +nbuf_t *nbuf_clone(const nbuf_t *buf, error *err); + +/** + * Get the byte at the specified index. + * + * If `buf` is `nil` or `index` is out of bounds, an error is yeeted. + * + * @param buf: `nbuf_t *` to get the byte from + * @param index: Byte index (counting from 0) + * @param err: Error pointer + * @returns The byte at position `index` (as a `u8`), unless an error occurred + */ +#define nbuf_byte(buf, index, err) ({ \ + u8 __byte = 0; \ + if (nlen(buf) <= index) { \ + yeet(err, ERANGE, "Buffer index out of bounds"); \ + } else { \ + neat(err); \ + __byte = buf->_data[index]; \ + } \ + __byte; \ +}) + +#define nbuf_foreach(cursor, buf) \ + for (cursor = &buf->_data[0]; \ + cursor != &buf->_data[nlen(buf)]; \ + cursor++) + +/** + * Compare two buffers. + * + * If the first buffer is found to be greater than the second one, the return + * value is greater than 0. + * If the two buffers are equal, the return value is zero. + * If the first buffer is found to be less than the second one, the return + * value is less than 0. + * + * If `buf1` or `buf2` is `nil`, an error is yeeted. + * + * @param buf1: First buffer to compare + * @param buf2: Second buffer to compare + * @param err: Error pointer + * @returns The difference between the buffers, unless an error occurred + */ +int nbuf_cmp(const nbuf_t *buf1, const nbuf_t *buf2, error *err); + +#define nbuf_eq(buf1, buf2, err) ( (bool)(nbuf_cmp(buf1, buf2, err) == 0) ) + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * libneo is non-violent software: you may only use, redistribute, + * and/or modify it under the terms of the CNPLv6+ as found in + * the LICENSE file in the source code root directory or at + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/include/neo/_stddef.h b/include/neo/_stddef.h index db51a4a..0f0335b 100644 --- a/include/neo/_stddef.h +++ b/include/neo/_stddef.h @@ -25,6 +25,18 @@ __neo_local_n < 0 ? -__neo_local_n : __neo_local_n; \ }) +#define nmax(x1, x2) ({ \ + typeof(x1) __x1 = (x1); \ + typeof(x2) __x2 = (x2); \ + __x1 > __x2 ? __x1 : __x2; \ +}) + +#define nmin(x1, x2) ({ \ + typeof(x1) __x1 = (x1); \ + typeof(x2) __x2 = (x2); \ + __x1 < __x2 ? __x1 : x2; \ +}) + /** * Quickly get the length (as in amount of items, not bytes) of any libneo data * structure that supports it. This includes strings, buffers, lists, and more. diff --git a/include/neo/_types.h b/include/neo/_types.h index c4f445f..3a4898e 100644 --- a/include/neo/_types.h +++ b/include/neo/_types.h @@ -64,11 +64,15 @@ struct _neo_nref { typedef struct _neo_nref nref_t; #define NREF_FIELD nref_t __neo_nref -struct _neo_nmut { - __neo_atomic_type _lock; +struct _neo_nbuf { + NREF_FIELD; + NLEN_FIELD(_size); + u8 _data[0]; }; -typedef struct _neo_nmut nmut_t; -#define NLOCK_FIELD nmut_t __neo_nmut +/** + * A statically sized, refcounted buffer. + */ +typedef struct _neo_nbuf nbuf_t; struct _neo_string { /* The *amount of Unicode code points*, NOT amount of bytes */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6709102..991dd2b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,6 +27,7 @@ target_sources(neo PRIVATE ./error.c ./list.c ./nalloc.c + ./nbuf.c ./nref.c ) diff --git a/src/nbuf.c b/src/nbuf.c new file mode 100644 index 0000000..9dc7431 --- /dev/null +++ b/src/nbuf.c @@ -0,0 +1,108 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include +#include + +#include "neo/_error.h" +#include "neo/_nalloc.h" +#include "neo/_nref.h" +#include "neo/_stddef.h" +#include "neo/_types.h" + +static void nbuf_destroy(struct _neo_nbuf *buf) +{ + nfree(buf); +} + +nbuf_t *nbuf_create(usize size, error *err) +{ + if (size == 0) { + yeet(err, ERANGE, "Cannot create zero-size buffer"); + return nil; + } + + /* + * Just like with strings, we allocate 4 extra bytes. + * The buffer functions depend on this behavior, don't change. + */ + struct _neo_nbuf *buf = nalloc(sizeof(*buf) + size + 4, err); + catch(err) { + return nil; + } + + buf->_size = size; + + for (unsigned int i = 0; i < 4; i++) + buf->_data[size + i] = 0; + + nref_init(buf, nbuf_destroy); + neat(err); + return buf; +} + +nbuf_t *nbuf_from(const void *data, usize len, error *err) +{ + if (data == nil) { + yeet(err, EFAULT, "Data is nil"); + return nil; + } + + struct _neo_nbuf *buf = nbuf_create(len, err); + catch(err) { + return nil; + } + + memcpy(&buf->_data[0], data, len); + return buf; +} + +nbuf_t *nbuf_clone(const nbuf_t *buf, error *err) +{ + if (buf == nil) { + yeet(err, EFAULT, "Source buffer is nil"); + return nil; + } + + return nbuf_from(&buf->_data[0], nlen(buf), err); +} + +int nbuf_cmp(const nbuf_t *buf1, const nbuf_t *buf2, error *err) +{ + if (buf1 == nil) { + yeet(err, EFAULT, "First buffer is nil"); + if (buf2 == nil) + return 0; + else + return -1; + } + if (buf2 == nil) { + yeet(err, EFAULT, "Second buffer is nil"); + return 1; + } + + neat(err); + + if (buf1 == buf2) + return 0; + + /* + * Extra byte because the two buffers might be of different size + * (one of the reasons nbuf_create() allocates 4 bytes more than needed) + */ + usize maxbytes = nmin(nlen(buf1), nlen(buf2)) + 1; + return memcmp(&buf1->_data[0], &buf2->_data[0], maxbytes); +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * libneo is non-violent software: you may only use, redistribute, + * and/or modify it under the terms of the CNPLv6+ as found in + * the LICENSE file in the source code root directory or at + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b02e38d..e15e32e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -20,6 +20,7 @@ include(string/string.cmake) target_sources(neo_test PRIVATE list.cpp + nbuf.cpp nref.cpp ) diff --git a/test/nbuf.cpp b/test/nbuf.cpp new file mode 100644 index 0000000..fd44cf7 --- /dev/null +++ b/test/nbuf.cpp @@ -0,0 +1,229 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include +#include + +#include + +TEST_CASE( "nbuf_create: Create regular buffer", "[src/nbuf.c]" ) +{ + error err; + nbuf_t *buf = nbuf_create(32, &err); + + REQUIRE( buf != nil ); + REQUIRE( nlen(buf) == 32 ); + REQUIRE( errnum(&err) == 0 ); +} + +TEST_CASE( "nbuf_create: Error if size is 0", "[src/nbuf.c]" ) +{ + error err; + nbuf_t *buf = nbuf_create(0, &err); + string *expected_msg = nstr("Cannot create zero-size buffer", nil); + + REQUIRE( buf == nil ); + REQUIRE( errnum(&err) == ERANGE ); + REQUIRE( nstreq(errmsg(&err), expected_msg, nil) ); + errput(&err); + nput(expected_msg); +} + +TEST_CASE( "nbuf_from: Copy data", "[src/nbuf.c]" ) +{ + error err; + const char *data = "i'm gay,,,"; + usize size = strlen(data); + nbuf_t *buf = nbuf_from(data, size, &err); + + REQUIRE( buf != nil ); + REQUIRE( nlen(buf) == size ); + REQUIRE( errnum(&err) == 0 ); + REQUIRE( memcmp(data, &buf->_data[0], size) == 0 ); + + nput(buf); +} + +TEST_CASE( "nbuf_from: Error if data is nil", "[src/nbuf.c]" ) +{ + error err; + nbuf_t *buf = nbuf_from(nil, 1, &err); + + string *expected_msg = nstr("Data is nil", nil); + + REQUIRE( buf == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + errput(&err); + nput(expected_msg); +} + +TEST_CASE( "nbuf_from: Error if size is 0", "[src/nbuf.c]" ) +{ + error err; + const char *data = "i'm gay,,,"; + nbuf_t *buf = nbuf_from(data, 0, &err); + + string *expected_msg = nstr("Cannot create zero-size buffer", nil); + + REQUIRE( buf == nil ); + REQUIRE( errnum(&err) == ERANGE ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + errput(&err); + nput(expected_msg); +} + +TEST_CASE( "nbuf_clone: Copy data", "[src/nbuf.c]" ) +{ + error err; + const char *data = "i'm gay,,,"; + usize size = strlen(data); + + nbuf_t *original = nbuf_from(data, size, nil); + nbuf_t *clone = nbuf_clone(original, &err); + + REQUIRE( clone != nil ); + REQUIRE( nlen(clone) == nlen(original) ); + REQUIRE( errnum(&err) == 0 ); + REQUIRE( memcmp(data, &clone->_data[0], size) == 0 ); + + nput(original); + nput(clone); +} + +TEST_CASE( "nbuf_clone: Error if original buffer is nil", "[src/nbuf.c]" ) +{ + error err; + nbuf_t *buf = nbuf_clone(nil, &err); + + string *expected_msg = nstr("Source buffer is nil", nil); + + REQUIRE( buf == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + errput(&err); + nput(expected_msg); +} + +TEST_CASE( "nbuf_cmp: Return 0 if buffers are equal", "[src/nbuf.c]" ) +{ + error err; + const char *data = "aaaaa"; + usize size = strlen(data); + + nbuf_t *original = nbuf_from(data, size, nil); + nbuf_t *clone = nbuf_clone(original, nil); + + int diff = nbuf_cmp(original, clone, &err); + + REQUIRE( diff == 0 ); + REQUIRE( errnum(&err) == 0 ); + + nput(original); + nput(clone); +} + +TEST_CASE( "nbuf_cmp: Return positive if buffer 1 > buffer 2", "[src/nbuf.c]" ) +{ + error err; + const char *data = "aaaaa"; + usize size = strlen(data); + + nbuf_t *buf1 = nbuf_from(data, size, nil); + nbuf_t *buf2 = nbuf_from(data, size - 1, nil); + + int diff = nbuf_cmp(buf1, buf2, &err); + + REQUIRE( diff > 0 ); + REQUIRE( errnum(&err) == 0 ); + + nput(buf1); + nput(buf2); +} + +TEST_CASE( "nbuf_cmp: Return negative if buffer 1 < buffer 2", "[src/nbuf.c]" ) +{ + error err; + const char *data = "aaaaa"; + usize size = strlen(data); + + nbuf_t *buf1 = nbuf_from(data, size - 1, nil); + nbuf_t *buf2 = nbuf_from(data, size, nil); + + int diff = nbuf_cmp(buf1, buf2, &err); + + REQUIRE( diff < 0 ); + REQUIRE( errnum(&err) == 0 ); + + nput(buf1); + nput(buf2); +} + +TEST_CASE( "nbuf_cmp: Error if first buffer is nil", "[src/nbuf.c]" ) +{ + error err; + const char *data = "aaaaa"; + usize size = strlen(data); + + nbuf_t *buf2 = nbuf_from(data, size, nil); + int diff = nbuf_cmp(nil, buf2, &err); + + string *expected_msg = nstr("First buffer is nil", nil); + + REQUIRE( diff < 0 ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + errput(&err); + nput(buf2); +} + +TEST_CASE( "nbuf_cmp: Error if second buffer is nil", "[src/nbuf.c]" ) +{ + error err; + const char *data = "aaaaa"; + usize size = strlen(data); + + nbuf_t *buf1 = nbuf_from(data, size, nil); + int diff = nbuf_cmp(buf1, nil, &err); + + string *expected_msg = nstr("Second buffer is nil", nil); + + REQUIRE( diff > 0 ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + errput(&err); + nput(buf1); +} + +TEST_CASE( "nbuf_cmp: Error if both buffers are nil", "[src/nbuf.c]" ) +{ + error err; + + int diff = nbuf_cmp(nil, nil, &err); + + string *expected_msg = nstr("First buffer is nil", nil); + + REQUIRE( diff == 0 ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + errput(&err); +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * libneo is non-violent software: you may only use, redistribute, + * and/or modify it under the terms of the CNPLv6+ as found in + * the LICENSE file in the source code root directory or at + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */