summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-10-13 11:51:18 +0200
committerThomas Haller <thaller@redhat.com>2017-10-13 12:47:55 +0200
commit4a2798434ef162b31a129cb6a857c950ec992f3e (patch)
tree38f6f0d3ca22657ae0bac1afa3c850f33516b001
parentfb6fecc036f466f569a7d05ed0d499164e032f15 (diff)
downloadNetworkManager-4a2798434ef162b31a129cb6a857c950ec992f3e.tar.gz
core: introduce NM_HASH_INIT() to initialize hash seed
Introduce a NM_HASH_INIT() function. It makes the places where we initialize a hash with a certain seed visually clear. Also, move them from "shared/nm-utils/nm-shared-utils.h" to "shared/nm-utils/nm-macros-internal.h". We might want to have NM_HASH_INIT() non-inline (hence, define it in the source file).
-rw-r--r--libnm-core/nm-utils.c2
-rw-r--r--shared/nm-utils/nm-dedup-multi.c2
-rw-r--r--shared/nm-utils/nm-macros-internal.h15
-rw-r--r--shared/nm-utils/nm-shared-utils.h28
-rw-r--r--src/devices/nm-device.c2
-rw-r--r--src/devices/nm-lldp-listener.c2
-rw-r--r--src/nm-core-utils.c2
-rw-r--r--src/nm-session-monitor.c6
-rw-r--r--src/platform/nm-platform.c30
-rw-r--r--src/platform/nmp-object.c46
10 files changed, 78 insertions, 57 deletions
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index 4a46681c47..5b8af9b205 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -4006,7 +4006,7 @@ _nm_utils_strstrdictkey_hash (gconstpointer a)
{
const NMUtilsStrStrDictKey *k = a;
const signed char *p;
- guint32 h = 5381;
+ guint32 h = NM_HASH_INIT (76642997u);
if (k) {
if (((int) k->type) & ~STRSTRDICTKEY_ALL_SET)
diff --git a/shared/nm-utils/nm-dedup-multi.c b/shared/nm-utils/nm-dedup-multi.c
index a1b6da114d..e7616074b0 100644
--- a/shared/nm-utils/nm-dedup-multi.c
+++ b/shared/nm-utils/nm-dedup-multi.c
@@ -182,7 +182,7 @@ _dict_idx_entries_hash (const NMDedupMultiEntry *entry)
nm_assert (obj);
h = idx_type->klass->idx_obj_partition_hash (idx_type, obj);
} else
- h = 1914869417;
+ h = NM_HASH_INIT (1914869417u);
if (!lookup_head)
h = NM_HASH_COMBINE (h, idx_type->klass->idx_obj_id_hash (idx_type, obj));
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index 435cef6568..6b70693258 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -426,21 +426,6 @@ _NM_IN_STRSET_streq (const char *x, const char *s)
/*****************************************************************************/
-static inline guint
-NM_HASH_COMBINE (guint h, guint val)
-{
- /* see g_str_hash() for reasons */
- return (h << 5) + h + val;
-}
-
-static inline guint
-NM_HASH_COMBINE_UINT64 (guint h, guint64 val)
-{
- return NM_HASH_COMBINE (h, (((guint) val) & 0xFFFFFFFFu) + ((guint) (val >> 32)));
-}
-
-/*****************************************************************************/
-
/* NM_CACHED_QUARK() returns the GQuark for @string, but caches
* it in a static variable to speed up future lookups.
*
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
index e7cfec0817..57f1619245 100644
--- a/shared/nm-utils/nm-shared-utils.h
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -378,6 +378,34 @@ GParamSpec *nm_g_object_class_find_property_from_gtype (GType gtype,
/*****************************************************************************/
+static inline guint
+NM_HASH_INIT (guint seed)
+{
+ return seed;
+}
+
+static inline guint
+NM_HASH_COMBINE (guint h, guint val)
+{
+ /* see g_str_hash() for reasons */
+ return (h << 5) + h + val;
+}
+
+static inline guint
+NM_HASH_COMBINE_UINT64 (guint h, guint64 val)
+{
+ return NM_HASH_COMBINE (h, (((guint) val) & 0xFFFFFFFFu) + ((guint) (val >> 32)));
+}
+
+static inline guint
+NM_HASH_POINTER (gconstpointer ptr)
+{
+ /* same as g_direct_hash(), but inline. */
+ return GPOINTER_TO_UINT (ptr);
+}
+
+/*****************************************************************************/
+
typedef enum {
NM_UTILS_STR_UTF8_SAFE_FLAG_NONE = 0,
NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL = 0x0001,
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c
index 3000adb32f..c8ec439e5a 100644
--- a/src/devices/nm-device.c
+++ b/src/devices/nm-device.c
@@ -2858,7 +2858,7 @@ typedef struct {
static guint
_v4_has_shadowed_routes_detect_hash (const IP4RPFilterData *d)
{
- guint h = 0;
+ guint h = NM_HASH_INIT (1105201169u);
h = NM_HASH_COMBINE (h, d->network);
h = NM_HASH_COMBINE (h, d->plen);
diff --git a/src/devices/nm-lldp-listener.c b/src/devices/nm-lldp-listener.c
index 8f12fd8595..28e7fd6c77 100644
--- a/src/devices/nm-lldp-listener.c
+++ b/src/devices/nm-lldp-listener.c
@@ -274,7 +274,7 @@ static guint
lldp_neighbor_id_hash (gconstpointer ptr)
{
const LldpNeighbor *neigh = ptr;
- guint hash = 23423423u;
+ guint hash = NM_HASH_INIT (23423423u);
hash = NM_HASH_COMBINE (hash, neigh->chassis_id ? g_str_hash (neigh->chassis_id) : 12321u);
hash = NM_HASH_COMBINE (hash, neigh->port_id ? g_str_hash (neigh->port_id) : 34342343u);
diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c
index 42bd47c1ba..48a4aa25db 100644
--- a/src/nm-core-utils.c
+++ b/src/nm-core-utils.c
@@ -188,7 +188,7 @@ nm_utils_exp10 (gint16 ex)
guint
nm_utils_in6_addr_hash (const struct in6_addr *addr)
{
- guint hash = (guint) 0x897da53981a13ULL;
+ guint hash = NM_HASH_INIT (3675559913u);
int i;
for (i = 0; i < sizeof (*addr); i++)
diff --git a/src/nm-session-monitor.c b/src/nm-session-monitor.c
index 151deec899..20781bd45d 100644
--- a/src/nm-session-monitor.c
+++ b/src/nm-session-monitor.c
@@ -260,9 +260,9 @@ ck_init (NMSessionMonitor *monitor)
if ((monitor->ck.monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error))) {
monitor->ck.cache = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
g_signal_connect (monitor->ck.monitor,
- "changed",
- G_CALLBACK (ck_changed),
- monitor);
+ "changed",
+ G_CALLBACK (ck_changed),
+ monitor);
} else {
_LOGE ("error monitoring " CKDB_PATH ": %s", error->message);
g_clear_error (&error);
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
index c04d4f85d3..eedd2d8243 100644
--- a/src/platform/nm-platform.c
+++ b/src/platform/nm-platform.c
@@ -5126,7 +5126,7 @@ nm_platform_ip6_route_to_string (const NMPlatformIP6Route *route, char *buf, gsi
guint
nm_platform_link_hash (const NMPlatformLink *obj)
{
- guint h = 99413953;
+ guint h = NM_HASH_INIT (99413953u);
guint8 i8;
h = NM_HASH_COMBINE (h, obj->ifindex);
@@ -5187,7 +5187,7 @@ nm_platform_link_cmp (const NMPlatformLink *a, const NMPlatformLink *b)
guint
nm_platform_lnk_gre_hash (const NMPlatformLnkGre *obj)
{
- guint h = 1887023311;
+ guint h = NM_HASH_INIT (1887023311u);
h = NM_HASH_COMBINE (h, obj->parent_ifindex);
h = NM_HASH_COMBINE (h, obj->input_flags);
@@ -5222,7 +5222,7 @@ nm_platform_lnk_gre_cmp (const NMPlatformLnkGre *a, const NMPlatformLnkGre *b)
guint
nm_platform_lnk_infiniband_hash (const NMPlatformLnkInfiniband *obj)
{
- guint h = 1748638583;
+ guint h = NM_HASH_INIT (1748638583u);
h = NM_HASH_COMBINE (h, obj->p_key);
if (obj->mode)
@@ -5242,7 +5242,7 @@ nm_platform_lnk_infiniband_cmp (const NMPlatformLnkInfiniband *a, const NMPlatfo
guint
nm_platform_lnk_ip6tnl_hash (const NMPlatformLnkIp6Tnl *obj)
{
- guint h = 1651660009;
+ guint h = NM_HASH_INIT (1651660009u);
h = NM_HASH_COMBINE (h, obj->parent_ifindex);
h = NM_HASH_COMBINE_IN6ADDR (h, &obj->local);
@@ -5273,7 +5273,7 @@ nm_platform_lnk_ip6tnl_cmp (const NMPlatformLnkIp6Tnl *a, const NMPlatformLnkIp6
guint
nm_platform_lnk_ipip_hash (const NMPlatformLnkIpIp *obj)
{
- guint h = 861934429;
+ guint h = NM_HASH_INIT (861934429u);
h = NM_HASH_COMBINE (h, obj->parent_ifindex);
h = NM_HASH_COMBINE (h, obj->local);
@@ -5300,7 +5300,7 @@ nm_platform_lnk_ipip_cmp (const NMPlatformLnkIpIp *a, const NMPlatformLnkIpIp *b
guint
nm_platform_lnk_macsec_hash (const NMPlatformLnkMacsec *obj)
{
- guint h = 226984267;
+ guint h = NM_HASH_INIT (226984267u);
h = NM_HASH_COMBINE (h, obj->sci);
h = NM_HASH_COMBINE_UINT64 (h, obj->icv_length);
@@ -5339,7 +5339,7 @@ nm_platform_lnk_macsec_cmp (const NMPlatformLnkMacsec *a, const NMPlatformLnkMac
guint
nm_platform_lnk_macvlan_hash (const NMPlatformLnkMacvlan *obj)
{
- guint h = 771014989;
+ guint h = NM_HASH_INIT (771014989u);
h = NM_HASH_COMBINE (h, obj->mode);
h = NM_HASH_COMBINE (h, obj->no_promisc);
@@ -5360,7 +5360,7 @@ nm_platform_lnk_macvlan_cmp (const NMPlatformLnkMacvlan *a, const NMPlatformLnkM
guint
nm_platform_lnk_sit_hash (const NMPlatformLnkSit *obj)
{
- guint h = 1690154969;
+ guint h = NM_HASH_INIT (1690154969u);
h = NM_HASH_COMBINE (h, obj->parent_ifindex);
h = NM_HASH_COMBINE (h, obj->local);
@@ -5391,7 +5391,7 @@ nm_platform_lnk_sit_cmp (const NMPlatformLnkSit *a, const NMPlatformLnkSit *b)
guint
nm_platform_lnk_vlan_hash (const NMPlatformLnkVlan *obj)
{
- guint h = 58751383;
+ guint h = NM_HASH_INIT (58751383u);
h = NM_HASH_COMBINE (h, obj->id);
h = NM_HASH_COMBINE (h, obj->flags);
@@ -5410,7 +5410,7 @@ nm_platform_lnk_vlan_cmp (const NMPlatformLnkVlan *a, const NMPlatformLnkVlan *b
guint
nm_platform_lnk_vxlan_hash (const NMPlatformLnkVxlan *obj)
{
- guint h = 461041297;
+ guint h = NM_HASH_INIT (461041297u);
h = NM_HASH_COMBINE (h, obj->parent_ifindex);
h = NM_HASH_COMBINE (h, obj->id);
@@ -5461,7 +5461,7 @@ nm_platform_lnk_vxlan_cmp (const NMPlatformLnkVxlan *a, const NMPlatformLnkVxlan
guint
nm_platform_ip4_address_hash (const NMPlatformIP4Address *obj)
{
- guint h = 469681301;
+ guint h = NM_HASH_INIT (469681301u);
if (obj) {
h = NM_HASH_COMBINE (h, obj->ifindex);
@@ -5498,7 +5498,7 @@ nm_platform_ip4_address_cmp (const NMPlatformIP4Address *a, const NMPlatformIP4A
guint
nm_platform_ip6_address_hash (const NMPlatformIP6Address *obj)
{
- guint h = 605908909;
+ guint h = NM_HASH_INIT (605908909u);
if (obj) {
h = NM_HASH_COMBINE (h, obj->ifindex);
@@ -5537,8 +5537,9 @@ nm_platform_ip6_address_cmp (const NMPlatformIP6Address *a, const NMPlatformIP6A
guint
nm_platform_ip4_route_hash (const NMPlatformIP4Route *obj, NMPlatformIPRouteCmpType cmp_type)
{
- guint h = NM_HASH_COMBINE (1228913327, cmp_type);
+ guint h = NM_HASH_INIT (1228913327u);
+ h = NM_HASH_COMBINE (h, cmp_type);
if (obj) {
switch (cmp_type) {
case NM_PLATFORM_IP_ROUTE_CMP_TYPE_WEAK_ID:
@@ -5688,8 +5689,9 @@ nm_platform_ip4_route_cmp (const NMPlatformIP4Route *a, const NMPlatformIP4Route
guint
nm_platform_ip6_route_hash (const NMPlatformIP6Route *obj, NMPlatformIPRouteCmpType cmp_type)
{
- guint h = NM_HASH_COMBINE (1053326051, cmp_type);
+ guint h = NM_HASH_INIT (1053326051u);
+ h = NM_HASH_COMBINE (h, cmp_type);
if (obj) {
switch (cmp_type) {
case NM_PLATFORM_IP_ROUTE_CMP_TYPE_WEAK_ID:
diff --git a/src/platform/nmp-object.c b/src/platform/nmp-object.c
index e58868645e..24651a66d0 100644
--- a/src/platform/nmp-object.c
+++ b/src/platform/nmp-object.c
@@ -150,7 +150,8 @@ _idx_obj_part (const DedupMultiIdxType *idx_type,
if (obj_b)
return NMP_OBJECT_GET_TYPE (obj_a) == NMP_OBJECT_GET_TYPE (obj_b);
if (request_hash) {
- h = (guint) idx_type->cache_id_type;
+ h = NM_HASH_INIT (487703243u);
+ h = NM_HASH_COMBINE (h, idx_type->cache_id_type);
h = NM_HASH_COMBINE (h, NMP_OBJECT_GET_TYPE (obj_a));
return _HASH_NON_ZERO (h);
}
@@ -170,7 +171,8 @@ _idx_obj_part (const DedupMultiIdxType *idx_type,
}
if (request_hash) {
/* we request a hash from obj_a. Hash the relevant parts. */
- h = (guint) idx_type->cache_id_type;
+ h = NM_HASH_INIT (2126752699u);
+ h = NM_HASH_COMBINE (h, idx_type->cache_id_type);
h = NM_HASH_COMBINE (h, g_str_hash (obj_a->link.name));
return _HASH_NON_ZERO (h);
}
@@ -189,7 +191,8 @@ _idx_obj_part (const DedupMultiIdxType *idx_type,
&& nmp_object_is_visible (obj_b);
}
if (request_hash) {
- h = (guint) idx_type->cache_id_type;
+ h = NM_HASH_INIT (4278960223u);
+ h = NM_HASH_COMBINE (h, idx_type->cache_id_type);
h = NM_HASH_COMBINE (h, NMP_OBJECT_GET_TYPE (obj_a));
return _HASH_NON_ZERO (h);
}
@@ -209,7 +212,8 @@ _idx_obj_part (const DedupMultiIdxType *idx_type,
&& nmp_object_is_visible (obj_b);
}
if (request_hash) {
- h = (guint) idx_type->cache_id_type;
+ h = NM_HASH_INIT (920415631u);
+ h = NM_HASH_COMBINE (h, idx_type->cache_id_type);
h = NM_HASH_COMBINE (h, NMP_OBJECT_GET_TYPE (obj_a));
h = NM_HASH_COMBINE (h, obj_a->object.ifindex);
return _HASH_NON_ZERO (h);
@@ -230,7 +234,8 @@ _idx_obj_part (const DedupMultiIdxType *idx_type,
: (nm_platform_ip6_route_cmp (&obj_a->ip6_route, &obj_b->ip6_route, NM_PLATFORM_IP_ROUTE_CMP_TYPE_WEAK_ID) == 0));
}
if (request_hash) {
- h = (guint) idx_type->cache_id_type;
+ h = NM_HASH_INIT (778646573u);
+ h = NM_HASH_COMBINE (h, idx_type->cache_id_type);
if (obj_type == NMP_OBJECT_TYPE_IP4_ROUTE)
h = NM_HASH_COMBINE (h, nm_platform_ip4_route_hash (&obj_a->ip4_route, NM_PLATFORM_IP_ROUTE_CMP_TYPE_WEAK_ID));
else
@@ -300,7 +305,7 @@ static guint
_vlan_xgress_qos_mappings_hash (guint n_map,
const NMVlanQosMapping *map)
{
- guint h = 1453577309;
+ guint h = NM_HASH_INIT (1453577309u);
guint i;
for (i = 0; i < n_map; i++) {
@@ -756,19 +761,20 @@ nmp_object_hash (const NMPObject *obj)
klass = NMP_OBJECT_GET_CLASS (obj);
+ h = NM_HASH_INIT (816200863u);
if (klass->cmd_obj_hash)
- h = klass->cmd_obj_hash (obj);
+ h = NM_HASH_COMBINE (h, klass->cmd_obj_hash (obj));
else if (klass->cmd_plobj_hash)
- h = klass->cmd_plobj_hash (&obj->object);
+ h = NM_HASH_COMBINE (h, klass->cmd_plobj_hash (&obj->object));
else
- return GPOINTER_TO_UINT (obj);
+ return NM_HASH_POINTER (obj);
return NM_HASH_COMBINE (h, klass->obj_type);
}
static guint
_vt_cmd_obj_hash_link (const NMPObject *obj)
{
- guint h = 1228913327;
+ guint h = NM_HASH_INIT (3448776161u);
nm_assert (NMP_OBJECT_GET_TYPE (obj) == NMP_OBJECT_TYPE_LINK);
@@ -783,7 +789,7 @@ _vt_cmd_obj_hash_link (const NMPObject *obj)
static guint
_vt_cmd_obj_hash_lnk_vlan (const NMPObject *obj)
{
- guint h = 914932607;
+ guint h = NM_HASH_INIT (914932607u);
nm_assert (NMP_OBJECT_GET_TYPE (obj) == NMP_OBJECT_TYPE_LNK_VLAN);
@@ -1077,7 +1083,7 @@ nmp_object_id_hash (const NMPObject *obj)
if (!klass->cmd_plobj_id_hash) {
/* The klass doesn't implement ID compare. It means, to use pointer
* equality (g_direct_hash). */
- return g_direct_hash (obj);
+ return NM_HASH_POINTER (obj);
}
return klass->cmd_plobj_id_hash (&obj->object);
@@ -1093,29 +1099,29 @@ _vt_cmd_plobj_id_hash_##type (const NMPlatformObject *_obj) \
return hash; \
}
_vt_cmd_plobj_id_hash (link, NMPlatformLink, {
- hash = (guint) 3982791431u;
- hash = hash + ((guint) obj->ifindex);
+ hash = NM_HASH_INIT (3982791431u);
+ hash = NM_HASH_COMBINE (hash, ((guint) obj->ifindex));
})
_vt_cmd_plobj_id_hash (ip4_address, NMPlatformIP4Address, {
- hash = (guint) 3591309853u;
- hash = hash + ((guint) obj->ifindex);
+ hash = NM_HASH_INIT (3591309853u);
+ hash = NM_HASH_COMBINE (hash, ((guint) obj->ifindex));
hash = NM_HASH_COMBINE (hash, obj->plen);
hash = NM_HASH_COMBINE (hash, obj->address);
/* for IPv4 we must also consider the net-part of the peer-address (IFA_ADDRESS) */
hash = NM_HASH_COMBINE (hash, (obj->peer_address & _nm_utils_ip4_prefix_to_netmask (obj->plen)));
})
_vt_cmd_plobj_id_hash (ip6_address, NMPlatformIP6Address, {
- hash = (guint) 2907861637u;
- hash = hash + ((guint) obj->ifindex);
+ hash = NM_HASH_INIT (2907861637u);
+ hash = NM_HASH_COMBINE (hash, ((guint) obj->ifindex));
/* for IPv6 addresses, the prefix length is not part of the primary identifier. */
hash = NM_HASH_COMBINE (hash, nm_utils_in6_addr_hash (&obj->address));
})
_vt_cmd_plobj_id_hash (ip4_route, NMPlatformIP4Route, {
- hash = (guint) 1038302471u;
+ hash = NM_HASH_INIT (1038302471u);
hash = NM_HASH_COMBINE (hash, nm_platform_ip4_route_hash (obj, NM_PLATFORM_IP_ROUTE_CMP_TYPE_ID));
})
_vt_cmd_plobj_id_hash (ip6_route, NMPlatformIP6Route, {
- hash = (guint) 1233384151u;
+ hash = NM_HASH_INIT (1233384151u);
hash = NM_HASH_COMBINE (hash, nm_platform_ip6_route_hash (obj, NM_PLATFORM_IP_ROUTE_CMP_TYPE_ID));
})