| 1 | /* |
| 2 | * string.c |
| 3 | * |
| 4 | * BabelTrace - String 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/align.h> |
| 21 | #include <babeltrace/format.h> |
| 22 | |
| 23 | static |
| 24 | struct definition *_string_definition_new(struct declaration *declaration, |
| 25 | struct definition_scope *parent_scope, |
| 26 | GQuark field_name, int index, |
| 27 | const char *root_name); |
| 28 | static |
| 29 | void _string_definition_free(struct definition *definition); |
| 30 | |
| 31 | static |
| 32 | void _string_declaration_free(struct declaration *declaration) |
| 33 | { |
| 34 | struct declaration_string *string_declaration = |
| 35 | container_of(declaration, struct declaration_string, p); |
| 36 | g_free(string_declaration); |
| 37 | } |
| 38 | |
| 39 | struct declaration_string * |
| 40 | string_declaration_new(enum ctf_string_encoding encoding) |
| 41 | { |
| 42 | struct declaration_string *string_declaration; |
| 43 | |
| 44 | string_declaration = g_new(struct declaration_string, 1); |
| 45 | string_declaration->p.id = CTF_TYPE_STRING; |
| 46 | string_declaration->p.alignment = CHAR_BIT; |
| 47 | string_declaration->p.declaration_free = _string_declaration_free; |
| 48 | string_declaration->p.definition_new = _string_definition_new; |
| 49 | string_declaration->p.definition_free = _string_definition_free; |
| 50 | string_declaration->p.ref = 1; |
| 51 | string_declaration->encoding = encoding; |
| 52 | return string_declaration; |
| 53 | } |
| 54 | |
| 55 | static |
| 56 | struct definition * |
| 57 | _string_definition_new(struct declaration *declaration, |
| 58 | struct definition_scope *parent_scope, |
| 59 | GQuark field_name, int index, |
| 60 | const char *root_name) |
| 61 | { |
| 62 | struct declaration_string *string_declaration = |
| 63 | container_of(declaration, struct declaration_string, p); |
| 64 | struct definition_string *string; |
| 65 | int ret; |
| 66 | |
| 67 | string = g_new(struct definition_string, 1); |
| 68 | declaration_ref(&string_declaration->p); |
| 69 | string->p.declaration = declaration; |
| 70 | string->declaration = string_declaration; |
| 71 | string->p.ref = 1; |
| 72 | /* |
| 73 | * Use INT_MAX order to ensure that all fields of the parent |
| 74 | * scope are seen as being prior to this scope. |
| 75 | */ |
| 76 | string->p.index = root_name ? INT_MAX : index; |
| 77 | string->p.name = field_name; |
| 78 | string->p.path = new_definition_path(parent_scope, field_name, |
| 79 | root_name); |
| 80 | string->p.scope = NULL; |
| 81 | string->value = NULL; |
| 82 | string->len = 0; |
| 83 | string->alloc_len = 0; |
| 84 | ret = register_field_definition(field_name, &string->p, |
| 85 | parent_scope); |
| 86 | assert(!ret); |
| 87 | return &string->p; |
| 88 | } |
| 89 | |
| 90 | static |
| 91 | void _string_definition_free(struct definition *definition) |
| 92 | { |
| 93 | struct definition_string *string = |
| 94 | container_of(definition, struct definition_string, p); |
| 95 | |
| 96 | declaration_unref(string->p.declaration); |
| 97 | g_free(string->value); |
| 98 | g_free(string); |
| 99 | } |