Definition scope: update
[babeltrace.git] / types / sequence.c
1 /*
2 * sequence.c
3 *
4 * BabelTrace - Sequence Type Converter
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
21
22 #ifndef max
23 #define max(a, b) ((a) < (b) ? (b) : (a))
24 #endif
25
26 static
27 struct definition *_sequence_definition_new(struct declaration *declaration,
28 struct definition_scope *parent_scope,
29 GQuark field_name, int index);
30 static
31 void _sequence_definition_free(struct definition *definition);
32
33 void sequence_copy(struct stream_pos *dest, const struct format *fdest,
34 struct stream_pos *src, const struct format *fsrc,
35 struct definition *definition)
36 {
37 struct definition_sequence *sequence =
38 container_of(definition, struct definition_sequence, p);
39 struct declaration_sequence *sequence_declaration = sequence->declaration;
40 uint64_t i;
41
42 fsrc->sequence_begin(src, sequence_declaration);
43 fdest->sequence_begin(dest, sequence_declaration);
44
45 sequence->len->p.declaration->copy(dest, fdest, src, fsrc,
46 &sequence->len->p);
47
48 for (i = 0; i < sequence->len->value._unsigned; i++) {
49 struct definition *elem =
50 sequence->current_element.definition;
51 elem->declaration->copy(dest, fdest, src, fsrc, elem);
52 }
53 fsrc->sequence_end(src, sequence_declaration);
54 fdest->sequence_end(dest, sequence_declaration);
55 }
56
57 static
58 void _sequence_declaration_free(struct declaration *declaration)
59 {
60 struct declaration_sequence *sequence_declaration =
61 container_of(declaration, struct declaration_sequence, p);
62
63 free_declaration_scope(sequence_declaration->scope);
64 declaration_unref(&sequence_declaration->len_declaration->p);
65 declaration_unref(sequence_declaration->elem);
66 g_free(sequence_declaration);
67 }
68
69 struct declaration_sequence *
70 sequence_declaration_new(const char *name, struct declaration_integer *len_declaration,
71 struct declaration *elem_declaration,
72 struct declaration_scope *parent_scope)
73 {
74 struct declaration_sequence *sequence_declaration;
75 struct declaration *declaration;
76
77 sequence_declaration = g_new(struct declaration_sequence, 1);
78 declaration = &sequence_declaration->p;
79 assert(!len_declaration->signedness);
80 declaration_ref(&len_declaration->p);
81 sequence_declaration->len_declaration = len_declaration;
82 declaration_ref(elem_declaration);
83 sequence_declaration->elem = elem_declaration;
84 sequence_declaration->scope = new_declaration_scope(parent_scope);
85 declaration->id = CTF_TYPE_SEQUENCE;
86 declaration->name = g_quark_from_string(name);
87 declaration->alignment = max(len_declaration->p.alignment, elem_declaration->alignment);
88 declaration->copy = sequence_copy;
89 declaration->declaration_free = _sequence_declaration_free;
90 declaration->definition_new = _sequence_definition_new;
91 declaration->definition_free = _sequence_definition_free;
92 declaration->ref = 1;
93 return sequence_declaration;
94 }
95
96 static
97 struct definition *_sequence_definition_new(struct declaration *declaration,
98 struct definition_scope *parent_scope,
99 GQuark field_name, int index)
100 {
101 struct declaration_sequence *sequence_declaration =
102 container_of(declaration, struct declaration_sequence, p);
103 struct definition_sequence *sequence;
104 struct definition *len_parent;
105
106 sequence = g_new(struct definition_sequence, 1);
107 declaration_ref(&sequence_declaration->p);
108 sequence->p.declaration = declaration;
109 sequence->declaration = sequence_declaration;
110 sequence->p.ref = 1;
111 sequence->p.index = index;
112 sequence->scope = new_definition_scope(parent_scope, field_name);
113 len_parent = sequence_declaration->len_declaration->p.definition_new(&sequence_declaration->len_declaration->p,
114 parent_scope,
115 g_quark_from_static_string("length"), 0);
116 sequence->len =
117 container_of(len_parent, struct definition_integer, p);
118 sequence->current_element.definition =
119 sequence_declaration->elem->definition_new(sequence_declaration->elem,
120 parent_scope,
121 g_quark_from_static_string("[]"), 1);
122 return &sequence->p;
123 }
124
125 static
126 void _sequence_definition_free(struct definition *definition)
127 {
128 struct definition_sequence *sequence =
129 container_of(definition, struct definition_sequence, p);
130 struct definition *len_definition = &sequence->len->p;
131 struct definition *elem_definition =
132 sequence->current_element.definition;
133
134 len_definition->declaration->definition_free(len_definition);
135 elem_definition->declaration->definition_free(elem_definition);
136 free_definition_scope(sequence->scope);
137 declaration_unref(sequence->p.declaration);
138 g_free(sequence);
139 }
This page took 0.030692 seconds and 4 git commands to generate.