From: Jérémie Galarneau Date: Fri, 20 Mar 2015 01:41:29 +0000 (-0400) Subject: ir: add weak reference to parent stream to bt_ctf_event X-Git-Tag: v2.0.0-pre1~1289 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=123fbdeca6a37f9de585488caf67f9d70eeb5e8a ir: add weak reference to parent stream to bt_ctf_event Signed-off-by: Jérémie Galarneau --- diff --git a/formats/ctf/ir/event.c b/formats/ctf/ir/event.c index d8def625..6fbfb446 100644 --- a/formats/ctf/ir/event.c +++ b/formats/ctf/ir/event.c @@ -1185,3 +1185,25 @@ end: } return ret; } + +BT_HIDDEN +int bt_ctf_event_set_stream(struct bt_ctf_event *event, + struct bt_ctf_stream *stream) +{ + int ret = 0; + + if (!event) { + ret = -1; + goto end; + } + + if (event->stream && stream) { + /* Already attached to a stream */ + ret = -1; + goto end; + } + + event->stream = stream; +end: + return ret; +} diff --git a/formats/ctf/ir/stream.c b/formats/ctf/ir/stream.c index 858cd98b..219f6c18 100644 --- a/formats/ctf/ir/stream.c +++ b/formats/ctf/ir/stream.c @@ -246,6 +246,13 @@ end: return ret; } +static +void put_event(struct bt_ctf_event *event) +{ + bt_ctf_event_set_stream(event, NULL); + bt_ctf_event_put(event); +} + BT_HIDDEN struct bt_ctf_stream *bt_ctf_stream_create( struct bt_ctf_stream_class *stream_class, @@ -297,7 +304,7 @@ struct bt_ctf_stream *bt_ctf_stream_create( stream->stream_class = stream_class; bt_ctf_stream_class_get(stream_class); stream->events = g_ptr_array_new_with_free_func( - (GDestroyNotify) bt_ctf_event_put); + (GDestroyNotify) put_event); if (!stream->events) { goto error_destroy; } @@ -510,6 +517,13 @@ int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, goto end; } + ret = bt_ctf_event_set_stream(event, stream); + if (ret) { + /* Event was already associated to a stream */ + ret = -1; + goto end; + } + ret = bt_ctf_event_populate_event_header(event); if (ret) { goto end; @@ -543,6 +557,9 @@ int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, g_ptr_array_add(stream->event_contexts, event_context_copy); } end: + if (ret) { + (void) bt_ctf_event_set_stream(event, NULL); + } return ret; } diff --git a/include/babeltrace/ctf-ir/event-internal.h b/include/babeltrace/ctf-ir/event-internal.h index 171e7c78..2a172a94 100644 --- a/include/babeltrace/ctf-ir/event-internal.h +++ b/include/babeltrace/ctf-ir/event-internal.h @@ -33,6 +33,8 @@ #include #include #include +#include +#include #include #define BT_CTF_EVENT_CLASS_ATTR_ID_INDEX 0 @@ -41,7 +43,10 @@ struct bt_ctf_event_class { struct bt_ctf_ref ref_count; struct bt_object *attributes; - /* An event class does not have ownership of a stream class */ + /* + * Weak reference; an event class does not have ownership of a + * stream class. + */ struct bt_ctf_stream_class *stream_class; /* Structure type containing the event's context */ struct bt_ctf_field_type *context; @@ -53,6 +58,8 @@ struct bt_ctf_event_class { struct bt_ctf_event { struct bt_ctf_ref ref_count; struct bt_ctf_event_class *event_class; + /* Weak reference; an event does not have ownership of a stream */ + struct bt_ctf_stream *stream; struct bt_ctf_field *event_header; struct bt_ctf_field *context_payload; struct bt_ctf_field *fields_payload; @@ -81,6 +88,10 @@ BT_HIDDEN int bt_ctf_event_serialize(struct bt_ctf_event *event, struct ctf_stream_pos *pos); +BT_HIDDEN +int bt_ctf_event_set_stream(struct bt_ctf_event *event, + struct bt_ctf_stream *stream); + /* * Attempt to populate the "id" and "timestamp" fields of the event header if * they are present, unset and their types are integers.