Start packet mmap work
[babeltrace.git] / types / string.c
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 static
28 void _string_definition_free(struct definition *definition);
29
30 void string_copy(struct stream_pos *dest, const struct format *fdest,
31 struct stream_pos *src, const struct format *fsrc,
32 struct definition *definition)
33 {
34 struct definition_string *string =
35 container_of(definition, struct definition_string, p);
36 struct declaration_string *string_declaration = string->declaration;
37
38 if (fdest && (fsrc->string_copy == fdest->string_copy)) {
39 fsrc->string_copy(dest, src, string_declaration);
40 } else {
41 char *tmp = NULL;
42
43 fsrc->string_read(&tmp, src, string_declaration);
44 if (fdest)
45 fdest->string_write(dest, tmp, string_declaration);
46 fsrc->string_free_temp(tmp);
47 }
48 }
49
50 static
51 void _string_declaration_free(struct declaration *declaration)
52 {
53 struct declaration_string *string_declaration =
54 container_of(declaration, struct declaration_string, p);
55 g_free(string_declaration);
56 }
57
58 struct declaration_string *
59 string_declaration_new(enum ctf_string_encoding encoding)
60 {
61 struct declaration_string *string_declaration;
62
63 string_declaration = g_new(struct declaration_string, 1);
64 string_declaration->p.id = CTF_TYPE_STRING;
65 string_declaration->p.alignment = CHAR_BIT;
66 string_declaration->p.copy = string_copy;
67 string_declaration->p.declaration_free = _string_declaration_free;
68 string_declaration->p.definition_new = _string_definition_new;
69 string_declaration->p.definition_free = _string_definition_free;
70 string_declaration->p.ref = 1;
71 string_declaration->encoding = encoding;
72 return string_declaration;
73 }
74
75 static
76 struct definition *
77 _string_definition_new(struct declaration *declaration,
78 struct definition_scope *parent_scope,
79 GQuark field_name, int index)
80 {
81 struct declaration_string *string_declaration =
82 container_of(declaration, struct declaration_string, p);
83 struct definition_string *string;
84
85 string = g_new(struct definition_string, 1);
86 declaration_ref(&string_declaration->p);
87 string->p.declaration = declaration;
88 string->declaration = string_declaration;
89 string->p.ref = 1;
90 string->p.index = index;
91 string->value = NULL;
92 return &string->p;
93 }
94
95 static
96 void _string_definition_free(struct definition *definition)
97 {
98 struct definition_string *string =
99 container_of(definition, struct definition_string, p);
100
101 declaration_unref(string->p.declaration);
102 g_free(string->value);
103 g_free(string);
104 }
This page took 0.029983 seconds and 4 git commands to generate.