Commit | Line | Data |
---|---|---|
bed864a7 | 1 | /* |
ccd7e1c8 | 2 | * string.c |
bed864a7 | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - String Type Converter |
bed864a7 | 5 | * |
c054553d | 6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
bed864a7 | 7 | * |
ccd7e1c8 MD |
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: | |
bed864a7 | 14 | * |
ccd7e1c8 MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
bed864a7 MD |
17 | */ |
18 | ||
19 | #include <babeltrace/compiler.h> | |
20 | #include <babeltrace/align.h> | |
4c8bfb7e | 21 | #include <babeltrace/format.h> |
bed864a7 | 22 | |
c054553d | 23 | static |
f6625916 | 24 | struct definition *_string_definition_new(struct declaration *declaration, |
05c749e5 | 25 | struct definition_scope *parent_scope, |
98df1c9f MD |
26 | GQuark field_name, int index, |
27 | const char *root_name); | |
c054553d | 28 | static |
e1151715 | 29 | void _string_definition_free(struct definition *definition); |
c054553d | 30 | |
c054553d | 31 | static |
f6625916 | 32 | void _string_declaration_free(struct declaration *declaration) |
bed864a7 | 33 | { |
f6625916 MD |
34 | struct declaration_string *string_declaration = |
35 | container_of(declaration, struct declaration_string, p); | |
36 | g_free(string_declaration); | |
bed864a7 MD |
37 | } |
38 | ||
ab4cf058 MD |
39 | struct declaration_string * |
40 | string_declaration_new(enum ctf_string_encoding encoding) | |
bed864a7 | 41 | { |
f6625916 | 42 | struct declaration_string *string_declaration; |
bed864a7 | 43 | |
f6625916 MD |
44 | string_declaration = g_new(struct declaration_string, 1); |
45 | string_declaration->p.id = CTF_TYPE_STRING; | |
f6625916 | 46 | string_declaration->p.alignment = CHAR_BIT; |
f6625916 MD |
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; | |
ab4cf058 | 51 | string_declaration->encoding = encoding; |
f6625916 | 52 | return string_declaration; |
bed864a7 | 53 | } |
c054553d MD |
54 | |
55 | static | |
e1151715 | 56 | struct definition * |
f6625916 | 57 | _string_definition_new(struct declaration *declaration, |
05c749e5 | 58 | struct definition_scope *parent_scope, |
98df1c9f MD |
59 | GQuark field_name, int index, |
60 | const char *root_name) | |
c054553d | 61 | { |
f6625916 MD |
62 | struct declaration_string *string_declaration = |
63 | container_of(declaration, struct declaration_string, p); | |
e1151715 | 64 | struct definition_string *string; |
98df1c9f | 65 | int ret; |
c054553d | 66 | |
e1151715 | 67 | string = g_new(struct definition_string, 1); |
f6625916 MD |
68 | declaration_ref(&string_declaration->p); |
69 | string->p.declaration = declaration; | |
70 | string->declaration = string_declaration; | |
c054553d | 71 | string->p.ref = 1; |
98df1c9f MD |
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; | |
b1a2f580 | 77 | string->p.name = field_name; |
98df1c9f MD |
78 | string->p.path = new_definition_path(parent_scope, field_name, |
79 | root_name); | |
c054553d | 80 | string->value = NULL; |
d11e9c49 MD |
81 | string->len = 0; |
82 | string->alloc_len = 0; | |
98df1c9f MD |
83 | ret = register_field_definition(field_name, &string->p, |
84 | parent_scope); | |
85 | assert(!ret); | |
c054553d MD |
86 | return &string->p; |
87 | } | |
88 | ||
89 | static | |
e1151715 | 90 | void _string_definition_free(struct definition *definition) |
c054553d | 91 | { |
e1151715 MD |
92 | struct definition_string *string = |
93 | container_of(definition, struct definition_string, p); | |
c054553d | 94 | |
f6625916 | 95 | declaration_unref(string->p.declaration); |
c054553d MD |
96 | g_free(string->value); |
97 | g_free(string); | |
98 | } |