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 declaration
*_sequence_declaration_new(struct type
*type
,
28 struct declaration_scope
*parent_scope
);
30 void _sequence_declaration_free(struct declaration
*declaration
);
32 void sequence_copy(struct stream_pos
*dest
, const struct format
*fdest
,
33 struct stream_pos
*src
, const struct format
*fsrc
,
34 struct declaration
*declaration
)
36 struct declaration_sequence
*sequence
=
37 container_of(declaration
, struct declaration_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 declaration
*elem
=
49 sequence
->current_element
.declaration
;
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 type_unref(&sequence_type
->len_type
->p
);
63 type_unref(sequence_type
->elem
);
64 g_free(sequence_type
);
67 struct type_sequence
*
68 sequence_type_new(const char *name
, struct type_integer
*len_type
,
69 struct type
*elem_type
)
71 struct type_sequence
*sequence_type
;
74 sequence_type
= g_new(struct type_sequence
, 1);
75 type
= &sequence_type
->p
;
76 assert(!len_type
->signedness
);
77 type_ref(&len_type
->p
);
78 sequence_type
->len_type
= len_type
;
80 sequence_type
->elem
= elem_type
;
81 type
->name
= g_quark_from_string(name
);
82 type
->alignment
= max(len_type
->p
.alignment
, elem_type
->alignment
);
83 type
->copy
= sequence_copy
;
84 type
->type_free
= _sequence_type_free
;
85 type
->declaration_new
= _sequence_declaration_new
;
86 type
->declaration_free
= _sequence_declaration_free
;
92 struct declaration
*_sequence_declaration_new(struct type
*type
,
93 struct declaration_scope
*parent_scope
)
95 struct type_sequence
*sequence_type
=
96 container_of(type
, struct type_sequence
, p
);
97 struct declaration_sequence
*sequence
;
98 struct declaration
*len_parent
;
100 sequence
= g_new(struct declaration_sequence
, 1);
101 type_ref(&sequence_type
->p
);
102 sequence
->p
.type
= type
;
103 sequence
->type
= sequence_type
;
105 sequence
->scope
= new_declaration_scope(parent_scope
);
106 len_parent
= sequence_type
->len_type
->p
.declaration_new(&sequence_type
->len_type
->p
,
109 container_of(len_parent
, struct declaration_integer
, p
);
110 sequence
->current_element
.declaration
=
111 sequence_type
->elem
->declaration_new(sequence_type
->elem
,
117 void _sequence_declaration_free(struct declaration
*declaration
)
119 struct declaration_sequence
*sequence
=
120 container_of(declaration
, struct declaration_sequence
, p
);
121 struct declaration
*len_declaration
= &sequence
->len
->p
;
122 struct declaration
*elem_declaration
=
123 sequence
->current_element
.declaration
;
125 len_declaration
->type
->declaration_free(len_declaration
);
126 elem_declaration
->type
->declaration_free(elem_declaration
);
127 free_declaration_scope(sequence
->scope
);
128 type_unref(sequence
->p
.type
);