Build fix
[babeltrace.git] / types / sequence.c
CommitLineData
2e7d72cf 1/*
ccd7e1c8 2 * sequence.c
2e7d72cf 3 *
ccd7e1c8 4 * BabelTrace - Sequence Type Converter
2e7d72cf 5 *
ccd7e1c8 6 * Copyright 2010 - 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
MD
21
22void 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;
4c8bfb7e 39 elem_class->copy(dest, fdest, src, fsrc, elem_class);
2e7d72cf
MD
40 }
41 fsrc->sequence_end(src, sequence_class);
42 fdest->sequence_end(dest, sequence_class);
43}
44
45void sequence_type_free(struct type_class_sequence *sequence_class)
46{
4c8bfb7e
MD
47 sequence_class->elem->free(sequence_class->elem);
48 type_unref(&sequence_class->len_class->p);
49 type_unref(sequence_class->elem);
2e7d72cf
MD
50 g_free(sequence_class);
51}
52
53static void _sequence_type_free(struct type_class *type_class)
54{
4c8bfb7e 55 struct type_class_sequence *sequence_class =
2e7d72cf
MD
56 container_of(type_class, struct type_class_sequence, p);
57 sequence_type_free(sequence_class);
58}
59
60struct type_class_sequence *
61sequence_type_new(const char *name, struct type_class_integer *len_class,
62 struct type_class *elem)
63{
64 struct type_class_sequence *sequence_class;
4c8bfb7e 65 struct type_class *type_class;
2e7d72cf
MD
66 int ret;
67
68 sequence_class = g_new(struct type_class_sequence, 1);
4c8bfb7e 69 type_class = &sequence_class->p;
2e7d72cf
MD
70
71 assert(!len_class->signedness);
72
4c8bfb7e
MD
73 type_ref(&len_class->p);
74 sequence_class->len_class = len_class;
75 type_ref(elem);
76 sequence_class->elem = elem;
2e7d72cf
MD
77 type_class->name = g_quark_from_string(name);
78 type_class->alignment = max(len_class->p.alignment,
4c8bfb7e 79 elem->alignment);
2e7d72cf
MD
80 type_class->copy = sequence_copy;
81 type_class->free = _sequence_type_free;
4c8bfb7e 82 type_class->ref = 1;
2e7d72cf
MD
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
91error_register:
4c8bfb7e
MD
92 type_unref(&len_class->p);
93 type_unref(elem);
2e7d72cf
MD
94 g_free(sequence_class);
95 return NULL;
96}
This page took 0.025778 seconds and 4 git commands to generate.