diff mbox series

[Bluez,v1,01/14] lib: add hash functions for bt_uuid_t

Message ID 20210708142059.Bluez.v1.1.I69278fab3bf86adb578c5cba0a39e5bcf7f9581e@changeid
State New
Headers show
Series [Bluez,v1,01/14] lib: add hash functions for bt_uuid_t | expand

Commit Message

Yun-hao Chung July 8, 2021, 6:23 a.m. UTC
This adds function GHashFunc and GEqualFunc for bt_uuid_t.
With these functions, we can add uuids into a GHashTable with bt_uuid_t
format.

Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
---

 lib/uuid.c | 27 +++++++++++++++++++++++++++
 lib/uuid.h |  3 +++
 2 files changed, 30 insertions(+)

Comments

Luiz Augusto von Dentz July 9, 2021, 5:21 a.m. UTC | #1
Hi Howard,

On Wed, Jul 7, 2021 at 11:23 PM Howard Chung <howardchung@google.com> wrote:
>
> This adds function GHashFunc and GEqualFunc for bt_uuid_t.
> With these functions, we can add uuids into a GHashTable with bt_uuid_t
> format.

We will likely move away from GLib dependency so I wouldn't want to
introduce a dependency to it specially as part of libbluetooth.

> Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
> ---
>
>  lib/uuid.c | 27 +++++++++++++++++++++++++++
>  lib/uuid.h |  3 +++
>  2 files changed, 30 insertions(+)
>
> diff --git a/lib/uuid.c b/lib/uuid.c
> index 3d97dc8359c7..a09f5c428b87 100644
> --- a/lib/uuid.c
> +++ b/lib/uuid.c
> @@ -16,6 +16,7 @@
>  #include <string.h>
>  #include <stdlib.h>
>  #include <errno.h>
> +#include <glib.h>
>
>  #include "lib/bluetooth.h"
>  #include "uuid.h"
> @@ -120,6 +121,32 @@ int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2)
>         return bt_uuid128_cmp(&u1, &u2);
>  }
>
> +guint bt_uuid_hash(gconstpointer key)
> +{
> +       const bt_uuid_t *uuid = key;
> +       bt_uuid_t uuid_128;
> +       uint64_t *val;
> +
> +       if (!uuid)
> +               return 0;
> +
> +       bt_uuid_to_uuid128(uuid, &uuid_128);
> +       val = (uint64_t *)&uuid_128.value.u128;
> +
> +       return g_int64_hash(val) ^ g_int64_hash(val+1);
> +}
> +
> +gboolean bt_uuid_equal(gconstpointer v1, gconstpointer v2)
> +{
> +       const bt_uuid_t *uuid1 = v1;
> +       const bt_uuid_t *uuid2 = v2;
> +
> +       if (!uuid1 || !uuid2)
> +               return !uuid1 && !uuid2;
> +
> +       return bt_uuid_cmp(uuid1, uuid2) == 0;
> +}
> +
>  /*
>   * convert the UUID to string, copying a maximum of n characters.
>   */
> diff --git a/lib/uuid.h b/lib/uuid.h
> index 1a4029b68730..e47ccccb9fd2 100644
> --- a/lib/uuid.h
> +++ b/lib/uuid.h
> @@ -17,6 +17,7 @@ extern "C" {
>  #endif
>
>  #include <stdint.h>
> +#include <glib.h>
>
>  #define GENERIC_AUDIO_UUID     "00001203-0000-1000-8000-00805f9b34fb"
>
> @@ -167,6 +168,8 @@ int bt_uuid128_create(bt_uuid_t *btuuid, uint128_t value);
>
>  int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2);
>  void bt_uuid_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst);
> +guint bt_uuid_hash(gconstpointer key);
> +gboolean bt_uuid_equal(gconstpointer v1, gconstpointer v2);
>
>  #define MAX_LEN_UUID_STR 37
>
> --
> 2.32.0.93.g670b81a890-goog
>
diff mbox series

Patch

diff --git a/lib/uuid.c b/lib/uuid.c
index 3d97dc8359c7..a09f5c428b87 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -16,6 +16,7 @@ 
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <glib.h>
 
 #include "lib/bluetooth.h"
 #include "uuid.h"
@@ -120,6 +121,32 @@  int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2)
 	return bt_uuid128_cmp(&u1, &u2);
 }
 
+guint bt_uuid_hash(gconstpointer key)
+{
+	const bt_uuid_t *uuid = key;
+	bt_uuid_t uuid_128;
+	uint64_t *val;
+
+	if (!uuid)
+		return 0;
+
+	bt_uuid_to_uuid128(uuid, &uuid_128);
+	val = (uint64_t *)&uuid_128.value.u128;
+
+	return g_int64_hash(val) ^ g_int64_hash(val+1);
+}
+
+gboolean bt_uuid_equal(gconstpointer v1, gconstpointer v2)
+{
+	const bt_uuid_t *uuid1 = v1;
+	const bt_uuid_t *uuid2 = v2;
+
+	if (!uuid1 || !uuid2)
+		return !uuid1 && !uuid2;
+
+	return bt_uuid_cmp(uuid1, uuid2) == 0;
+}
+
 /*
  * convert the UUID to string, copying a maximum of n characters.
  */
diff --git a/lib/uuid.h b/lib/uuid.h
index 1a4029b68730..e47ccccb9fd2 100644
--- a/lib/uuid.h
+++ b/lib/uuid.h
@@ -17,6 +17,7 @@  extern "C" {
 #endif
 
 #include <stdint.h>
+#include <glib.h>
 
 #define GENERIC_AUDIO_UUID	"00001203-0000-1000-8000-00805f9b34fb"
 
@@ -167,6 +168,8 @@  int bt_uuid128_create(bt_uuid_t *btuuid, uint128_t value);
 
 int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2);
 void bt_uuid_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst);
+guint bt_uuid_hash(gconstpointer key);
+gboolean bt_uuid_equal(gconstpointer v1, gconstpointer v2);
 
 #define MAX_LEN_UUID_STR 37