cleanup
[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 26static
e19c3d69
MD
27struct declaration *_sequence_declaration_new(struct type *type,
28 struct declaration_scope *parent_scope);
c054553d 29static
e19c3d69 30void _sequence_declaration_free(struct declaration *declaration);
c054553d 31
2e7d72cf
MD
32void sequence_copy(struct stream_pos *dest, const struct format *fdest,
33 struct stream_pos *src, const struct format *fsrc,
e19c3d69 34 struct declaration *declaration)
2e7d72cf 35{
e19c3d69
MD
36 struct declaration_sequence *sequence =
37 container_of(declaration, struct declaration_sequence, p);
38 struct type_sequence *sequence_type = sequence->type;
c054553d 39 uint64_t i;
2e7d72cf 40
e19c3d69
MD
41 fsrc->sequence_begin(src, sequence_type);
42 fdest->sequence_begin(dest, sequence_type);
2e7d72cf 43
e19c3d69
MD
44 sequence->len->p.type->copy(dest, fdest, src, fsrc,
45 &sequence->len->p);
2e7d72cf 46
c054553d 47 for (i = 0; i < sequence->len->value._unsigned; i++) {
6ee5115e 48 struct declaration *elem =
e19c3d69 49 sequence->current_element.declaration;
6ee5115e 50 elem->type->copy(dest, fdest, src, fsrc, elem);
2e7d72cf 51 }
e19c3d69
MD
52 fsrc->sequence_end(src, sequence_type);
53 fdest->sequence_end(dest, sequence_type);
2e7d72cf
MD
54}
55
c054553d 56static
e19c3d69 57void _sequence_type_free(struct type *type)
2e7d72cf 58{
e19c3d69
MD
59 struct type_sequence *sequence_type =
60 container_of(type, struct type_sequence, p);
c054553d 61
e19c3d69
MD
62 type_unref(&sequence_type->len_type->p);
63 type_unref(sequence_type->elem);
64 g_free(sequence_type);
2e7d72cf
MD
65}
66
e19c3d69
MD
67struct type_sequence *
68 sequence_type_new(const char *name, struct type_integer *len_type,
69 struct type *elem_type)
2e7d72cf 70{
e19c3d69
MD
71 struct type_sequence *sequence_type;
72 struct type *type;
2e7d72cf 73
e19c3d69
MD
74 sequence_type = g_new(struct type_sequence, 1);
75 type = &sequence_type->p;
76 assert(!len_type->signedness);
77 type_ref(&len_type->p);
78 sequence_type->len_type = len_type;
79 type_ref(elem_type);
80 sequence_type->elem = elem_type;
81 type->name = g_quark_from_string(name);
82 type->alignment = max(len_type->p.alignment, elem_type->alignment);
83 type->copy = sequence_copy;
84 type->type_free = _sequence_type_free;
85 type->declaration_new = _sequence_declaration_new;
86 type->declaration_free = _sequence_declaration_free;
87 type->ref = 1;
e19c3d69 88 return sequence_type;
2e7d72cf 89}
c054553d
MD
90
91static
e19c3d69 92struct declaration *_sequence_declaration_new(struct type *type,
c054553d
MD
93 struct declaration_scope *parent_scope)
94{
e19c3d69
MD
95 struct type_sequence *sequence_type =
96 container_of(type, struct type_sequence, p);
97 struct declaration_sequence *sequence;
98 struct declaration *len_parent;
c054553d 99
e19c3d69
MD
100 sequence = g_new(struct declaration_sequence, 1);
101 type_ref(&sequence_type->p);
6ee5115e
MD
102 sequence->p.type = type;
103 sequence->type = sequence_type;
c054553d
MD
104 sequence->p.ref = 1;
105 sequence->scope = new_declaration_scope(parent_scope);
6ee5115e
MD
106 len_parent = sequence_type->len_type->p.declaration_new(&sequence_type->len_type->p,
107 parent_scope);
e19c3d69
MD
108 sequence->len =
109 container_of(len_parent, struct declaration_integer, p);
110 sequence->current_element.declaration =
6ee5115e
MD
111 sequence_type->elem->declaration_new(sequence_type->elem,
112 parent_scope);
c054553d
MD
113 return &sequence->p;
114}
115
116static
e19c3d69 117void _sequence_declaration_free(struct declaration *declaration)
c054553d 118{
6ee5115e
MD
119 struct declaration_sequence *sequence =
120 container_of(declaration, struct declaration_sequence, p);
121 struct declaration *len_declaration = &sequence->len->p;
e19c3d69
MD
122 struct declaration *elem_declaration =
123 sequence->current_element.declaration;
c054553d 124
6ee5115e
MD
125 len_declaration->type->declaration_free(len_declaration);
126 elem_declaration->type->declaration_free(elem_declaration);
c054553d 127 free_declaration_scope(sequence->scope);
e19c3d69 128 type_unref(sequence->p.type);
c054553d
MD
129 g_free(sequence);
130}
This page took 0.028183 seconds and 4 git commands to generate.