From: Roger Pau Monne Date: Fri, 27 Jun 2014 14:06:28 +0000 (+0200) Subject: libxl: add support for FreeBSD uuid implementation X-Git-Tag: 4.5.0-rc1~568 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=18b102a93341cd6c29ca97bcebbd19d3ae697771;p=xen.git 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é Cc: Ian Jackson Cc: Ian Campbell Acked-by: Ian Campbell --- diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 2eea5575ff..e8f2abb5f5 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -3184,6 +3184,15 @@ int libxl__string_parse_json(libxl__gc *gc, const libxl__json_object *o, int libxl__random_bytes(libxl__gc *gc, uint8_t *buf, size_t len); +/* + * 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 172b7d1f61..7d4a03266e 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 0c2a1e7528..196b5bc3e9 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 +#include #include #include #include #include -#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"