+static inline
+int bt_field_variant_initialize(struct bt_field *field,
+ struct bt_field_type *type,
+ struct bt_field_methods *methods,
+ bt_field_create_func field_create_func,
+ GDestroyNotify field_release_func)
+{
+ int ret = 0;
+ struct bt_field_type_variant *variant_type = (void *) type;
+ struct bt_field_variant *variant = (void *) field;
+ size_t i;
+
+ BT_LOGD("Initializing variant field object: ft-addr=%p", type);
+ bt_field_initialize(field, type, methods);
+ ret = bt_field_type_variant_update_choices(type);
+ if (ret) {
+ BT_LOGE("Cannot update variant field type choices: "
+ "ret=%d", ret);
+ goto end;
+ }
+
+ variant->fields = g_ptr_array_new_with_free_func(field_release_func);
+ g_ptr_array_set_size(variant->fields, variant_type->choices->len);
+
+ /* Create all fields contained in the variant field. */
+ for (i = 0; i < variant_type->choices->len; i++) {
+ struct bt_field *field;
+ struct bt_field_type_variant_choice *var_choice =
+ BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(
+ variant_type, i);
+
+ field = field_create_func(var_choice->type);
+ if (!field) {
+ BT_LOGE("Failed to create variant field's member: name=\"%s\", index=%zu",
+ g_quark_to_string(var_choice->name), i);
+ ret = -1;
+ goto end;
+ }
+
+ g_ptr_array_index(variant->fields, i) = field;
+ }
+
+ BT_LOGD("Initialized variant field object: addr=%p, ft-addr=%p",
+ field, type);
+
+end:
+ return ret;
+}
+
+static inline
+int bt_field_string_initialize(struct bt_field *field,
+ struct bt_field_type *type,
+ struct bt_field_methods *methods)
+{
+ int ret = 0;
+ struct bt_field_string *string = (void *) field;
+
+ BT_LOGD("Initializing string field object: ft-addr=%p", type);
+ bt_field_initialize(field, type, methods);
+ string->buf = g_array_sized_new(FALSE, FALSE, sizeof(char), 1);
+ if (!string->buf) {
+ ret = -1;
+ goto end;
+ }
+
+ g_array_index(string->buf, char, 0) = '\0';
+ BT_LOGD("Initialized string field object: addr=%p, ft-addr=%p",
+ field, type);
+
+end:
+ return ret;
+}
+