hashtable: silence -fsanitize=address warning for `hashlittle()` function
[lttng-tools.git] / src / common / hashtable / utils.c
index 3a0e2b76c8e644d0b4930214e2ac04ab8bd88e93..2c41e6c65f4060f31439640cec8c8c6d62b724c1 100644 (file)
@@ -1,20 +1,10 @@
 /*
- * Copyright (C) - Bob Jenkins, May 2006
- * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
- * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright (C) 2006 Bob Jenkins
+ * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
+ * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 /*
@@ -49,6 +39,7 @@
  * byte), but shoehorning those bytes into integers efficiently is messy.
  */
 
+#define _LGPL_SOURCE
 #include <assert.h>
 #include <stdint.h>     /* defines uint32_t etc */
 #include <stdio.h>      /* defines printf for tests */
@@ -59,6 +50,8 @@
 
 #include "utils.h"
 #include <common/compat/endian.h>    /* attempt to define endianness */
+#include <common/common.h>
+#include <common/hashtable/hashtable.h>
 
 /*
  * My best guess at if you are big-endian or little-endian.  This may
@@ -271,7 +264,9 @@ static void __attribute__((unused)) hashword2(const uint32_t *k, size_t length,
  * Use for hash table lookup, or anything where one collision in 2^^32 is
  * acceptable.  Do NOT use for cryptographic purposes.
  */
-static uint32_t __attribute__((unused)) hashlittle(const void *key,
+LTTNG_NO_SANITIZE_ADDRESS
+__attribute__((unused))
+static uint32_t hashlittle(const void *key,
                size_t length, uint32_t initval)
 {
        uint32_t a,b,c;
@@ -445,11 +440,8 @@ static uint32_t __attribute__((unused)) hashlittle(const void *key,
        return c;
 }
 
-#if (CAA_BITS_PER_LONG == 64)
-/*
- * Hash function for number value.
- */
-unsigned long hash_key_ulong(void *_key, unsigned long seed)
+LTTNG_HIDDEN
+unsigned long hash_key_u64(const void *_key, unsigned long seed)
 {
        union {
                uint64_t v64;
@@ -461,15 +453,29 @@ unsigned long hash_key_ulong(void *_key, unsigned long seed)
        } key;
 
        v.v64 = (uint64_t) seed;
-       key.v64 = (uint64_t) _key;
+       key.v64 = *(const uint64_t *) _key;
        hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
        return v.v64;
 }
+
+#if (CAA_BITS_PER_LONG == 64)
+/*
+ * Hash function for number value.
+ * Pass the value itself as the key, not its address.
+ */
+LTTNG_HIDDEN
+unsigned long hash_key_ulong(const void *_key, unsigned long seed)
+{
+       uint64_t __key = (uint64_t) _key;
+       return (unsigned long) hash_key_u64(&__key, seed);
+}
 #else
 /*
  * Hash function for number value.
+ * Pass the value itself as the key, not its address.
  */
-unsigned long hash_key_ulong(void *_key, unsigned long seed)
+LTTNG_HIDDEN
+unsigned long hash_key_ulong(const void *_key, unsigned long seed)
 {
        uint32_t key = (uint32_t) _key;
 
@@ -480,15 +486,29 @@ unsigned long hash_key_ulong(void *_key, unsigned long seed)
 /*
  * Hash function for string.
  */
-unsigned long hash_key_str(void *key, unsigned long seed)
+LTTNG_HIDDEN
+unsigned long hash_key_str(const void *key, unsigned long seed)
 {
-       return hashlittle(key, strlen((char *) key), seed);
+       return hashlittle(key, strlen((const char *) key), seed);
+}
+
+/*
+ * Hash function for two uint64_t.
+ */
+LTTNG_HIDDEN
+unsigned long hash_key_two_u64(const void *key, unsigned long seed)
+{
+       const struct lttng_ht_two_u64 *k =
+               (const struct lttng_ht_two_u64 *) key;
+
+       return hash_key_u64(&k->key1, seed) ^ hash_key_u64(&k->key2, seed);
 }
 
 /*
  * Hash function compare for number value.
  */
-int hash_match_key_ulong(void *key1, void *key2)
+LTTNG_HIDDEN
+int hash_match_key_ulong(const void *key1, const void *key2)
 {
        if (key1 == key2) {
                return 1;
@@ -497,10 +517,24 @@ int hash_match_key_ulong(void *key1, void *key2)
        return 0;
 }
 
+/*
+ * Hash function compare for number value.
+ */
+LTTNG_HIDDEN
+int hash_match_key_u64(const void *key1, const void *key2)
+{
+       if (*(const uint64_t *) key1 == *(const uint64_t *) key2) {
+               return 1;
+       }
+
+       return 0;
+}
+
 /*
  * Hash compare function for string.
  */
-int hash_match_key_str(void *key1, void *key2)
+LTTNG_HIDDEN
+int hash_match_key_str(const void *key1, const void *key2)
 {
        if (strcmp(key1, key2) == 0) {
                return 1;
@@ -508,3 +542,22 @@ int hash_match_key_str(void *key1, void *key2)
 
        return 0;
 }
+
+/*
+ * Hash function compare two uint64_t.
+ */
+LTTNG_HIDDEN
+int hash_match_key_two_u64(const void *key1, const void *key2)
+{
+       const struct lttng_ht_two_u64 *k1 =
+               (const struct lttng_ht_two_u64 *) key1;
+       const struct lttng_ht_two_u64 *k2 =
+               (const struct lttng_ht_two_u64 *) key2;
+
+       if (hash_match_key_u64(&k1->key1, &k2->key1) &&
+                       hash_match_key_u64(&k1->key2, &k2->key2)) {
+               return 1;
+       }
+
+       return 0;
+}
This page took 0.024982 seconds and 4 git commands to generate.