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:
@@ -49,8 +49,8 @@ _ref_string_equal(gconstpointer ptr_a, gconstpointer ptr_b)
|
|||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
static void
|
void
|
||||||
_ASSERT(const NMRefString *rstr)
|
_nm_assert_nm_ref_string(NMRefString *rstr)
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
@@ -168,33 +168,9 @@ nm_ref_string_new_len(const char *cstr, gsize len)
|
|||||||
return rstr;
|
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
|
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);
|
G_LOCK(gl_lock);
|
||||||
|
|
||||||
nm_assert(g_hash_table_lookup(gl_hash, rstr) == rstr);
|
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);
|
NMRefString *nm_ref_string_new_len(const char *cstr, gsize len);
|
||||||
|
|
||||||
static inline NMRefString *
|
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;
|
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
|
static inline void
|
||||||
nm_ref_string_unref(NMRefString *rstr)
|
nm_ref_string_unref(NMRefString *rstr)
|
||||||
{
|
{
|
||||||
if (rstr)
|
int r;
|
||||||
_nm_ref_string_unref_non_null(rstr);
|
|
||||||
|
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)
|
#define nm_auto_ref_string nm_auto(_nm_auto_ref_string)
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
Reference in New Issue
Block a user