| 1 | /* |
| 2 | * BabelTrace - Array Type Converter |
| 3 | * |
| 4 | * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <babeltrace/compiler.h> |
| 22 | #include <babeltrace/types.h> |
| 23 | |
| 24 | void array_copy(struct stream_pos *dest, const struct format *fdest, |
| 25 | struct stream_pos *src, const struct format *fsrc, |
| 26 | const struct type_class *type_class) |
| 27 | { |
| 28 | struct type_class_array *array_class = |
| 29 | container_of(type_class, struct type_class_array, p); |
| 30 | unsigned int i; |
| 31 | |
| 32 | fsrc->array_begin(src, array_class); |
| 33 | fdest->array_begin(dest, array_class); |
| 34 | |
| 35 | for (i = 0; i < array_class->len; i++) { |
| 36 | struct type_class *elem_class = array_class->elem; |
| 37 | elem_class->copy(dest, fdest, src, fsrc, &elem_class->p); |
| 38 | } |
| 39 | fsrc->array_end(src, array_class); |
| 40 | fdest->array_end(dest, array_class); |
| 41 | } |
| 42 | |
| 43 | void array_type_free(struct type_class_array *array_class) |
| 44 | { |
| 45 | array_class->elem->free(&array_class->elem->p); |
| 46 | g_free(array_class); |
| 47 | } |
| 48 | |
| 49 | static void _array_type_free(struct type_class *type_class) |
| 50 | { |
| 51 | struct type_class_struct *array_class = |
| 52 | container_of(type_class, struct type_class_array, p); |
| 53 | array_type_free(array_class); |
| 54 | } |
| 55 | |
| 56 | struct type_class_array *array_type_new(const char *name, size_t len, |
| 57 | struct type_class *elem) |
| 58 | { |
| 59 | struct type_class_array *array_class; |
| 60 | int ret; |
| 61 | |
| 62 | array_class = g_new(struct type_class_array, 1); |
| 63 | type_class = &float_class->p; |
| 64 | |
| 65 | array_class->len = len; |
| 66 | type_class->name = g_quark_from_string(name); |
| 67 | /* No need to align the array, the first element will align itself */ |
| 68 | type_class->alignment = 1; |
| 69 | type_class->copy = array_copy; |
| 70 | type_class->free = _array_type_free; |
| 71 | |
| 72 | if (type_class->name) { |
| 73 | ret = ctf_register_type(type_class); |
| 74 | if (ret) |
| 75 | goto error_register; |
| 76 | } |
| 77 | return array_class; |
| 78 | |
| 79 | error_register: |
| 80 | g_free(array_class); |
| 81 | return NULL; |
| 82 | } |