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