Add missing _Imaginary type
[babeltrace.git] / types / array.c
CommitLineData
d06d03db 1/*
ccd7e1c8 2 * array.c
d06d03db 3 *
ccd7e1c8 4 * BabelTrace - Array Type Converter
d06d03db 5 *
c054553d 6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d06d03db 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:
d06d03db 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.
d06d03db
MD
17 */
18
19#include <babeltrace/compiler.h>
4c8bfb7e 20#include <babeltrace/format.h>
d06d03db 21
c054553d 22static
f6625916 23struct definition *_array_definition_new(struct declaration *declaration,
e1151715 24 struct definition_scope *parent_scope);
c054553d 25static
e1151715 26void _array_definition_free(struct definition *definition);
c054553d 27
d06d03db
MD
28void array_copy(struct stream_pos *dest, const struct format *fdest,
29 struct stream_pos *src, const struct format *fsrc,
e1151715 30 struct definition *definition)
d06d03db 31{
e1151715
MD
32 struct definition_array *array =
33 container_of(definition, struct definition_array, p);
f6625916 34 struct declaration_array *array_declaration = array->declaration;
c054553d 35 uint64_t i;
d06d03db 36
f6625916
MD
37 fsrc->array_begin(src, array_declaration);
38 fdest->array_begin(dest, array_declaration);
d06d03db 39
f6625916 40 for (i = 0; i < array_declaration->len; i++) {
e1151715 41 struct definition *elem = array->current_element.definition;
f6625916 42 elem->declaration->copy(dest, fdest, src, fsrc, elem);
d06d03db 43 }
f6625916
MD
44 fsrc->array_end(src, array_declaration);
45 fdest->array_end(dest, array_declaration);
d06d03db
MD
46}
47
c054553d 48static
f6625916 49void _array_declaration_free(struct declaration *declaration)
d06d03db 50{
f6625916
MD
51 struct declaration_array *array_declaration =
52 container_of(declaration, struct declaration_array, p);
c054553d 53
f6625916
MD
54 free_declaration_scope(array_declaration->scope);
55 declaration_unref(array_declaration->elem);
56 g_free(array_declaration);
d06d03db
MD
57}
58
f6625916
MD
59struct declaration_array *
60 array_declaration_new(const char *name, size_t len,
61 struct declaration *elem_declaration,
62 struct declaration_scope *parent_scope)
d06d03db 63{
f6625916
MD
64 struct declaration_array *array_declaration;
65 struct declaration *declaration;
d06d03db 66
f6625916
MD
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);
d06d03db 75 /* No need to align the array, the first element will align itself */
f6625916
MD
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;
d06d03db 83}
c054553d
MD
84
85static
e1151715 86struct definition *
f6625916 87 _array_definition_new(struct declaration *declaration,
e1151715 88 struct definition_scope *parent_scope)
c054553d 89{
f6625916
MD
90 struct declaration_array *array_declaration =
91 container_of(declaration, struct declaration_array, p);
e1151715 92 struct definition_array *array;
c054553d 93
e1151715 94 array = g_new(struct definition_array, 1);
f6625916
MD
95 declaration_ref(&array_declaration->p);
96 array->p.declaration = declaration;
97 array->declaration = array_declaration;
c054553d 98 array->p.ref = 1;
e1151715
MD
99 array->scope = new_definition_scope(parent_scope);
100 array->current_element.definition =
f6625916 101 array_declaration->elem->definition_new(array_declaration->elem,
6ee5115e 102 parent_scope);
c054553d
MD
103 return &array->p;
104}
105
106static
e1151715 107void _array_definition_free(struct definition *definition)
c054553d 108{
e1151715
MD
109 struct definition_array *array =
110 container_of(definition, struct definition_array, p);
111 struct definition *elem_definition =
112 array->current_element.definition;
c054553d 113
f6625916 114 elem_definition->declaration->definition_free(elem_definition);
e1151715 115 free_definition_scope(array->scope);
f6625916 116 declaration_unref(array->p.declaration);
c054553d
MD
117 g_free(array);
118}
This page took 0.028061 seconds and 4 git commands to generate.