Use compiler-agnostic defines to silence warning
[lttng-tools.git] / src / bin / lttng-sessiond / snapshot.cpp
index f7821da95da978ac54f1111d7622a4080e8b31ca..69e4ff79de7c493269ce04910c665e56e889242a 100644 (file)
@@ -6,14 +6,17 @@
  */
 
 #define _LGPL_SOURCE
-#include <inttypes.h>
-#include <string.h>
-#include <urcu/uatomic.h>
+#include "consumer-output.hpp"
+#include "snapshot-output.hpp"
+#include "snapshot.hpp"
+#include "utils.hpp"
 
 #include <common/defaults.hpp>
+#include <common/urcu.hpp>
 
-#include "snapshot.hpp"
-#include "utils.hpp"
+#include <inttypes.h>
+#include <string.h>
+#include <urcu/uatomic.h>
 
 /*
  * Return the atomically incremented value of next_output_id.
@@ -28,16 +31,17 @@ static inline unsigned long get_next_output_id(struct snapshot *snapshot)
  *
  * Return 0 on success or else a negative value.
  */
-static int output_init(const struct ltt_session *session,
-               uint64_t max_size, const char *name,
-               struct lttng_uri *uris, size_t nb_uri,
-               struct consumer_output *consumer, struct snapshot_output *output,
-               struct snapshot *snapshot)
+static int output_init(const ltt_session::locked_ref& session,
+                      uint64_t max_size,
+                      const char *name,
+                      struct lttng_uri *uris,
+                      size_t nb_uri,
+                      struct consumer_output *consumer,
+                      struct snapshot_output *output,
+                      struct snapshot *snapshot)
 {
        int ret = 0, i;
 
-       memset(output, 0, sizeof(struct snapshot_output));
-
        /*
         * max_size of -1ULL means unset. Set to default (unlimited).
         */
@@ -58,8 +62,11 @@ static int output_init(const struct ltt_session *session,
                }
        } else {
                /* Set default name. */
-               ret = snprintf(output->name, sizeof(output->name), "%s-%" PRIu32,
-                               DEFAULT_SNAPSHOT_NAME, output->id);
+               ret = snprintf(output->name,
+                              sizeof(output->name),
+                              "%s-%" PRIu32,
+                              DEFAULT_SNAPSHOT_NAME,
+                              output->id);
                if (ret < 0) {
                        ret = -ENOMEM;
                        goto error;
@@ -84,11 +91,12 @@ static int output_init(const struct ltt_session *session,
        }
 
        if (uris[0].dtype == LTTNG_DST_PATH) {
-               memset(output->consumer->dst.session_root_path, 0,
-                               sizeof(output->consumer->dst.session_root_path));
+               memset(output->consumer->dst.session_root_path,
+                      0,
+                      sizeof(output->consumer->dst.session_root_path));
                if (lttng_strncpy(output->consumer->dst.session_root_path,
-                               uris[0].dst.path,
-                               sizeof(output->consumer->dst.session_root_path))) {
+                                 uris[0].dst.path,
+                                 sizeof(output->consumer->dst.session_root_path))) {
                        ret = -LTTNG_ERR_INVALID;
                        goto error;
                }
@@ -103,10 +111,9 @@ static int output_init(const struct ltt_session *session,
                goto error;
        }
 
-       for (i = 0; i < nb_uri; i ++) {
+       for (i = 0; i < nb_uri; i++) {
                /* Network URIs */
-               ret = consumer_set_network_uri(session, output->consumer,
-                               &uris[i]);
+               ret = consumer_set_network_uri(session, output->consumer, &uris[i]);
                if (ret < 0) {
                        goto error;
                }
@@ -123,14 +130,16 @@ end:
  *
  * Return 0 on success or else a negative value.
  */
-int snapshot_output_init_with_uri(const struct ltt_session *session,
-               uint64_t max_size, const char *name,
-               struct lttng_uri *uris, size_t nb_uri,
-               struct consumer_output *consumer, struct snapshot_output *output,
-               struct snapshot *snapshot)
+int snapshot_output_init_with_uri(const ltt_session::locked_ref& session,
+                                 uint64_t max_size,
+                                 const char *name,
+                                 struct lttng_uri *uris,
+                                 size_t nb_uri,
+                                 struct consumer_output *consumer,
+                                 struct snapshot_output *output,
+                                 struct snapshot *snapshot)
 {
-       return output_init(session, max_size, name, uris, nb_uri, consumer,
-                       output, snapshot);
+       return output_init(session, max_size, name, uris, nb_uri, consumer, output, snapshot);
 }
 
 /*
@@ -139,14 +148,17 @@ int snapshot_output_init_with_uri(const struct ltt_session *session,
  *
  * Return 0 on success or else a negative value.
  */
-int snapshot_output_init(const struct ltt_session *session,
-               uint64_t max_size, const char *name,
-               const char *ctrl_url, const char *data_url,
-               struct consumer_output *consumer, struct snapshot_output *output,
-               struct snapshot *snapshot)
+int snapshot_output_init(const ltt_session::locked_ref& session,
+                        uint64_t max_size,
+                        const char *name,
+                        const char *ctrl_url,
+                        const char *data_url,
+                        struct consumer_output *consumer,
+                        struct snapshot_output *output,
+                        struct snapshot *snapshot)
 {
        int ret = 0, nb_uri;
-       struct lttng_uri *uris = NULL;
+       struct lttng_uri *uris = nullptr;
 
        /* Create an array of URIs from URLs. */
        nb_uri = uri_parse_str_urls(ctrl_url, data_url, &uris);
@@ -155,24 +167,22 @@ int snapshot_output_init(const struct ltt_session *session,
                goto error;
        }
 
-       ret = output_init(session, max_size, name, uris, nb_uri, consumer,
-                       output, snapshot);
+       ret = output_init(session, max_size, name, uris, nb_uri, consumer, output, snapshot);
 
 error:
        free(uris);
        return ret;
 }
 
-struct snapshot_output *snapshot_output_alloc(void)
+struct snapshot_output *snapshot_output_alloc()
 {
-       return (snapshot_output *) zmalloc(sizeof(struct snapshot_output));
+       return new snapshot_output;
 }
 
 /*
  * Delete output from the snapshot object.
  */
-void snapshot_delete_output(struct snapshot *snapshot,
-               struct snapshot_output *output)
+void snapshot_delete_output(struct snapshot *snapshot, struct snapshot_output *output)
 {
        int ret;
        struct lttng_ht_iter iter;
@@ -182,9 +192,8 @@ void snapshot_delete_output(struct snapshot *snapshot,
        LTTNG_ASSERT(output);
 
        iter.iter.node = &output->node.node;
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
        ret = lttng_ht_del(snapshot->output_ht, &iter);
-       rcu_read_unlock();
        LTTNG_ASSERT(!ret);
        /*
         * This is safe because the ownership of a snapshot object is in a session
@@ -196,16 +205,14 @@ void snapshot_delete_output(struct snapshot *snapshot,
 /*
  * Add output object to the snapshot.
  */
-void snapshot_add_output(struct snapshot *snapshot,
-               struct snapshot_output *output)
+void snapshot_add_output(struct snapshot *snapshot, struct snapshot_output *output)
 {
        LTTNG_ASSERT(snapshot);
        LTTNG_ASSERT(snapshot->output_ht);
        LTTNG_ASSERT(output);
 
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
        lttng_ht_add_unique_ulong(snapshot->output_ht, &output->node);
-       rcu_read_unlock();
        /*
         * This is safe because the ownership of a snapshot object is in a session
         * for which the session lock need to be acquired to read and modify it.
@@ -224,7 +231,8 @@ void snapshot_output_destroy(struct snapshot_output *obj)
                consumer_output_send_destroy_relayd(obj->consumer);
                consumer_output_put(obj->consumer);
        }
-       free(obj);
+
+       delete obj;
 }
 
 /*
@@ -233,25 +241,22 @@ void snapshot_output_destroy(struct snapshot_output *obj)
  *
  * Return the reference on success or else NULL.
  */
-struct snapshot_output *snapshot_find_output_by_name(const char *name,
-               struct snapshot *snapshot)
+struct snapshot_output *snapshot_find_output_by_name(const char *name, struct snapshot *snapshot)
 {
-       struct lttng_ht_iter iter;
-       struct snapshot_output *output = NULL;
-
        LTTNG_ASSERT(snapshot);
        LTTNG_ASSERT(name);
-       ASSERT_RCU_READ_LOCKED();
 
-       cds_lfht_for_each_entry(snapshot->output_ht->ht, &iter.iter, output,
-               node.node) {
+       for (auto *output : lttng::urcu::lfht_iteration_adapter<snapshot_output,
+                                                               decltype(snapshot_output::node),
+                                                               &snapshot_output::node>(
+                    *snapshot->output_ht->ht)) {
                if (!strncmp(output->name, name, strlen(name))) {
                        return output;
                }
        }
 
        /* Not found */
-       return NULL;
+       return nullptr;
 }
 
 /*
@@ -260,23 +265,22 @@ struct snapshot_output *snapshot_find_output_by_name(const char *name,
  *
  * Return the reference on success or else NULL.
  */
-struct snapshot_output *snapshot_find_output_by_id(uint32_t id,
-               struct snapshot *snapshot)
+struct snapshot_output *snapshot_find_output_by_id(uint32_t id, struct snapshot *snapshot)
 {
        struct lttng_ht_node_ulong *node;
        struct lttng_ht_iter iter;
-       struct snapshot_output *output = NULL;
+       struct snapshot_output *output = nullptr;
 
        LTTNG_ASSERT(snapshot);
        ASSERT_RCU_READ_LOCKED();
 
-       lttng_ht_lookup(snapshot->output_ht, (void *)((unsigned long) id), &iter);
-       node = lttng_ht_iter_get_node_ulong(&iter);
+       lttng_ht_lookup(snapshot->output_ht, (void *) ((unsigned long) id), &iter);
+       node = lttng_ht_iter_get_node<lttng_ht_node_ulong>(&iter);
        if (!node) {
                DBG3("Snapshot output not found with id %" PRId32, id);
                goto error;
        }
-       output = caa_container_of(node, struct snapshot_output, node);
+       output = lttng::utils::container_of(node, &snapshot_output::node);
 
 error:
        return output;
@@ -313,19 +317,17 @@ error:
  */
 void snapshot_destroy(struct snapshot *obj)
 {
-       struct lttng_ht_iter iter;
-       struct snapshot_output *output;
-
        if (!obj->output_ht) {
                return;
        }
 
-       rcu_read_lock();
-       cds_lfht_for_each_entry(obj->output_ht->ht, &iter.iter, output,
-                       node.node) {
+       for (auto *output :
+            lttng::urcu::lfht_iteration_adapter<snapshot_output,
+                                                decltype(snapshot_output::node),
+                                                &snapshot_output::node>(*obj->output_ht->ht)) {
                snapshot_delete_output(obj, output);
                snapshot_output_destroy(output);
        }
-       rcu_read_unlock();
+
        lttng_ht_destroy(obj->output_ht);
 }
This page took 0.027647 seconds and 4 git commands to generate.