rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rref.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2016-2017 Haakon Sporsheim <haakon.sporsheim@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library.
16 * See the COPYING file at the root of the source repository.
17 */
18#ifndef __R_REF_H__
19#define __R_REF_H__
20
21#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
22#error "#include <rlib.h> only please."
23#endif
24
47#include <rlib/rtypes.h>
49
51
58typedef struct {
59 rauint refcount;
60 raptr weaklst;
62} RRef;
63
65#define r_ref_refcount(ref) r_atomic_uint_load (&((RRef *)ref)->refcount)
73#define R_REF_STATIC_INIT(destroy) { 0, 0, (RDestroyNotify)destroy }
75#define r_ref_init(self, destroy) R_STMT_START { \
76 r_atomic_uint_store (&((RRef *)self)->refcount, 1); \
77 r_atomic_ptr_store (&((RRef *)self)->weaklst, NULL); \
78 ((RRef *)self)->notify = (RDestroyNotify)destroy; \
79} R_STMT_END
80
85
97
98R_END_DECLS
99
102#endif /* __R_REF_H__ */
103
#define R_API
Public-API decoration: resolves to R_API_EXPORT while building rlib and R_API_IMPORT for consumers.
Definition rmacros.h:115
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
rpointer r_ref_weak_ref(rpointer ref, RFunc notify, rpointer data)
Register a weak-reference callback that fires when ref is destroyed.
void r_ref_unref(rpointer ref)
Decrement the refcount; runs the destructor when it reaches zero.
rboolean r_ref_weak_unref(rpointer ref, RFunc notify, rpointer data)
Cancel a previously registered weak-reference callback.
rpointer r_ref_ref(rpointer ref)
Increment the refcount and return the same pointer.
int rboolean
Boolean type (typedef'd to int).
Definition rtypes.h:59
void(* RFunc)(rpointer data, rpointer user)
Generic iteration callback over (data, user) pairs.
Definition rtypes.h:403
void(* RDestroyNotify)(rpointer ptr)
Destructor callback: free / release the value at ptr.
Definition rtypes.h:401
void * rpointer
Generic pointer (alias for void *).
Definition rtypes.h:327
Atomic load / store / exchange / CAS / fetch-modify operations on integer, unsigned-integer and point...
Foundational type aliases used by every rlib header.
Refcount base struct.
Definition rref.h:58
raptr weaklst
Definition rref.h:60
rauint refcount
Definition rref.h:59
RDestroyNotify notify
Definition rref.h:61