add refcounts

main
anna 3 years ago
parent 7eed96edcf
commit 957f5e2a35
Signed by: fef
GPG Key ID: EC22E476DC2D3D84

@ -0,0 +1,31 @@
/** See the end of this file for copyright and license terms. */
#pragma once
#include "neo/_types.h"
#define nref_init(ptr, destroy) ({ \
(ptr)->__neo_nref._offset = offsetof(typeof(ptr), __neo_nref); \
void (*__destroy_typechecked)(typeof ptr) = destroy; \
(ptr)->__neo_nref._destroy = (void (*)(void *))__destroy_typechecked; \
(ptr)->__neo_nref._count = 1 \
})
int _neo_nget(struct _neo_nref *ref);
int _neo_nput(struct _neo_nref *ref);
#define nget(thing) (_neo_nget( &(thing)->__neo_nref ))
#define nput(thing) (_neo_nput( &(thing)->__neo_nref ))
/*
* 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,7 +6,34 @@
#define nil ((void *)0)
#ifndef true
#define true ((bool)1)
#endif
#ifndef false
#define false ((bool)0)
#endif
#ifndef offsetof
#define offsetof(type, member) __builtin_offsetof(type, member)
#endif
/**
* Declare a length field in a structure.
* This makes it compatible with the `nlen` macro.
*
* @param name: field name, will be of type `usize`
*/
#define NLEN_FIELD(name) \
union { \
usize name; \
const usize __neo_nlen; \
}
/**
* Quickly get the length (as a `const usize`) of any libneo data structure
* that supports it. This includes strings, buffers, lists, and more.
*/
#define nlen(thing) ((thing)->__neo_nlen)
/*
* This file is part of libneo.

@ -2,6 +2,8 @@
#pragma once
#include "neo/_stddef.h"
typedef __INT8_TYPE__ i8;
typedef __INT16_TYPE__ i16;
typedef __INT32_TYPE__ i32;
@ -22,16 +24,34 @@ typedef long double f128;
typedef _Bool bool;
#define atomic _Atomic
#define complex _Complex
struct _neo_nref {
void (*_destroy)(void *);
/** byte offset into the struct this is embedded in */
usize _offset;
atomic int _count;
};
/**
* A basic reference counter for data structures.
* Embed this into your data structure as the field `__neo_nref` and
*/
typedef struct _neo_nref nref_t;
#define NREF_FIELD nref_t __neo_nref
struct _neo_string {
usize _len;
NLEN_FIELD(_len);
NREF_FIELD;
usize _capacity;
char *_data;
};
typedef struct _neo_string string;
struct _neo_error {
int _number;
string *_error;
string *_message;
u32 _number;
u32 _flags; /* see src/error.c */
};
typedef struct _neo_error *error;

@ -25,7 +25,7 @@ target_include_directories(neo PRIVATE
)
target_sources(neo PRIVATE
./entry.c
./nref.c
)
# This file is part of libneo.

@ -0,0 +1,34 @@
/** See the end of this file for copyright and license terms. */
#include "neo/_nref.h"
#include "neo/_types.h"
int _neo_nget(struct _neo_nref *ref)
{
return ++ref->_count;
}
int _neo_nput(struct _neo_nref *ref)
{
int count = --ref->_count;
if (count == 0) {
void *container = (void *)ref - ref->_offset;
ref->_destroy(container);
}
return count;
}
/*
* 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