4 * BabelTrace - Sequence Type Converter
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
23 #define max(a, b) ((a) < (b) ? (b) : (a))
27 struct definition
*_sequence_definition_new(struct type
*type
,
28 struct definition_scope
*parent_scope
);
30 void _sequence_definition_free(struct definition
*definition
);
32 void sequence_copy(struct stream_pos
*dest
, const struct format
*fdest
,
33 struct stream_pos
*src
, const struct format
*fsrc
,
34 struct definition
*definition
)
36 struct definition_sequence
*sequence
=
37 container_of(definition
, struct definition_sequence
, p
);
38 struct type_sequence
*sequence_type
= sequence
->type
;
41 fsrc
->sequence_begin(src
, sequence_type
);
42 fdest
->sequence_begin(dest
, sequence_type
);
44 sequence
->len
->p
.type
->copy(dest
, fdest
, src
, fsrc
,
47 for (i
= 0; i
< sequence
->len
->value
._unsigned
; i
++) {
48 struct definition
*elem
=
49 sequence
->current_element
.definition
;
50 elem
->type
->copy(dest
, fdest
, src
, fsrc
, elem
);
52 fsrc
->sequence_end(src
, sequence_type
);
53 fdest
->sequence_end(dest
, sequence_type
);
57 void _sequence_type_free(struct type
*type
)
59 struct type_sequence
*sequence_type
=
60 container_of(type
, struct type_sequence
, p
);
62 free_type_scope(sequence_type
->scope
);
63 type_unref(&sequence_type
->len_type
->p
);
64 type_unref(sequence_type
->elem
);
65 g_free(sequence_type
);
68 struct type_sequence
*
69 sequence_type_new(const char *name
, struct type_integer
*len_type
,
70 struct type
*elem_type
,
71 struct type_scope
*parent_scope
)
73 struct type_sequence
*sequence_type
;
76 sequence_type
= g_new(struct type_sequence
, 1);
77 type
= &sequence_type
->p
;
78 assert(!len_type
->signedness
);
79 type_ref(&len_type
->p
);
80 sequence_type
->len_type
= len_type
;
82 sequence_type
->elem
= elem_type
;
83 sequence_type
->scope
= new_type_scope(parent_scope
);
84 type
->id
= CTF_TYPE_SEQUENCE
;
85 type
->name
= g_quark_from_string(name
);
86 type
->alignment
= max(len_type
->p
.alignment
, elem_type
->alignment
);
87 type
->copy
= sequence_copy
;
88 type
->type_free
= _sequence_type_free
;
89 type
->definition_new
= _sequence_definition_new
;
90 type
->definition_free
= _sequence_definition_free
;
96 struct definition
*_sequence_definition_new(struct type
*type
,
97 struct definition_scope
*parent_scope
)
99 struct type_sequence
*sequence_type
=
100 container_of(type
, struct type_sequence
, p
);
101 struct definition_sequence
*sequence
;
102 struct definition
*len_parent
;
104 sequence
= g_new(struct definition_sequence
, 1);
105 type_ref(&sequence_type
->p
);
106 sequence
->p
.type
= type
;
107 sequence
->type
= sequence_type
;
109 sequence
->scope
= new_definition_scope(parent_scope
);
110 len_parent
= sequence_type
->len_type
->p
.definition_new(&sequence_type
->len_type
->p
,
113 container_of(len_parent
, struct definition_integer
, p
);
114 sequence
->current_element
.definition
=
115 sequence_type
->elem
->definition_new(sequence_type
->elem
,
121 void _sequence_definition_free(struct definition
*definition
)
123 struct definition_sequence
*sequence
=
124 container_of(definition
, struct definition_sequence
, p
);
125 struct definition
*len_definition
= &sequence
->len
->p
;
126 struct definition
*elem_definition
=
127 sequence
->current_element
.definition
;
129 len_definition
->type
->definition_free(len_definition
);
130 elem_definition
->type
->definition_free(elem_definition
);
131 free_definition_scope(sequence
->scope
);
132 type_unref(sequence
->p
.type
);