| 1 | /* |
| 2 | * array.c |
| 3 | * |
| 4 | * BabelTrace - Array Type Converter |
| 5 | * |
| 6 | * Copyright 2010, 2011 - 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 | static |
| 23 | struct definition *_array_definition_new(struct type *type, |
| 24 | struct definition_scope *parent_scope); |
| 25 | static |
| 26 | void _array_definition_free(struct definition *definition); |
| 27 | |
| 28 | void array_copy(struct stream_pos *dest, const struct format *fdest, |
| 29 | struct stream_pos *src, const struct format *fsrc, |
| 30 | struct definition *definition) |
| 31 | { |
| 32 | struct definition_array *array = |
| 33 | container_of(definition, struct definition_array, p); |
| 34 | struct type_array *array_type = array->type; |
| 35 | uint64_t i; |
| 36 | |
| 37 | fsrc->array_begin(src, array_type); |
| 38 | fdest->array_begin(dest, array_type); |
| 39 | |
| 40 | for (i = 0; i < array_type->len; i++) { |
| 41 | struct definition *elem = array->current_element.definition; |
| 42 | elem->type->copy(dest, fdest, src, fsrc, elem); |
| 43 | } |
| 44 | fsrc->array_end(src, array_type); |
| 45 | fdest->array_end(dest, array_type); |
| 46 | } |
| 47 | |
| 48 | static |
| 49 | void _array_type_free(struct type *type) |
| 50 | { |
| 51 | struct type_array *array_type = |
| 52 | container_of(type, struct type_array, p); |
| 53 | |
| 54 | free_type_scope(array_type->scope); |
| 55 | type_unref(array_type->elem); |
| 56 | g_free(array_type); |
| 57 | } |
| 58 | |
| 59 | struct type_array * |
| 60 | array_type_new(const char *name, size_t len, struct type *elem_type, |
| 61 | struct type_scope *parent_scope) |
| 62 | { |
| 63 | struct type_array *array_type; |
| 64 | struct type *type; |
| 65 | |
| 66 | array_type = g_new(struct type_array, 1); |
| 67 | type = &array_type->p; |
| 68 | array_type->len = len; |
| 69 | type_ref(elem_type); |
| 70 | array_type->elem = elem_type; |
| 71 | array_type->scope = new_type_scope(parent_scope); |
| 72 | type->id = CTF_TYPE_ARRAY; |
| 73 | type->name = g_quark_from_string(name); |
| 74 | /* No need to align the array, the first element will align itself */ |
| 75 | type->alignment = 1; |
| 76 | type->copy = array_copy; |
| 77 | type->type_free = _array_type_free; |
| 78 | type->definition_new = _array_definition_new; |
| 79 | type->definition_free = _array_definition_free; |
| 80 | type->ref = 1; |
| 81 | return array_type; |
| 82 | } |
| 83 | |
| 84 | static |
| 85 | struct definition * |
| 86 | _array_definition_new(struct type *type, |
| 87 | struct definition_scope *parent_scope) |
| 88 | { |
| 89 | struct type_array *array_type = |
| 90 | container_of(type, struct type_array, p); |
| 91 | struct definition_array *array; |
| 92 | |
| 93 | array = g_new(struct definition_array, 1); |
| 94 | type_ref(&array_type->p); |
| 95 | array->p.type = type; |
| 96 | array->type = array_type; |
| 97 | array->p.ref = 1; |
| 98 | array->scope = new_definition_scope(parent_scope); |
| 99 | array->current_element.definition = |
| 100 | array_type->elem->definition_new(array_type->elem, |
| 101 | parent_scope); |
| 102 | return &array->p; |
| 103 | } |
| 104 | |
| 105 | static |
| 106 | void _array_definition_free(struct definition *definition) |
| 107 | { |
| 108 | struct definition_array *array = |
| 109 | container_of(definition, struct definition_array, p); |
| 110 | struct definition *elem_definition = |
| 111 | array->current_element.definition; |
| 112 | |
| 113 | elem_definition->type->definition_free(elem_definition); |
| 114 | free_definition_scope(array->scope); |
| 115 | type_unref(array->p.type); |
| 116 | g_free(array); |
| 117 | } |