Commit | Line | Data |
---|---|---|
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 MD |
22 | static |
23 | struct type *_array_type_new(struct type_class *type_class, | |
24 | struct declaration_scope *parent_scope); | |
25 | static | |
26 | void _array_type_free(struct type *type); | |
27 | ||
d06d03db MD |
28 | void array_copy(struct stream_pos *dest, const struct format *fdest, |
29 | struct stream_pos *src, const struct format *fsrc, | |
c054553d | 30 | struct type *type) |
d06d03db | 31 | { |
c054553d MD |
32 | struct type_sequence *array = container_of(type, struct type_array, p); |
33 | struct type_class_array *array_class = array->_class; | |
34 | uint64_t i; | |
d06d03db MD |
35 | |
36 | fsrc->array_begin(src, array_class); | |
37 | fdest->array_begin(dest, array_class); | |
38 | ||
39 | for (i = 0; i < array_class->len; i++) { | |
c054553d MD |
40 | struct type_class *elem_class = array->current_element.type; |
41 | elem_type->p._class->copy(dest, fdest, src, fsrc, elem_type); | |
d06d03db MD |
42 | } |
43 | fsrc->array_end(src, array_class); | |
44 | fdest->array_end(dest, array_class); | |
45 | } | |
46 | ||
c054553d MD |
47 | static |
48 | void _array_type_class_free(struct type_class *type_class) | |
d06d03db | 49 | { |
4c8bfb7e | 50 | struct type_class_array *array_class = |
d06d03db | 51 | container_of(type_class, struct type_class_array, p); |
c054553d MD |
52 | |
53 | type_class_unref(array_class->elem); | |
54 | g_free(array_class); | |
d06d03db MD |
55 | } |
56 | ||
c054553d MD |
57 | struct type_class_array * |
58 | array_type_class_new(const char *name, size_t len, struct type_class *elem) | |
d06d03db MD |
59 | { |
60 | struct type_class_array *array_class; | |
4c8bfb7e | 61 | struct type_class *type_class; |
d06d03db MD |
62 | int ret; |
63 | ||
64 | array_class = g_new(struct type_class_array, 1); | |
4c8bfb7e | 65 | type_class = &array_class->p; |
d06d03db | 66 | array_class->len = len; |
4c8bfb7e MD |
67 | type_ref(elem); |
68 | array_class->elem = elem; | |
d06d03db MD |
69 | type_class->name = g_quark_from_string(name); |
70 | /* No need to align the array, the first element will align itself */ | |
71 | type_class->alignment = 1; | |
72 | type_class->copy = array_copy; | |
c054553d MD |
73 | type_class->class_free = _array_type_class_free; |
74 | type_class->type_new = _array_type_new; | |
75 | type_class->type_free = _array_type_free; | |
4c8bfb7e | 76 | type_class->ref = 1; |
d06d03db MD |
77 | |
78 | if (type_class->name) { | |
be85c1c7 | 79 | ret = register_type(type_class); |
d06d03db MD |
80 | if (ret) |
81 | goto error_register; | |
82 | } | |
83 | return array_class; | |
84 | ||
85 | error_register: | |
c054553d | 86 | type_class_unref(array_class->elem); |
d06d03db MD |
87 | g_free(array_class); |
88 | return NULL; | |
89 | } | |
c054553d MD |
90 | |
91 | static | |
92 | struct type_array *_array_type_new(struct type_class *type_class, | |
93 | struct declaration_scope *parent_scope) | |
94 | { | |
95 | struct type_class_array *array_class = | |
96 | container_of(type_class, struct type_class_array, p); | |
97 | struct type_array *array; | |
98 | ||
99 | array = g_new(struct type_array, 1); | |
100 | type_class_ref(&array_class->p); | |
101 | array->p._class = array_class; | |
102 | array->p.ref = 1; | |
103 | array->scope = new_declaration_scope(parent_scope); | |
104 | array->current_element.type = | |
105 | array_class->elem.p->type_new(&array_class->elem.p, | |
106 | parent_scope); | |
107 | return &array->p; | |
108 | } | |
109 | ||
110 | static | |
111 | void _array_type_free(struct type *type) | |
112 | { | |
113 | struct type_array *array = | |
114 | container_of(type, struct type_array, p); | |
115 | struct type *elem_type = array->current_element.type; | |
116 | ||
117 | elem_type->p._class->type_free(elem_type); | |
118 | free_declaration_scope(array->scope); | |
119 | type_class_unref(array->p._class); | |
120 | g_free(array); | |
121 | } |