| 1 | /* |
| 2 | * array.c |
| 3 | * |
| 4 | * BabelTrace - Array Type Converter |
| 5 | * |
| 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 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: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | */ |
| 18 | |
| 19 | #include <babeltrace/compiler.h> |
| 20 | #include <babeltrace/format.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); |
| 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 | type_unref(array_class->elem); |
| 44 | g_free(array_class); |
| 45 | } |
| 46 | |
| 47 | static void _array_type_free(struct type_class *type_class) |
| 48 | { |
| 49 | struct type_class_array *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 | struct type_class *type_class; |
| 59 | int ret; |
| 60 | |
| 61 | array_class = g_new(struct type_class_array, 1); |
| 62 | type_class = &array_class->p; |
| 63 | |
| 64 | array_class->len = len; |
| 65 | type_ref(elem); |
| 66 | array_class->elem = elem; |
| 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; |
| 72 | type_class->ref = 1; |
| 73 | |
| 74 | if (type_class->name) { |
| 75 | ret = ctf_register_type(type_class); |
| 76 | if (ret) |
| 77 | goto error_register; |
| 78 | } |
| 79 | return array_class; |
| 80 | |
| 81 | error_register: |
| 82 | type_unref(array_class->elem); |
| 83 | g_free(array_class); |
| 84 | return NULL; |
| 85 | } |