Build fixes.
[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 declaration *_sequence_declaration_new(struct type *type,
28 struct declaration_scope *parent_scope);
29 static
30 void _sequence_declaration_free(struct declaration *declaration);
31
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)
35 {
36 struct declaration_sequence *sequence =
37 container_of(declaration, struct declaration_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 declaration *elem =
49 sequence->current_element.declaration;
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 type_unref(&sequence_type->len_type->p);
63 type_unref(sequence_type->elem);
64 g_free(sequence_type);
65 }
66
67 struct type_sequence *
68 sequence_type_new(const char *name, struct type_integer *len_type,
69 struct type *elem_type)
70 {
71 struct type_sequence *sequence_type;
72 struct type *type;
73
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;
79 type_ref(elem_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;
87 type->ref = 1;
88 return sequence_type;
89 }
90
91 static
92 struct declaration *_sequence_declaration_new(struct type *type,
93 struct declaration_scope *parent_scope)
94 {
95 struct type_sequence *sequence_type =
96 container_of(type, struct type_sequence, p);
97 struct declaration_sequence *sequence;
98 struct declaration *len_parent;
99
100 sequence = g_new(struct declaration_sequence, 1);
101 type_ref(&sequence_type->p);
102 sequence->p.type = type;
103 sequence->type = sequence_type;
104 sequence->p.ref = 1;
105 sequence->scope = new_declaration_scope(parent_scope);
106 len_parent = sequence_type->len_type->p.declaration_new(&sequence_type->len_type->p,
107 parent_scope);
108 sequence->len =
109 container_of(len_parent, struct declaration_integer, p);
110 sequence->current_element.declaration =
111 sequence_type->elem->declaration_new(sequence_type->elem,
112 parent_scope);
113 return &sequence->p;
114 }
115
116 static
117 void _sequence_declaration_free(struct declaration *declaration)
118 {
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;
124
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);
129 g_free(sequence);
130 }
This page took 0.031686 seconds and 4 git commands to generate.