lib: trace IR, values: reset pointers to `NULL` on destruction
[babeltrace.git] / lib / trace-ir / stream.c
1 /*
2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #define BT_LOG_TAG "STREAM"
26 #include <babeltrace/lib-logging-internal.h>
27
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/trace-ir/private-stream.h>
30 #include <babeltrace/trace-ir/stream.h>
31 #include <babeltrace/trace-ir/stream-internal.h>
32 #include <babeltrace/trace-ir/stream-class.h>
33 #include <babeltrace/trace-ir/stream-class-internal.h>
34 #include <babeltrace/trace-ir/trace.h>
35 #include <babeltrace/trace-ir/trace-internal.h>
36 #include <babeltrace/trace-ir/packet-internal.h>
37 #include <babeltrace/object.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/align-internal.h>
40 #include <babeltrace/assert-internal.h>
41 #include <babeltrace/property-internal.h>
42 #include <inttypes.h>
43 #include <unistd.h>
44
45 #define BT_ASSERT_PRE_STREAM_HOT(_stream) \
46 BT_ASSERT_PRE_HOT((_stream), "Stream", ": %!+s", (_stream))
47
48 static
49 void destroy_stream(struct bt_object *obj)
50 {
51 struct bt_stream *stream = (void *) obj;
52
53 BT_LIB_LOGD("Destroying stream object: %!+s", stream);
54
55 if (stream->name.str) {
56 g_string_free(stream->name.str, TRUE);
57 stream->name.str = NULL;
58 stream->name.value = NULL;
59 }
60
61 bt_object_pool_finalize(&stream->packet_pool);
62 g_free(stream);
63 }
64
65 static
66 void bt_stream_free_packet(struct bt_packet *packet, struct bt_stream *stream)
67 {
68 bt_packet_destroy(packet);
69 }
70
71 BT_ASSERT_PRE_FUNC
72 static inline
73 bool stream_id_is_unique(struct bt_trace *trace,
74 struct bt_stream_class *stream_class, uint64_t id)
75 {
76 uint64_t i;
77 bool is_unique = true;
78
79 for (i = 0; i < trace->streams->len; i++) {
80 struct bt_stream *stream = trace->streams->pdata[i];
81
82 if (stream->class != stream_class) {
83 continue;
84 }
85
86 if (stream->id == id) {
87 is_unique = false;
88 goto end;
89 }
90 }
91
92 end:
93 return is_unique;
94 }
95
96 static
97 struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class,
98 uint64_t id)
99 {
100 int ret;
101 struct bt_stream *stream;
102 struct bt_trace *trace;
103
104 BT_ASSERT(stream_class);
105 trace = bt_stream_class_borrow_trace_inline(stream_class);
106 BT_ASSERT_PRE(stream_id_is_unique(trace, stream_class, id),
107 "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id);
108 BT_ASSERT_PRE(!trace->is_static,
109 "Trace is static: %![trace-]+t", trace);
110 BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64,
111 trace, id);
112 stream = g_new0(struct bt_stream, 1);
113 if (!stream) {
114 BT_LOGE_STR("Failed to allocate one stream.");
115 goto error;
116 }
117
118 bt_object_init_shared_with_parent(&stream->base, destroy_stream);
119 stream->name.str = g_string_new(NULL);
120 if (!stream->name.str) {
121 BT_LOGE_STR("Failed to allocate a GString.");
122 goto error;
123 }
124
125 stream->id = id;
126 ret = bt_object_pool_initialize(&stream->packet_pool,
127 (bt_object_pool_new_object_func) bt_packet_new,
128 (bt_object_pool_destroy_object_func) bt_stream_free_packet,
129 stream);
130 if (ret) {
131 BT_LOGE("Failed to initialize packet pool: ret=%d", ret);
132 goto error;
133 }
134
135 stream->class = stream_class;
136 bt_trace_add_stream(trace, stream);
137 bt_stream_class_freeze(stream_class);
138 BT_LIB_LOGD("Created stream object: %!+s", stream);
139 goto end;
140
141 error:
142 BT_OBJECT_PUT_REF_AND_RESET(stream);
143
144 end:
145 return stream;
146 }
147
148 struct bt_private_stream *bt_private_stream_create(
149 struct bt_private_stream_class *priv_stream_class)
150 {
151 struct bt_stream_class *stream_class = (void *) priv_stream_class;
152 uint64_t id;
153
154 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
155 BT_ASSERT_PRE(stream_class->assigns_automatic_stream_id,
156 "Stream class does not automatically assigns stream IDs: "
157 "%![sc-]+S", stream_class);
158 id = bt_trace_get_automatic_stream_id(
159 bt_stream_class_borrow_trace_inline(stream_class),
160 stream_class);
161 return (void *) create_stream_with_id(stream_class, id);
162 }
163
164 struct bt_private_stream *bt_private_stream_create_with_id(
165 struct bt_private_stream_class *priv_stream_class,
166 uint64_t id)
167 {
168 struct bt_stream_class *stream_class = (void *) priv_stream_class;
169
170 BT_ASSERT_PRE(!stream_class->assigns_automatic_stream_id,
171 "Stream class automatically assigns stream IDs: "
172 "%![sc-]+S", stream_class);
173 return (void *) create_stream_with_id(stream_class, id);
174 }
175
176 struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream)
177 {
178 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
179 return stream->class;
180 }
181
182 struct bt_private_stream_class *bt_private_stream_borrow_class(
183 struct bt_private_stream *priv_stream)
184 {
185 return (void *) bt_stream_borrow_class((void *) priv_stream);
186 }
187
188 const char *bt_stream_get_name(struct bt_stream *stream)
189 {
190 BT_ASSERT_PRE_NON_NULL(stream, "Stream class");
191 return stream->name.value;
192 }
193
194 int bt_private_stream_set_name(struct bt_private_stream *priv_stream,
195 const char *name)
196 {
197 struct bt_stream *stream = (void *) priv_stream;
198
199 BT_ASSERT_PRE_NON_NULL(stream, "Clock class");
200 BT_ASSERT_PRE_NON_NULL(name, "Name");
201 BT_ASSERT_PRE_STREAM_HOT(stream);
202 g_string_assign(stream->name.str, name);
203 stream->name.value = stream->name.str->str;
204 BT_LIB_LOGV("Set stream class's name: %!+s", stream);
205 return 0;
206 }
207
208 uint64_t bt_stream_get_id(struct bt_stream *stream)
209 {
210 BT_ASSERT_PRE_NON_NULL(stream, "Stream class");
211 return stream->id;
212 }
213
214 BT_HIDDEN
215 void _bt_stream_freeze(struct bt_stream *stream)
216 {
217 /* The field classes and default clock class are already frozen */
218 BT_ASSERT(stream);
219 BT_LIB_LOGD("Freezing stream: %!+s", stream);
220 stream->frozen = true;
221 }
This page took 0.033867 seconds and 4 git commands to generate.