Commit | Line | Data |
---|---|---|
d06d03db | 1 | /* |
ccd7e1c8 | 2 | * array.c |
d06d03db | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Array Type Converter |
d06d03db | 5 | * |
ccd7e1c8 | 6 | * Copyright 2010 - 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 MD |
21 | |
22 | void array_copy(struct stream_pos *dest, const struct format *fdest, | |
23 | struct stream_pos *src, const struct format *fsrc, | |
24 | const struct type_class *type_class) | |
25 | { | |
26 | struct type_class_array *array_class = | |
27 | container_of(type_class, struct type_class_array, p); | |
28 | unsigned int i; | |
29 | ||
30 | fsrc->array_begin(src, array_class); | |
31 | fdest->array_begin(dest, array_class); | |
32 | ||
33 | for (i = 0; i < array_class->len; i++) { | |
34 | struct type_class *elem_class = array_class->elem; | |
4c8bfb7e | 35 | elem_class->copy(dest, fdest, src, fsrc, elem_class); |
d06d03db MD |
36 | } |
37 | fsrc->array_end(src, array_class); | |
38 | fdest->array_end(dest, array_class); | |
39 | } | |
40 | ||
41 | void array_type_free(struct type_class_array *array_class) | |
42 | { | |
4c8bfb7e | 43 | type_unref(array_class->elem); |
d06d03db MD |
44 | g_free(array_class); |
45 | } | |
46 | ||
47 | static void _array_type_free(struct type_class *type_class) | |
48 | { | |
4c8bfb7e | 49 | struct type_class_array *array_class = |
d06d03db MD |
50 | container_of(type_class, struct type_class_array, p); |
51 | array_type_free(array_class); | |
52 | } | |
53 | ||
54 | struct type_class_array *array_type_new(const char *name, size_t len, | |
55 | struct type_class *elem) | |
56 | { | |
57 | struct type_class_array *array_class; | |
4c8bfb7e | 58 | struct type_class *type_class; |
d06d03db MD |
59 | int ret; |
60 | ||
61 | array_class = g_new(struct type_class_array, 1); | |
4c8bfb7e | 62 | type_class = &array_class->p; |
d06d03db MD |
63 | |
64 | array_class->len = len; | |
4c8bfb7e MD |
65 | type_ref(elem); |
66 | array_class->elem = elem; | |
d06d03db MD |
67 | type_class->name = g_quark_from_string(name); |
68 | /* No need to align the array, the first element will align itself */ | |
69 | type_class->alignment = 1; | |
70 | type_class->copy = array_copy; | |
71 | type_class->free = _array_type_free; | |
4c8bfb7e | 72 | type_class->ref = 1; |
d06d03db MD |
73 | |
74 | if (type_class->name) { | |
be85c1c7 | 75 | ret = register_type(type_class); |
d06d03db MD |
76 | if (ret) |
77 | goto error_register; | |
78 | } | |
79 | return array_class; | |
80 | ||
81 | error_register: | |
4c8bfb7e | 82 | type_unref(array_class->elem); |
d06d03db MD |
83 | g_free(array_class); |
84 | return NULL; | |
85 | } |