add refcounts
parent
7eed96edcf
commit
957f5e2a35
@ -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.
|
||||
*/
|
@ -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…
Reference in New Issue