Commit Graph

37 Commits

Author SHA1 Message Date
Thomas Haller
a2663803c3 shared: refactor nm_utils_is_power_of_two() to return false for 0
Returning TRUE for zero makes no sense. Obviously, zero is not a power
of two.

Also, the function is used to check whether a number has only one bit
(flag) set, so, an alternative name would be "has-one-bit-set", which
also should return FALSE for zero. All callers didn't really care for
the previous meaning "has-at-most-one-bit-set".

This also avoids the issue of checking (x >= 0), which causes
-Wtype-limits warnings for unsigned types. Which was avoided
by doing (x == 0 || x > 0), which caused -Wlogical-op warning,
which then was avoided (x == 0 || (x > 0 && 1)). Just don't.
2017-05-22 14:01:07 +02:00
Francesco Giudici
7c2ecaa4e0 build: work around GCC -Wlogical-op for "nm_utils_is_power_of_two" macros
We recently added -Wlogical-op in our build process
(commit #41e7fca59762dc928c9d67b555b1409c3477b2b0).
Seems that old versions of gcc (4.8.x) will hit that warning with our
implementation of our "nm_utils_is_power_of_two" and
"test_nm_utils_is_power_of_two_do" macros.
Fool it just adding an always TRUE check.
2017-05-22 12:05:51 +02:00
Beniamino Galvani
d19553392b libnm-core,shared: fix typo in '(allow-none)' annotation 2017-04-27 09:00:55 +02:00
Thomas Haller
31d0d0ef83 shared: add NM_PTRARRAY_LEN() utility macro
I used to use g_strv_length ((char **) p) instead, but that feels
ugly because it g_strv_length() is not designed to operate on
arbitrary pointer arrays.
2017-04-12 11:24:03 +02:00
Thomas Haller
d5bcc5826e shared: move NM_UTILS_LOOKUP() macro shared utils 2017-04-05 16:53:06 +02:00
Thomas Haller
f53218ed7c cli: add property type for enum and showcase for ipv6.addr-gen-mode 2017-03-30 13:09:58 +02:00
Thomas Haller
e4c0a4d0f2 shared: minor change to NM_FLAGS_HAS() and nm_utils_is_power_of_two() macros
NM_FLAGS_HAS() should reject negative flag values. So check for > 0.
Also change parentheses and line wrap.
2017-03-23 18:04:13 +01:00
Beniamino Galvani
f4a8a7f2a6 shared: increase max number of args for _NM_UTILS_MACRO_REST
30 should be enough for anybody.
2017-03-21 18:46:52 +01:00
Thomas Haller
c59532ee40 shared: trigger -Wenum-conversion warning in NM_IN_SET*() macros
and add NM_IN_SET*_TYPED() macros, which allow to explicitly select
the type of "x".
2017-03-16 18:27:33 +01:00
Thomas Haller
c2dc1c6fa3 shared/trivial: minor style fixes in "nm-utils/nm-macros-internal.h" 2017-03-16 12:09:22 +01:00
Thomas Haller
7d88bd24f3 shared: add _NM_GET_PRIVATE_VOID() macro
_NM_GET_PRIVATE() macro is used to implement a standard private-getter, but it
requires that "self" is a pointer of either "const type *" or "type *". That
is great in most cases, but sometimes we have predominatly self pointers of
different type, so it would require a lot of casts.

Add a different form _NM_GET_PRIVATE_VOID() where self pointer can be any
non-const pointer and returns a non-const private pointer after casting.
2017-03-10 11:06:02 +01:00
Thomas Haller
7e3b47deac shared: cleanup nm_strquote_a() and nm_sprintf_bufa()
- simplify nm_strquote_a().

- use '"' for quoting instad of '\''.

- have nm_sprintf_bufa() evalute @n_elements only once.
2017-02-10 17:31:16 +01:00
Thomas Haller
23040d68fc shared: add NM_CACHED_QUARK() and NM_CACHED_QUARK_FCN() macros
NM_CACHED_QUARK_FCN() is a replacement for G_DEFINE_QUARK().
G_DEFINE_QUARK() is mostly used to define GError quarks. As
such, it always appends _quark() to the function name, which
is unfavorable because it makes it harder to grep for the
definition of the function.

In general I think that macros that defined symbols by concatenating
something should be avoided because that makes it harder to locate
where the symbol was defined.
2017-02-10 14:33:52 +01:00
Thomas Haller
61da320d62 shared: add nm_offsetofend() macro 2017-01-16 17:20:35 +01:00
Thomas Haller
142009c6a5 shared: add nm_str_realloc() 2017-01-09 14:50:33 +01:00
Thomas Haller
207a663948 shared: add nm_str_skip_leading_spaces() macro 2016-12-25 15:38:00 +01:00
Thomas Haller
d2b9f37455 libnm: add public macro NM_VERSION for the current build-time version of libnm 2016-12-20 16:51:52 +01:00
Thomas Haller
99e1e4d6a1 shared: add NM_AUTO_PROTECT_ERRNO
Similar to systemd's PROTECT_ERRNO. The difference it, that it doesn't
treat the auto-variable as internal, so it is allowed to use it. E.g.

    if (!(fd = open (...)) {
        NM_AUTO_PROTECT_ERRNO (errno_saved);
        printf ("error: %s", g_strerror (errno_saved));
        return FALSE;
    }
2016-12-13 11:26:58 +01:00
Thomas Haller
312cea870d shared: add nm_auto_close and nm_auto_fclose
We already have gs_fd_close, which however doesn't preserve
errno and only checks for fd != -1. Add our own define.

Downside is, we have to include stdio.h and errno.h,
which effectively ends up to be included *everywhere*.
2016-12-13 11:26:58 +01:00
Thomas Haller
858daf57bc shared: add NM_STRCHAR_ALL() and NM_STRCHAR_ANY() macros 2016-11-09 12:07:33 +01:00
Thomas Haller
ba950cedee shared: add nm_assert_se() macro
We usually don't build NM with g_assert() disabled (G_DISABLE_ASSERT).
But even if we would, there is no assertion macro that always evaluates
the condition for possible side effects.

I think that is a useful thing to have.
2016-10-28 16:28:29 +02:00
Thomas Haller
34970e4141 shared: make nm_str_not_empty() inline function instead of macro
It was a macro to pass on the non-const-ness of the argument, but
that just doesn't make sense.

That is a signature

  char *nm_str_not_empty (char *)

does not make sense, because you cannot transfer ownership
conditionally without additional checks to avoid a leak. Which makes
this form is pointless. For example:

    char *
    foo (void)
    {
        char *s;

        s = _create_value ();
        return nm_str_not_empty (s); /* leaks "" */
    }
2016-10-24 10:14:02 +02:00
Thomas Haller
6b904a51ee shared: re-define _G_BOOLEAN_EXPR() to allow nesting g_assert()
g_assert() uses G_LIKELY(), which in turn uses _G_BOOLEAN_EXPR().
As glib's version of _G_BOOLEAN_EXPR() uses a local variable
_g_boolean_var_, we cannot nest a G_LIKELY() inside a G_LIKELY(),
or inside a g_assert(), or a g_assert() inside a g_assert().

Workaround that, by redefining the macro.

I already encountered this problem before, when having a nm_assert()
inside a ({...}) block, inside a g_assert(). Then I just avoided that
combination, but this situation is quite easy to encounter.
2016-10-11 13:14:43 +02:00
Thomas Haller
9ff1fa4ae1 shared: add NM_DECIMAL_STR_MAX() macro 2016-10-11 11:37:46 +02:00
Thomas Haller
c5f4e36fd9 shared: assert against buffer length in nm_sprintf_buf() 2016-10-11 11:37:46 +02:00
Thomas Haller
b4e66c4818 shared: add nm_clear_g_free() 2016-10-03 12:02:34 +02:00
Thomas Haller
a83eb773ce all: modify line separator comments to be 80 chars wide
sed 's#^/\*\{5\}\*\+/$#/*****************************************************************************/#' $(git grep -l '\*\{5\}' | grep '\.[hc]$') -i
2016-10-03 12:01:15 +02:00
Thomas Haller
ee86069601 shared: add test for NM_SET_OUT() 2016-09-26 17:00:38 +02:00
Thomas Haller
c3ecca225c core: add _nm_utils_array_find_binary_search()
Also add nm_cmp_uint32_p_with_data(). Will be used later.
2016-09-23 15:49:29 +02:00
Thomas Haller
b1fd5a06c4 macros: simplify NM_IN_SET() and NM_IN_STRSET() macros
and support up to 16 arguments.
2016-09-22 16:34:22 +02:00
Thomas Haller
2cae9ba348 shared: add _NM_GET_PRIVATE() macro 2016-09-08 00:21:21 +02:00
Thomas Haller
b2016fd2a5 shared: add NM_MIN()/NM_MAX() macros to replace glib's MIN()/MAX() 2016-09-08 00:21:21 +02:00
Thomas Haller
3227b9017b shared: add helper macros nm_str_not_empty() and nm_strdup_not_empty() 2016-09-06 16:07:02 +02:00
Thomas Haller
4041bf966f shared: add nm_strquote_a() helper 2016-07-05 23:08:22 +02:00
Beniamino Galvani
a5d1db08f8 dns: log DNS servers at TRACE level
Be more verbose at TRACE level and log the DNS servers associated to
configurations. This will help to debug issues like [0].

[0] https://bugzilla.redhat.com/show_bug.cgi?id=1348887
2016-06-28 16:24:28 +02:00
Thomas Haller
3bcec4067f shared: include "nm-glib.h" from "nm-macros-internal.h"
"nm-glib.h" is our most basic header. "nm-macros-internal.h" extends
on that. Thus, let "nm-macros-internal.h" include "nm-glib.h".
2016-06-16 10:45:54 +02:00
Thomas Haller
4b288136e1 shared: move shared files to subdirectory "shared/nm-utils/"
The "shared" directory contains files that are possibly used by all components
of NetworkManager repository.

Some of these files are even copied as-is to other projects (VPN plugins, nm-applet)
and used there without modification. Move those files to a separate directory.
By moving them to a common directory, it is clearer that they belong
together. Also, you can easier compare the copied versions to their
original via

  $ diff -r ./shared/nm-utils/ /path/to/nm-vpn-plugin/shared/nm-utils/
2016-06-16 10:45:53 +02:00