shared: add nm_strcmp0()

g_strcmp0() is not inlineable, so we first need to call to glib, only to
call to libc.
This commit is contained in:
Thomas Haller 2019-05-10 11:24:32 +02:00
parent 9e544d527a
commit 79ef1abcca
2 changed files with 32 additions and 3 deletions

View file

@ -865,6 +865,28 @@ fcn (void) \
/*****************************************************************************/
static inline int
nm_strcmp0 (const char *s1, const char *s2)
{
int c;
/* like g_strcmp0(), but this is inlinable.
*
* Also, it is guaranteed to return either -1, 0, or 1. */
if (s1 == s2)
return 0;
if (!s1)
return -1;
if (!s2)
return 1;
c = strcmp (s1, s2);
if (c < 0)
return -1;
if (c > 0)
return 1;
return 0;
}
static inline gboolean
nm_streq (const char *s1, const char *s2)
{

View file

@ -171,6 +171,13 @@ nm_ip4_addr_is_localhost (in_addr_t addr4)
return _cc < 0 ? -1 : 1; \
} G_STMT_END
#define NM_CMP_RETURN_DIRECT(c) \
G_STMT_START { \
const int _cc = (c); \
if (_cc) \
return _cc; \
} G_STMT_END
#define NM_CMP_SELF(a, b) \
G_STMT_START { \
typeof (a) _a = (a); \
@ -197,7 +204,7 @@ nm_ip4_addr_is_localhost (in_addr_t addr4)
NM_CMP_RETURN (memcmp ((a), (b), (size)))
#define NM_CMP_DIRECT_STRCMP0(a, b) \
NM_CMP_RETURN (g_strcmp0 ((a), (b)))
NM_CMP_RETURN_DIRECT (nm_strcmp0 ((a), (b)))
#define NM_CMP_DIRECT_IN6ADDR(a, b) \
G_STMT_START { \
@ -229,12 +236,12 @@ nm_ip4_addr_is_localhost (in_addr_t addr4)
const char *_b = ((b)->field); \
\
if (_a != _b) { \
NM_CMP_RETURN (g_strcmp0 (_a, _b)); \
NM_CMP_RETURN_DIRECT (nm_strcmp0 (_a, _b)); \
} \
} G_STMT_END
#define NM_CMP_FIELD_STR0(a, b, field) \
NM_CMP_RETURN (g_strcmp0 (((a)->field), ((b)->field)))
NM_CMP_RETURN_DIRECT (nm_strcmp0 (((a)->field), ((b)->field)))
#define NM_CMP_FIELD_MEMCMP_LEN(a, b, field, len) \
NM_CMP_RETURN (memcmp (&((a)->field), &((b)->field), \