NetworkManager/shared/nm-glib-aux
Thomas Haller eda47170ed shared: add NMStrBuf util
Our own implementation of a string buffer like GString.

Advantages (in decreasing relevance):

- Since we are in control, we can easily let it nm_explicit_bzero()
  the memory. The regular GString API cannot be used in such a case.
  While nm_explicit_bzero() may or may not be of questionable benefit,
  the problem is that if the underlying API counteracts the aim of
  clearing memory, it gets impossible. As API like NMStrBuf supports
  it, clearing memory is a easy as enable the right flag.
  This would for example be useful for example when we read passwords
  from a file or file descriptor (e.g. try_spawn_vpn_auth_helper()).

- We have API like

    nmp_object_to_string (const NMPObject *obj,
                          NMPObjectToStringMode to_string_mode,
                          char *buf,
                          gsize buf_size);

  which accept a fixed size output buffer. This has the problem of
  how choosing the right sized buffer. With NMStrBuf such API could
  be instead

    nmp_object_to_string (const NMPObject *obj,
                          NMPObjectToStringMode to_string_mode,
                          NMStrBuf *buf);

  which can automatically grow (using heap allocation). It would be
  easy to extend NMStrBuf to use a fixed buffer or limiting the
  maximum string length. The point is, that the to-string API wouldn't
  have to change. Depending on the NMStrBuf passed in, you can fill
  an unbounded heap allocated string, a heap allocated string up to
  a fixed length, or a static string of fixed length. NMStrBuf currently
  only implements the unbounded heap allocate string case, but it would
  be simple to extend.

  Note that we already have API like nm_utils_strbuf_*() to fill a buffer
  of fixed size. GString is not useable for that (efficiently), hence
  this API exists. NMStrBuf could be easily extended to replace this API
  without usability or performance penalty. So, while this adds one new
  API, it could replace other APIs.

- GString always requires a heap allocation for the container. In by far
  most of the cases where we use GString, we use it to simply construct
  a string dynamically. There is zero use for this overhead. If one
  really needs a heap allocated buffer, NMStrBuf can easily embedded
  in a malloc'ed memory and boxed that way.

- GString API supports inserting and removing range. We almost never
  make use of that. We only require append-only, which is simple to
  implement.

- GString needs to NUL terminate the buffer on every append. It
  has unnecessary overhead for allowing a usage of where intermediate
  buffer contents are valid strings too. That is not the case with
  NMStrBuf: the API requires the user to call nm_str_buf_get_str() or
  nm_str_buf_finalize(). In most cases, you would only access the string
  once at the end, and not while constructing it.

- GString always grows the buffer size by doubling it. I don't think
  that is optimal. I don't think there is one optimal approach for how
  to grow the buffer, it depends on the usage patterns. However, trying
  to make an optimal choice here makes a difference. QT also thinks so,
  and I adopted their approach in nm_utils_get_next_realloc_size().
2020-04-03 11:31:12 +02:00
..
tests shared: add nm_utils_get_next_realloc_size() helper 2020-04-03 11:31:12 +02:00
nm-c-list.h shared: add nm_c_list_for_each_entry_prev() helper macro 2019-10-18 22:09:18 +02:00
nm-dbus-aux.c shared: allow nm_dbus_connection_call_finish_variant_cb() with D-Bus methods that have no return value 2020-03-23 09:32:04 +01:00
nm-dbus-aux.h shared: add nm_dbus_connection_call_set() util 2020-02-10 19:11:50 +01:00
nm-dedup-multi.c all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-dedup-multi.h all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-enum-utils.c all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-enum-utils.h all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-errno.c shared: drop _STATIC variant of macros that define functions 2020-02-13 17:17:07 +01:00
nm-errno.h all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-glib.h license: relicense "shared/nm-glib-aux/nm-glib.h" under LGPL-2.1+ 2020-02-16 15:41:36 +01:00
nm-hash-utils.c shared: cleanup _get_hash_key_init() and better explain the reasoning 2020-04-03 11:26:49 +02:00
nm-hash-utils.h shared: cleanup _get_hash_key_init() and better explain the reasoning 2020-04-03 11:26:49 +02:00
nm-io-utils.c shared: use nm_secret_mem_try_realloc_take() in nm_utils_fd_get_contents() 2020-04-03 11:31:12 +02:00
nm-io-utils.h shared: add io-util to read data from a fd into a GString 2019-12-24 10:13:51 +01:00
nm-jansson.h license: relicense "shared/nm-glib-aux/nm-jansson.h" under LGPL-2.1+ 2020-02-16 15:41:40 +01:00
nm-json-aux.c all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-json-aux.h all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-keyfile-aux.c all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-keyfile-aux.h all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-logging-base.c shared: move log level info from core to "nm-logging-base.h" 2019-11-28 19:20:33 +01:00
nm-logging-base.h shared: move log level info from core to "nm-logging-base.h" 2019-11-28 19:20:33 +01:00
nm-logging-fwd.h shared: add LOGD_DHCP_from_addr_family() helper 2020-02-17 14:45:09 +01:00
nm-macros-internal.h shared: add NM_SWAP() macro 2020-03-26 21:28:56 +01:00
nm-obj.h all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-random-utils.c all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-random-utils.h all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00
nm-ref-string.c shared: assert in nm_ref_string_unref() for valid NMRefString instance 2019-11-07 11:34:36 +01:00
nm-ref-string.h shared: fix handling %NULL argument in nm_ref_string_equals_str() 2020-03-02 15:49:41 +01:00
nm-secret-utils.c shared: use G_UNLIKELY() macro for unlikely branch in nm_explicit_bzero() 2020-04-03 11:31:12 +02:00
nm-secret-utils.h shared: add nm_secret_mem_realloc() helpers 2020-04-03 11:31:12 +02:00
nm-shared-utils.c shared: add NMStrBuf util 2020-04-03 11:31:12 +02:00
nm-shared-utils.h shared: add nm_utils_get_next_realloc_size() helper 2020-04-03 11:31:12 +02:00
nm-str-buf.h shared: add NMStrBuf util 2020-04-03 11:31:12 +02:00
nm-time-utils.c shared/trivial: rename time related functions to use "nsec"/"msec" abbreviation instead of "ns"/"ms" 2020-02-10 19:11:50 +01:00
nm-time-utils.h shared: add nm_utils_get_monotonic_timestamp_msec_cached() helper 2020-02-10 19:11:50 +01:00
nm-value-type.h all: unify format of our Copyright source code comments 2019-10-02 17:03:52 +02:00