Rename "declaration" to "definition"
[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
e1151715
MD
27struct definition *_sequence_definition_new(struct type *type,
28 struct definition_scope *parent_scope);
c054553d 29static
e1151715 30void _sequence_definition_free(struct definition *definition);
c054553d 31
2e7d72cf
MD
32void sequence_copy(struct stream_pos *dest, const struct format *fdest,
33 struct stream_pos *src, const struct format *fsrc,
e1151715 34 struct definition *definition)
2e7d72cf 35{
e1151715
MD
36 struct definition_sequence *sequence =
37 container_of(definition, struct definition_sequence, p);
e19c3d69 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++) {
e1151715
MD
48 struct definition *elem =
49 sequence->current_element.definition;
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
64893f33 62 free_type_scope(sequence_type->scope);
e19c3d69
MD
63 type_unref(&sequence_type->len_type->p);
64 type_unref(sequence_type->elem);
65 g_free(sequence_type);
2e7d72cf
MD
66}
67
e19c3d69
MD
68struct type_sequence *
69 sequence_type_new(const char *name, struct type_integer *len_type,
64893f33
MD
70 struct type *elem_type,
71 struct type_scope *parent_scope)
2e7d72cf 72{
e19c3d69
MD
73 struct type_sequence *sequence_type;
74 struct type *type;
2e7d72cf 75
e19c3d69
MD
76 sequence_type = g_new(struct type_sequence, 1);
77 type = &sequence_type->p;
78 assert(!len_type->signedness);
79 type_ref(&len_type->p);
80 sequence_type->len_type = len_type;
81 type_ref(elem_type);
82 sequence_type->elem = elem_type;
64893f33 83 sequence_type->scope = new_type_scope(parent_scope);
05628561 84 type->id = CTF_TYPE_SEQUENCE;
e19c3d69
MD
85 type->name = g_quark_from_string(name);
86 type->alignment = max(len_type->p.alignment, elem_type->alignment);
87 type->copy = sequence_copy;
88 type->type_free = _sequence_type_free;
e1151715
MD
89 type->definition_new = _sequence_definition_new;
90 type->definition_free = _sequence_definition_free;
e19c3d69 91 type->ref = 1;
e19c3d69 92 return sequence_type;
2e7d72cf 93}
c054553d
MD
94
95static
e1151715
MD
96struct definition *_sequence_definition_new(struct type *type,
97 struct definition_scope *parent_scope)
c054553d 98{
e19c3d69
MD
99 struct type_sequence *sequence_type =
100 container_of(type, struct type_sequence, p);
e1151715
MD
101 struct definition_sequence *sequence;
102 struct definition *len_parent;
c054553d 103
e1151715 104 sequence = g_new(struct definition_sequence, 1);
e19c3d69 105 type_ref(&sequence_type->p);
6ee5115e
MD
106 sequence->p.type = type;
107 sequence->type = sequence_type;
c054553d 108 sequence->p.ref = 1;
e1151715
MD
109 sequence->scope = new_definition_scope(parent_scope);
110 len_parent = sequence_type->len_type->p.definition_new(&sequence_type->len_type->p,
6ee5115e 111 parent_scope);
e19c3d69 112 sequence->len =
e1151715
MD
113 container_of(len_parent, struct definition_integer, p);
114 sequence->current_element.definition =
115 sequence_type->elem->definition_new(sequence_type->elem,
6ee5115e 116 parent_scope);
c054553d
MD
117 return &sequence->p;
118}
119
120static
e1151715 121void _sequence_definition_free(struct definition *definition)
c054553d 122{
e1151715
MD
123 struct definition_sequence *sequence =
124 container_of(definition, struct definition_sequence, p);
125 struct definition *len_definition = &sequence->len->p;
126 struct definition *elem_definition =
127 sequence->current_element.definition;
c054553d 128
e1151715
MD
129 len_definition->type->definition_free(len_definition);
130 elem_definition->type->definition_free(elem_definition);
131 free_definition_scope(sequence->scope);
e19c3d69 132 type_unref(sequence->p.type);
c054553d
MD
133 g_free(sequence);
134}
This page took 0.028486 seconds and 4 git commands to generate.