nref: move main init work out of macro
This commit is contained in:
parent
f120bcc02a
commit
f99ad6162a
2 changed files with 8 additions and 5 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue