| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "../fundamental/assert-util.h" |
| 5 |
#include "../fundamental/cleanup-util.h" /* IWYU pragma: export */ |
| 6 |
|
| 7 |
typedef void (*free_func_t)(void *p); |
| 8 |
typedef void* (*mfree_func_t)(void *p); |
| 9 |
|
| 10 |
#define free_and_replace_full(a, b, free_func) \ |
| 11 |
({ \ |
| 12 |
typeof(a)* _a = &(a); \ |
| 13 |
typeof(b)* _b = &(b); \ |
| 14 |
free_func(*_a); \ |
| 15 |
*_a = *_b; \ |
| 16 |
*_b = NULL; \ |
| 17 |
0; \ |
| 18 |
}) |
| 19 |
|
| 20 |
/* This is similar to free_and_replace_full(), but NULL is not assigned to 'b', and its reference counter is |
| 21 |
* increased. */ |
| 22 |
#define unref_and_replace_new_ref(a, b, ref_func, unref_func) \ |
| 23 |
({ \ |
| 24 |
typeof(a)* _a = &(a); \ |
| 25 |
typeof(b) _b = ref_func(b); \ |
| 26 |
unref_func(*_a); \ |
| 27 |
*_a = _b; \ |
| 28 |
0; \ |
| 29 |
}) |
| 30 |
|
| 31 |
#define _DEFINE_TRIVIAL_REF_FUNC(type, name, scope) \ |
| 32 |
scope type *name##_ref(type *p) { \ |
| 33 |
if (!p) \ |
| 34 |
return NULL; \ |
| 35 |
\ |
| 36 |
/* For type check. */ \ |
| 37 |
unsigned *q = &p->n_ref; \ |
| 38 |
assert(*q > 0); \ |
| 39 |
assert_se(*q < UINT_MAX); \ |
| 40 |
\ |
| 41 |
(*q)++; \ |
| 42 |
return p; \ |
| 43 |
} |
| 44 |
|
| 45 |
#define _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, scope) \ |
| 46 |
scope type *name##_unref(type *p) { \ |
| 47 |
if (!p) \ |
| 48 |
return NULL; \ |
| 49 |
\ |
| 50 |
assert(p->n_ref > 0); \ |
| 51 |
p->n_ref--; \ |
| 52 |
if (p->n_ref > 0) \ |
| 53 |
return NULL; \ |
| 54 |
\ |
| 55 |
return free_func(p); \ |
| 56 |
} |
| 57 |
|
| 58 |
#define DEFINE_TRIVIAL_REF_FUNC(type, name) \ |
| 59 |
_DEFINE_TRIVIAL_REF_FUNC(type, name,) |
| 60 |
#define DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name) \ |
| 61 |
_DEFINE_TRIVIAL_REF_FUNC(type, name, static) |
| 62 |
#define DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name) \ |
| 63 |
_DEFINE_TRIVIAL_REF_FUNC(type, name, _public_) |
| 64 |
|
| 65 |
#define DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func) \ |
| 66 |
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func,) |
| 67 |
#define DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func) \ |
| 68 |
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, static) |
| 69 |
#define DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func) \ |
| 70 |
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, _public_) |
| 71 |
|
| 72 |
#define DEFINE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \ |
| 73 |
DEFINE_TRIVIAL_REF_FUNC(type, name); \ |
| 74 |
DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func); |
| 75 |
|
| 76 |
#define DECLARE_TRIVIAL_REF_FUNC(type, name) \ |
| 77 |
type* name##_ref(type *p) |
| 78 |
|
| 79 |
#define DECLARE_TRIVIAL_UNREF_FUNC(type, name) \ |
| 80 |
type* name##_unref(type *p) |
| 81 |
|
| 82 |
#define DECLARE_TRIVIAL_REF_UNREF_FUNC(type, name) \ |
| 83 |
DECLARE_TRIVIAL_REF_FUNC(type, name); \ |
| 84 |
DECLARE_TRIVIAL_UNREF_FUNC(type, name) |
| 85 |
|
| 86 |
#define DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \ |
| 87 |
DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name); \ |
| 88 |
DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func); |
| 89 |
|
| 90 |
#define DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \ |
| 91 |
DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name); \ |
| 92 |
DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func); |
| 93 |
|
| 94 |
typedef void (*void_func_t)(void); |
| 95 |
|
| 96 |
static inline void dispatch_void_func(void_func_t *f) { |
| 97 |
assert(f); |
| 98 |
assert(*f); |
| 99 |
(*f)(); |
| 100 |
} |
| 101 |
|
| 102 |
/* Inspired by Go's "defer" construct, but much more basic. This basically just calls a void function when |
| 103 |
* the current scope is left. Doesn't do function parameters (i.e. no closures). */ |
| 104 |
#define DEFER_VOID_CALL(x) _DEFER_VOID_CALL(UNIQ, x) |
| 105 |
#define _DEFER_VOID_CALL(uniq, x) _unused_ _cleanup_(dispatch_void_func) void_func_t UNIQ_T(defer, uniq) = (x) |