nref: move main init work out of macro

This commit is contained in:
anna 2021-07-21 16:55:19 +02:00
parent f120bcc02a
commit f99ad6162a
Signed by: fef
GPG key ID: EC22E476DC2D3D84
2 changed files with 8 additions and 5 deletions
include/neo
src

View file

@ -4,6 +4,8 @@
#include "neo/_types.h"
void _neo_nref_init(struct _neo_nref *ref, void (*destroy)(void *ptr), usize offset);
/**
* Initialize the reference counter in a structure.
*
@ -18,11 +20,10 @@
* only parameter and return type `void`, which will deallocate the struct
*/
#define nref_init(ptr, destroy) ({ \
struct _neo_nref *__nref = &(ptr)->__neo_nref; \
__nref->_offset = offsetof(typeof(*(ptr)), __neo_nref); \
void (*__destroy_typechecked)(typeof(ptr)) = destroy; \
__nref->_destroy = (void (*)(void *))__destroy_typechecked; \
__nref->_count = 1; \
_neo_nref_init(&(ptr)->__neo_nref, \
(void (*)(void *))__destroy_typechecked, \
offsetof(typeof(*(ptr)), __neo_nref)); \
})
int _neo_nget(struct _neo_nref *ref);

View file

@ -5,8 +5,10 @@
#include "neo/_nref.h"
#include "neo/_types.h"
void _neo_nref_init(struct _neo_nref *ref)
void _neo_nref_init(struct _neo_nref *ref, void (*destroy)(void *ptr), usize offset)
{
ref->_destroy = destroy;
ref->_offset = offset;
atomic_init(&ref->_count, 1);
}