| 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 type *_array_type_new(struct type_class *type_class, |
| 24 | struct declaration_scope *parent_scope); |
| 25 | static |
| 26 | void _array_type_free(struct type *type); |
| 27 | |
| 28 | void array_copy(struct stream_pos *dest, const struct format *fdest, |
| 29 | struct stream_pos *src, const struct format *fsrc, |
| 30 | struct type *type) |
| 31 | { |
| 32 | struct type_sequence *array = container_of(type, struct type_array, p); |
| 33 | struct type_class_array *array_class = array->_class; |
| 34 | uint64_t i; |
| 35 | |
| 36 | fsrc->array_begin(src, array_class); |
| 37 | fdest->array_begin(dest, array_class); |
| 38 | |
| 39 | for (i = 0; i < array_class->len; i++) { |
| 40 | struct type_class *elem_class = array->current_element.type; |
| 41 | elem_type->p._class->copy(dest, fdest, src, fsrc, elem_type); |
| 42 | } |
| 43 | fsrc->array_end(src, array_class); |
| 44 | fdest->array_end(dest, array_class); |
| 45 | } |
| 46 | |
| 47 | static |
| 48 | void _array_type_class_free(struct type_class *type_class) |
| 49 | { |
| 50 | struct type_class_array *array_class = |
| 51 | container_of(type_class, struct type_class_array, p); |
| 52 | |
| 53 | type_class_unref(array_class->elem); |
| 54 | g_free(array_class); |
| 55 | } |
| 56 | |
| 57 | struct type_class_array * |
| 58 | array_type_class_new(const char *name, size_t len, struct type_class *elem) |
| 59 | { |
| 60 | struct type_class_array *array_class; |
| 61 | struct type_class *type_class; |
| 62 | int ret; |
| 63 | |
| 64 | array_class = g_new(struct type_class_array, 1); |
| 65 | type_class = &array_class->p; |
| 66 | array_class->len = len; |
| 67 | type_ref(elem); |
| 68 | array_class->elem = elem; |
| 69 | type_class->name = g_quark_from_string(name); |
| 70 | /* No need to align the array, the first element will align itself */ |
| 71 | type_class->alignment = 1; |
| 72 | type_class->copy = array_copy; |
| 73 | type_class->class_free = _array_type_class_free; |
| 74 | type_class->type_new = _array_type_new; |
| 75 | type_class->type_free = _array_type_free; |
| 76 | type_class->ref = 1; |
| 77 | |
| 78 | if (type_class->name) { |
| 79 | ret = register_type(type_class); |
| 80 | if (ret) |
| 81 | goto error_register; |
| 82 | } |
| 83 | return array_class; |
| 84 | |
| 85 | error_register: |
| 86 | type_class_unref(array_class->elem); |
| 87 | g_free(array_class); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | static |
| 92 | struct type_array *_array_type_new(struct type_class *type_class, |
| 93 | struct declaration_scope *parent_scope) |
| 94 | { |
| 95 | struct type_class_array *array_class = |
| 96 | container_of(type_class, struct type_class_array, p); |
| 97 | struct type_array *array; |
| 98 | |
| 99 | array = g_new(struct type_array, 1); |
| 100 | type_class_ref(&array_class->p); |
| 101 | array->p._class = array_class; |
| 102 | array->p.ref = 1; |
| 103 | array->scope = new_declaration_scope(parent_scope); |
| 104 | array->current_element.type = |
| 105 | array_class->elem.p->type_new(&array_class->elem.p, |
| 106 | parent_scope); |
| 107 | return &array->p; |
| 108 | } |
| 109 | |
| 110 | static |
| 111 | void _array_type_free(struct type *type) |
| 112 | { |
| 113 | struct type_array *array = |
| 114 | container_of(type, struct type_array, p); |
| 115 | struct type *elem_type = array->current_element.type; |
| 116 | |
| 117 | elem_type->p._class->type_free(elem_type); |
| 118 | free_declaration_scope(array->scope); |
| 119 | type_class_unref(array->p._class); |
| 120 | g_free(array); |
| 121 | } |