]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon/session2_{inc,dec}_refs() nit: allow compiler to inline
authorVladimír Čunát <vladimir.cunat@nic.cz>
Sat, 10 May 2025 09:11:14 +0000 (11:11 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Sat, 10 May 2025 09:11:14 +0000 (11:11 +0200)
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)

daemon/session2.c
daemon/session2.h
lib/utils.c
lib/utils.h

index fc7822e8cc29f1f51c3434512b3b5568b8ab824b..0916514db33ede44b9bbd1029363b9fc61d40e0e 100644 (file)
@@ -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)
 {
index 897c31b929a541c6d72679aeb007187d552de619..32950dd4f3a63aef1500bd2e69b4936e1bf90d06 100644 (file)
@@ -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. */
index fb297788ee1f9a5439ee1d10ec2626442921b9da..016072c5873dcdbb8c91b203e9524b2d8cc2c56c 100644 (file)
@@ -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;
index b6b350c8d8b3724411dbc361a2b9d83a749870e3..bc7cc0164fc670335d9a0c481a670d383698ed1c 100644 (file)
@@ -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);