Rename "declaration" to "definition"
[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 type *type,
28 struct definition_scope *parent_scope);
29 static
30 void _sequence_definition_free(struct definition *definition);
31
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)
35 {
36 struct definition_sequence *sequence =
37 container_of(definition, struct definition_sequence, p);
38 struct type_sequence *sequence_type = sequence->type;
39 uint64_t i;
40
41 fsrc->sequence_begin(src, sequence_type);
42 fdest->sequence_begin(dest, sequence_type);
43
44 sequence->len->p.type->copy(dest, fdest, src, fsrc,
45 &sequence->len->p);
46
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);
51 }
52 fsrc->sequence_end(src, sequence_type);
53 fdest->sequence_end(dest, sequence_type);
54 }
55
56 static
57 void _sequence_type_free(struct type *type)
58 {
59 struct type_sequence *sequence_type =
60 container_of(type, struct type_sequence, p);
61
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);
66 }
67
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)
72 {
73 struct type_sequence *sequence_type;
74 struct type *type;
75
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;
81 type_ref(elem_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;
91 type->ref = 1;
92 return sequence_type;
93 }
94
95 static
96 struct definition *_sequence_definition_new(struct type *type,
97 struct definition_scope *parent_scope)
98 {
99 struct type_sequence *sequence_type =
100 container_of(type, struct type_sequence, p);
101 struct definition_sequence *sequence;
102 struct definition *len_parent;
103
104 sequence = g_new(struct definition_sequence, 1);
105 type_ref(&sequence_type->p);
106 sequence->p.type = type;
107 sequence->type = sequence_type;
108 sequence->p.ref = 1;
109 sequence->scope = new_definition_scope(parent_scope);
110 len_parent = sequence_type->len_type->p.definition_new(&sequence_type->len_type->p,
111 parent_scope);
112 sequence->len =
113 container_of(len_parent, struct definition_integer, p);
114 sequence->current_element.definition =
115 sequence_type->elem->definition_new(sequence_type->elem,
116 parent_scope);
117 return &sequence->p;
118 }
119
120 static
121 void _sequence_definition_free(struct definition *definition)
122 {
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;
128
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);
133 g_free(sequence);
134 }
This page took 0.031178 seconds and 4 git commands to generate.