X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=types%2Fsequence.c;h=9d84d0eece59e8a9d4a1f878e5da8cb5296a7818;hp=39a047022fc02b261e32109b2b9d8dabc193c114;hb=05628561ca57ff5d269571a72a12cb86854c5f70;hpb=2e7d72cf823b70b03d84e287ae4fda5b9ead7cb5 diff --git a/types/sequence.c b/types/sequence.c index 39a04702..9d84d0ee 100644 --- a/types/sequence.c +++ b/types/sequence.c @@ -1,91 +1,134 @@ /* - * BabelTrace - Sequence Type Converter + * sequence.c * - * Copyright (c) 2010 Mathieu Desnoyers + * BabelTrace - Sequence Type Converter * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * Copyright 2010, 2011 - Mathieu Desnoyers * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. */ #include -#include +#include + +#ifndef max +#define max(a, b) ((a) < (b) ? (b) : (a)) +#endif + +static +struct declaration *_sequence_declaration_new(struct type *type, + struct declaration_scope *parent_scope); +static +void _sequence_declaration_free(struct declaration *declaration); void sequence_copy(struct stream_pos *dest, const struct format *fdest, struct stream_pos *src, const struct format *fsrc, - const struct type_class *type_class) + struct declaration *declaration) { - struct type_class_sequence *sequence_class = - container_of(type_class, struct type_class_sequence, p); - unsigned int i; - size_t len; + struct declaration_sequence *sequence = + container_of(declaration, struct declaration_sequence, p); + struct type_sequence *sequence_type = sequence->type; + uint64_t i; - fsrc->sequence_begin(src, sequence_class); - fdest->sequence_begin(dest, sequence_class); + fsrc->sequence_begin(src, sequence_type); + fdest->sequence_begin(dest, sequence_type); - len = fsrc->uint_read(src, sequence_class->len_class); - fdest->uint_write(dest, sequence_class->len_class, len); + sequence->len->p.type->copy(dest, fdest, src, fsrc, + &sequence->len->p); - for (i = 0; i < len; i++) { - struct type_class *elem_class = sequence_class->elem; - elem_class->copy(dest, fdest, src, fsrc, &elem_class->p); + for (i = 0; i < sequence->len->value._unsigned; i++) { + struct declaration *elem = + sequence->current_element.declaration; + elem->type->copy(dest, fdest, src, fsrc, elem); } - fsrc->sequence_end(src, sequence_class); - fdest->sequence_end(dest, sequence_class); + fsrc->sequence_end(src, sequence_type); + fdest->sequence_end(dest, sequence_type); } -void sequence_type_free(struct type_class_sequence *sequence_class) +static +void _sequence_type_free(struct type *type) { - sequence_class->elem->free(&sequence_class->elem->p); - g_free(sequence_class); -} + struct type_sequence *sequence_type = + container_of(type, struct type_sequence, p); -static void _sequence_type_free(struct type_class *type_class) -{ - struct type_class_struct *sequence_class = - container_of(type_class, struct type_class_sequence, p); - sequence_type_free(sequence_class); + free_type_scope(sequence_type->scope); + type_unref(&sequence_type->len_type->p); + type_unref(sequence_type->elem); + g_free(sequence_type); } -struct type_class_sequence * -sequence_type_new(const char *name, struct type_class_integer *len_class, - struct type_class *elem) +struct type_sequence * + sequence_type_new(const char *name, struct type_integer *len_type, + struct type *elem_type, + struct type_scope *parent_scope) { - struct type_class_sequence *sequence_class; - int ret; + struct type_sequence *sequence_type; + struct type *type; - sequence_class = g_new(struct type_class_sequence, 1); - type_class = &float_class->p; + sequence_type = g_new(struct type_sequence, 1); + type = &sequence_type->p; + assert(!len_type->signedness); + type_ref(&len_type->p); + sequence_type->len_type = len_type; + type_ref(elem_type); + sequence_type->elem = elem_type; + sequence_type->scope = new_type_scope(parent_scope); + type->id = CTF_TYPE_SEQUENCE; + type->name = g_quark_from_string(name); + type->alignment = max(len_type->p.alignment, elem_type->alignment); + type->copy = sequence_copy; + type->type_free = _sequence_type_free; + type->declaration_new = _sequence_declaration_new; + type->declaration_free = _sequence_declaration_free; + type->ref = 1; + return sequence_type; +} - assert(!len_class->signedness); +static +struct declaration *_sequence_declaration_new(struct type *type, + struct declaration_scope *parent_scope) +{ + struct type_sequence *sequence_type = + container_of(type, struct type_sequence, p); + struct declaration_sequence *sequence; + struct declaration *len_parent; - sequence_class->len = len; - type_class->name = g_quark_from_string(name); - type_class->alignment = max(len_class->p.alignment, - elem->p.alignment); - type_class->copy = sequence_copy; - type_class->free = _sequence_type_free; + sequence = g_new(struct declaration_sequence, 1); + type_ref(&sequence_type->p); + sequence->p.type = type; + sequence->type = sequence_type; + sequence->p.ref = 1; + sequence->scope = new_declaration_scope(parent_scope); + len_parent = sequence_type->len_type->p.declaration_new(&sequence_type->len_type->p, + parent_scope); + sequence->len = + container_of(len_parent, struct declaration_integer, p); + sequence->current_element.declaration = + sequence_type->elem->declaration_new(sequence_type->elem, + parent_scope); + return &sequence->p; +} - if (type_class->name) { - ret = ctf_register_type(type_class); - if (ret) - goto error_register; - } - return sequence_class; +static +void _sequence_declaration_free(struct declaration *declaration) +{ + struct declaration_sequence *sequence = + container_of(declaration, struct declaration_sequence, p); + struct declaration *len_declaration = &sequence->len->p; + struct declaration *elem_declaration = + sequence->current_element.declaration; -error_register: - len_class->p.free(&len_class->p); - sequence_class->elem->free(&sequence_class->elem->p); - g_free(sequence_class); - return NULL; + len_declaration->type->declaration_free(len_declaration); + elem_declaration->type->declaration_free(elem_declaration); + free_declaration_scope(sequence->scope); + type_unref(sequence->p.type); + g_free(sequence); }