Rename "type" to "declaration"
[babeltrace.git] / types / array.c
1 /*
2 * array.c
3 *
4 * BabelTrace - Array Type Converter
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
21
22 static
23 struct definition *_array_definition_new(struct declaration *declaration,
24 struct definition_scope *parent_scope);
25 static
26 void _array_definition_free(struct definition *definition);
27
28 void array_copy(struct stream_pos *dest, const struct format *fdest,
29 struct stream_pos *src, const struct format *fsrc,
30 struct definition *definition)
31 {
32 struct definition_array *array =
33 container_of(definition, struct definition_array, p);
34 struct declaration_array *array_declaration = array->declaration;
35 uint64_t i;
36
37 fsrc->array_begin(src, array_declaration);
38 fdest->array_begin(dest, array_declaration);
39
40 for (i = 0; i < array_declaration->len; i++) {
41 struct definition *elem = array->current_element.definition;
42 elem->declaration->copy(dest, fdest, src, fsrc, elem);
43 }
44 fsrc->array_end(src, array_declaration);
45 fdest->array_end(dest, array_declaration);
46 }
47
48 static
49 void _array_declaration_free(struct declaration *declaration)
50 {
51 struct declaration_array *array_declaration =
52 container_of(declaration, struct declaration_array, p);
53
54 free_declaration_scope(array_declaration->scope);
55 declaration_unref(array_declaration->elem);
56 g_free(array_declaration);
57 }
58
59 struct declaration_array *
60 array_declaration_new(const char *name, size_t len,
61 struct declaration *elem_declaration,
62 struct declaration_scope *parent_scope)
63 {
64 struct declaration_array *array_declaration;
65 struct declaration *declaration;
66
67 array_declaration = g_new(struct declaration_array, 1);
68 declaration = &array_declaration->p;
69 array_declaration->len = len;
70 declaration_ref(elem_declaration);
71 array_declaration->elem = elem_declaration;
72 array_declaration->scope = new_declaration_scope(parent_scope);
73 declaration->id = CTF_TYPE_ARRAY;
74 declaration->name = g_quark_from_string(name);
75 /* No need to align the array, the first element will align itself */
76 declaration->alignment = 1;
77 declaration->copy = array_copy;
78 declaration->declaration_free = _array_declaration_free;
79 declaration->definition_new = _array_definition_new;
80 declaration->definition_free = _array_definition_free;
81 declaration->ref = 1;
82 return array_declaration;
83 }
84
85 static
86 struct definition *
87 _array_definition_new(struct declaration *declaration,
88 struct definition_scope *parent_scope)
89 {
90 struct declaration_array *array_declaration =
91 container_of(declaration, struct declaration_array, p);
92 struct definition_array *array;
93
94 array = g_new(struct definition_array, 1);
95 declaration_ref(&array_declaration->p);
96 array->p.declaration = declaration;
97 array->declaration = array_declaration;
98 array->p.ref = 1;
99 array->scope = new_definition_scope(parent_scope);
100 array->current_element.definition =
101 array_declaration->elem->definition_new(array_declaration->elem,
102 parent_scope);
103 return &array->p;
104 }
105
106 static
107 void _array_definition_free(struct definition *definition)
108 {
109 struct definition_array *array =
110 container_of(definition, struct definition_array, p);
111 struct definition *elem_definition =
112 array->current_element.definition;
113
114 elem_definition->declaration->definition_free(elem_definition);
115 free_definition_scope(array->scope);
116 declaration_unref(array->p.declaration);
117 g_free(array);
118 }
This page took 0.047364 seconds and 4 git commands to generate.