From: Vladimír Čunát Date: Sat, 10 May 2025 09:11:14 +0000 (+0200) Subject: daemon/session2_{inc,dec}_refs() nit: allow compiler to inline X-Git-Tag: v6.0.13~8^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0ce597f6b91e803ddf9145344af028a95b886c56;p=thirdparty%2Fknot-resolver.git daemon/session2_{inc,dec}_refs() nit: allow compiler to inline The `inline + extern inline` combination is kind-of arcane, but I find it nice to leave it to compiler whether to inline or not. (in particular, in debug builds it's probably better not to inline this) --- diff --git a/daemon/session2.c b/daemon/session2.c index fc7822e8c..0916514db 100644 --- a/daemon/session2.c +++ b/daemon/session2.c @@ -896,7 +896,7 @@ struct session2 *session2_new(enum session2_transport_type transport_type, /** De-allocates the session. Must only be called once the underlying IO handle * and timer are already closed, otherwise may leak resources. */ -static void session2_free(struct session2 *s) +void session2_free(struct session2 *s) { const struct protolayer_grp *grp = &protolayer_grps[s->proto]; for (size_t i = 0; i < grp->num_layers; i++) { @@ -913,23 +913,8 @@ static void session2_free(struct session2 *s) free(s); } -void session2_inc_refs(struct session2 *s) -{ - kr_assert(s->ref_count < INT_MAX); - s->ref_count++; -} - -void session2_dec_refs(struct session2 *s) -{ - if (kr_fails_assert(s->ref_count > 0)) { - session2_free(s); - return; - } - - s->ref_count--; - if (s->ref_count <= 0) - session2_free(s); -} +extern inline void session2_inc_refs(struct session2 *s); +extern inline void session2_dec_refs(struct session2 *s); int session2_start_read(struct session2 *session) { diff --git a/daemon/session2.h b/daemon/session2.h index 897c31b92..32950dd4f 100644 --- a/daemon/session2.h +++ b/daemon/session2.h @@ -934,10 +934,26 @@ struct session2 *session2_new(enum session2_transport_type transport_type, size_t layer_param_count, bool outgoing); + +void session2_free(struct session2 *s); /** Used for counting references from unclosed libUV handles owned by the session and from defer. * Once all owned handles are closed and nothing is deferred, the session is freed. */ -void session2_inc_refs(struct session2 *s); -void session2_dec_refs(struct session2 *s); +inline void session2_inc_refs(struct session2 *s) +{ + kr_assert(s->ref_count < INT_MAX); + s->ref_count++; +} +inline void session2_dec_refs(struct session2 *s) +{ + if (kr_fails_assert(s->ref_count > 0)) { + session2_free(s); + return; + } + + s->ref_count--; + if (s->ref_count <= 0) + session2_free(s); +} /** Allocates and initializes a new session with the specified protocol layer * group, using a *libuv handle* as its transport. */ diff --git a/lib/utils.c b/lib/utils.c index fb297788e..016072c58 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -57,6 +57,11 @@ extern inline uint64_t kr_rand_bytes(unsigned int size); bool kr_dbg_assertion_abort = DBG_ASSERTION_ABORT; int kr_dbg_assertion_fork = DBG_ASSERTION_FORK; + +// Non-inline instance to allow warn-free use of kr_assert() etc. in other `extern inline`. +extern inline bool kr_assert_func(bool result, const char *expr, const char *func, + const char *file, int line); + void kr_fail(bool is_fatal, const char *expr, const char *func, const char *file, int line) { const int errno_orig = errno; diff --git a/lib/utils.h b/lib/utils.h index b6b350c8d..bc7cc0164 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -100,8 +100,8 @@ KR_EXPORT KR_COLD void kr_fail(bool is_fatal, const char* expr, const char *func /** Use kr_require(), kr_assert() or kr_fails_assert() instead of directly this function. */ __attribute__ ((warn_unused_result)) -static inline bool kr_assert_func(bool result, const char *expr, const char *func, - const char *file, int line) +KR_EXPORT inline bool kr_assert_func(bool result, const char *expr, const char *func, + const char *file, int line) { if (!result) kr_fail(false, expr, func, file, line);