+int lttng_context_add_rcu(struct lttng_ctx **ctx_p,
+ const struct lttng_ctx_field *f)
+{
+ struct lttng_ctx *old_ctx = *ctx_p, *new_ctx = NULL;
+ struct lttng_ctx_field *new_fields = NULL;
+ struct lttng_ctx_field *nf;
+
+ if (old_ctx) {
+ new_ctx = zmalloc(sizeof(struct lttng_ctx));
+ if (!new_ctx)
+ return -ENOMEM;
+ *new_ctx = *old_ctx;
+ new_fields = zmalloc(new_ctx->allocated_fields
+ * sizeof(struct lttng_ctx_field));
+ if (!new_fields) {
+ free(new_ctx);
+ return -ENOMEM;
+ }
+ memcpy(new_fields, old_ctx->fields,
+ sizeof(*old_ctx->fields) * old_ctx->nr_fields);
+ new_ctx->fields = new_fields;
+ }
+ nf = lttng_append_context(&new_ctx);
+ if (!nf) {
+ free(new_fields);
+ free(new_ctx);
+ return -ENOMEM;
+ }
+ *nf = *f;
+ lttng_context_update(new_ctx);
+ rcu_assign_pointer(*ctx_p, new_ctx);
+ synchronize_trace();
+ if (old_ctx) {
+ free(old_ctx->fields);
+ free(old_ctx);
+ }
+ return 0;
+}
+
+/*
+ * lttng_context_update() should be called at least once between context
+ * modification and trace start.
+ */
+void lttng_context_update(struct lttng_ctx *ctx)
+{
+ int i;
+ size_t largest_align = 8; /* in bits */
+
+ for (i = 0; i < ctx->nr_fields; i++) {
+ struct lttng_type *type;
+ size_t field_align = 8;
+
+ type = &ctx->fields[i].event_field.type;
+ switch (type->atype) {
+ case atype_integer:
+ field_align = type->u.basic.integer.alignment;
+ break;
+ case atype_array:
+ {
+ struct lttng_basic_type *btype;
+
+ btype = &type->u.array.elem_type;
+ switch (btype->atype) {
+ case atype_integer:
+ field_align = btype->u.basic.integer.alignment;
+ break;
+ case atype_string:
+ break;
+
+ case atype_array:
+ case atype_sequence:
+ default:
+ WARN_ON_ONCE(1);
+ break;
+ }
+ break;
+ }
+ case atype_sequence:
+ {
+ struct lttng_basic_type *btype;
+
+ btype = &type->u.sequence.length_type;
+ switch (btype->atype) {
+ case atype_integer:
+ field_align = btype->u.basic.integer.alignment;
+ break;
+
+ case atype_string:
+ case atype_array:
+ case atype_sequence:
+ default:
+ WARN_ON_ONCE(1);
+ break;
+ }
+
+ btype = &type->u.sequence.elem_type;
+ switch (btype->atype) {
+ case atype_integer:
+ field_align = max_t(size_t,
+ field_align,
+ btype->u.basic.integer.alignment);
+ break;
+
+ case atype_string:
+ break;
+
+ case atype_array:
+ case atype_sequence:
+ default:
+ WARN_ON_ONCE(1);
+ break;
+ }
+ break;
+ }
+ case atype_string:
+ break;
+ case atype_dynamic:
+ break;
+ case atype_enum:
+ default:
+ WARN_ON_ONCE(1);
+ break;
+ }
+ largest_align = max_t(size_t, largest_align, field_align);
+ }
+ ctx->largest_align = largest_align >> 3; /* bits to bytes */
+}
+