Add sequence
[babeltrace.git] / types / sequence.c
1 /*
2 * BabelTrace - Sequence Type Converter
3 *
4 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/types.h>
23
24 void sequence_copy(struct stream_pos *dest, const struct format *fdest,
25 struct stream_pos *src, const struct format *fsrc,
26 const struct type_class *type_class)
27 {
28 struct type_class_sequence *sequence_class =
29 container_of(type_class, struct type_class_sequence, p);
30 unsigned int i;
31 size_t len;
32
33 fsrc->sequence_begin(src, sequence_class);
34 fdest->sequence_begin(dest, sequence_class);
35
36 len = fsrc->uint_read(src, sequence_class->len_class);
37 fdest->uint_write(dest, sequence_class->len_class, len);
38
39 for (i = 0; i < len; i++) {
40 struct type_class *elem_class = sequence_class->elem;
41 elem_class->copy(dest, fdest, src, fsrc, &elem_class->p);
42 }
43 fsrc->sequence_end(src, sequence_class);
44 fdest->sequence_end(dest, sequence_class);
45 }
46
47 void sequence_type_free(struct type_class_sequence *sequence_class)
48 {
49 sequence_class->elem->free(&sequence_class->elem->p);
50 g_free(sequence_class);
51 }
52
53 static void _sequence_type_free(struct type_class *type_class)
54 {
55 struct type_class_struct *sequence_class =
56 container_of(type_class, struct type_class_sequence, p);
57 sequence_type_free(sequence_class);
58 }
59
60 struct type_class_sequence *
61 sequence_type_new(const char *name, struct type_class_integer *len_class,
62 struct type_class *elem)
63 {
64 struct type_class_sequence *sequence_class;
65 int ret;
66
67 sequence_class = g_new(struct type_class_sequence, 1);
68 type_class = &float_class->p;
69
70 assert(!len_class->signedness);
71
72 sequence_class->len = len;
73 type_class->name = g_quark_from_string(name);
74 type_class->alignment = max(len_class->p.alignment,
75 elem->p.alignment);
76 type_class->copy = sequence_copy;
77 type_class->free = _sequence_type_free;
78
79 if (type_class->name) {
80 ret = ctf_register_type(type_class);
81 if (ret)
82 goto error_register;
83 }
84 return sequence_class;
85
86 error_register:
87 len_class->p.free(&len_class->p);
88 sequence_class->elem->free(&sequence_class->elem->p);
89 g_free(sequence_class);
90 return NULL;
91 }
This page took 0.031196 seconds and 5 git commands to generate.