Compare commits

...

3 Commits

@ -29,6 +29,30 @@ nbuf_t *nbuf_create(usize size, error *err);
*/
nbuf_t *nbuf_from(const void *restrict data, usize size, error *err);
/**
* Create a new buffer of fixed size and copy a string into it.
*
* The original string is neither modified not deallocated.
* If allocation fails or `s` is `nil`, an error is yeeted.
*
* @param s: String to fill the buffer with
* @param err: Error pointer
* @returns The buffer, except if an error occurred
*/
nbuf_t *nbuf_from_str(const char *restrict s, error *err);
/**
* Create a new buffer of fixed size and copy a neo string into it.
*
* The original string is unmodified.
* If allocation fails or `s` is `nil`, an error is yeeted.
*
* @param s: Neo string to fill the buffer with
* @param err: Error pointer
* @returns The buffer, except if an error occurred
*/
nbuf_t *nbuf_from_nstr(nstr_t *s, error *err);
/**
* Return a new copy of `buf`.
*
@ -38,7 +62,7 @@ nbuf_t *nbuf_from(const void *restrict data, usize size, error *err);
* @param err: Error pointer
* @returns A copy of `buf`, unless an error occurred
*/
nbuf_t *nbuf_clone(const nbuf_t *buf, error *err);
nbuf_t *nbuf_clone(nbuf_t *buf, error *err);
/**
* Get the byte at the specified index.

@ -10,6 +10,9 @@
* the `NSTR_DEFINE` macro. This is written to a special data section that
* is iterated over before program start; see `_neo_nstr_init_array` in
* `src/string/nstr.c` for details.
*
* Changing this might require also changing the linker script because of
* alignment requirements.
*/
struct _neo_nstr_init_info {
nstr_t **dest;
@ -106,7 +109,7 @@ nstr_t *u2nstr(u64 u, int radix, error *err);
* @param err: Error pointer
* @returns The duplicated string, unless an error occurred
*/
nstr_t *nstrdup(const nstr_t *s, error *err);
nstr_t *nstrdup(nstr_t *s, error *err);
/**
* Repeat a string `n` times and return the new string.
@ -120,7 +123,7 @@ nstr_t *nstrdup(const nstr_t *s, error *err);
* @param err: Error pointer
* @returns A new string with the repeated content, unless an error occurred
*/
nstr_t *nstrmul(const nstr_t *s, usize n, error *err);
nstr_t *nstrmul(nstr_t *s, usize n, error *err);
/**
* Create a string considting of `n` repetitions of `c`.
@ -185,7 +188,7 @@ int nstrcmp(const nstr_t *s1, const nstr_t *s2, error *err);
* @param err: Error pointer
* @returns The new padded string
*/
nstr_t *leftpad(const nstr_t *s, usize length, nchar fill, error *err);
nstr_t *leftpad(nstr_t *s, usize length, nchar fill, error *err);
/**
* Iterate over each character in a string.
@ -202,6 +205,9 @@ nstr_t *leftpad(const nstr_t *s, usize length, nchar fill, error *err);
*__pos != '\0' && (err) != nil && (err)->_number + 1 < 2; \
__pos += utf8_to_nchr(cursor, __pos, err))
/** Internal callback for nref */
void _neo_nstr_destroy(nstr_t *s);
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.

@ -67,7 +67,12 @@ typedef struct _neo_nref nref_t;
struct _neo_nbuf {
NREF_FIELD;
NLEN_FIELD(_size);
u8 _data[0];
/**
* If this buffer was cloned or converted from a string, this points to
* the original structure's refcounter. Otherwise, it is nil.
*/
nref_t *_borrow;
const byte *_data;
};
/**
* A statically sized, refcounted buffer.
@ -80,7 +85,12 @@ struct _neo_nstr {
NREF_FIELD;
/* physical size in bytes, including the four NUL terminators */
usize _size;
char *_data;
/**
* If this string was cloned or converted from a buffer, this points to
* the original structure's refcounter. Otherwise, it is nil.
*/
nref_t *_borrow;
const char *_data;
};
typedef struct _neo_nstr nstr_t;

