NetworkManager/src/libnm-glib-aux/nm-ref-string.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

189 lines
4.6 KiB
C
Raw Normal View History

/* SPDX-License-Identifier: LGPL-2.1-or-later */
shared: add NMRefString I'd like to refactor libnm's caching. Note that cached D-Bus objects have repeated strings all over the place. For example every object will have a set of D-Bus interfaces (strings) and properties (strings) and an object path (which is referenced by other objects). We can save a lot of redundant strings by deduplicating/interning them. Also, by interning them, we can compare them using pointer equality. Add a NMRefString implementation for this. Maybe an alternative name would be NMInternedString or NMDedupString, because this string gets always interned. There is no way to create a NMRefString that is not interned. Still, NMRefString name sounds better. It is ref-counted after all. Notes: - glib has GQuark and g_intern_string(). However, such strings cannot be unrefered and are leaked indefinitely. It is thus unsuited for anything but a fixed set of well-known strings. - glib 2.58 adds GRefString, but we cannot use that because we currently still use glib 2.40. There are some differences: - GRefString is just a typedef to char. That means, the glib API exposes GRefString like regular character strings. NMRefString intentionally does that not. This makes it slightly less convenient to pass it to API that expects "const char *". But it makes it clear to the reader, that an instance is in fact a NMRefString, which means it indicates that the string is interned and can be referenced without additional copy. - GRefString can be optionally interned. That means you can only use pointer equality for comparing values if you know that the GRefString was created with g_ref_string_new_intern(). So, GRefString looks like a "const char *" pointer and even if you know it's a GRefString, you might not know whether it is interned. NMRefString is always interned, and you can always compare it using pointer equality. - In the past I already proposed a different implementation for a ref-string. That made different choices. For example NMRefString then was a typedef to "const char *", it did not support interning but deduplication (without a global cache), ref/unref was not thread safe (but then there was no global cache so that two threads could still use the API independently). The point is, there are various choices to make. GRefString, the previous NMRefString implementation and the one here, all have pros and cons. I think for the purpose where I intend NMRefString (dedup and efficient comparison), it is a preferable implementation. Ah, and of course NMRefString is an immutable string, which is a nice property.
2019-09-02 07:54:28 +02:00
#ifndef __NM_REF_STRING_H__
#define __NM_REF_STRING_H__
/*****************************************************************************/
typedef struct _NMRefString {
const gsize len;
union {
struct {
volatile int _ref_count;
const char str[];
};
struct {
/* This union field is only used during lookup by external string.
* In that case, len will be set to G_MAXSIZE, and the actual len/str values
* are set in _priv_lookup. */
gsize l_len;
const char *l_str;
} _priv_lookup;
};
shared: add NMRefString I'd like to refactor libnm's caching. Note that cached D-Bus objects have repeated strings all over the place. For example every object will have a set of D-Bus interfaces (strings) and properties (strings) and an object path (which is referenced by other objects). We can save a lot of redundant strings by deduplicating/interning them. Also, by interning them, we can compare them using pointer equality. Add a NMRefString implementation for this. Maybe an alternative name would be NMInternedString or NMDedupString, because this string gets always interned. There is no way to create a NMRefString that is not interned. Still, NMRefString name sounds better. It is ref-counted after all. Notes: - glib has GQuark and g_intern_string(). However, such strings cannot be unrefered and are leaked indefinitely. It is thus unsuited for anything but a fixed set of well-known strings. - glib 2.58 adds GRefString, but we cannot use that because we currently still use glib 2.40. There are some differences: - GRefString is just a typedef to char. That means, the glib API exposes GRefString like regular character strings. NMRefString intentionally does that not. This makes it slightly less convenient to pass it to API that expects "const char *". But it makes it clear to the reader, that an instance is in fact a NMRefString, which means it indicates that the string is interned and can be referenced without additional copy. - GRefString can be optionally interned. That means you can only use pointer equality for comparing values if you know that the GRefString was created with g_ref_string_new_intern(). So, GRefString looks like a "const char *" pointer and even if you know it's a GRefString, you might not know whether it is interned. NMRefString is always interned, and you can always compare it using pointer equality. - In the past I already proposed a different implementation for a ref-string. That made different choices. For example NMRefString then was a typedef to "const char *", it did not support interning but deduplication (without a global cache), ref/unref was not thread safe (but then there was no global cache so that two threads could still use the API independently). The point is, there are various choices to make. GRefString, the previous NMRefString implementation and the one here, all have pros and cons. I think for the purpose where I intend NMRefString (dedup and efficient comparison), it is a preferable implementation. Ah, and of course NMRefString is an immutable string, which is a nice property.
2019-09-02 07:54:28 +02:00
} 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
}
/*****************************************************************************/
shared: add NMRefString I'd like to refactor libnm's caching. Note that cached D-Bus objects have repeated strings all over the place. For example every object will have a set of D-Bus interfaces (strings) and properties (strings) and an object path (which is referenced by other objects). We can save a lot of redundant strings by deduplicating/interning them. Also, by interning them, we can compare them using pointer equality. Add a NMRefString implementation for this. Maybe an alternative name would be NMInternedString or NMDedupString, because this string gets always interned. There is no way to create a NMRefString that is not interned. Still, NMRefString name sounds better. It is ref-counted after all. Notes: - glib has GQuark and g_intern_string(). However, such strings cannot be unrefered and are leaked indefinitely. It is thus unsuited for anything but a fixed set of well-known strings. - glib 2.58 adds GRefString, but we cannot use that because we currently still use glib 2.40. There are some differences: - GRefString is just a typedef to char. That means, the glib API exposes GRefString like regular character strings. NMRefString intentionally does that not. This makes it slightly less convenient to pass it to API that expects "const char *". But it makes it clear to the reader, that an instance is in fact a NMRefString, which means it indicates that the string is interned and can be referenced without additional copy. - GRefString can be optionally interned. That means you can only use pointer equality for comparing values if you know that the GRefString was created with g_ref_string_new_intern(). So, GRefString looks like a "const char *" pointer and even if you know it's a GRefString, you might not know whether it is interned. NMRefString is always interned, and you can always compare it using pointer equality. - In the past I already proposed a different implementation for a ref-string. That made different choices. For example NMRefString then was a typedef to "const char *", it did not support interning but deduplication (without a global cache), ref/unref was not thread safe (but then there was no global cache so that two threads could still use the API independently). The point is, there are various choices to make. GRefString, the previous NMRefString implementation and the one here, all have pros and cons. I think for the purpose where I intend NMRefString (dedup and efficient comparison), it is a preferable implementation. Ah, and of course NMRefString is an immutable string, which is a nice property.
2019-09-02 07:54:28 +02:00
NMRefString *nm_ref_string_new_len(const char *cstr, gsize len);
static inline NMRefString *
nm_ref_string_new(const char *cstr)
{
return cstr ? nm_ref_string_new_len(cstr, strlen(cstr)) : NULL;
}
/*****************************************************************************/
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);
shared: add NMRefString I'd like to refactor libnm's caching. Note that cached D-Bus objects have repeated strings all over the place. For example every object will have a set of D-Bus interfaces (strings) and properties (strings) and an object path (which is referenced by other objects). We can save a lot of redundant strings by deduplicating/interning them. Also, by interning them, we can compare them using pointer equality. Add a NMRefString implementation for this. Maybe an alternative name would be NMInternedString or NMDedupString, because this string gets always interned. There is no way to create a NMRefString that is not interned. Still, NMRefString name sounds better. It is ref-counted after all. Notes: - glib has GQuark and g_intern_string(). However, such strings cannot be unrefered and are leaked indefinitely. It is thus unsuited for anything but a fixed set of well-known strings. - glib 2.58 adds GRefString, but we cannot use that because we currently still use glib 2.40. There are some differences: - GRefString is just a typedef to char. That means, the glib API exposes GRefString like regular character strings. NMRefString intentionally does that not. This makes it slightly less convenient to pass it to API that expects "const char *". But it makes it clear to the reader, that an instance is in fact a NMRefString, which means it indicates that the string is interned and can be referenced without additional copy. - GRefString can be optionally interned. That means you can only use pointer equality for comparing values if you know that the GRefString was created with g_ref_string_new_intern(). So, GRefString looks like a "const char *" pointer and even if you know it's a GRefString, you might not know whether it is interned. NMRefString is always interned, and you can always compare it using pointer equality. - In the past I already proposed a different implementation for a ref-string. That made different choices. For example NMRefString then was a typedef to "const char *", it did not support interning but deduplication (without a global cache), ref/unref was not thread safe (but then there was no global cache so that two threads could still use the API independently). The point is, there are various choices to make. GRefString, the previous NMRefString implementation and the one here, all have pros and cons. I think for the purpose where I intend NMRefString (dedup and efficient comparison), it is a preferable implementation. Ah, and of course NMRefString is an immutable string, which is a nice property.
2019-09-02 07:54:28 +02:00
static inline void
nm_ref_string_unref(NMRefString *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);
shared: add NMRefString I'd like to refactor libnm's caching. Note that cached D-Bus objects have repeated strings all over the place. For example every object will have a set of D-Bus interfaces (strings) and properties (strings) and an object path (which is referenced by other objects). We can save a lot of redundant strings by deduplicating/interning them. Also, by interning them, we can compare them using pointer equality. Add a NMRefString implementation for this. Maybe an alternative name would be NMInternedString or NMDedupString, because this string gets always interned. There is no way to create a NMRefString that is not interned. Still, NMRefString name sounds better. It is ref-counted after all. Notes: - glib has GQuark and g_intern_string(). However, such strings cannot be unrefered and are leaked indefinitely. It is thus unsuited for anything but a fixed set of well-known strings. - glib 2.58 adds GRefString, but we cannot use that because we currently still use glib 2.40. There are some differences: - GRefString is just a typedef to char. That means, the glib API exposes GRefString like regular character strings. NMRefString intentionally does that not. This makes it slightly less convenient to pass it to API that expects "const char *". But it makes it clear to the reader, that an instance is in fact a NMRefString, which means it indicates that the string is interned and can be referenced without additional copy. - GRefString can be optionally interned. That means you can only use pointer equality for comparing values if you know that the GRefString was created with g_ref_string_new_intern(). So, GRefString looks like a "const char *" pointer and even if you know it's a GRefString, you might not know whether it is interned. NMRefString is always interned, and you can always compare it using pointer equality. - In the past I already proposed a different implementation for a ref-string. That made different choices. For example NMRefString then was a typedef to "const char *", it did not support interning but deduplication (without a global cache), ref/unref was not thread safe (but then there was no global cache so that two threads could still use the API independently). The point is, there are various choices to make. GRefString, the previous NMRefString implementation and the one here, all have pros and cons. I think for the purpose where I intend NMRefString (dedup and efficient comparison), it is a preferable implementation. Ah, and of course NMRefString is an immutable string, which is a nice property.
2019-09-02 07:54:28 +02:00
}
NM_AUTO_DEFINE_FCN_VOID(NMRefString *, _nm_auto_ref_string, nm_ref_string_unref);
shared: add NMRefString I'd like to refactor libnm's caching. Note that cached D-Bus objects have repeated strings all over the place. For example every object will have a set of D-Bus interfaces (strings) and properties (strings) and an object path (which is referenced by other objects). We can save a lot of redundant strings by deduplicating/interning them. Also, by interning them, we can compare them using pointer equality. Add a NMRefString implementation for this. Maybe an alternative name would be NMInternedString or NMDedupString, because this string gets always interned. There is no way to create a NMRefString that is not interned. Still, NMRefString name sounds better. It is ref-counted after all. Notes: - glib has GQuark and g_intern_string(). However, such strings cannot be unrefered and are leaked indefinitely. It is thus unsuited for anything but a fixed set of well-known strings. - glib 2.58 adds GRefString, but we cannot use that because we currently still use glib 2.40. There are some differences: - GRefString is just a typedef to char. That means, the glib API exposes GRefString like regular character strings. NMRefString intentionally does that not. This makes it slightly less convenient to pass it to API that expects "const char *". But it makes it clear to the reader, that an instance is in fact a NMRefString, which means it indicates that the string is interned and can be referenced without additional copy. - GRefString can be optionally interned. That means you can only use pointer equality for comparing values if you know that the GRefString was created with g_ref_string_new_intern(). So, GRefString looks like a "const char *" pointer and even if you know it's a GRefString, you might not know whether it is interned. NMRefString is always interned, and you can always compare it using pointer equality. - In the past I already proposed a different implementation for a ref-string. That made different choices. For example NMRefString then was a typedef to "const char *", it did not support interning but deduplication (without a global cache), ref/unref was not thread safe (but then there was no global cache so that two threads could still use the API independently). The point is, there are various choices to make. GRefString, the previous NMRefString implementation and the one here, all have pros and cons. I think for the purpose where I intend NMRefString (dedup and efficient comparison), it is a preferable implementation. Ah, and of course NMRefString is an immutable string, which is a nice property.
2019-09-02 07:54:28 +02:00
#define nm_auto_ref_string nm_auto(_nm_auto_ref_string)
/*****************************************************************************/
static inline const char *
nm_ref_string_get_str(NMRefString *rstr)
{
return rstr ? rstr->str : NULL;
}
static inline gsize
nm_ref_string_get_len(NMRefString *rstr)
{
return rstr ? rstr->len : 0u;
}
static inline gboolean
nm_ref_string_equal(NMRefString *a, NMRefString *b)
{
return a == b;
}
static inline int
nm_ref_string_cmp(NMRefString *a, NMRefString *b)
{
NM_CMP_SELF(a, b);
/* It would be cheaper to first compare by length. But this
* way we get a nicer, ASCIIbethical sort order. */
NM_CMP_DIRECT_MEMCMP(a->str, b->str, NM_MIN(a->len, b->len));
NM_CMP_DIRECT(a->len, b->len);
return nm_assert_unreachable_val(0);
}
#define NM_CMP_DIRECT_REF_STRING(a, b) NM_CMP_RETURN_DIRECT(nm_ref_string_cmp((a), (b)))
static inline gboolean
nm_ref_string_equal_str(NMRefString *rstr, const char *str)
{
if (!str)
return (!!rstr);
if (!rstr)
return FALSE;
return rstr->len == strlen(str) && (rstr->str == str || memcmp(rstr->str, str, rstr->len) == 0);
}
2019-10-22 16:49:05 +02:00
static inline gboolean
NM_IS_REF_STRING(NMRefString *rstr)
2019-10-22 16:49:05 +02:00
{
if (rstr)
nm_assert_nm_ref_string(rstr);
2019-10-22 16:49:05 +02:00
/* Technically, %NULL is also a valid NMRefString (according to nm_ref_string_new(),
* nm_ref_string_get_str() and nm_ref_string_unref()). However, NM_IS_REF_STRING()
* does not think so. If callers want to allow %NULL, they need to check
* separately. */
return !!rstr;
}
static inline NMRefString *
NM_REF_STRING_UPCAST(const char *str)
{
NMRefString *rstr;
if (!str)
return NULL;
rstr = (gpointer) (((char *) str) - G_STRUCT_OFFSET(NMRefString, str));
nm_assert_nm_ref_string(rstr);
return rstr;
}
static inline NMRefString *
nm_ref_string_ref_upcast(const char *str)
{
return nm_ref_string_ref(NM_REF_STRING_UPCAST(str));
}
static inline void
nm_ref_string_unref_upcast(const char *str)
{
nm_ref_string_unref(NM_REF_STRING_UPCAST(str));
}
static inline gboolean
nm_ref_string_reset_str(NMRefString **ptr, const char *str)
{
nm_auto_ref_string NMRefString *rstr = NULL;
gsize l;
nm_assert(ptr);
if (!str)
return nm_clear_pointer(ptr, nm_ref_string_unref);
l = strlen(str);
if ((*ptr) && (*ptr)->len == l && ((*ptr)->str == str || memcmp((*ptr)->str, str, l) == 0))
return FALSE;
rstr = *ptr;
*ptr = nm_ref_string_new_len(str, l);
return TRUE;
}
shared: add NMRefString I'd like to refactor libnm's caching. Note that cached D-Bus objects have repeated strings all over the place. For example every object will have a set of D-Bus interfaces (strings) and properties (strings) and an object path (which is referenced by other objects). We can save a lot of redundant strings by deduplicating/interning them. Also, by interning them, we can compare them using pointer equality. Add a NMRefString implementation for this. Maybe an alternative name would be NMInternedString or NMDedupString, because this string gets always interned. There is no way to create a NMRefString that is not interned. Still, NMRefString name sounds better. It is ref-counted after all. Notes: - glib has GQuark and g_intern_string(). However, such strings cannot be unrefered and are leaked indefinitely. It is thus unsuited for anything but a fixed set of well-known strings. - glib 2.58 adds GRefString, but we cannot use that because we currently still use glib 2.40. There are some differences: - GRefString is just a typedef to char. That means, the glib API exposes GRefString like regular character strings. NMRefString intentionally does that not. This makes it slightly less convenient to pass it to API that expects "const char *". But it makes it clear to the reader, that an instance is in fact a NMRefString, which means it indicates that the string is interned and can be referenced without additional copy. - GRefString can be optionally interned. That means you can only use pointer equality for comparing values if you know that the GRefString was created with g_ref_string_new_intern(). So, GRefString looks like a "const char *" pointer and even if you know it's a GRefString, you might not know whether it is interned. NMRefString is always interned, and you can always compare it using pointer equality. - In the past I already proposed a different implementation for a ref-string. That made different choices. For example NMRefString then was a typedef to "const char *", it did not support interning but deduplication (without a global cache), ref/unref was not thread safe (but then there was no global cache so that two threads could still use the API independently). The point is, there are various choices to make. GRefString, the previous NMRefString implementation and the one here, all have pros and cons. I think for the purpose where I intend NMRefString (dedup and efficient comparison), it is a preferable implementation. Ah, and of course NMRefString is an immutable string, which is a nice property.
2019-09-02 07:54:28 +02:00
#endif /* __NM_REF_STRING_H__ */