From: Jérémie Galarneau Date: Mon, 2 May 2022 19:33:09 +0000 (-0400) Subject: Change backing type of lttng_uuid to std::array X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=328c2fe7297c941aa9cbcfa4ce944fca1bd7300f;p=deliverable%2Flttng-tools.git Change backing type of lttng_uuid to std::array Changing the backing type of lttng_uuid to std::array allows us to return lttng_uuid from a function. This, in return, makes it possible to initialize const attributes from the return value of a function returning a UUID. Signed-off-by: Jérémie Galarneau Change-Id: Ie092eab4a848a41ddd9c63f779514f1e4ca2a441 --- diff --git a/src/bin/lttng-relayd/cmd-2-11.cpp b/src/bin/lttng-relayd/cmd-2-11.cpp index 680cb307b..73c3a3c51 100644 --- a/src/bin/lttng-relayd/cmd-2-11.cpp +++ b/src/bin/lttng-relayd/cmd-2-11.cpp @@ -22,7 +22,7 @@ int cmd_create_session_2_11(const struct lttng_buffer_view *payload, char *session_name, char *hostname, char *base_path, uint32_t *live_timer, bool *snapshot, uint64_t *id_sessiond, - lttng_uuid sessiond_uuid, + lttng_uuid& sessiond_uuid, bool *has_current_chunk, uint64_t *current_chunk_id, time_t *creation_time, bool *session_name_contains_creation_time) @@ -53,7 +53,7 @@ int cmd_create_session_2_11(const struct lttng_buffer_view *payload, header.creation_time = be64toh(header.creation_time); header.session_id = be64toh(header.session_id); - lttng_uuid_copy(sessiond_uuid, header.sessiond_uuid); + std::copy(std::begin(header.sessiond_uuid), std::end(header.sessiond_uuid), sessiond_uuid.begin()); received_names_size = header.session_name_len + header.hostname_len + header.base_path_len; diff --git a/src/bin/lttng-relayd/cmd-2-11.hpp b/src/bin/lttng-relayd/cmd-2-11.hpp index 4b004f0d3..79f241aaa 100644 --- a/src/bin/lttng-relayd/cmd-2-11.hpp +++ b/src/bin/lttng-relayd/cmd-2-11.hpp @@ -15,7 +15,7 @@ int cmd_create_session_2_11(const struct lttng_buffer_view *payload, char *session_name, char *hostname, char *base_path, uint32_t *live_timer, bool *snapshot, - uint64_t *id_sessiond, lttng_uuid sessiond_uuid, + uint64_t *id_sessiond, lttng_uuid& sessiond_uuid, bool *has_current_chunk, uint64_t *current_chunk_id, time_t *creation_time, bool *session_name_contains_creation_time); diff --git a/src/bin/lttng-relayd/session.cpp b/src/bin/lttng-relayd/session.cpp index a8a152069..a776587b3 100644 --- a/src/bin/lttng-relayd/session.cpp +++ b/src/bin/lttng-relayd/session.cpp @@ -281,7 +281,7 @@ struct relay_session *session_create(const char *session_name, const char *hostname, const char *base_path, uint32_t live_timer, bool snapshot, - const lttng_uuid sessiond_uuid, + const lttng_uuid& sessiond_uuid, const uint64_t *id_sessiond, const uint64_t *current_chunk_id, const time_t *creation_time, @@ -363,7 +363,7 @@ struct relay_session *session_create(const char *session_name, session->live_timer = live_timer; session->snapshot = snapshot; - lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid); + session->sessiond_uuid = sessiond_uuid; if (id_sessiond) { LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond); @@ -527,8 +527,7 @@ bool session_has_ongoing_rotation(const struct relay_session *session) goto next_session; } - if (!lttng_uuid_is_equal(session->sessiond_uuid, - iterated_session->sessiond_uuid)) { + if (session->sessiond_uuid != iterated_session->sessiond_uuid) { /* Sessions do not originate from the same sessiond. */ goto next_session; } diff --git a/src/bin/lttng-relayd/session.hpp b/src/bin/lttng-relayd/session.hpp index 9c61b942b..986eca68f 100644 --- a/src/bin/lttng-relayd/session.hpp +++ b/src/bin/lttng-relayd/session.hpp @@ -142,7 +142,7 @@ struct relay_session *session_create(const char *session_name, const char *hostname, const char *base_path, uint32_t live_timer, bool snapshot, - const lttng_uuid sessiond_uuid, + const lttng_uuid& sessiond_uuid, const uint64_t *id_sessiond, const uint64_t *current_chunk_id, const time_t *creation_time, diff --git a/src/bin/lttng-relayd/sessiond-trace-chunks.cpp b/src/bin/lttng-relayd/sessiond-trace-chunks.cpp index 34f14f187..60f1b9754 100644 --- a/src/bin/lttng-relayd/sessiond-trace-chunks.cpp +++ b/src/bin/lttng-relayd/sessiond-trace-chunks.cpp @@ -77,8 +77,8 @@ static unsigned long trace_chunk_registry_ht_key_hash( const struct trace_chunk_registry_ht_key *key) { - uint64_t uuid_h1 = ((uint64_t *) key->sessiond_uuid)[0]; - uint64_t uuid_h2 = ((uint64_t *) key->sessiond_uuid)[1]; + const uint64_t uuid_h1 = *reinterpret_cast(&key->sessiond_uuid[0]); + const uint64_t uuid_h2 = *reinterpret_cast(&key->sessiond_uuid[1]); return hash_key_u64(&uuid_h1, lttng_ht_seed) ^ hash_key_u64(&uuid_h2, lttng_ht_seed); @@ -94,8 +94,7 @@ int trace_chunk_registry_ht_key_match(struct cds_lfht_node *node, struct trace_chunk_registry_ht_element *registry; registry = container_of(node, typeof(*registry), ht_node); - return lttng_uuid_is_equal(key->sessiond_uuid, - registry->key.sessiond_uuid); + return key->sessiond_uuid == registry->key.sessiond_uuid; } static @@ -295,13 +294,13 @@ void sessiond_trace_chunk_registry_destroy( int sessiond_trace_chunk_registry_session_created( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid) + const lttng_uuid& sessiond_uuid) { int ret = 0; struct trace_chunk_registry_ht_key key; struct trace_chunk_registry_ht_element *element; - lttng_uuid_copy(key.sessiond_uuid, sessiond_uuid); + key.sessiond_uuid = sessiond_uuid; element = trace_chunk_registry_ht_element_find(sessiond_registry, &key); if (element) { @@ -321,7 +320,7 @@ end: int sessiond_trace_chunk_registry_session_destroyed( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid) + const lttng_uuid& sessiond_uuid) { int ret = 0; struct trace_chunk_registry_ht_key key; @@ -329,7 +328,7 @@ int sessiond_trace_chunk_registry_session_destroyed( char uuid_str[LTTNG_UUID_STR_LEN]; lttng_uuid_to_str(sessiond_uuid, uuid_str); - lttng_uuid_copy(key.sessiond_uuid, sessiond_uuid); + key.sessiond_uuid = sessiond_uuid; element = trace_chunk_registry_ht_element_find(sessiond_registry, &key); if (element) { @@ -351,7 +350,7 @@ int sessiond_trace_chunk_registry_session_destroyed( struct lttng_trace_chunk *sessiond_trace_chunk_registry_publish_chunk( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid, uint64_t session_id, + const lttng_uuid& sessiond_uuid, uint64_t session_id, struct lttng_trace_chunk *new_chunk) { enum lttng_trace_chunk_status status; @@ -365,7 +364,7 @@ struct lttng_trace_chunk *sessiond_trace_chunk_registry_publish_chunk( bool trace_chunk_already_published; lttng_uuid_to_str(sessiond_uuid, uuid_str); - lttng_uuid_copy(key.sessiond_uuid, sessiond_uuid); + key.sessiond_uuid = sessiond_uuid; status = lttng_trace_chunk_get_id(new_chunk, &chunk_id); if (status == LTTNG_TRACE_CHUNK_STATUS_OK) { @@ -428,7 +427,7 @@ end: struct lttng_trace_chunk * sessiond_trace_chunk_registry_get_anonymous_chunk( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid, + const lttng_uuid& sessiond_uuid, uint64_t session_id) { struct lttng_trace_chunk *chunk = NULL; @@ -438,7 +437,7 @@ sessiond_trace_chunk_registry_get_anonymous_chunk( lttng_uuid_to_str(sessiond_uuid, uuid_str); - lttng_uuid_copy(key.sessiond_uuid, sessiond_uuid); + key.sessiond_uuid = sessiond_uuid; element = trace_chunk_registry_ht_element_find(sessiond_registry, &key); if (!element) { ERR("Failed to find trace chunk registry of sessiond {%s}", @@ -457,7 +456,7 @@ end: struct lttng_trace_chunk * sessiond_trace_chunk_registry_get_chunk( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid, + const lttng_uuid& sessiond_uuid, uint64_t session_id, uint64_t chunk_id) { struct lttng_trace_chunk *chunk = NULL; @@ -467,7 +466,7 @@ sessiond_trace_chunk_registry_get_chunk( lttng_uuid_to_str(sessiond_uuid, uuid_str); - lttng_uuid_copy(key.sessiond_uuid, sessiond_uuid); + key.sessiond_uuid = sessiond_uuid; element = trace_chunk_registry_ht_element_find(sessiond_registry, &key); if (!element) { ERR("Failed to find trace chunk registry of sessiond {%s}", @@ -485,14 +484,14 @@ end: int sessiond_trace_chunk_registry_chunk_exists( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid, + const lttng_uuid& sessiond_uuid, uint64_t session_id, uint64_t chunk_id, bool *chunk_exists) { int ret; struct trace_chunk_registry_ht_element *element; struct trace_chunk_registry_ht_key key; - lttng_uuid_copy(key.sessiond_uuid, sessiond_uuid); + key.sessiond_uuid = sessiond_uuid; element = trace_chunk_registry_ht_element_find(sessiond_registry, &key); if (!element) { char uuid_str[LTTNG_UUID_STR_LEN]; diff --git a/src/bin/lttng-relayd/sessiond-trace-chunks.hpp b/src/bin/lttng-relayd/sessiond-trace-chunks.hpp index 2e494292a..4d5759d87 100644 --- a/src/bin/lttng-relayd/sessiond-trace-chunks.hpp +++ b/src/bin/lttng-relayd/sessiond-trace-chunks.hpp @@ -33,32 +33,32 @@ void sessiond_trace_chunk_registry_destroy( int sessiond_trace_chunk_registry_session_created( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid); + const lttng_uuid& sessiond_uuid); int sessiond_trace_chunk_registry_session_destroyed( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid); + const lttng_uuid& sessiond_uuid); struct lttng_trace_chunk *sessiond_trace_chunk_registry_publish_chunk( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid, uint64_t session_id, + const lttng_uuid& sessiond_uuid, uint64_t session_id, struct lttng_trace_chunk *chunk); struct lttng_trace_chunk * sessiond_trace_chunk_registry_get_anonymous_chunk( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid, + const lttng_uuid& sessiond_uuid, uint64_t session_id); struct lttng_trace_chunk * sessiond_trace_chunk_registry_get_chunk( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid, + const lttng_uuid& sessiond_uuid, uint64_t session_id, uint64_t chunk_id); int sessiond_trace_chunk_registry_chunk_exists( struct sessiond_trace_chunk_registry *sessiond_registry, - const lttng_uuid sessiond_uuid, + const lttng_uuid& sessiond_uuid, uint64_t session_id, uint64_t chunk_id, bool *chunk_exists); #endif /* SESSIOND_TRACE_CHUNK_REGISTRY_H */ diff --git a/src/bin/lttng-sessiond/consumer.cpp b/src/bin/lttng-sessiond/consumer.cpp index bdda39135..b87e1d73d 100644 --- a/src/bin/lttng-sessiond/consumer.cpp +++ b/src/bin/lttng-sessiond/consumer.cpp @@ -924,7 +924,7 @@ void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg, const char *name, uint64_t relayd_id, uint64_t key, - unsigned char *uuid, + const lttng_uuid& uuid, uint32_t chan_id, uint64_t tracefile_size, uint64_t tracefile_count, @@ -979,7 +979,7 @@ void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg, msg->u.ask_channel.ust_app_uid = ust_app_uid; msg->u.ask_channel.blocking_timeout = blocking_timeout; - memcpy(msg->u.ask_channel.uuid, uuid, sizeof(msg->u.ask_channel.uuid)); + std::copy(uuid.begin(), uuid.end(), msg->u.ask_channel.uuid); if (pathname) { strncpy(msg->u.ask_channel.pathname, pathname, @@ -1782,7 +1782,7 @@ error_socket: } int consumer_init(struct consumer_socket *socket, - const lttng_uuid sessiond_uuid) + const lttng_uuid& sessiond_uuid) { int ret; struct lttcomm_consumer_msg msg = { @@ -1793,7 +1793,7 @@ int consumer_init(struct consumer_socket *socket, LTTNG_ASSERT(socket); DBG("Sending consumer initialization command"); - lttng_uuid_copy(msg.u.init.sessiond_uuid, sessiond_uuid); + std::copy(sessiond_uuid.begin(), sessiond_uuid.end(), msg.u.init.sessiond_uuid); health_code_update(); ret = consumer_send_msg(socket, &msg); diff --git a/src/bin/lttng-sessiond/consumer.hpp b/src/bin/lttng-sessiond/consumer.hpp index 15e2a2fc7..d76ec025f 100644 --- a/src/bin/lttng-sessiond/consumer.hpp +++ b/src/bin/lttng-sessiond/consumer.hpp @@ -261,7 +261,7 @@ void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg, const char *name, uint64_t relayd_id, uint64_t key, - unsigned char *uuid, + const lttng_uuid& uuid, uint32_t chan_id, uint64_t tracefile_size, uint64_t tracefile_count, @@ -321,7 +321,7 @@ enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket, int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key, struct consumer_output *output, bool is_metadata_channel); int consumer_init(struct consumer_socket *socket, - const lttng_uuid sessiond_uuid); + const lttng_uuid& sessiond_uuid); int consumer_create_trace_chunk(struct consumer_socket *socket, uint64_t relayd_id, uint64_t session_id, diff --git a/src/bin/lttng-sessiond/ust-registry.hpp b/src/bin/lttng-sessiond/ust-registry.hpp index d2781ba0b..88bd502db 100644 --- a/src/bin/lttng-sessiond/ust-registry.hpp +++ b/src/bin/lttng-sessiond/ust-registry.hpp @@ -68,7 +68,7 @@ public: /* Next enumeration ID available. */ uint64_t _next_enum_id = 0; /* Universal unique identifier used by the tracer. */ - unsigned char _uuid[LTTNG_UUID_LEN] = {}; + lttng_uuid _uuid = {}; /* session ABI description */ diff --git a/src/common/consumer/consumer.cpp b/src/common/consumer/consumer.cpp index a9de1696e..a5691b1c2 100644 --- a/src/common/consumer/consumer.cpp +++ b/src/common/consumer/consumer.cpp @@ -4697,7 +4697,7 @@ end: enum lttcomm_return_code lttng_consumer_init_command( struct lttng_consumer_local_data *ctx, - const lttng_uuid sessiond_uuid) + const lttng_uuid& sessiond_uuid) { enum lttcomm_return_code ret; char uuid_str[LTTNG_UUID_STR_LEN]; @@ -4708,7 +4708,7 @@ enum lttcomm_return_code lttng_consumer_init_command( } ctx->sessiond_uuid.is_set = true; - memcpy(ctx->sessiond_uuid.value, sessiond_uuid, sizeof(lttng_uuid)); + ctx->sessiond_uuid.value = sessiond_uuid; ret = LTTCOMM_CONSUMERD_SUCCESS; lttng_uuid_to_str(sessiond_uuid, uuid_str); DBG("Received session daemon UUID: %s", uuid_str); diff --git a/src/common/consumer/consumer.hpp b/src/common/consumer/consumer.hpp index 4bcfe5d95..2c16ae1fa 100644 --- a/src/common/consumer/consumer.hpp +++ b/src/common/consumer/consumer.hpp @@ -1075,7 +1075,7 @@ enum lttcomm_return_code lttng_consumer_trace_chunk_exists( void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair *relayd); enum lttcomm_return_code lttng_consumer_init_command( struct lttng_consumer_local_data *ctx, - const lttng_uuid sessiond_uuid); + const lttng_uuid& sessiond_uuid); int lttng_consumer_clear_channel(struct lttng_consumer_channel *channel); enum lttcomm_return_code lttng_consumer_open_channel_packets( struct lttng_consumer_channel *channel); diff --git a/src/common/kernel-consumer/kernel-consumer.cpp b/src/common/kernel-consumer/kernel-consumer.cpp index 95036a44c..9bc66a478 100644 --- a/src/common/kernel-consumer/kernel-consumer.cpp +++ b/src/common/kernel-consumer/kernel-consumer.cpp @@ -1244,9 +1244,13 @@ error_rotate_channel: case LTTNG_CONSUMER_INIT: { int ret_send_status; + lttng_uuid sessiond_uuid; + + std::copy(std::begin(msg.u.init.sessiond_uuid), std::end(msg.u.init.sessiond_uuid), + sessiond_uuid.begin()); ret_code = lttng_consumer_init_command(ctx, - msg.u.init.sessiond_uuid); + sessiond_uuid); health_code_update(); ret_send_status = consumer_send_status_msg(sock, ret_code); if (ret_send_status < 0) { diff --git a/src/common/relayd/relayd.cpp b/src/common/relayd/relayd.cpp index 5148bd03a..f2a190505 100644 --- a/src/common/relayd/relayd.cpp +++ b/src/common/relayd/relayd.cpp @@ -144,7 +144,7 @@ static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock, const char *session_name, const char *hostname, const char *base_path, int session_live_timer, unsigned int snapshot, uint64_t sessiond_session_id, - const lttng_uuid sessiond_uuid, const uint64_t *current_chunk_id, + const lttng_uuid& sessiond_uuid, const uint64_t *current_chunk_id, time_t creation_time, bool session_name_contains_creation_time, struct lttcomm_relayd_create_session_reply_2_11 *reply, char *output_path) @@ -201,7 +201,7 @@ static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock, msg->live_timer = htobe32(session_live_timer); msg->snapshot = !!snapshot; - lttng_uuid_copy(msg->sessiond_uuid, sessiond_uuid); + std::copy(sessiond_uuid.begin(), sessiond_uuid.end(), msg->sessiond_uuid); msg->session_id = htobe64(sessiond_session_id); msg->session_name_contains_creation_time = session_name_contains_creation_time; if (current_chunk_id) { @@ -316,7 +316,7 @@ int relayd_create_session(struct lttcomm_relayd_sock *rsock, const char *session_name, const char *hostname, const char *base_path, int session_live_timer, unsigned int snapshot, uint64_t sessiond_session_id, - const lttng_uuid sessiond_uuid, + const lttng_uuid& sessiond_uuid, const uint64_t *current_chunk_id, time_t creation_time, bool session_name_contains_creation_time, char *output_path) diff --git a/src/common/relayd/relayd.hpp b/src/common/relayd/relayd.hpp index d4a903ace..05d1ec684 100644 --- a/src/common/relayd/relayd.hpp +++ b/src/common/relayd/relayd.hpp @@ -34,7 +34,7 @@ int relayd_create_session(struct lttcomm_relayd_sock *rsock, const char *session_name, const char *hostname, const char *base_path, int session_live_timer, unsigned int snapshot, uint64_t sessiond_session_id, - const lttng_uuid sessiond_uuid, + const lttng_uuid& sessiond_uuid, const uint64_t *current_chunk_id, time_t creation_time, bool session_name_contains_creation_time, char *output_path); diff --git a/src/common/sessiond-comm/relayd.hpp b/src/common/sessiond-comm/relayd.hpp index febbe4e73..0f5adaa94 100644 --- a/src/common/sessiond-comm/relayd.hpp +++ b/src/common/sessiond-comm/relayd.hpp @@ -202,7 +202,7 @@ struct lttcomm_relayd_create_session_2_11 { uint8_t snapshot; uint8_t session_name_contains_creation_time; /* Sessiond instance UUID */ - lttng_uuid sessiond_uuid; + uint8_t sessiond_uuid[LTTNG_UUID_LEN]; /* Sessiond session id */ uint64_t session_id; /* Session creation time, in seconds since UNIX Epoch. */ diff --git a/src/common/sessiond-comm/sessiond-comm.hpp b/src/common/sessiond-comm/sessiond-comm.hpp index ae3111855..257d70206 100644 --- a/src/common/sessiond-comm/sessiond-comm.hpp +++ b/src/common/sessiond-comm/sessiond-comm.hpp @@ -830,7 +830,7 @@ struct lttcomm_consumer_msg { uint64_t chunk_id; } LTTNG_PACKED trace_chunk_exists; struct { - lttng_uuid sessiond_uuid; + uint8_t sessiond_uuid[LTTNG_UUID_LEN]; } LTTNG_PACKED init; struct { uint64_t key; diff --git a/src/common/ust-consumer/ust-consumer.cpp b/src/common/ust-consumer/ust-consumer.cpp index 431bb98f6..0933c33c8 100644 --- a/src/common/ust-consumer/ust-consumer.cpp +++ b/src/common/ust-consumer/ust-consumer.cpp @@ -2093,9 +2093,11 @@ end_rotate_channel_nosignal: case LTTNG_CONSUMER_INIT: { int ret_send_status; + lttng_uuid sessiond_uuid; - ret_code = lttng_consumer_init_command(ctx, - msg.u.init.sessiond_uuid); + std::copy(std::begin(msg.u.init.sessiond_uuid), std::end(msg.u.init.sessiond_uuid), + sessiond_uuid.begin()); + ret_code = lttng_consumer_init_command(ctx, sessiond_uuid); health_code_update(); ret_send_status = consumer_send_status_msg(sock, ret_code); if (ret_send_status < 0) { diff --git a/src/common/uuid.cpp b/src/common/uuid.cpp index 23672a96f..a820a8a41 100644 --- a/src/common/uuid.cpp +++ b/src/common/uuid.cpp @@ -16,20 +16,22 @@ #include "uuid.hpp" -static const lttng_uuid nil_uuid = { 0 }; -static bool lttng_uuid_is_init; +namespace { +const lttng_uuid nil_uuid = {}; +bool lttng_uuid_is_init; +} /* namespace */ -void lttng_uuid_to_str(const lttng_uuid uuid, char *uuid_str) +void lttng_uuid_to_str(const lttng_uuid& uuid, char *uuid_str) { sprintf(uuid_str, LTTNG_UUID_FMT, LTTNG_UUID_FMT_VALUES(uuid)); } -int lttng_uuid_from_str(const char *str_in, lttng_uuid uuid_out) +int lttng_uuid_from_str(const char *str_in, lttng_uuid& uuid_out) { int ret = 0; lttng_uuid uuid_scan; - if ((str_in == NULL) || (uuid_out == NULL)) { + if (str_in == nullptr) { ret = -1; goto end; } @@ -45,35 +47,23 @@ int lttng_uuid_from_str(const char *str_in, lttng_uuid uuid_out) ret = -1; } - lttng_uuid_copy(uuid_out, uuid_scan); + uuid_out = uuid_scan; end: return ret; } -bool lttng_uuid_is_equal(const lttng_uuid a, const lttng_uuid b) +bool lttng_uuid_is_nil(const lttng_uuid& uuid) { - return memcmp(a, b, LTTNG_UUID_LEN) == 0; -} - -bool lttng_uuid_is_nil(const lttng_uuid uuid) -{ - return memcmp(nil_uuid, uuid, sizeof(lttng_uuid)) == 0; -} - -void lttng_uuid_copy(lttng_uuid dst, const lttng_uuid src) -{ - memcpy(dst, src, LTTNG_UUID_LEN); + return uuid == nil_uuid; } /* * Generate a random UUID according to RFC4122, section 4.4. */ -int lttng_uuid_generate(lttng_uuid uuid_out) +int lttng_uuid_generate(lttng_uuid& uuid_out) { int i, ret = 0; - LTTNG_ASSERT(uuid_out); - if (!lttng_uuid_is_init) { /* * We don't need cryptographic quality randomness to diff --git a/src/common/uuid.hpp b/src/common/uuid.hpp index 431490ef2..0cd3966be 100644 --- a/src/common/uuid.hpp +++ b/src/common/uuid.hpp @@ -12,6 +12,7 @@ #include #include #include +#include /* * Includes final \0. @@ -36,9 +37,9 @@ &(uuid)[6], &(uuid)[7], &(uuid)[8], &(uuid)[9], &(uuid)[10], &(uuid)[11], \ &(uuid)[12], &(uuid)[13], &(uuid)[14], &(uuid)[15] -typedef uint8_t lttng_uuid[LTTNG_UUID_LEN]; +using lttng_uuid = std::array; -int lttng_uuid_from_str(const char *str_in, lttng_uuid uuid_out); +int lttng_uuid_from_str(const char *str_in, lttng_uuid& uuid_out); /* * Convert a UUID to a human-readable, NULL-terminated, string of the form @@ -46,17 +47,15 @@ int lttng_uuid_from_str(const char *str_in, lttng_uuid uuid_out); * * Assumes uuid_str is at least LTTNG_UUID_STR_LEN byte long. */ -void lttng_uuid_to_str(const lttng_uuid uuid, char *uuid_str); +void lttng_uuid_to_str(const lttng_uuid& uuid, char *uuid_str); -bool lttng_uuid_is_equal(const lttng_uuid a, const lttng_uuid b); +bool lttng_uuid_is_equal(const lttng_uuid& a, const lttng_uuid& b); -bool lttng_uuid_is_nil(const lttng_uuid uuid); - -void lttng_uuid_copy(lttng_uuid dst, const lttng_uuid src); +bool lttng_uuid_is_nil(const lttng_uuid& uuid); /* * Generate a random UUID according to RFC4122, section 4.4. */ -int lttng_uuid_generate(lttng_uuid uuid_out); +int lttng_uuid_generate(lttng_uuid& uuid_out); #endif /* LTTNG_UUID_H */ diff --git a/tests/unit/test_uuid.cpp b/tests/unit/test_uuid.cpp index 26ec6b6c6..403e1f0ea 100644 --- a/tests/unit/test_uuid.cpp +++ b/tests/unit/test_uuid.cpp @@ -110,22 +110,22 @@ void run_test_lttng_uuid_is_equal(void) lttng_uuid_from_str(valid_str_1, uuid1); lttng_uuid_from_str(valid_str_1, uuid2); - ret = lttng_uuid_is_equal(uuid1, uuid2); + ret = uuid1 == uuid2; ok(ret == true, "lttng_uuid_is_equal - Compare same UUID, expect success"); lttng_uuid_from_str(valid_str_2, uuid2); - ret = lttng_uuid_is_equal(uuid1, uuid2); + ret = uuid1 == uuid2; ok(ret == false, "lttng_uuid_is_equal - Compare different UUID, expect failure"); } static void run_test_lttng_uuid_copy(void) { - int ret; + bool ret; lttng_uuid uuid1; - lttng_uuid_copy(uuid1, valid_uuid_1); - ret = lttng_uuid_is_equal(uuid1, valid_uuid_1); + uuid1 = valid_uuid_1; + ret = uuid1 == valid_uuid_1; ok(ret == true, "lttng_uuid_copy - Compare copied UUID with source, expect success"); } @@ -139,7 +139,7 @@ void run_test_lttng_uuid_generate(void) lttng_uuid_generate(uuid1); lttng_uuid_generate(uuid2); - ok(lttng_uuid_is_equal(uuid1, uuid2) == false, "lttng_uuid_generate - Generated UUIDs are different"); + ok(uuid1 != uuid2, "lttng_uuid_generate - Generated UUIDs are different"); /* * Set the two most significant bits (bits 6 and 7) of the