@ -12,6 +12,9 @@
static void nbuf_destroy(struct _neo_nbuf *buf)
{
if (buf->_borrow != nil)
_neo_nput(buf->_borrow);
nfree(buf);
}
@ -31,11 +34,13 @@ nbuf_t *nbuf_create(usize size, error *err)
return nil;
}
buf->_size = size;
byte *data = (byte *)buf + sizeof(*buf);
for (unsigned int i = 0; i < 4; i++)
buf->_data[size + i] = 0;
data[size + i] = 0;
buf->_size = size;
buf->_data = data;
buf->_borrow = nil;
nref_init(buf, nbuf_destroy);
neat(err);
return buf;
@ -53,18 +58,65 @@ nbuf_t *nbuf_from(const void *data, usize len, error *err)
return nil;
}
memcpy(&buf->_data[0], data, len);
byte *buf_data = (byte *)buf + sizeof(*buf);
memcpy(buf_data, data, len);
/* _data field already filled by nbuf_create */
return buf;
}
nbuf_t *nbuf_from_str(const char *s, error *err)
{
if (s == nil) {
yeet(err, EFAULT, "String is nil");
return nil;
}
usize size = strlen(s) + 1;
return nbuf_from(s, size, err);
}
nbuf_t *nbuf_from_nstr(nstr_t *s, error *err)
{
if (s == nil) {
yeet(err, EFAULT, "String is nil");
return nil;
}
nbuf_t *buf = nalloc(sizeof(*buf), err);
catch(err) {
return nil;
}
nget(s);
buf->_size = s->_size;
buf->_borrow = &s->__neo_nref;
buf->_data = (const byte *)s->_data;
nref_init(buf, nbuf_destroy);
return buf;
}
nbuf_t *nbuf_clone(const nbuf_t *buf, error *err)
nbuf_t *nbuf_clone(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);
nbuf_t *clone = nalloc(sizeof(*clone), err);
catch(err) {
return nil;
}
nget(buf);
clone->_size = buf->_size;
clone->_borrow = &buf->__neo_nref;
clone->_data = buf->_data;
nref_init(clone, nbuf_destroy);
return clone;
}
int nbuf_cmp(const nbuf_t *buf1, const nbuf_t *buf2, error *err)
@ -83,7 +135,7 @@ int nbuf_cmp(const nbuf_t *buf1, const nbuf_t *buf2, error *err)
neat(err);
if (buf1 == buf2)
if (buf1->_data == buf2->_data)
return 0;
/*

@ -16,9 +16,10 @@ SECTIONS
* referenced in this section because their length cannot be
* calculated at compile time (because UTF-8).
*
* See nstr_init_array() in src/string/nstr.c for the
* See _neo_nstr_init_array() in src/string/nstr.c for the
* initialization routine.
*/
. = ALIGN(16); /* sizeof(struct _neo_nstr_init_info) @ 64 bit */
__neo_nstr_array_start = .;
KEEP(*(.data.__neo.nstr_array))
__neo_nstr_array_end = .;

