Rename: type_class, type -> type, declaration
[babeltrace.git] / types / sequence.c
CommitLineData
2e7d72cf 1/*
ccd7e1c8 2 * sequence.c
2e7d72cf 3 *
ccd7e1c8 4 * BabelTrace - Sequence Type Converter
2e7d72cf 5 *
c054553d 6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2e7d72cf 7 *
ccd7e1c8
MD
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:
2e7d72cf 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
2e7d72cf
MD
17 */
18
19#include <babeltrace/compiler.h>
4c8bfb7e 20#include <babeltrace/format.h>
2e7d72cf 21
be85c1c7
MD
22#ifndef max
23#define max(a, b) ((a) < (b) ? (b) : (a))
24#endif
25
c054553d 26static
e19c3d69
MD
27struct declaration *_sequence_declaration_new(struct type *type,
28 struct declaration_scope *parent_scope);
c054553d 29static
e19c3d69 30void _sequence_declaration_free(struct declaration *declaration);
c054553d 31
2e7d72cf
MD
32void sequence_copy(struct stream_pos *dest, const struct format *fdest,
33 struct stream_pos *src, const struct format *fsrc,
e19c3d69 34 struct declaration *declaration)
2e7d72cf 35{
e19c3d69
MD
36 struct declaration_sequence *sequence =
37 container_of(declaration, struct declaration_sequence, p);
38 struct type_sequence *sequence_type = sequence->type;
c054553d 39 uint64_t i;
2e7d72cf 40
e19c3d69
MD
41 fsrc->sequence_begin(src, sequence_type);
42 fdest->sequence_begin(dest, sequence_type);
2e7d72cf 43
e19c3d69
MD
44 sequence->len->p.type->copy(dest, fdest, src, fsrc,
45 &sequence->len->p);
2e7d72cf 46
c054553d 47 for (i = 0; i < sequence->len->value._unsigned; i++) {
e19c3d69
MD
48 struct declaration *elem_type =
49 sequence->current_element.declaration;
50 elem_type->p.type->copy(dest, fdest, src, fsrc, elem_type);
2e7d72cf 51 }
e19c3d69
MD
52 fsrc->sequence_end(src, sequence_type);
53 fdest->sequence_end(dest, sequence_type);
2e7d72cf
MD
54}
55
c054553d 56static
e19c3d69 57void _sequence_type_free(struct type *type)
2e7d72cf 58{
e19c3d69
MD
59 struct type_sequence *sequence_type =
60 container_of(type, struct type_sequence, p);
c054553d 61
e19c3d69
MD
62 type_unref(&sequence_type->len_type->p);
63 type_unref(sequence_type->elem);
64 g_free(sequence_type);
2e7d72cf
MD
65}
66
e19c3d69
MD
67struct type_sequence *
68 sequence_type_new(const char *name, struct type_integer *len_type,
69 struct type *elem_type)
2e7d72cf 70{
e19c3d69
MD
71 struct type_sequence *sequence_type;
72 struct type *type;
2e7d72cf
MD
73 int ret;
74
e19c3d69
MD
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);
2e7d72cf
MD
92 if (ret)
93 goto error_register;
94 }
e19c3d69 95 return sequence_type;
2e7d72cf
MD
96
97error_register:
e19c3d69
MD
98 type_unref(&len_type->p);
99 type_unref(elem_type);
100 g_free(sequence_type);
2e7d72cf
MD
101 return NULL;
102}
c054553d
MD
103
104static
e19c3d69 105struct declaration *_sequence_declaration_new(struct type *type,
c054553d
MD
106 struct declaration_scope *parent_scope)
107{
e19c3d69
MD
108 struct type_sequence *sequence_type =
109 container_of(type, struct type_sequence, p);
110 struct declaration_sequence *sequence;
111 struct declaration *len_parent;
c054553d 112
e19c3d69
MD
113 sequence = g_new(struct declaration_sequence, 1);
114 type_ref(&sequence_type->p);
115 sequence->p.type = sequence_type;
c054553d
MD
116 sequence->p.ref = 1;
117 sequence->scope = new_declaration_scope(parent_scope);
e19c3d69
MD
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);
c054553d
MD
126 return &sequence->p;
127}
128
129static
e19c3d69 130void _sequence_declaration_free(struct declaration *declaration)
c054553d
MD
131{
132 struct type_sequence *sequence =
e19c3d69
MD
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;
c054553d 137
e19c3d69
MD
138 len_declaration->p.type->declaration_free(len_declaration);
139 elem_declaration->p.type->declaration_free(elem_declaration);
c054553d 140 free_declaration_scope(sequence->scope);
e19c3d69 141 type_unref(sequence->p.type);
c054553d
MD
142 g_free(sequence);
143}
This page took 0.028747 seconds and 4 git commands to generate.