Unref: handle null pointers
[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
f6625916 27struct definition *_sequence_definition_new(struct declaration *declaration,
05c749e5
MD
28 struct definition_scope *parent_scope,
29 GQuark field_name, int index);
c054553d 30static
e1151715 31void _sequence_definition_free(struct definition *definition);
c054553d 32
2e7d72cf
MD
33void sequence_copy(struct stream_pos *dest, const struct format *fdest,
34 struct stream_pos *src, const struct format *fsrc,
e1151715 35 struct definition *definition)
2e7d72cf 36{
e1151715
MD
37 struct definition_sequence *sequence =
38 container_of(definition, struct definition_sequence, p);
f6625916 39 struct declaration_sequence *sequence_declaration = sequence->declaration;
c054553d 40 uint64_t i;
2e7d72cf 41
f6625916
MD
42 fsrc->sequence_begin(src, sequence_declaration);
43 fdest->sequence_begin(dest, sequence_declaration);
2e7d72cf 44
f6625916 45 sequence->len->p.declaration->copy(dest, fdest, src, fsrc,
e19c3d69 46 &sequence->len->p);
2e7d72cf 47
c054553d 48 for (i = 0; i < sequence->len->value._unsigned; i++) {
e1151715
MD
49 struct definition *elem =
50 sequence->current_element.definition;
f6625916 51 elem->declaration->copy(dest, fdest, src, fsrc, elem);
2e7d72cf 52 }
f6625916
MD
53 fsrc->sequence_end(src, sequence_declaration);
54 fdest->sequence_end(dest, sequence_declaration);
2e7d72cf
MD
55}
56
c054553d 57static
f6625916 58void _sequence_declaration_free(struct declaration *declaration)
2e7d72cf 59{
f6625916
MD
60 struct declaration_sequence *sequence_declaration =
61 container_of(declaration, struct declaration_sequence, p);
c054553d 62
f6625916
MD
63 free_declaration_scope(sequence_declaration->scope);
64 declaration_unref(&sequence_declaration->len_declaration->p);
65 declaration_unref(sequence_declaration->elem);
66 g_free(sequence_declaration);
2e7d72cf
MD
67}
68
f6625916 69struct declaration_sequence *
ab4cf058 70 sequence_declaration_new(struct declaration_integer *len_declaration,
f6625916
MD
71 struct declaration *elem_declaration,
72 struct declaration_scope *parent_scope)
2e7d72cf 73{
f6625916
MD
74 struct declaration_sequence *sequence_declaration;
75 struct declaration *declaration;
2e7d72cf 76
f6625916
MD
77 sequence_declaration = g_new(struct declaration_sequence, 1);
78 declaration = &sequence_declaration->p;
79 assert(!len_declaration->signedness);
80 declaration_ref(&len_declaration->p);
81 sequence_declaration->len_declaration = len_declaration;
82 declaration_ref(elem_declaration);
83 sequence_declaration->elem = elem_declaration;
84 sequence_declaration->scope = new_declaration_scope(parent_scope);
85 declaration->id = CTF_TYPE_SEQUENCE;
f6625916
MD
86 declaration->alignment = max(len_declaration->p.alignment, elem_declaration->alignment);
87 declaration->copy = sequence_copy;
88 declaration->declaration_free = _sequence_declaration_free;
89 declaration->definition_new = _sequence_definition_new;
90 declaration->definition_free = _sequence_definition_free;
91 declaration->ref = 1;
92 return sequence_declaration;
2e7d72cf 93}
c054553d
MD
94
95static
f6625916 96struct definition *_sequence_definition_new(struct declaration *declaration,
05c749e5
MD
97 struct definition_scope *parent_scope,
98 GQuark field_name, int index)
c054553d 99{
f6625916
MD
100 struct declaration_sequence *sequence_declaration =
101 container_of(declaration, struct declaration_sequence, p);
e1151715
MD
102 struct definition_sequence *sequence;
103 struct definition *len_parent;
c054553d 104
e1151715 105 sequence = g_new(struct definition_sequence, 1);
f6625916
MD
106 declaration_ref(&sequence_declaration->p);
107 sequence->p.declaration = declaration;
108 sequence->declaration = sequence_declaration;
c054553d 109 sequence->p.ref = 1;
05c749e5
MD
110 sequence->p.index = index;
111 sequence->scope = new_definition_scope(parent_scope, field_name);
f6625916 112 len_parent = sequence_declaration->len_declaration->p.definition_new(&sequence_declaration->len_declaration->p,
05c749e5
MD
113 parent_scope,
114 g_quark_from_static_string("length"), 0);
e19c3d69 115 sequence->len =
e1151715
MD
116 container_of(len_parent, struct definition_integer, p);
117 sequence->current_element.definition =
f6625916 118 sequence_declaration->elem->definition_new(sequence_declaration->elem,
05c749e5
MD
119 parent_scope,
120 g_quark_from_static_string("[]"), 1);
c054553d
MD
121 return &sequence->p;
122}
123
124static
e1151715 125void _sequence_definition_free(struct definition *definition)
c054553d 126{
e1151715
MD
127 struct definition_sequence *sequence =
128 container_of(definition, struct definition_sequence, p);
129 struct definition *len_definition = &sequence->len->p;
130 struct definition *elem_definition =
131 sequence->current_element.definition;
c054553d 132
f6625916
MD
133 len_definition->declaration->definition_free(len_definition);
134 elem_definition->declaration->definition_free(elem_definition);
e1151715 135 free_definition_scope(sequence->scope);
f6625916 136 declaration_unref(sequence->p.declaration);
c054553d
MD
137 g_free(sequence);
138}
This page took 0.02882 seconds and 4 git commands to generate.