fix cx_hash_key_cxstr() being a macro

Sun, 22 Dec 2024 22:53:53 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 22 Dec 2024 22:53:53 +0100
changeset 1049
415bf2ce6bab
parent 1048
12f38affefd5
child 1050
3df63e95921a

fix cx_hash_key_cxstr() being a macro

issue #536

CHANGELOG file | annotate | diff | comparison | revisions
src/cx/hash_key.h file | annotate | diff | comparison | revisions
--- a/CHANGELOG	Sun Dec 22 22:14:57 2024 +0100
+++ b/CHANGELOG	Sun Dec 22 22:53:53 2024 +0100
@@ -30,6 +30,7 @@
  * removes CMake
  * removes GTest dependency
  * removes flags to disable SBO in tests
+ * fixes cx_hash_key_cxstr() being a macro, evaluating the argument twice
  * fixes wrong link from UCX 2 documentation to UCX 3 documentation
  * fixes critical bug that produced wrong results when comparing lists of different type but same size
 
--- a/src/cx/hash_key.h	Sun Dec 22 22:14:57 2024 +0100
+++ b/src/cx/hash_key.h	Sun Dec 22 22:53:53 2024 +0100
@@ -38,6 +38,7 @@
 #define UCX_HASH_KEY_H
 
 #include "common.h"
+#include "string.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -112,22 +113,51 @@
  */
 cx_attr_nodiscard
 cx_attr_access_r(1, 2)
-__attribute__((__warn_unused_result__))
 CxHashKey cx_hash_key(
         const void *obj,
         size_t len
 );
 
+#ifdef __cplusplus
+} // extern "C"
+
+cx_attr_nodiscard
+static inline CxHashKey cx_hash_key_cxstr(cxstring str) {
+    return cx_hash_key(str.ptr, str.length);
+}
+
+cx_attr_nodiscard
+static inline CxHashKey cx_hash_key_cxstr(cxmutstr str) {
+    return cx_hash_key(str.ptr, str.length);
+}
+
+#else
 /**
  * Computes a hash key from a UCX string.
  *
  * @param str the string
  * @return the hash key
  */
-#define cx_hash_key_cxstr(str) cx_hash_key((void*)(str).ptr, (str).length)
+#define cx_hash_key_cxstr(str) _Generic((str), \
+    cxstring: cx_hash_key_cxstring, \
+    cxmutstr: cx_hash_key_cxmutstr) \
+    (str)
 
-#ifdef __cplusplus
-} // extern "C"
+/**
+ * @copydoc cx_hash_key_cxstr()
+ */
+cx_attr_nodiscard
+static inline CxHashKey cx_hash_key_cxstring(cxstring str) {
+    return cx_hash_key(str.ptr, str.length);
+}
+
+/**
+ * @copydoc cx_hash_key_cxstr()
+ */
+cx_attr_nodiscard
+static inline CxHashKey cx_hash_key_cxmutstr(cxmutstr str) {
+    return cx_hash_key(str.ptr, str.length);
+}
 #endif
 
 #endif // UCX_HASH_KEY_H

mercurial