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).
This commit is contained in:
Thomas Haller 2017-10-13 11:51:18 +02:00
parent fb6fecc036
commit 4a2798434e
10 changed files with 78 additions and 57 deletions

View file

@ -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)

View file

@ -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));

View file

@ -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.
*

View file

@ -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,

View file

@ -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);

View file

@ -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);

View file

@ -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++)

View file

@ -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:

View file

@ -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));
})