lib: remove some unnecessary uses of `GString`
[babeltrace.git] / src / lib / trace-ir / stream.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/STREAM"
9 #include "lib/logging.h"
10
11 #include "lib/assert-cond.h"
12 #include <babeltrace2/trace-ir/stream.h>
13 #include <babeltrace2/trace-ir/stream-class.h>
14 #include <babeltrace2/trace-ir/trace.h>
15 #include "compat/compiler.h"
16 #include "common/assert.h"
17 #include <inttypes.h>
18 #include <stdbool.h>
19 #include <unistd.h>
20
21 #include "packet.h"
22 #include "stream-class.h"
23 #include "stream.h"
24 #include "trace.h"
25 #include "lib/value.h"
26 #include "lib/func-status.h"
27
28 #define BT_ASSERT_PRE_DEV_STREAM_HOT(_stream) \
29 BT_ASSERT_PRE_DEV_HOT("stream", (_stream), "Stream", \
30 ": %!+s", (_stream))
31
32 static
33 void destroy_stream(struct bt_object *obj)
34 {
35 struct bt_stream *stream = (void *) obj;
36
37 BT_LIB_LOGD("Destroying stream object: %!+s", stream);
38 BT_OBJECT_PUT_REF_AND_RESET(stream->user_attributes);
39
40 g_free(stream->name);
41 BT_LOGD_STR("Putting stream's class.");
42 bt_object_put_ref(stream->class);
43 bt_object_pool_finalize(&stream->packet_pool);
44 g_free(stream);
45 }
46
47 static
48 void bt_stream_free_packet(struct bt_packet *packet,
49 struct bt_stream *stream __attribute__((unused)))
50 {
51 bt_packet_destroy(packet);
52 }
53
54 static inline
55 bool stream_id_is_unique(struct bt_trace *trace,
56 struct bt_stream_class *stream_class, uint64_t id)
57 {
58 uint64_t i;
59 bool is_unique = true;
60
61 for (i = 0; i < trace->streams->len; i++) {
62 struct bt_stream *stream = trace->streams->pdata[i];
63
64 if (stream->class != stream_class) {
65 continue;
66 }
67
68 if (stream->id == id) {
69 is_unique = false;
70 goto end;
71 }
72 }
73
74 end:
75 return is_unique;
76 }
77
78 static
79 struct bt_stream *create_stream_with_id(struct bt_stream_class *stream_class,
80 struct bt_trace *trace, uint64_t id, const char *api_func)
81 {
82 int ret;
83 struct bt_stream *stream;
84
85 BT_ASSERT(stream_class);
86 BT_ASSERT(trace);
87 BT_ASSERT_PRE_FROM_FUNC(api_func,
88 "trace-class-is-stream-class-trace-class",
89 trace->class ==
90 bt_stream_class_borrow_trace_class_inline(stream_class),
91 "Trace's class is different from stream class's parent trace class: "
92 "%![sc-]+S, %![trace-]+t", stream_class, trace);
93 BT_ASSERT_PRE_FROM_FUNC(api_func, "stream-id-is-unique",
94 stream_id_is_unique(trace, stream_class, id),
95 "Duplicate stream ID: %![trace-]+t, id=%" PRIu64, trace, id);
96 BT_LIB_LOGD("Creating stream object: %![trace-]+t, id=%" PRIu64,
97 trace, id);
98 stream = g_new0(struct bt_stream, 1);
99 if (!stream) {
100 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one stream.");
101 goto error;
102 }
103
104 bt_object_init_shared_with_parent(&stream->base, destroy_stream);
105 stream->user_attributes = bt_value_map_create();
106 if (!stream->user_attributes) {
107 BT_LIB_LOGE_APPEND_CAUSE(
108 "Failed to create a map value object.");
109 goto error;
110 }
111
112 stream->id = id;
113 ret = bt_object_pool_initialize(&stream->packet_pool,
114 (bt_object_pool_new_object_func) bt_packet_new,
115 (bt_object_pool_destroy_object_func) bt_stream_free_packet,
116 stream);
117 if (ret) {
118 BT_LIB_LOGE_APPEND_CAUSE(
119 "Failed to initialize packet pool: ret=%d", ret);
120 goto error;
121 }
122
123 stream->class = stream_class;
124 bt_object_get_ref_no_null_check(stream_class);
125
126 /* bt_trace_add_stream() sets the parent trace, and freezes the trace */
127 bt_trace_add_stream(trace, stream);
128
129 bt_stream_class_freeze(stream_class);
130 BT_LIB_LOGD("Created stream object: %!+s", stream);
131 goto end;
132
133 error:
134 BT_OBJECT_PUT_REF_AND_RESET(stream);
135
136 end:
137 return stream;
138 }
139
140 BT_EXPORT
141 struct bt_stream *bt_stream_create(struct bt_stream_class *stream_class,
142 struct bt_trace *trace)
143 {
144 uint64_t id;
145
146 BT_ASSERT_PRE_NO_ERROR();
147 BT_ASSERT_PRE_SC_NON_NULL(stream_class);
148 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
149 BT_ASSERT_PRE("stream-class-automatically-assigns-stream-ids",
150 stream_class->assigns_automatic_stream_id,
151 "Stream class does not automatically assigns stream IDs: "
152 "%![sc-]+S", stream_class);
153 id = bt_trace_get_automatic_stream_id(trace, stream_class);
154 return create_stream_with_id(stream_class, trace, id, __func__);
155 }
156
157 BT_EXPORT
158 struct bt_stream *bt_stream_create_with_id(struct bt_stream_class *stream_class,
159 struct bt_trace *trace, uint64_t id)
160 {
161 BT_ASSERT_PRE_NO_ERROR();
162 BT_ASSERT_PRE_SC_NON_NULL(stream_class);
163 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
164 BT_ASSERT_PRE("stream-class-does-not-automatically-assigns-stream-ids",
165 !stream_class->assigns_automatic_stream_id,
166 "Stream class automatically assigns stream IDs: "
167 "%![sc-]+S", stream_class);
168 return create_stream_with_id(stream_class, trace, id, __func__);
169 }
170
171 BT_EXPORT
172 struct bt_stream_class *bt_stream_borrow_class(struct bt_stream *stream)
173 {
174 BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream);
175 return stream->class;
176 }
177
178 BT_EXPORT
179 const struct bt_stream_class *bt_stream_borrow_class_const(
180 const struct bt_stream *stream)
181 {
182 return bt_stream_borrow_class((void *) stream);
183 }
184
185 BT_EXPORT
186 struct bt_trace *bt_stream_borrow_trace(struct bt_stream *stream)
187 {
188 BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream);
189 return bt_stream_borrow_trace_inline(stream);
190 }
191
192 BT_EXPORT
193 const struct bt_trace *bt_stream_borrow_trace_const(
194 const struct bt_stream *stream)
195 {
196 return bt_stream_borrow_trace((void *) stream);
197 }
198
199 BT_EXPORT
200 const char *bt_stream_get_name(const struct bt_stream *stream)
201 {
202 BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream);
203 return stream->name;
204 }
205
206 BT_EXPORT
207 enum bt_stream_set_name_status bt_stream_set_name(struct bt_stream *stream,
208 const char *name)
209 {
210 BT_ASSERT_PRE_NO_ERROR();
211 BT_ASSERT_PRE_STREAM_NON_NULL(stream);
212 BT_ASSERT_PRE_NAME_NON_NULL(name);
213 BT_ASSERT_PRE_DEV_STREAM_HOT(stream);
214 g_free(stream->name);
215 stream->name = g_strdup(name);
216 BT_LIB_LOGD("Set stream's name: %!+s", stream);
217 return BT_FUNC_STATUS_OK;
218 }
219
220 BT_EXPORT
221 uint64_t bt_stream_get_id(const struct bt_stream *stream)
222 {
223 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream);
224 return stream->id;
225 }
226
227 void _bt_stream_freeze(const struct bt_stream *stream)
228 {
229 BT_ASSERT(stream);
230 BT_LIB_LOGD("Freezing stream's user attributes: %!+v",
231 stream->user_attributes);
232 bt_value_freeze(stream->user_attributes);
233 BT_LIB_LOGD("Freezing stream: %!+s", stream);
234 ((struct bt_stream *) stream)->frozen = true;
235 }
236
237 BT_EXPORT
238 const struct bt_value *bt_stream_borrow_user_attributes_const(
239 const struct bt_stream *stream)
240 {
241 BT_ASSERT_PRE_DEV_STREAM_NON_NULL(stream);
242 return stream->user_attributes;
243 }
244
245 BT_EXPORT
246 struct bt_value *bt_stream_borrow_user_attributes(struct bt_stream *stream)
247 {
248 return (void *) bt_stream_borrow_user_attributes_const((void *) stream);
249 }
250
251 BT_EXPORT
252 void bt_stream_set_user_attributes(struct bt_stream *stream,
253 const struct bt_value *user_attributes)
254 {
255 BT_ASSERT_PRE_STREAM_NON_NULL(stream);
256 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
257 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
258 BT_ASSERT_PRE_DEV_STREAM_HOT(stream);
259 bt_object_put_ref_no_null_check(stream->user_attributes);
260 stream->user_attributes = (void *) user_attributes;
261 bt_object_get_ref_no_null_check(stream->user_attributes);
262 }
263
264 BT_EXPORT
265 void bt_stream_get_ref(const struct bt_stream *stream)
266 {
267 bt_object_get_ref(stream);
268 }
269
270 BT_EXPORT
271 void bt_stream_put_ref(const struct bt_stream *stream)
272 {
273 bt_object_put_ref(stream);
274 }
This page took 0.036992 seconds and 5 git commands to generate.