Fix: add mmap_base_offset to ctf_stream_pos
[babeltrace.git] / types / string.c
CommitLineData
bed864a7 1/*
ccd7e1c8 2 * string.c
bed864a7 3 *
ccd7e1c8 4 * BabelTrace - String Type Converter
bed864a7 5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
bed864a7 9 *
ccd7e1c8
MD
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
bed864a7 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
bed864a7
MD
19 */
20
21#include <babeltrace/compiler.h>
22#include <babeltrace/align.h>
4c8bfb7e 23#include <babeltrace/format.h>
3122e6f0 24#include <babeltrace/types.h>
bed864a7 25
c054553d 26static
f6625916 27struct definition *_string_definition_new(struct declaration *declaration,
05c749e5 28 struct definition_scope *parent_scope,
98df1c9f
MD
29 GQuark field_name, int index,
30 const char *root_name);
c054553d 31static
e1151715 32void _string_definition_free(struct definition *definition);
c054553d 33
c054553d 34static
f6625916 35void _string_declaration_free(struct declaration *declaration)
bed864a7 36{
f6625916
MD
37 struct declaration_string *string_declaration =
38 container_of(declaration, struct declaration_string, p);
39 g_free(string_declaration);
bed864a7
MD
40}
41
ab4cf058
MD
42struct declaration_string *
43 string_declaration_new(enum ctf_string_encoding encoding)
bed864a7 44{
f6625916 45 struct declaration_string *string_declaration;
bed864a7 46
f6625916
MD
47 string_declaration = g_new(struct declaration_string, 1);
48 string_declaration->p.id = CTF_TYPE_STRING;
f6625916 49 string_declaration->p.alignment = CHAR_BIT;
f6625916
MD
50 string_declaration->p.declaration_free = _string_declaration_free;
51 string_declaration->p.definition_new = _string_definition_new;
52 string_declaration->p.definition_free = _string_definition_free;
53 string_declaration->p.ref = 1;
ab4cf058 54 string_declaration->encoding = encoding;
f6625916 55 return string_declaration;
bed864a7 56}
c054553d
MD
57
58static
e1151715 59struct definition *
f6625916 60 _string_definition_new(struct declaration *declaration,
05c749e5 61 struct definition_scope *parent_scope,
98df1c9f
MD
62 GQuark field_name, int index,
63 const char *root_name)
c054553d 64{
f6625916
MD
65 struct declaration_string *string_declaration =
66 container_of(declaration, struct declaration_string, p);
e1151715 67 struct definition_string *string;
98df1c9f 68 int ret;
c054553d 69
e1151715 70 string = g_new(struct definition_string, 1);
f6625916
MD
71 declaration_ref(&string_declaration->p);
72 string->p.declaration = declaration;
73 string->declaration = string_declaration;
c054553d 74 string->p.ref = 1;
98df1c9f
MD
75 /*
76 * Use INT_MAX order to ensure that all fields of the parent
77 * scope are seen as being prior to this scope.
78 */
79 string->p.index = root_name ? INT_MAX : index;
b1a2f580 80 string->p.name = field_name;
98df1c9f
MD
81 string->p.path = new_definition_path(parent_scope, field_name,
82 root_name);
a35173fe 83 string->p.scope = NULL;
c054553d 84 string->value = NULL;
d11e9c49
MD
85 string->len = 0;
86 string->alloc_len = 0;
98df1c9f
MD
87 ret = register_field_definition(field_name, &string->p,
88 parent_scope);
89 assert(!ret);
c054553d
MD
90 return &string->p;
91}
92
93static
e1151715 94void _string_definition_free(struct definition *definition)
c054553d 95{
e1151715
MD
96 struct definition_string *string =
97 container_of(definition, struct definition_string, p);
c054553d 98
f6625916 99 declaration_unref(string->p.declaration);
c054553d
MD
100 g_free(string->value);
101 g_free(string);
102}
98b68326 103
8673030f
JD
104enum ctf_string_encoding get_string_encoding(const struct definition *field)
105{
106 struct definition_string *string_definition;
107 const struct declaration_string *string_declaration;
108
109 string_definition = container_of(field, struct definition_string, p);
110 string_declaration = string_definition->declaration;
111
112 return string_declaration->encoding;
113}
114
da320b83 115char *get_string(const struct definition *field)
98b68326
JD
116{
117 struct definition_string *string_definition =
118 container_of(field, struct definition_string, p);
119
120 assert(string_definition->value != NULL);
121
122 return string_definition->value;
123}
This page took 0.045278 seconds and 4 git commands to generate.