From: Jérémie Galarneau Date: Mon, 22 Jul 2024 19:16:27 +0000 (+0000) Subject: Clean-up: sessiond: automatic memory management of ltt_ust_event X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=5e3dc005e20046f3380aa464f83a78a3e7296c05;p=lttng-tools.git Clean-up: sessiond: automatic memory management of ltt_ust_event Simplify trace_ust_create_event by wrapping the created ltt_ust_event in a unique_ptr until it is returned (on success). Change-Id: I1b14df589f30d4cd19c9712b70f6c5656335123b Signed-off-by: Jérémie Galarneau --- diff --git a/src/bin/lttng-sessiond/trace-ust.cpp b/src/bin/lttng-sessiond/trace-ust.cpp index 4d15bd758..8937dd205 100644 --- a/src/bin/lttng-sessiond/trace-ust.cpp +++ b/src/bin/lttng-sessiond/trace-ust.cpp @@ -449,8 +449,8 @@ enum lttng_error_code trace_ust_create_event(struct lttng_event *ev, bool internal_event, struct ltt_ust_event **ust_event) { - struct ltt_ust_event *local_ust_event; enum lttng_error_code ret = LTTNG_OK; + std::unique_ptr local_ust_event; LTTNG_ASSERT(ev); @@ -459,9 +459,10 @@ enum lttng_error_code trace_ust_create_event(struct lttng_event *ev, goto error; } - local_ust_event = zmalloc(); - if (local_ust_event == nullptr) { - PERROR("ust event zmalloc"); + try { + local_ust_event = lttng::make_unique(); + } catch (const std::bad_alloc &ex) { + ERR_FMT("Failed to allocate ltt_ust_event"); ret = LTTNG_ERR_NOMEM; goto error; } @@ -484,7 +485,7 @@ enum lttng_error_code trace_ust_create_event(struct lttng_event *ev, default: ERR("Unknown ust instrumentation type (%d)", ev->type); ret = LTTNG_ERR_INVALID; - goto error_free_event; + goto error; } /* Copy event name */ @@ -507,7 +508,7 @@ enum lttng_error_code trace_ust_create_event(struct lttng_event *ev, default: ERR("Unknown ust loglevel type (%d)", ev->loglevel_type); ret = LTTNG_ERR_INVALID; - goto error_free_event; + goto error; } /* Same layout. */ @@ -523,12 +524,10 @@ enum lttng_error_code trace_ust_create_event(struct lttng_event *ev, local_ust_event->attr.loglevel_type, local_ust_event->attr.loglevel); - *ust_event = local_ust_event; + *ust_event = local_ust_event.release(); return ret; -error_free_event: - free(local_ust_event); error: free(filter_expression); free(filter); @@ -1231,7 +1230,7 @@ void trace_ust_destroy_event(struct ltt_ust_event *event) free(event->filter_expression); free(event->filter); free(event->exclusion); - free(event); + delete event; } /* diff --git a/src/bin/lttng-sessiond/trace-ust.hpp b/src/bin/lttng-sessiond/trace-ust.hpp index ea84722f1..778870717 100644 --- a/src/bin/lttng-sessiond/trace-ust.hpp +++ b/src/bin/lttng-sessiond/trace-ust.hpp @@ -40,19 +40,19 @@ struct ltt_ust_context { /* UST event */ struct ltt_ust_event { - bool enabled; - struct lttng_ust_abi_event attr; - struct lttng_ht_node_str node; - char *filter_expression; - struct lttng_bytecode *filter; - struct lttng_event_exclusion *exclusion; + bool enabled = false; + struct lttng_ust_abi_event attr = {}; + struct lttng_ht_node_str node = {}; + char *filter_expression = nullptr; + struct lttng_bytecode *filter = nullptr; + struct lttng_event_exclusion *exclusion = nullptr; /* * An internal event is an event which was created by the session daemon * through which, for example, events emitted in Agent domains are * "funelled". This is used to hide internal events from external * clients as they should never be modified by the external world. */ - bool internal; + bool internal = false; }; /* UST channel */