mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager
synced 2024-10-15 12:34:55 +00:00
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.
This commit is contained in:
parent
bec8928341
commit
19d4027824
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
||||
/*****************************************************************************/
|
||||
|
|
Loading…
Reference in a new issue