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> | |
20 | #include <babeltrace/types.h> | |
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; | |
35 | elem_class->copy(dest, fdest, src, fsrc, &elem_class->p); | |
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 | { | |
43 | array_class->elem->free(&array_class->elem->p); | |
44 | g_free(array_class); | |
45 | } | |
46 | ||
47 | static void _array_type_free(struct type_class *type_class) | |
48 | { | |
49 | struct type_class_struct *array_class = | |
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; | |
58 | int ret; | |
59 | ||
60 | array_class = g_new(struct type_class_array, 1); | |
61 | type_class = &float_class->p; | |
62 | ||
63 | array_class->len = len; | |
64 | type_class->name = g_quark_from_string(name); | |
65 | /* No need to align the array, the first element will align itself */ | |
66 | type_class->alignment = 1; | |
67 | type_class->copy = array_copy; | |
68 | type_class->free = _array_type_free; | |
69 | ||
70 | if (type_class->name) { | |
71 | ret = ctf_register_type(type_class); | |
72 | if (ret) | |
73 | goto error_register; | |
74 | } | |
75 | return array_class; | |
76 | ||
77 | error_register: | |
2e7d72cf | 78 | array_class->elem->free(&array_class->elem->p); |
d06d03db MD |
79 | g_free(array_class); |
80 | return NULL; | |
81 | } |