[openib-general] Re: [PATCH] use union in ibv_get_device_guid()

Roland Dreier rolandd at cisco.com
Wed Sep 7 12:45:20 PDT 2005


Because of MST's warning about union member aliasing, I think it's
better to fix this mess up like this.  No reason to mess around with
pointer aliasing at all -- let's just define htonll() and use that.

 - R.

--- libibverbs/include/infiniband/arch.h	(revision 3327)
+++ libibverbs/include/infiniband/arch.h	(working copy)
@@ -35,6 +35,17 @@
 #ifndef INFINIBAND_ARCH_H
 #define INFINIBAND_ARCH_H
 
+#include <endian.h>
+#include <byteswap.h>
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+static inline uint64_t htonll(uint64_t x) { return bswap_64(x); }
+static inline uint64_t ntohll(uint64_t x) { return bswap_64(x); }
+#elif __BYTE_ORDER == __BIG_ENDIAN
+static inline uint64_t htonll(uint64_t x) { return x; }
+static inline uint64_t ntohll(uint64_t x) { return x; }
+#endif
+
 /*
  * Architecture-specific defines.  Currently, an architecture is
  * required to implement the following operations:
--- libibverbs/src/device.c	(revision 3335)
+++ libibverbs/src/device.c	(working copy)
@@ -44,6 +44,8 @@
 #include <unistd.h>
 #include <alloca.h>
 
+#include <infiniband/arch.h>
+
 #include "ibverbs.h"
 
 static struct dlist *device_list;
@@ -63,7 +65,8 @@ const char *ibv_get_device_name(struct i
 uint64_t ibv_get_device_guid(struct ibv_device *device)
 {
 	struct sysfs_attribute *attr;
-	uint16_t guid[4];
+	uint64_t guid = 0;
+	uint16_t parts[4];
 	int i;
 
 	attr = sysfs_get_classdev_attr(device->ibdev, "node_guid");
@@ -71,13 +74,13 @@ uint64_t ibv_get_device_guid(struct ibv_
 		return 0;
 
 	if (sscanf(attr->value, "%hx:%hx:%hx:%hx",
-		   guid, guid + 1, guid + 2, guid + 3) != 4)
+		   parts, parts + 1, parts + 2, parts + 3) != 4)
 		return 0;
 
 	for (i = 0; i < 4; ++i)
-		guid[i] = htons(guid[i]);
+		guid = (guid << 16) | parts[i];
 
-	return *(uint64_t *) guid;
+	return htonll(guid);
 }
 
 struct ibv_context *ibv_open_device(struct ibv_device *device)
--- libmthca/src/mthca.h	(revision 3327)
+++ libmthca/src/mthca.h	(working copy)
@@ -36,9 +36,6 @@
 #ifndef MTHCA_H
 #define MTHCA_H
 
-#include <endian.h>
-#include <byteswap.h>
-
 #include <infiniband/driver.h>
 #include <infiniband/arch.h>
 
@@ -212,14 +209,6 @@ struct mthca_ah {
 	uint32_t              key;
 };
 
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-static inline uint64_t htonll(uint64_t x) { return bswap_64(x); }
-static inline uint64_t ntohll(uint64_t x) { return bswap_64(x); }
-#elif __BYTE_ORDER == __BIG_ENDIAN
-static inline uint64_t htonll(uint64_t x) { return x; }
-static inline uint64_t ntohll(uint64_t x) { return x; }
-#endif
-
 static inline unsigned long align(unsigned long val, unsigned long align)
 {
 	return (val + align - 1) & ~(align - 1);



More information about the general mailing list