Build fix
[babeltrace.git] / types / sequence.c
1 /*
2 * sequence.c
3 *
4 * BabelTrace - Sequence Type Converter
5 *
6 * Copyright 2010 - 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 void sequence_copy(struct stream_pos *dest, const struct format *fdest,
23 struct stream_pos *src, const struct format *fsrc,
24 const struct type_class *type_class)
25 {
26 struct type_class_sequence *sequence_class =
27 container_of(type_class, struct type_class_sequence, p);
28 unsigned int i;
29 size_t len;
30
31 fsrc->sequence_begin(src, sequence_class);
32 fdest->sequence_begin(dest, sequence_class);
33
34 len = fsrc->uint_read(src, sequence_class->len_class);
35 fdest->uint_write(dest, sequence_class->len_class, len);
36
37 for (i = 0; i < len; i++) {
38 struct type_class *elem_class = sequence_class->elem;
39 elem_class->copy(dest, fdest, src, fsrc, elem_class);
40 }
41 fsrc->sequence_end(src, sequence_class);
42 fdest->sequence_end(dest, sequence_class);
43 }
44
45 void sequence_type_free(struct type_class_sequence *sequence_class)
46 {
47 sequence_class->elem->free(sequence_class->elem);
48 type_unref(&sequence_class->len_class->p);
49 type_unref(sequence_class->elem);
50 g_free(sequence_class);
51 }
52
53 static void _sequence_type_free(struct type_class *type_class)
54 {
55 struct type_class_sequence *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 struct type_class *type_class;
66 int ret;
67
68 sequence_class = g_new(struct type_class_sequence, 1);
69 type_class = &sequence_class->p;
70
71 assert(!len_class->signedness);
72
73 type_ref(&len_class->p);
74 sequence_class->len_class = len_class;
75 type_ref(elem);
76 sequence_class->elem = elem;
77 type_class->name = g_quark_from_string(name);
78 type_class->alignment = max(len_class->p.alignment,
79 elem->alignment);
80 type_class->copy = sequence_copy;
81 type_class->free = _sequence_type_free;
82 type_class->ref = 1;
83
84 if (type_class->name) {
85 ret = ctf_register_type(type_class);
86 if (ret)
87 goto error_register;
88 }
89 return sequence_class;
90
91 error_register:
92 type_unref(&len_class->p);
93 type_unref(elem);
94 g_free(sequence_class);
95 return NULL;
96 }
This page took 0.031286 seconds and 5 git commands to generate.