X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=types%2Fstruct.c;h=779cca5c504064d3c03bb4a86962ab58a1917808;hp=ba3295f9a67eeedfe286dd2f812f76bebac03bfe;hb=05628561ca57ff5d269571a72a12cb86854c5f70;hpb=e19c3d69b39d2fa422ab54b5ec7192799f536680 diff --git a/types/struct.c b/types/struct.c index ba3295f9..779cca5c 100644 --- a/types/struct.c +++ b/types/struct.c @@ -44,9 +44,9 @@ void struct_copy(struct stream_pos *dest, const struct format *fdest, for (i = 0; i < _struct->fields->len; i++) { struct field *field = &g_array_index(_struct->fields, struct field, i); - struct type *field_type = field->type->p.type; + struct type *field_type = field->declaration->type; - field_type->copy(dest, fdest, src, fsrc, &field->type->p); + field_type->copy(dest, fdest, src, fsrc, field->declaration); } fsrc->struct_end(src, struct_type); @@ -60,23 +60,24 @@ void _struct_type_free(struct type *type) container_of(type, struct type_struct, p); unsigned long i; + free_type_scope(struct_type->scope); g_hash_table_destroy(struct_type->fields_by_name); for (i = 0; i < struct_type->fields->len; i++) { - struct field *type_field = + struct type_field *type_field = &g_array_index(struct_type->fields, struct type_field, i); - type_unref(field->type); + type_unref(type_field->type); } g_array_free(struct_type->fields, true); g_free(struct_type); } -struct type_struct *struct_type_new(const char *name) +struct type_struct *struct_type_new(const char *name, + struct type_scope *parent_scope) { struct type_struct *struct_type; struct type *type; - int ret; struct_type = g_new(struct type_struct, 1); type = &struct_type->p; @@ -85,6 +86,8 @@ struct type_struct *struct_type_new(const char *name) struct_type->fields = g_array_sized_new(FALSE, TRUE, sizeof(struct type_field), DEFAULT_NR_STRUCT_FIELDS); + struct_type->scope = new_type_scope(parent_scope); + type->id = CTF_TYPE_STRUCT; type->name = g_quark_from_string(name); type->alignment = 1; type->copy = struct_copy; @@ -92,19 +95,7 @@ struct type_struct *struct_type_new(const char *name) type->declaration_new = _struct_declaration_new; type->declaration_free = _struct_declaration_free; type->ref = 1; - - if (type->name) { - ret = register_type(type); - if (ret) - goto error_register; - } return struct_type; - -error_register: - g_hash_table_destroy(struct_type->fields_by_name); - g_array_free(struct_type->fields, true); - g_free(struct_type); - return NULL; } static @@ -115,15 +106,34 @@ struct declaration * struct type_struct *struct_type = container_of(type, struct type_struct, p); struct declaration_struct *_struct; + unsigned long i; + int ret; _struct = g_new(struct declaration_struct, 1); type_ref(&struct_type->p); - _struct->p.type = struct_type; + _struct->p.type = type; + _struct->type = struct_type; _struct->p.ref = 1; _struct->scope = new_declaration_scope(parent_scope); _struct->fields = g_array_sized_new(FALSE, TRUE, sizeof(struct field), DEFAULT_NR_STRUCT_FIELDS); + g_array_set_size(_struct->fields, struct_type->fields->len); + for (i = 0; i < struct_type->fields->len; i++) { + struct type_field *type_field = + &g_array_index(struct_type->fields, + struct type_field, i); + struct field *field = &g_array_index(_struct->fields, + struct field, i); + + field->name = type_field->name; + field->declaration = + type_field->type->declaration_new(type_field->type, + _struct->scope); + ret = register_declaration(field->name, + field->declaration, _struct->scope); + assert(!ret); + } return &_struct->p; } @@ -134,6 +144,7 @@ void _struct_declaration_free(struct declaration *declaration) container_of(declaration, struct declaration_struct, p); unsigned long i; + assert(_struct->fields->len == _struct->type->fields->len); for (i = 0; i < _struct->fields->len; i++) { struct field *field = &g_array_index(_struct->fields, struct field, i);