shared: move cmp functions to nm-shared-utils.c

For one, these functions are not often needed. No need to define them in the
"nm-macros-internal.h" header, which is included everywhere. Move them to
"nm-shared-utils.h", which must be explicitly included.

Also, these functions are usually not called directly, but by passing their
function pointer to a sort function or similar. There is no point in having
defined in the header file.
This commit is contained in:
Thomas Haller 2018-04-16 08:28:26 +02:00
parent 21f6058cfe
commit bc1b15cf05
3 changed files with 56 additions and 48 deletions

View file

@ -1092,54 +1092,6 @@ nm_strcmp_p (gconstpointer a, gconstpointer b)
return strcmp (s1, s2);
}
/* like nm_strcmp_p(), suitable for g_ptr_array_sort_with_data().
* g_ptr_array_sort() just casts nm_strcmp_p() to a function of different
* signature. I guess, in glib there are knowledgeable people that ensure
* that this additional argument doesn't cause problems due to different ABI
* for every architecture that glib supports.
* For NetworkManager, we'd rather avoid such stunts.
**/
static inline int
nm_strcmp_p_with_data (gconstpointer a, gconstpointer b, gpointer user_data)
{
const char *s1 = *((const char **) a);
const char *s2 = *((const char **) b);
return strcmp (s1, s2);
}
static inline int
nm_cmp_uint32_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data)
{
const guint32 a = *((const guint32 *) p_a);
const guint32 b = *((const guint32 *) p_b);
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
static inline int
nm_cmp_int2ptr_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data)
{
/* p_a and p_b are two pointers to a pointer, where the pointer is
* interpreted as a integer using GPOINTER_TO_INT().
*
* That is the case of a hash-table that uses GINT_TO_POINTER() to
* convert integers as pointers, and the resulting keys-as-array
* array. */
const int a = GPOINTER_TO_INT (*((gconstpointer *) p_a));
const int b = GPOINTER_TO_INT (*((gconstpointer *) p_b));
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
/*****************************************************************************/
/* Taken from systemd's UNIQ_T and UNIQ macros. */

View file

@ -499,6 +499,56 @@ _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 ma
/*****************************************************************************/
/* like nm_strcmp_p(), suitable for g_ptr_array_sort_with_data().
* g_ptr_array_sort() just casts nm_strcmp_p() to a function of different
* signature. I guess, in glib there are knowledgeable people that ensure
* that this additional argument doesn't cause problems due to different ABI
* for every architecture that glib supports.
* For NetworkManager, we'd rather avoid such stunts.
**/
int
nm_strcmp_p_with_data (gconstpointer a, gconstpointer b, gpointer user_data)
{
const char *s1 = *((const char **) a);
const char *s2 = *((const char **) b);
return strcmp (s1, s2);
}
int
nm_cmp_uint32_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data)
{
const guint32 a = *((const guint32 *) p_a);
const guint32 b = *((const guint32 *) p_b);
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
int
nm_cmp_int2ptr_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data)
{
/* p_a and p_b are two pointers to a pointer, where the pointer is
* interpreted as a integer using GPOINTER_TO_INT().
*
* That is the case of a hash-table that uses GINT_TO_POINTER() to
* convert integers as pointers, and the resulting keys-as-array
* array. */
const int a = GPOINTER_TO_INT (*((gconstpointer *) p_a));
const int b = GPOINTER_TO_INT (*((gconstpointer *) p_b));
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
/*****************************************************************************/
/**
* nm_utils_strsplit_set:
* @str: the string to split.

View file

@ -440,6 +440,12 @@ nm_g_variant_unref_floating (GVariant *var)
/*****************************************************************************/
int nm_strcmp_p_with_data (gconstpointer a, gconstpointer b, gpointer user_data);
int nm_cmp_uint32_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data);
int nm_cmp_int2ptr_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data);
/*****************************************************************************/
typedef struct {
const char *name;
} NMUtilsNamedEntry;