Scope path: split dynamic scope path
[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,
05c749e5
MD
24 struct definition_scope *parent_scope,
25 GQuark field_name, int index);
c054553d 26static
e1151715 27void _array_definition_free(struct definition *definition);
c054553d 28
d06d03db
MD
29void array_copy(struct stream_pos *dest, const struct format *fdest,
30 struct stream_pos *src, const struct format *fsrc,
e1151715 31 struct definition *definition)
d06d03db 32{
e1151715
MD
33 struct definition_array *array =
34 container_of(definition, struct definition_array, p);
f6625916 35 struct declaration_array *array_declaration = array->declaration;
c054553d 36 uint64_t i;
d06d03db 37
f6625916
MD
38 fsrc->array_begin(src, array_declaration);
39 fdest->array_begin(dest, array_declaration);
d06d03db 40
f6625916 41 for (i = 0; i < array_declaration->len; i++) {
e1151715 42 struct definition *elem = array->current_element.definition;
f6625916 43 elem->declaration->copy(dest, fdest, src, fsrc, elem);
d06d03db 44 }
f6625916
MD
45 fsrc->array_end(src, array_declaration);
46 fdest->array_end(dest, array_declaration);
d06d03db
MD
47}
48
c054553d 49static
f6625916 50void _array_declaration_free(struct declaration *declaration)
d06d03db 51{
f6625916
MD
52 struct declaration_array *array_declaration =
53 container_of(declaration, struct declaration_array, p);
c054553d 54
f6625916
MD
55 free_declaration_scope(array_declaration->scope);
56 declaration_unref(array_declaration->elem);
57 g_free(array_declaration);
d06d03db
MD
58}
59
f6625916
MD
60struct declaration_array *
61 array_declaration_new(const char *name, size_t len,
62 struct declaration *elem_declaration,
63 struct declaration_scope *parent_scope)
d06d03db 64{
f6625916
MD
65 struct declaration_array *array_declaration;
66 struct declaration *declaration;
d06d03db 67
f6625916
MD
68 array_declaration = g_new(struct declaration_array, 1);
69 declaration = &array_declaration->p;
70 array_declaration->len = len;
71 declaration_ref(elem_declaration);
72 array_declaration->elem = elem_declaration;
73 array_declaration->scope = new_declaration_scope(parent_scope);
74 declaration->id = CTF_TYPE_ARRAY;
75 declaration->name = g_quark_from_string(name);
d06d03db 76 /* No need to align the array, the first element will align itself */
f6625916
MD
77 declaration->alignment = 1;
78 declaration->copy = array_copy;
79 declaration->declaration_free = _array_declaration_free;
80 declaration->definition_new = _array_definition_new;
81 declaration->definition_free = _array_definition_free;
82 declaration->ref = 1;
83 return array_declaration;
d06d03db 84}
c054553d
MD
85
86static
e1151715 87struct definition *
f6625916 88 _array_definition_new(struct declaration *declaration,
05c749e5
MD
89 struct definition_scope *parent_scope,
90 GQuark field_name, int index)
c054553d 91{
f6625916
MD
92 struct declaration_array *array_declaration =
93 container_of(declaration, struct declaration_array, p);
e1151715 94 struct definition_array *array;
c054553d 95
e1151715 96 array = g_new(struct definition_array, 1);
f6625916
MD
97 declaration_ref(&array_declaration->p);
98 array->p.declaration = declaration;
99 array->declaration = array_declaration;
c054553d 100 array->p.ref = 1;
05c749e5
MD
101 array->p.index = index;
102 array->scope = new_definition_scope(parent_scope, field_name);
e1151715 103 array->current_element.definition =
f6625916 104 array_declaration->elem->definition_new(array_declaration->elem,
05c749e5
MD
105 parent_scope,
106 g_quark_from_static_string("[]"),
107 0);
c054553d
MD
108 return &array->p;
109}
110
111static
e1151715 112void _array_definition_free(struct definition *definition)
c054553d 113{
e1151715
MD
114 struct definition_array *array =
115 container_of(definition, struct definition_array, p);
116 struct definition *elem_definition =
117 array->current_element.definition;
c054553d 118
f6625916 119 elem_definition->declaration->definition_free(elem_definition);
e1151715 120 free_definition_scope(array->scope);
f6625916 121 declaration_unref(array->p.declaration);
c054553d
MD
122 g_free(array);
123}
This page took 0.041312 seconds and 4 git commands to generate.