Relicense BabelTrace library to MIT (BSD-style)
[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>
20#include <babeltrace/types.h>
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;
39 elem_class->copy(dest, fdest, src, fsrc, &elem_class->p);
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{
47 sequence_class->elem->free(&sequence_class->elem->p);
48 g_free(sequence_class);
49}
50
51static void _sequence_type_free(struct type_class *type_class)
52{
53 struct type_class_struct *sequence_class =
54 container_of(type_class, struct type_class_sequence, p);
55 sequence_type_free(sequence_class);
56}
57
58struct type_class_sequence *
59sequence_type_new(const char *name, struct type_class_integer *len_class,
60 struct type_class *elem)
61{
62 struct type_class_sequence *sequence_class;
63 int ret;
64
65 sequence_class = g_new(struct type_class_sequence, 1);
66 type_class = &float_class->p;
67
68 assert(!len_class->signedness);
69
70 sequence_class->len = len;
71 type_class->name = g_quark_from_string(name);
72 type_class->alignment = max(len_class->p.alignment,
73 elem->p.alignment);
74 type_class->copy = sequence_copy;
75 type_class->free = _sequence_type_free;
76
77 if (type_class->name) {
78 ret = ctf_register_type(type_class);
79 if (ret)
80 goto error_register;
81 }
82 return sequence_class;
83
84error_register:
85 len_class->p.free(&len_class->p);
86 sequence_class->elem->free(&sequence_class->elem->p);
87 g_free(sequence_class);
88 return NULL;
89}
This page took 0.037469 seconds and 4 git commands to generate.