+/*
+ * Allocate the ltt_sessions_ht_by_id HT.
+ *
+ * The session list lock must be held.
+ */
+int ltt_sessions_ht_alloc(void)
+{
+ int ret = 0;
+
+ DBG("Allocating ltt_sessions_ht_by_id");
+ ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
+ if (!ltt_sessions_ht_by_id) {
+ ret = -1;
+ ERR("Failed to allocate ltt_sessions_ht_by_id");
+ goto end;
+ }
+end:
+ return ret;
+}
+
+/*
+ * Destroy the ltt_sessions_ht_by_id HT.
+ *
+ * The session list lock must be held.
+ */
+static void ltt_sessions_ht_destroy(void)
+{
+ if (!ltt_sessions_ht_by_id) {
+ return;
+ }
+ ht_cleanup_push(ltt_sessions_ht_by_id);
+ ltt_sessions_ht_by_id = NULL;
+}
+
+/*
+ * Add a ltt_session to the ltt_sessions_ht_by_id.
+ * If unallocated, the ltt_sessions_ht_by_id HT is allocated.
+ * The session list lock must be held.
+ */
+static void add_session_ht(struct ltt_session *ls)
+{
+ int ret;
+
+ assert(ls);
+
+ if (!ltt_sessions_ht_by_id) {
+ ret = ltt_sessions_ht_alloc();
+ if (ret) {
+ ERR("Error allocating the sessions HT");
+ goto end;
+ }
+ }
+ lttng_ht_node_init_u64(&ls->node, ls->id);
+ lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
+
+end:
+ return;
+}
+
+/*
+ * Test if ltt_sessions_ht_by_id is empty.
+ * Return 1 if empty, 0 if not empty.
+ * The session list lock must be held.
+ */
+static int ltt_sessions_ht_empty(void)
+{
+ int ret;
+
+ if (!ltt_sessions_ht_by_id) {
+ ret = 1;
+ goto end;
+ }
+
+ ret = lttng_ht_get_count(ltt_sessions_ht_by_id) ? 0 : 1;
+end:
+ return ret;
+}
+
+/*
+ * Remove a ltt_session from the ltt_sessions_ht_by_id.
+ * If empty, the ltt_sessions_ht_by_id HT is freed.
+ * The session list lock must be held.
+ */
+static void del_session_ht(struct ltt_session *ls)
+{
+ struct lttng_ht_iter iter;
+ int ret;
+
+ assert(ls);
+ assert(ltt_sessions_ht_by_id);
+
+ iter.iter.node = &ls->node.node;
+ ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
+ assert(!ret);
+
+ if (ltt_sessions_ht_empty()) {
+ DBG("Empty ltt_sessions_ht_by_id, destroying it");
+ ltt_sessions_ht_destroy();
+ }
+}
+