Babeltrace type: build warning cleanup
[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 21
be85c1c7
MD
22#ifndef max
23#define max(a, b) ((a) < (b) ? (b) : (a))
24#endif
25
2e7d72cf
MD
26void sequence_copy(struct stream_pos *dest, const struct format *fdest,
27 struct stream_pos *src, const struct format *fsrc,
28 const struct type_class *type_class)
29{
30 struct type_class_sequence *sequence_class =
31 container_of(type_class, struct type_class_sequence, p);
32 unsigned int i;
33 size_t len;
34
35 fsrc->sequence_begin(src, sequence_class);
36 fdest->sequence_begin(dest, sequence_class);
37
38 len = fsrc->uint_read(src, sequence_class->len_class);
39 fdest->uint_write(dest, sequence_class->len_class, len);
40
41 for (i = 0; i < len; i++) {
42 struct type_class *elem_class = sequence_class->elem;
4c8bfb7e 43 elem_class->copy(dest, fdest, src, fsrc, elem_class);
2e7d72cf
MD
44 }
45 fsrc->sequence_end(src, sequence_class);
46 fdest->sequence_end(dest, sequence_class);
47}
48
49void sequence_type_free(struct type_class_sequence *sequence_class)
50{
4c8bfb7e
MD
51 sequence_class->elem->free(sequence_class->elem);
52 type_unref(&sequence_class->len_class->p);
53 type_unref(sequence_class->elem);
2e7d72cf
MD
54 g_free(sequence_class);
55}
56
57static void _sequence_type_free(struct type_class *type_class)
58{
4c8bfb7e 59 struct type_class_sequence *sequence_class =
2e7d72cf
MD
60 container_of(type_class, struct type_class_sequence, p);
61 sequence_type_free(sequence_class);
62}
63
64struct type_class_sequence *
65sequence_type_new(const char *name, struct type_class_integer *len_class,
66 struct type_class *elem)
67{
68 struct type_class_sequence *sequence_class;
4c8bfb7e 69 struct type_class *type_class;
2e7d72cf
MD
70 int ret;
71
72 sequence_class = g_new(struct type_class_sequence, 1);
4c8bfb7e 73 type_class = &sequence_class->p;
2e7d72cf
MD
74
75 assert(!len_class->signedness);
76
4c8bfb7e
MD
77 type_ref(&len_class->p);
78 sequence_class->len_class = len_class;
79 type_ref(elem);
80 sequence_class->elem = elem;
2e7d72cf
MD
81 type_class->name = g_quark_from_string(name);
82 type_class->alignment = max(len_class->p.alignment,
4c8bfb7e 83 elem->alignment);
2e7d72cf
MD
84 type_class->copy = sequence_copy;
85 type_class->free = _sequence_type_free;
4c8bfb7e 86 type_class->ref = 1;
2e7d72cf
MD
87
88 if (type_class->name) {
be85c1c7 89 ret = register_type(type_class);
2e7d72cf
MD
90 if (ret)
91 goto error_register;
92 }
93 return sequence_class;
94
95error_register:
4c8bfb7e
MD
96 type_unref(&len_class->p);
97 type_unref(elem);
2e7d72cf
MD
98 g_free(sequence_class);
99 return NULL;
100}
This page took 0.039131 seconds and 4 git commands to generate.