NetworkManager/shared
Thomas Haller 908fadec96 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-21 14:58:26 +02:00
..
c-list shared/c-list: reimport 2019-04-14 17:22:04 +02:00
c-rbtree shared/c-rbtree: reimport 2019-04-14 17:23:01 +02:00
c-siphash shared/c-siphash: reimport 2019-04-14 17:23:25 +02:00
c-stdaux shared: patch c-stdaux.h to not include <stdatomic.h> 2019-04-14 17:17:52 +02:00
n-acd n-acd: fix leaking socket handle in n_acd_socket_new() when setsockopt() fails 2019-08-02 11:26:25 +02:00
n-dhcp4 n-dhcp4: arm timers in bound state 2019-09-18 09:29:51 +02:00
nm-glib-aux shared: add NMRefString 2019-09-21 14:58:26 +02:00
nm-libnm-core-aux all: SPDX header conversion 2019-09-10 11:19:56 +02:00
nm-libnm-core-intern all: SPDX header conversion 2019-09-10 11:19:56 +02:00
nm-std-aux all: SPDX header conversion 2019-09-10 11:19:56 +02:00
nm-udev-aux all: SPDX header conversion 2019-09-10 11:19:56 +02:00
nm-utils all: SPDX header conversion 2019-09-10 11:19:56 +02:00
systemd all: SPDX header conversion 2019-09-10 11:19:56 +02:00
meson.build shared: add NMRefString 2019-09-21 14:58:26 +02:00
nm-default.h all: SPDX header conversion 2019-09-10 11:19:56 +02:00
nm-meta-setting.c all: SPDX header conversion 2019-09-10 11:19:56 +02:00
nm-meta-setting.h all: SPDX header conversion 2019-09-10 11:19:56 +02:00
nm-test-libnm-utils.h all: SPDX header conversion 2019-09-10 11:19:56 +02:00
nm-test-utils-impl.c all: SPDX header conversion 2019-09-10 11:19:56 +02:00
nm-version-macros.h.in all: SPDX header conversion 2019-09-10 11:19:56 +02:00