summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2021-03-17 17:34:18 +0100
committerThomas Haller <thaller@redhat.com>2021-03-19 12:05:08 +0100
commit19d4027824882cc56b1308e3547a767cfd510b7b (patch)
tree948cb03f10ed91c91e4820ab8ebfd3fc405ef8eb
parentbec8928341f9588611e9ef413e7538a5e50ced28 (diff)
downloadNetworkManager-19d4027824882cc56b1308e3547a767cfd510b7b.tar.gz
refstr: inline nm_ref_string_{ref,unref}()
In the fast path, ref/unref is just a atomic increment/decrement of an integer. Let's inline that.
-rw-r--r--src/libnm-glib-aux/nm-ref-string.c30
-rw-r--r--src/libnm-glib-aux/nm-ref-string.h45
2 files changed, 43 insertions, 32 deletions
diff --git a/src/libnm-glib-aux/nm-ref-string.c b/src/libnm-glib-aux/nm-ref-string.c
index 596656e1ea..6fb39024e6 100644
--- a/src/libnm-glib-aux/nm-ref-string.c
+++ b/src/libnm-glib-aux/nm-ref-string.c
@@ -49,8 +49,8 @@ _ref_string_equal(gconstpointer ptr_a, gconstpointer ptr_b)
/*****************************************************************************/
-static void
-_ASSERT(const NMRefString *rstr)
+void
+_nm_assert_nm_ref_string(NMRefString *rstr)
{
int r;
@@ -168,33 +168,9 @@ nm_ref_string_new_len(const char *cstr, gsize len)
return rstr;
}
-NMRefString *
-nm_ref_string_ref(NMRefString *rstr)
-{
- if (!rstr)
- return NULL;
-
- _ASSERT(rstr);
-
- g_atomic_int_inc(&rstr->_ref_count);
- return rstr;
-}
-
void
-_nm_ref_string_unref_non_null(NMRefString *rstr)
+_nm_ref_string_unref_slow_path(NMRefString *rstr)
{
- int r;
-
- _ASSERT(rstr);
-
- /* fast-path: first try to decrement the ref-count without bringing it
- * to zero. */
- r = rstr->_ref_count;
- if (G_LIKELY(r > 1 && g_atomic_int_compare_and_exchange(&rstr->_ref_count, r, r - 1)))
- return;
-
- /* We apparently are about to return the last reference. Take a lock. */
-
G_LOCK(gl_lock);
nm_assert(g_hash_table_lookup(gl_hash, rstr) == rstr);
diff --git a/src/libnm-glib-aux/nm-ref-string.h b/src/libnm-glib-aux/nm-ref-string.h
index 9a3785bdf0..c55abdf9cf 100644
--- a/src/libnm-glib-aux/nm-ref-string.h
+++ b/src/libnm-glib-aux/nm-ref-string.h
@@ -24,6 +24,18 @@ typedef struct _NMRefString {
/*****************************************************************************/
+void _nm_assert_nm_ref_string(NMRefString *rstr);
+
+static inline void
+nm_assert_nm_ref_string(NMRefString *rstr)
+{
+#if NM_MORE_ASSERTS
+ _nm_assert_nm_ref_string(rstr);
+#endif
+}
+
+/*****************************************************************************/
+
NMRefString *nm_ref_string_new_len(const char *cstr, gsize len);
static inline NMRefString *
@@ -32,17 +44,40 @@ nm_ref_string_new(const char *cstr)
return cstr ? nm_ref_string_new_len(cstr, strlen(cstr)) : NULL;
}
-NMRefString *nm_ref_string_ref(NMRefString *rstr);
-void _nm_ref_string_unref_non_null(NMRefString *rstr);
+/*****************************************************************************/
+
+static inline NMRefString *
+nm_ref_string_ref(NMRefString *rstr)
+{
+ if (rstr) {
+ nm_assert_nm_ref_string(rstr);
+ g_atomic_int_inc(&rstr->_ref_count);
+ }
+ return rstr;
+}
+
+void _nm_ref_string_unref_slow_path(NMRefString *rstr);
static inline void
nm_ref_string_unref(NMRefString *rstr)
{
- if (rstr)
- _nm_ref_string_unref_non_null(rstr);
+ int r;
+
+ if (!rstr)
+ return;
+
+ nm_assert_nm_ref_string(rstr);
+
+ /* fast-path: first try to decrement the ref-count without bringing it
+ * to zero. */
+ r = rstr->_ref_count;
+ if (G_LIKELY(r > 1 && g_atomic_int_compare_and_exchange(&rstr->_ref_count, r, r - 1)))
+ return;
+
+ _nm_ref_string_unref_slow_path(rstr);
}
-NM_AUTO_DEFINE_FCN_VOID0(NMRefString *, _nm_auto_ref_string, _nm_ref_string_unref_non_null);
+NM_AUTO_DEFINE_FCN_VOID(NMRefString *, _nm_auto_ref_string, nm_ref_string_unref);
#define nm_auto_ref_string nm_auto(_nm_auto_ref_string)
/*****************************************************************************/