Start packet mmap work
[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>
0f980a35 21#include <inttypes.h>
d06d03db 22
c054553d 23static
f6625916 24struct definition *_array_definition_new(struct declaration *declaration,
05c749e5
MD
25 struct definition_scope *parent_scope,
26 GQuark field_name, int index);
c054553d 27static
e1151715 28void _array_definition_free(struct definition *definition);
c054553d 29
d06d03db
MD
30void array_copy(struct stream_pos *dest, const struct format *fdest,
31 struct stream_pos *src, const struct format *fsrc,
e1151715 32 struct definition *definition)
d06d03db 33{
e1151715
MD
34 struct definition_array *array =
35 container_of(definition, struct definition_array, p);
f6625916 36 struct declaration_array *array_declaration = array->declaration;
c054553d 37 uint64_t i;
d06d03db 38
f6625916 39 fsrc->array_begin(src, array_declaration);
0f980a35
MD
40 if (fdest)
41 fdest->array_begin(dest, array_declaration);
d06d03db 42
f6625916 43 for (i = 0; i < array_declaration->len; i++) {
0f980a35
MD
44 struct definition *elem =
45 g_array_index(array->elems, struct field, i).definition;
f6625916 46 elem->declaration->copy(dest, fdest, src, fsrc, elem);
d06d03db 47 }
f6625916 48 fsrc->array_end(src, array_declaration);
0f980a35
MD
49 if (fdest)
50 fdest->array_end(dest, array_declaration);
d06d03db
MD
51}
52
c054553d 53static
f6625916 54void _array_declaration_free(struct declaration *declaration)
d06d03db 55{
f6625916
MD
56 struct declaration_array *array_declaration =
57 container_of(declaration, struct declaration_array, p);
c054553d 58
f6625916
MD
59 free_declaration_scope(array_declaration->scope);
60 declaration_unref(array_declaration->elem);
61 g_free(array_declaration);
d06d03db
MD
62}
63
f6625916 64struct declaration_array *
ab4cf058 65 array_declaration_new(size_t len,
f6625916
MD
66 struct declaration *elem_declaration,
67 struct declaration_scope *parent_scope)
d06d03db 68{
f6625916
MD
69 struct declaration_array *array_declaration;
70 struct declaration *declaration;
d06d03db 71
f6625916
MD
72 array_declaration = g_new(struct declaration_array, 1);
73 declaration = &array_declaration->p;
74 array_declaration->len = len;
75 declaration_ref(elem_declaration);
76 array_declaration->elem = elem_declaration;
77 array_declaration->scope = new_declaration_scope(parent_scope);
78 declaration->id = CTF_TYPE_ARRAY;
d06d03db 79 /* No need to align the array, the first element will align itself */
f6625916
MD
80 declaration->alignment = 1;
81 declaration->copy = array_copy;
82 declaration->declaration_free = _array_declaration_free;
83 declaration->definition_new = _array_definition_new;
84 declaration->definition_free = _array_definition_free;
85 declaration->ref = 1;
86 return array_declaration;
d06d03db 87}
c054553d
MD
88
89static
e1151715 90struct definition *
f6625916 91 _array_definition_new(struct declaration *declaration,
05c749e5
MD
92 struct definition_scope *parent_scope,
93 GQuark field_name, int index)
c054553d 94{
f6625916
MD
95 struct declaration_array *array_declaration =
96 container_of(declaration, struct declaration_array, p);
e1151715 97 struct definition_array *array;
0f980a35 98 uint64_t i;
c054553d 99
e1151715 100 array = g_new(struct definition_array, 1);
f6625916
MD
101 declaration_ref(&array_declaration->p);
102 array->p.declaration = declaration;
103 array->declaration = array_declaration;
c054553d 104 array->p.ref = 1;
05c749e5
MD
105 array->p.index = index;
106 array->scope = new_definition_scope(parent_scope, field_name);
0f980a35
MD
107 array->elems = g_array_sized_new(FALSE, TRUE, sizeof(struct field),
108 array_declaration->len);
109 g_array_set_size(array->elems, array_declaration->len);
110 for (i = 0; i < array_declaration->len; i++) {
111 struct field *field;
112 GString *str;
113 GQuark name;
114
115 str = g_string_new("");
116 g_string_printf(str, "[%" PRIu64 "]", i);
117 name = g_quark_from_string(str->str);
118 (void) g_string_free(str, TRUE);
119
120 field = &g_array_index(array->elems, struct field, i);
121 field->name = name;
122 field->definition = array_declaration->elem->definition_new(array_declaration->elem,
123 array->scope,
124 name, i);
125 }
c054553d
MD
126 return &array->p;
127}
128
129static
e1151715 130void _array_definition_free(struct definition *definition)
c054553d 131{
e1151715
MD
132 struct definition_array *array =
133 container_of(definition, struct definition_array, p);
0f980a35 134 uint64_t i;
c054553d 135
0f980a35
MD
136 for (i = 0; i < array->elems->len; i++) {
137 struct field *field;
138
139 field = &g_array_index(array->elems, struct field, i);
140 field->definition->declaration->definition_free(field->definition);
141 }
142 (void) g_array_free(array->elems, TRUE);
e1151715 143 free_definition_scope(array->scope);
f6625916 144 declaration_unref(array->p.declaration);
c054553d
MD
145 g_free(array);
146}
0f980a35
MD
147
148struct definition *array_index(struct definition_array *array, uint64_t i)
149{
150 if (i >= array->elems->len)
151 return NULL;
152 return g_array_index(array->elems, struct field, i).definition;
153}
This page took 0.029314 seconds and 4 git commands to generate.