[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v4] libxl: add support for FreeBSD uuid implementation



Add the FreeBSD specific uuid implementation. Since uuid_t is not
defined as an array, but as a struct on FreeBSD, use a union with a
array in order to be able to return the uuid as a bytearray.

Also, added a libxl internal compile time assert macro, that is used
to assert that the size of uuid_t is the same as the union used in
libxl.

Signed-off-by: Roger Pau Monnà <roger.pau@xxxxxxxxxx>
Cc: Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx>
Cc: Ian Campbell <ian.campbell@xxxxxxxxxx>
---
Changes since v2:
 - Unify the FreeBSD and NetBSD uuid implementations as much as
   possible.
---
 tools/libxl/libxl_internal.h |    8 ++++++++
 tools/libxl/libxl_uuid.c     |   40 ++++++++++++++++++++++++++++++++--------
 tools/libxl/libxl_uuid.h     |   12 +++++++-----
 3 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index a0d4f24..c7a34f0 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -3179,6 +3179,14 @@ int libxl__uint64_parse_json(libxl__gc *gc, const 
libxl__json_object *o,
                              void *p);
 int libxl__string_parse_json(libxl__gc *gc, const libxl__json_object *o,
                              char **p);
+/*
+ * Compile time assertion
+ */
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
+#define BUILD_BUG_ON(p) ({ _Static_assert(!(p), "!(" #p ")"); })
+#else
+#define BUILD_BUG_ON(p) ((void)sizeof(char[1 - 2 * !!(p)]))
+#endif
 
 #endif
 
diff --git a/tools/libxl/libxl_uuid.c b/tools/libxl/libxl_uuid.c
index 172b7d1..7d4a032 100644
--- a/tools/libxl/libxl_uuid.c
+++ b/tools/libxl/libxl_uuid.c
@@ -59,21 +59,35 @@ uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
     return uuid->uuid;
 }
 
-#elif defined(__NetBSD__)
+#elif defined(__FreeBSD__) || defined(__NetBSD__)
 
 int libxl_uuid_is_nil(const libxl_uuid *uuid)
 {
     uint32_t status;
-    return uuid_is_nil((uuid_t *)uuid->uuid, &status);
+
+    return uuid_is_nil(&uuid->uuid, &status);
 }
 
 void libxl_uuid_generate(libxl_uuid *uuid)
 {
     uint32_t status;
-    uuid_create((uuid_t *)uuid->uuid, &status);
+
+    BUILD_BUG_ON(sizeof(libxl_uuid) != sizeof(uuid_t));
+    uuid_create(&uuid->uuid, &status);
     assert(status == uuid_s_ok);
 }
 
+#ifdef __FreeBSD__
+int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
+{
+    uint32_t status;
+
+    uuid_from_string(in, &uuid->uuid, &status);
+    if (status != uuid_s_ok)
+        return -1;
+    return 0;
+}
+#else
 #define LIBXL__UUID_PTRS(uuid) &uuid[0], &uuid[1], &uuid[2], &uuid[3], \
                                &uuid[4], &uuid[5], &uuid[6], &uuid[7], \
                                &uuid[8], &uuid[9], &uuid[10],&uuid[11], \
@@ -85,33 +99,43 @@ int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
     return 0;
 }
 #undef LIBXL__UUID_PTRS
+#endif
 
 void libxl_uuid_copy(libxl_ctx *ctx_opt, libxl_uuid *dst,
                      const libxl_uuid *src)
 {
-     memcpy(dst->uuid, src->uuid, sizeof(dst->uuid));
+    memcpy(&dst->uuid, &src->uuid, sizeof(dst->uuid));
 }
 
 void libxl_uuid_clear(libxl_uuid *uuid)
 {
-     memset(uuid->uuid, 0, sizeof(uuid->uuid));
+    memset(&uuid->uuid, 0, sizeof(uuid->uuid));
 }
 
+#ifdef __FreeBSD__
+int libxl_uuid_compare(const libxl_uuid *uuid1, const libxl_uuid *uuid2)
+{
+
+    return uuid_compare(&uuid1->uuid, &uuid2->uuid, NULL);
+}
+#else
 int libxl_uuid_compare(const libxl_uuid *uuid1, const libxl_uuid *uuid2)
 {
      return memcmp(uuid1->uuid, uuid2->uuid, sizeof(uuid1->uuid));
 }
+#endif
 
 const uint8_t *libxl_uuid_bytearray_const(const libxl_uuid *uuid)
 {
-    return uuid->uuid;
+
+    return uuid->uuid_raw;
 }
 
 uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
 {
-    return uuid->uuid;
-}
 
+    return uuid->uuid_raw;
+}
 #else
 
 #error "Please update libxl_uuid.c for your OS"
diff --git a/tools/libxl/libxl_uuid.h b/tools/libxl/libxl_uuid.h
index 0c2a1e7..196b5bc 100644
--- a/tools/libxl/libxl_uuid.h
+++ b/tools/libxl/libxl_uuid.h
@@ -33,20 +33,22 @@ typedef struct {
 
 #define LIBXL_UUID_BYTES(arg) LIBXL__UUID_BYTES(((uint8_t *)arg.uuid))
 
-#elif defined(__NetBSD__)
+#elif defined(__FreeBSD__) || defined(__NetBSD__)
 
 #include <uuid.h>
+#include <stdint.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
 
-#define LIBXL_UUID_BYTES(arg) LIBXL__UUID_BYTES(arg.uuid)
-
-typedef struct {
-    uint8_t uuid[16];
+typedef union {
+    uuid_t uuid;
+    uint8_t uuid_raw[16];
 } libxl_uuid;
 
+#define LIBXL_UUID_BYTES(arg) LIBXL__UUID_BYTES(arg.uuid_raw)
+
 #else
 
 #error "Please update libxl_uuid.h for your OS"
-- 
1.7.7.5 (Apple Git-26)


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.