lib: make public reference count functions have strict types
[babeltrace.git] / plugins / utils / trimmer / copy.c
1 /*
2 * copy.c
3 *
4 * Babeltrace Copy Trace Structure
5 *
6 * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com>
7 *
8 * Author: Julien Desfossez <jdesfossez@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT-COPY"
30 #include "logging.h"
31
32 #include <babeltrace/assert-internal.h>
33 #include <babeltrace/babeltrace.h>
34
35 #include <ctfcopytrace.h>
36 #include "iterator.h"
37
38 static
39 const struct bt_packet *lookup_packet(struct trimmer_iterator *trim_it,
40 const struct bt_packet *packet)
41 {
42 return (const struct bt_packet *) g_hash_table_lookup(
43 trim_it->packet_map,
44 (gpointer) packet);
45 }
46
47 static
48 const struct bt_packet *insert_new_packet(struct trimmer_iterator *trim_it,
49 const struct bt_packet *packet,
50 const struct bt_stream *stream)
51 {
52 const struct bt_packet *writer_packet = NULL;
53 int ret;
54
55 BT_LOGD_STR("Inserting a new packet.");
56 writer_packet = bt_packet_create(stream);
57 if (!writer_packet) {
58 BT_LOGE_STR("Failed to create a new packet.");
59 goto error;
60 }
61
62 ret = ctf_packet_copy_header(trim_it->err, packet, writer_packet);
63 if (ret) {
64 BT_LOGE_STR("Failed to copy packet header.");
65 goto error;
66 }
67
68 g_hash_table_insert(trim_it->packet_map, (gpointer) packet,
69 writer_packet);
70 goto end;
71
72 error:
73 BT_PACKET_PUT_REF_AND_RESET(writer_packet);
74 end:
75 return writer_packet;
76 }
77
78 BT_HIDDEN
79 enum bt_component_status update_packet_context_field(FILE *err,
80 const struct bt_packet *writer_packet,
81 const char *name, int64_t value)
82 {
83 enum bt_component_status ret;
84 const struct bt_field *packet_context = NULL, *writer_packet_context = NULL;
85 const struct bt_field_class *struct_class = NULL, *field_class = NULL;
86 const struct bt_field *field = NULL, *writer_field = NULL;
87 int nr_fields, i, int_ret;
88
89 BT_LOGD("Updating packet context field: name=%s", name);
90 packet_context = bt_packet_get_context(writer_packet);
91 BT_ASSERT(packet_context);
92
93 struct_class = bt_field_get_class(packet_context);
94 BT_ASSERT(struct_class);
95
96 writer_packet_context = bt_packet_get_context(writer_packet);
97 BT_ASSERT(writer_packet_context);
98
99 nr_fields = bt_field_class_structure_get_field_count(struct_class);
100 for (i = 0; i < nr_fields; i++) {
101 const char *field_name;
102
103 field = bt_field_structure_get_field_by_index(
104 packet_context, i);
105 if (!field) {
106 BT_LOGE("Failed to get field in packet-context: field-name=\"%s\"",
107 name);
108 goto error;
109 }
110 if (bt_field_class_structure_get_field_by_index(struct_class,
111 &field_name, &field_class, i) < 0) {
112 BT_LOGE("Failed to get field: field-name=\"%s\"",
113 field_name);
114 goto error;
115 }
116 if (strcmp(field_name, name)) {
117 BT_OBJECT_PUT_REF_AND_RESET(field_class);
118 BT_OBJECT_PUT_REF_AND_RESET(field);
119 continue;
120 }
121 if (bt_field_class_id(field_class) !=
122 BT_FIELD_CLASS_TYPE_INTEGER) {
123 BT_LOGE("Expecting an integer for this field: field-name=\"%s\"",
124 name);
125 goto error;
126 }
127
128 writer_field = bt_field_structure_get_field_by_name(writer_packet_context,
129 field_name);
130 BT_ASSERT(writer_field);
131
132 int_ret = bt_field_unsigned_integer_set_value(writer_field, value);
133 BT_ASSERT(int_ret == 0);
134
135 BT_OBJECT_PUT_REF_AND_RESET(writer_field);
136 BT_OBJECT_PUT_REF_AND_RESET(field_class);
137 BT_OBJECT_PUT_REF_AND_RESET(field);
138 }
139
140 ret = BT_COMPONENT_STATUS_OK;
141 goto end;
142
143 error:
144 bt_object_put_ref(writer_field);
145 bt_object_put_ref(field_class);
146 bt_object_put_ref(field);
147 ret = BT_COMPONENT_STATUS_ERROR;
148 end:
149 bt_object_put_ref(struct_class);
150 bt_object_put_ref(packet_context);
151 return ret;
152 }
153
154 BT_HIDDEN
155 const struct bt_packet *trimmer_new_packet(
156 struct trimmer_iterator *trim_it,
157 const struct bt_packet *packet)
158 {
159 const struct bt_stream *stream = NULL;
160 const struct bt_packet *writer_packet = NULL;
161 int int_ret;
162
163 stream = bt_packet_get_stream(packet);
164 BT_ASSERT(stream);
165
166 /*
167 * If a packet was already opened, close it and remove it from
168 * the HT.
169 */
170 writer_packet = lookup_packet(trim_it, packet);
171 if (writer_packet) {
172 g_hash_table_remove(trim_it->packet_map, packet);
173 BT_PACKET_PUT_REF_AND_RESET(writer_packet);
174 }
175
176 writer_packet = insert_new_packet(trim_it, packet, stream);
177 if (!writer_packet) {
178 BT_LOGE_STR("Failed to insert new packet.");
179 goto error;
180 }
181 bt_packet_get_ref(writer_packet);
182
183 int_ret = ctf_packet_copy_context(trim_it->err, packet,
184 stream, writer_packet);
185 if (int_ret < 0) {
186 BT_LOGE_STR("Failed to copy packet context.");
187 goto error;
188 }
189
190 goto end;
191
192 error:
193 BT_PACKET_PUT_REF_AND_RESET(writer_packet);
194 end:
195 bt_stream_put_ref(stream);
196 return writer_packet;
197 }
198
199 BT_HIDDEN
200 const struct bt_packet *trimmer_close_packet(
201 struct trimmer_iterator *trim_it,
202 const struct bt_packet *packet)
203 {
204 const struct bt_packet *writer_packet = NULL;
205
206 writer_packet = lookup_packet(trim_it, packet);
207 if (!writer_packet) {
208 BT_LOGE_STR("Failed to find existing packet.");
209 goto end;
210 }
211
212 g_hash_table_remove(trim_it->packet_map, packet);
213
214 end:
215 return writer_packet;
216 }
217
218 BT_HIDDEN
219 const struct bt_event *trimmer_output_event(
220 struct trimmer_iterator *trim_it,
221 const struct bt_event *event)
222 {
223 const struct bt_event_class *event_class = NULL;
224 const struct bt_event *writer_event = NULL;
225 const struct bt_packet *packet = NULL, *writer_packet = NULL;
226 const char *event_name;
227 int int_ret;
228
229 event_class = bt_event_get_class(event);
230 BT_ASSERT(event_class);
231
232 event_name = bt_event_class_get_name(event_class);
233
234 writer_event = ctf_copy_event(trim_it->err, event, event_class, false);
235 if (!writer_event) {
236 BT_LOGE("Failed to copy event: event-class-name=\"%s\", event-name=\"%s\"",
237 bt_event_class_get_name(event_class),
238 event_name);
239 goto error;
240 }
241
242 packet = bt_event_get_packet(event);
243 BT_ASSERT(packet);
244
245 writer_packet = lookup_packet(trim_it, packet);
246 if (!writer_packet) {
247 BT_LOGE_STR("Failed to find existing packet.");
248 goto error;
249 }
250 bt_packet_get_ref(writer_packet);
251
252 int_ret = bt_event_set_packet(writer_event, writer_packet);
253 if (int_ret < 0) {
254 BT_LOGE("Failed to append event: event-class-name=\"%s\", event-name=\"%s\"",
255 bt_event_class_get_name(event_class),
256 event_name);
257 goto error;
258 }
259
260 /* We keep the reference on the writer_event to create a notification. */
261 goto end;
262
263 error:
264 BT_OBJECT_PUT_REF_AND_RESET(writer_event);
265 end:
266 bt_packet_put_ref(writer_packet);
267 bt_packet_put_ref(packet);
268 bt_event_class_put_ref(event_class);
269 return writer_event;
270 }
This page took 0.03589 seconds and 4 git commands to generate.