Rename: type_class, type -> type, declaration
[babeltrace.git] / types / sequence.c
... / ...
CommitLineData
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
26static
27struct declaration *_sequence_declaration_new(struct type *type,
28 struct declaration_scope *parent_scope);
29static
30void _sequence_declaration_free(struct declaration *declaration);
31
32void 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_type =
49 sequence->current_element.declaration;
50 elem_type->p.type->copy(dest, fdest, src, fsrc, elem_type);
51 }
52 fsrc->sequence_end(src, sequence_type);
53 fdest->sequence_end(dest, sequence_type);
54}
55
56static
57void _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
67struct 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 int ret;
74
75 sequence_type = g_new(struct type_sequence, 1);
76 type = &sequence_type->p;
77 assert(!len_type->signedness);
78 type_ref(&len_type->p);
79 sequence_type->len_type = len_type;
80 type_ref(elem_type);
81 sequence_type->elem = elem_type;
82 type->name = g_quark_from_string(name);
83 type->alignment = max(len_type->p.alignment, elem_type->alignment);
84 type->copy = sequence_copy;
85 type->type_free = _sequence_type_free;
86 type->declaration_new = _sequence_declaration_new;
87 type->declaration_free = _sequence_declaration_free;
88 type->ref = 1;
89
90 if (type->name) {
91 ret = register_type(type);
92 if (ret)
93 goto error_register;
94 }
95 return sequence_type;
96
97error_register:
98 type_unref(&len_type->p);
99 type_unref(elem_type);
100 g_free(sequence_type);
101 return NULL;
102}
103
104static
105struct declaration *_sequence_declaration_new(struct type *type,
106 struct declaration_scope *parent_scope)
107{
108 struct type_sequence *sequence_type =
109 container_of(type, struct type_sequence, p);
110 struct declaration_sequence *sequence;
111 struct declaration *len_parent;
112
113 sequence = g_new(struct declaration_sequence, 1);
114 type_ref(&sequence_type->p);
115 sequence->p.type = sequence_type;
116 sequence->p.ref = 1;
117 sequence->scope = new_declaration_scope(parent_scope);
118 len_parent =
119 sequence_type->len_type.p->type_new(&sequence_type->len_type.p,
120 parent_scope);
121 sequence->len =
122 container_of(len_parent, struct declaration_integer, p);
123 sequence->current_element.declaration =
124 sequence_type->elem.p->type_new(&sequence_type->elem.p,
125 parent_scope);
126 return &sequence->p;
127}
128
129static
130void _sequence_declaration_free(struct declaration *declaration)
131{
132 struct type_sequence *sequence =
133 container_of(declaration, struct type_sequence, p);
134 struct declaration *len_declaration = sequence->len;
135 struct declaration *elem_declaration =
136 sequence->current_element.declaration;
137
138 len_declaration->p.type->declaration_free(len_declaration);
139 elem_declaration->p.type->declaration_free(elem_declaration);
140 free_declaration_scope(sequence->scope);
141 type_unref(sequence->p.type);
142 g_free(sequence);
143}
This page took 0.022571 seconds and 4 git commands to generate.