CTF float/types compile fixes
[babeltrace.git] / types / sequence.c
CommitLineData
2e7d72cf 1/*
ccd7e1c8 2 * sequence.c
2e7d72cf 3 *
ccd7e1c8 4 * BabelTrace - Sequence Type Converter
2e7d72cf 5 *
c054553d 6 * Copyright 2010, 2011 - 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
c054553d
MD
26static
27struct type *_sequence_type_new(struct type_class *type_class,
28 struct declaration_scope *parent_scope);
29static
30void _sequence_type_free(struct type *type);
31
2e7d72cf
MD
32void sequence_copy(struct stream_pos *dest, const struct format *fdest,
33 struct stream_pos *src, const struct format *fsrc,
c054553d 34 struct type *type)
2e7d72cf 35{
c054553d
MD
36 struct type_sequence *sequence =
37 container_of(type, struct type_sequence, p);
38 struct type_class_sequence *sequence_class = sequence->_class;
39 uint64_t i;
2e7d72cf 40
c054553d
MD
41 fsrc->sequence_begin(src, sequence);
42 fdest->sequence_begin(dest, sequence);
2e7d72cf 43
c054553d
MD
44 sequence->len->p._class->copy(dest, fdest, src, fsrc,
45 &sequence->len->p);
2e7d72cf 46
c054553d
MD
47 for (i = 0; i < sequence->len->value._unsigned; i++) {
48 struct type *elem_type = sequence->current_element.type;
49 elem_type->p._class->copy(dest, fdest, src, fsrc, elem_type);
2e7d72cf 50 }
c054553d
MD
51 fsrc->sequence_end(src, sequence);
52 fdest->sequence_end(dest, sequence);
2e7d72cf
MD
53}
54
c054553d
MD
55static
56void _sequence_type_class_free(struct type_class *type_class)
2e7d72cf 57{
4c8bfb7e 58 struct type_class_sequence *sequence_class =
2e7d72cf 59 container_of(type_class, struct type_class_sequence, p);
c054553d
MD
60
61 type_class_unref(&sequence_class->len_class->p);
62 type_class_unref(sequence_class->elem);
63 g_free(sequence_class);
2e7d72cf
MD
64}
65
66struct type_class_sequence *
c054553d
MD
67sequence_type_class_new(const char *name, struct type_class_integer *len_class,
68 struct type_class *elem)
2e7d72cf
MD
69{
70 struct type_class_sequence *sequence_class;
4c8bfb7e 71 struct type_class *type_class;
2e7d72cf
MD
72 int ret;
73
74 sequence_class = g_new(struct type_class_sequence, 1);
4c8bfb7e 75 type_class = &sequence_class->p;
2e7d72cf 76 assert(!len_class->signedness);
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 84 type_class->copy = sequence_copy;
c054553d
MD
85 type_class->class_free = _sequence_type_class_free;
86 type_class->type_new = _sequence_type_new;
87 type_class->type_free = _sequence_type_free;
4c8bfb7e 88 type_class->ref = 1;
2e7d72cf
MD
89
90 if (type_class->name) {
be85c1c7 91 ret = register_type(type_class);
2e7d72cf
MD
92 if (ret)
93 goto error_register;
94 }
95 return sequence_class;
96
97error_register:
c054553d
MD
98 type_class_unref(&len_class->p);
99 type_class_unref(elem);
2e7d72cf
MD
100 g_free(sequence_class);
101 return NULL;
102}
c054553d
MD
103
104static
105struct type *_sequence_type_new(struct type_class *type_class,
106 struct declaration_scope *parent_scope)
107{
108 struct type_class_sequence *sequence_class =
109 container_of(type_class, struct type_class_sequence, p);
110 struct type_sequence *sequence;
111
112 sequence = g_new(struct type_sequence, 1);
113 type_class_ref(&sequence_class->p);
114 sequence->p._class = sequence_class;
115 sequence->p.ref = 1;
116 sequence->scope = new_declaration_scope(parent_scope);
117 sequence->len.type =
118 sequence_class->len_class.p->type_new(&sequence_class->len_class.p,
119 parent_scope);
120 sequence->current_element.type =
121 sequence_class->elem.p->type_new(&sequence_class->elem.p,
122 parent_scope);
123 return &sequence->p;
124}
125
126static
127void _sequence_type_free(struct type *type)
128{
129 struct type_sequence *sequence =
130 container_of(type, struct type_sequence, p);
131 struct type *len_type = sequence->len.type;
132 struct type *elem_type = sequence->current_element.type;
133
134 len_type->p._class->type_free(len_type);
135 elem_type->p._class->type_free(elem_type);
136 free_declaration_scope(sequence->scope);
137 type_class_unref(sequence->p._class);
138 g_free(sequence);
139}
This page took 0.028472 seconds and 4 git commands to generate.