6dd3b9ca8a6c869e419ef17c54ff9141ebf8c31f
[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 declaration *_array_declaration_new(struct type *type,
24 struct declaration_scope *parent_scope);
25 static
26 void _array_declaration_free(struct declaration *declaration);
27
28 void array_copy(struct stream_pos *dest, const struct format *fdest,
29 struct stream_pos *src, const struct format *fsrc,
30 struct declaration *declaration)
31 {
32 struct declaration_array *array =
33 container_of(declaration, struct declaration_array, p);
34 struct type_array *array_type = array->type;
35 uint64_t i;
36
37 fsrc->array_begin(src, array_type);
38 fdest->array_begin(dest, array_type);
39
40 for (i = 0; i < array_type->len; i++) {
41 struct type *elem_type = array->current_element.type;
42 elem_type->p.type->copy(dest, fdest, src, fsrc, elem_type);
43 }
44 fsrc->array_end(src, array_type);
45 fdest->array_end(dest, array_type);
46 }
47
48 static
49 void _array_type_free(struct type *type)
50 {
51 struct type_array *array_type =
52 container_of(type, struct type_array, p);
53
54 type_unref(array_type->elem);
55 g_free(array_type);
56 }
57
58 struct type_array *
59 array_type_new(const char *name, size_t len, struct type *elem_type)
60 {
61 struct type_array *array_type;
62 struct type *type;
63 int ret;
64
65 array_type = g_new(struct type_array, 1);
66 type = &array_type->p;
67 array_type->len = len;
68 type_ref(elem_type);
69 array_type->elem = elem_type;
70 type->name = g_quark_from_string(name);
71 /* No need to align the array, the first element will align itself */
72 type->alignment = 1;
73 type->copy = array_copy;
74 type->type_free = _array_type_free;
75 type->declaration_new = _array_declaration_new;
76 type->declaration_free = _array_declaration_free;
77 type->ref = 1;
78
79 if (type->name) {
80 ret = register_type(type);
81 if (ret)
82 goto error_register;
83 }
84 return array_type;
85
86 error_register:
87 type_unref(array_type->elem);
88 g_free(array_type);
89 return NULL;
90 }
91
92 static
93 struct declaration *
94 _array_declaration_new(struct type *type,
95 struct declaration_scope *parent_scope)
96 {
97 struct type_array *array_type =
98 container_of(type, struct type_array, p);
99 struct declaration_array *array;
100
101 array = g_new(struct declaration_array, 1);
102 type_ref(&array_type->p);
103 array->p.type = array_type;
104 array->p.ref = 1;
105 array->scope = new_declaration_scope(parent_scope);
106 array->current_element.declaration =
107 array_type->elem.p->declaration_new(&array_type->elem.p,
108 parent_scope);
109 return &array->p;
110 }
111
112 static
113 void _array_declaration_free(struct declaration *declaration)
114 {
115 struct type_array *array =
116 container_of(declaration, struct type_array, p);
117 struct declaration *elem_declaration =
118 array->current_element.declaration;
119
120 elem_type->p.type->declaration_free(elem_type);
121 free_declaration_scope(array->scope);
122 type_unref(array->p.type);
123 g_free(array);
124 }
This page took 0.030357 seconds and 3 git commands to generate.