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