+static void destroy_enum(struct ust_registry_enum *reg_enum)
+{
+ if (!reg_enum) {
+ return;
+ }
+ free(reg_enum->entries);
+ free(reg_enum);
+}
+
+static void destroy_enum_rcu(struct rcu_head *head)
+{
+ struct ust_registry_enum *reg_enum =
+ caa_container_of(head, struct ust_registry_enum, rcu_head);
+
+ destroy_enum(reg_enum);
+}
+
+/*
+ * Lookup enumeration by name and comparing enumeration entries.
+ * Needs to be called from RCU read-side critical section.
+ */
+struct ust_registry_enum *
+ ust_registry_lookup_enum(struct ust_registry_session *session,
+ const struct ust_registry_enum *reg_enum_lookup)
+{
+ struct ust_registry_enum *reg_enum = NULL;
+ struct lttng_ht_node_str *node;
+ struct lttng_ht_iter iter;
+
+ cds_lfht_lookup(session->enums->ht,
+ ht_hash_enum((void *) ®_enum_lookup, lttng_ht_seed),
+ ht_match_enum, ®_enum_lookup, &iter.iter);
+ node = lttng_ht_iter_get_node_str(&iter);
+ if (!node) {
+ goto end;
+ }
+ reg_enum = caa_container_of(node, struct ust_registry_enum, node);
+end:
+ return reg_enum;
+}
+
+/*
+ * Lookup enumeration by enum ID.
+ * Needs to be called from RCU read-side critical section.
+ */
+struct ust_registry_enum *
+ ust_registry_lookup_enum_by_id(struct ust_registry_session *session,
+ const char *enum_name, uint64_t enum_id)
+{
+ struct ust_registry_enum *reg_enum = NULL;
+ struct lttng_ht_node_str *node;
+ struct lttng_ht_iter iter;
+ struct ust_registry_enum reg_enum_lookup;
+
+ memset(®_enum_lookup, 0, sizeof(reg_enum_lookup));
+ strncpy(reg_enum_lookup.name, enum_name, LTTNG_UST_SYM_NAME_LEN);
+ reg_enum_lookup.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
+ reg_enum_lookup.id = enum_id;
+ cds_lfht_lookup(session->enums->ht,
+ ht_hash_enum((void *) ®_enum_lookup, lttng_ht_seed),
+ ht_match_enum_id, ®_enum_lookup, &iter.iter);
+ node = lttng_ht_iter_get_node_str(&iter);
+ if (!node) {
+ goto end;
+ }
+ reg_enum = caa_container_of(node, struct ust_registry_enum, node);
+end:
+ return reg_enum;
+}
+
+/*
+ * Create a ust_registry_enum from the given parameters and add it to the
+ * registry hash table, or find it if already there.
+ *
+ * On success, return 0 else a negative value.
+ *
+ * Should be called with session registry mutex held.
+ *
+ * We receive ownership of entries.
+ */
+int ust_registry_create_or_find_enum(struct ust_registry_session *session,
+ int session_objd, char *enum_name,
+ struct ustctl_enum_entry *entries, size_t nr_entries,
+ uint64_t *enum_id)
+{
+ int ret = 0;
+ struct cds_lfht_node *nodep;
+ struct ust_registry_enum *reg_enum = NULL, *old_reg_enum;
+
+ assert(session);
+ assert(enum_name);
+
+ rcu_read_lock();
+
+ /*
+ * This should not happen but since it comes from the UST tracer, an
+ * external party, don't assert and simply validate values.
+ */
+ if (session_objd < 0) {
+ ret = -EINVAL;
+ goto end;
+ }
+
+ /* Check if the enumeration was already dumped */
+ reg_enum = zmalloc(sizeof(*reg_enum));
+ if (!reg_enum) {
+ PERROR("zmalloc ust registry enumeration");
+ ret = -ENOMEM;
+ goto end;
+ }
+ strncpy(reg_enum->name, enum_name, LTTNG_UST_SYM_NAME_LEN);
+ reg_enum->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
+ /* entries will be owned by reg_enum. */
+ reg_enum->entries = entries;
+ reg_enum->nr_entries = nr_entries;
+ entries = NULL;
+
+ old_reg_enum = ust_registry_lookup_enum(session, reg_enum);
+ if (old_reg_enum) {
+ DBG("enum %s already in sess_objd: %u", enum_name, session_objd);
+ /* Fall through. Use prior enum. */
+ destroy_enum(reg_enum);
+ reg_enum = old_reg_enum;
+ } else {
+ DBG("UST registry creating enum: %s, sess_objd: %u",
+ enum_name, session_objd);
+ if (session->next_enum_id == -1ULL) {
+ ret = -EOVERFLOW;
+ destroy_enum(reg_enum);
+ goto end;
+ }
+ reg_enum->id = session->next_enum_id++;
+ cds_lfht_node_init(®_enum->node.node);
+ nodep = cds_lfht_add_unique(session->enums->ht,
+ ht_hash_enum(reg_enum, lttng_ht_seed),
+ ht_match_enum_id, reg_enum,
+ ®_enum->node.node);
+ assert(nodep == ®_enum->node.node);
+ }
+ DBG("UST registry reply with enum %s with id %" PRIu64 " in sess_objd: %u",
+ enum_name, reg_enum->id, session_objd);
+ *enum_id = reg_enum->id;
+end:
+ free(entries);
+ rcu_read_unlock();
+ return ret;
+}
+
+/*
+ * For a given enumeration in a registry, delete the entry and destroy
+ * the enumeration.
+ * This MUST be called within a RCU read side lock section.
+ */
+void ust_registry_destroy_enum(struct ust_registry_session *reg_session,
+ struct ust_registry_enum *reg_enum)
+{
+ int ret;
+ struct lttng_ht_iter iter;
+
+ assert(reg_session);
+ assert(reg_enum);
+
+ /* Delete the node first. */
+ iter.iter.node = ®_enum->node.node;
+ ret = lttng_ht_del(reg_session->enums, &iter);
+ assert(!ret);
+ call_rcu(®_enum->rcu_head, destroy_enum_rcu);
+}
+