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:
Thomas Haller 2021-03-17 17:34:18 +01:00
parent bec8928341
commit 19d4027824
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 43 additions and 32 deletions

View file

@ -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);

View file

@ -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)
/*****************************************************************************/