@ -47,7 +47,7 @@ static inline nstr_t *leftpad_unsafe(const nstr_t *s, usize len, nchar fillchr,
return padded;
}
nstr_t *leftpad(const nstr_t *s, usize len, nchar fillchr, error *err)
nstr_t *leftpad(nstr_t *s, usize len, nchar fillchr, error *err)
{
if (s == nil) {
yeet(err, EFAULT, "String is nil");

@ -14,12 +14,14 @@
#include "neo/_types.h"
#include "neo/utf.h"
static void nstr_destroy(nstr_t *str)
void _neo_nstr_destroy(nstr_t *str)
{
if (str->_borrow != nil)
_neo_nput(str->_borrow);
nfree(str);
}
static nstr_t *nstr_unsafe(const char *restrict s, usize size_without_nul, error *err)
static nstr_t *nstr_unsafe(const char *s, usize size_without_nul, error *err)
{
usize len = utf8_ncheck(s, size_without_nul, err);
catch(err) {
@ -49,16 +51,16 @@ static nstr_t *nstr_unsafe(const char *restrict s, usize size_without_nul, error
* stored immediately after the string itself. This also saves us an
* additional memory allocation.
*/
str->_data = (char *)str + sizeof(*str);
char *data = (char *)str + sizeof(*str);
memcpy(data, s, size_without_nul);
for (unsigned int i = 0; i < 4; i++)
data[size_without_nul + i] = '\0';
str->_data = data;
str->_len = len;
str->_borrow = nil;
str->_size = size_without_nul + 4;
nref_init(str, nstr_destroy);
memcpy(str->_data, s, size_without_nul);
str->_data[size_without_nul] = '\0';
str->_data[size_without_nul + 1] = '\0';
str->_data[size_without_nul + 2] = '\0';
str->_data[size_without_nul + 3] = '\0';
nref_init(str, _neo_nstr_destroy);
return str;
}
@ -97,7 +99,7 @@ nchar nchrat(const nstr_t *s, usize index, error *err)
return '\0';
}
char *ptr = s->_data;
const char *ptr = s->_data;
while (index != 0)
index -= (*ptr++ & 0xc0) != 0x80;
ptr--;

@ -3,17 +3,31 @@
#include <errno.h>
#include "neo/_error.h"
#include "neo/_nalloc.h"
#include "neo/_nref.h"
#include "neo/_nstr.h"
#include "neo/_types.h"
nstr_t *nstrdup(const nstr_t *s, error *err)
nstr_t *nstrdup(nstr_t *s, error *err)
{
if (s == nil) {
yeet(err, EFAULT, "String is nil");
return nil;
}
return nstr(s->_data, err);
nstr_t *copy = nalloc(sizeof(*copy), err);
catch(err) {
return nil;
}
nget(s);
copy->_len = s->_len;
copy->_size = s->_size;
copy->_borrow = &s->__neo_nref;
copy->_data = s->_data;
nref_init(copy, _neo_nstr_destroy);
return copy;
}
/*

@ -11,7 +11,7 @@
#include "neo/_types.h"
#include "neo/utf.h"
nstr_t *nstrmul(const nstr_t *s, usize n, error *err)
nstr_t *nstrmul(nstr_t *s, usize n, error *err)
{
if (s == nil) {
yeet(err, EFAULT, "String is nil");

@ -16,11 +16,11 @@ list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/contrib)
include(Catch)
add_executable(neo_test neo_test.cpp)
include(nbuf/nbuf.cmake)
include(string/string.cmake)
target_sources(neo_test PRIVATE
list.cpp
nbuf.cpp
nref.cpp
)

@ -0,0 +1,21 @@
# See the end of this file for copyright and license terms.
target_sources(neo_test PRIVATE
nbuf/nbuf_clone.cpp
nbuf/nbuf_cmp.cpp
nbuf/nbuf_create.cpp
nbuf/nbuf_from.cpp
nbuf/nbuf_from_nstr.cpp
nbuf/nbuf_from_str.cpp
)
# This file is part of libneo.
# Copyright (c) 2021 Fefie <owo@fef.moe>.
#
# 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
# <https://git.pixie.town/thufie/CNPL>.
#
# libneo comes with ABSOLUTELY NO WARRANTY, to the extent
# permitted by applicable law. See the CNPLv6+ for details.

@ -0,0 +1,78 @@
/** See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
#include <string.h>
#include <neo.h>
SCENARIO( "nbuf_clone: Zero-copy clone", "[src/nbuf.c]" )
{
const char *data = "i'm gay,,,";
usize size = strlen(data);
GIVEN( "a populated buffer" )
{
nbuf_t *original = nbuf_from(data, size, nil);
WHEN( "the buffer is cloned" )
{
error err;
nbuf_t *clone = nbuf_clone(original, &err);
REQUIRE( clone != nil );
REQUIRE( errnum(&err) == 0 );
THEN( "the clone's data pointer is equal to the original" )
{
REQUIRE( clone->_data == original->_data );
}
WHEN( "the original buffer is put" )
{
nput(original);
THEN( "the original is not deallocated" )
{
REQUIRE( original != nil );
}
}
WHEN( "the cloned buffer is put" )
{
nput(clone);
THEN( "the original is not deallocated" )
{
REQUIRE( clone == nil );
REQUIRE( original->__neo_nref._count == 1 );
}
}
}
}
}
TEST_CASE( "nbuf_clone: Error if original buffer is nil", "[src/nbuf.c]" )
{
error err;
nbuf_t *buf = nbuf_clone(nil, &err);
nstr_t *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);
}
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.
*
* 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
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

@ -6,108 +6,6 @@
#include <neo.h>
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);
nstr_t *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);
nstr_t *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);
nstr_t *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);
nstr_t *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;

@ -0,0 +1,43 @@
/** See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
#include <string.h>
#include <neo.h>
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);
nstr_t *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);
}
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.
*
* 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
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

@ -0,0 +1,66 @@
/** See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
#include <string.h>
#include <neo.h>
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);
nstr_t *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);
nstr_t *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);
}
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.
*
* 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
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

@ -0,0 +1,78 @@
/** See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
#include <string.h>
#include <neo.h>
SCENARIO( "nbuf_from_nstr: Zero-copy from neo string", "[src/nbuf.c]" )
{
const char *data = "i'm gay,,,";
usize size = strlen(data);
GIVEN( "a neo string" )
{
nbuf_t *original = nbuf_from(data, size, nil);
WHEN( "a buffer is created from it" )
{
error err;
nbuf_t *clone = nbuf_clone(original, &err);
REQUIRE( clone != nil );
REQUIRE( errnum(&err) == 0 );
THEN( "the buffer's data pointer is equal to the string's" )
{
REQUIRE( clone->_data == original->_data );
}
WHEN( "the string is put" )
{
nput(original);
THEN( "the original is not deallocated" )
{
REQUIRE( original != nil );
}
}
WHEN( "the buffer is put" )
{
nput(clone);
THEN( "the string is not deallocated" )
{
REQUIRE( clone == nil );
REQUIRE( original->__neo_nref._count == 1 );
}
}
}
}
}
TEST_CASE( "nbuf_from_nstr: Error if string is nil", "[src/nbuf.c]" )
{
error err;
nbuf_t *buf = nbuf_from_nstr(nil, &err);
nstr_t *expected_msg = nstr("String is nil", nil);
REQUIRE( buf == nil );
REQUIRE( errnum(&err) == EFAULT );
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
errput(&err);
nput(expected_msg);
}
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.
*
* 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
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

@ -0,0 +1,50 @@
/** See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
#include <string.h>
#include <neo.h>
TEST_CASE( "nbuf_from_str: Copy data", "[src/nbuf.c]" )
{
error err;
const char *s = "i'm gay,,,";
usize size = strlen(s) + 1;
nbuf_t *buf = nbuf_from_str(s, &err);
REQUIRE( buf != nil );
REQUIRE( nlen(buf) == size );
REQUIRE( errnum(&err) == 0 );
REQUIRE( memcmp(s, &buf->_data[0], size) == 0 );
nput(buf);
}
TEST_CASE( "nbuf_from_str: Error if string is nil", "[src/nbuf.c]" )
{
error err;
nbuf_t *buf = nbuf_from_str(nil, &err);
nstr_t *expected_msg = nstr("String is nil", nil);
REQUIRE( buf == nil );
REQUIRE( errnum(&err) == EFAULT );
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
errput(&err);
nput(expected_msg);
}
/*
* This file is part of libneo.
* Copyright (c) 2021 Fefie <owo@fef.moe>.
*
* 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
* <https://git.pixie.town/thufie/CNPL>.
*
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/
Loading…
Cancel
Save