Document libbabeltrace2's C API
[babeltrace.git] / src / lib / graph / message / message.h
CommitLineData
d6e69534
PP
1#ifndef BABELTRACE_GRAPH_MESSAGE_MESSAGE_INTERNAL_H
2#define BABELTRACE_GRAPH_MESSAGE_MESSAGE_INTERNAL_H
3
4/*
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
870631a2
PP
27/* Protection: this file uses BT_LIB_LOG*() macros directly */
28#ifndef BT_LIB_LOG_SUPPORTED
29# error Please include "lib/logging.h" before including this file.
30#endif
31
91d81473 32#include "common/macros.h"
578e048b
MJ
33#include "lib/object.h"
34#include "common/assert.h"
3fadfbc0 35#include <babeltrace2/graph/graph.h>
43c59509 36#include <babeltrace2/graph/message.h>
3fadfbc0 37#include <babeltrace2/trace-ir/stream.h>
578e048b 38#include "lib/object-pool.h"
3fadfbc0 39#include <babeltrace2/types.h>
d6e69534 40
f137d351
PP
41/* Protection: this file uses BT_LIB_LOG*() macros directly */
42#ifndef BT_LIB_LOG_SUPPORTED
43# error Please include "lib/logging.h" before including this file.
44#endif
45
d6e69534
PP
46typedef struct bt_stream *(*get_stream_func)(
47 struct bt_message *message);
48
49struct bt_message {
50 struct bt_object base;
51 enum bt_message_type type;
d6e69534
PP
52 bt_bool frozen;
53
54 /* Owned by this; keeps the graph alive while the msg. is alive */
55 struct bt_graph *graph;
56};
57
bdb288b3
PP
58#define _BT_ASSERT_PRE_MSG_IS_TYPE_COND(_msg, _type) \
59 (((struct bt_message *) (_msg))->type == (_type))
60
61#define _BT_ASSERT_PRE_MSG_IS_TYPE_FMT \
62 "Message has the wrong type: expected-type=%s, %![msg-]+n"
63
64#define BT_ASSERT_PRE_MSG_IS_TYPE(_msg, _type) \
65 BT_ASSERT_PRE( \
66 _BT_ASSERT_PRE_MSG_IS_TYPE_COND((_msg), (_type)), \
67 _BT_ASSERT_PRE_MSG_IS_TYPE_FMT, \
68 bt_message_type_string(_type), (_msg))
69
70#define BT_ASSERT_PRE_DEV_MSG_IS_TYPE(_msg, _type) \
71 BT_ASSERT_PRE_DEV( \
72 _BT_ASSERT_PRE_MSG_IS_TYPE_COND((_msg), (_type)), \
73 _BT_ASSERT_PRE_MSG_IS_TYPE_FMT, \
74 bt_message_type_string(_type), (_msg))
d6e69534
PP
75
76BT_HIDDEN
77void bt_message_init(struct bt_message *message,
78 enum bt_message_type type,
79 bt_object_release_func release,
80 struct bt_graph *graph);
81
82static inline
83void bt_message_reset(struct bt_message *message)
84{
98b15851 85 BT_ASSERT_DBG(message);
d6e69534
PP
86
87#ifdef BT_DEV_MODE
88 message->frozen = BT_FALSE;
d6e69534
PP
89#endif
90}
91
92static inline
93struct bt_message *bt_message_create_from_pool(
94 struct bt_object_pool *pool, struct bt_graph *graph)
95{
96 struct bt_message *msg = bt_object_pool_create_object(pool);
97
91d81473 98 if (G_UNLIKELY(!msg)) {
870631a2
PP
99 BT_LIB_LOGE_APPEND_CAUSE(
100 "Cannot allocate one message from message pool: "
d6e69534 101 "%![pool-]+o, %![graph-]+g", pool, graph);
d6e69534
PP
102 goto error;
103 }
104
91d81473 105 if (G_LIKELY(!msg->graph)) {
d6e69534
PP
106 msg->graph = graph;
107 }
108
109 goto end;
110
111error:
112 BT_ASSERT(!msg);
113
114end:
115 return msg;
116}
117
118static inline void _bt_message_freeze(struct bt_message *message)
119{
120 message->frozen = BT_TRUE;
121}
122
123BT_HIDDEN
124void bt_message_unlink_graph(struct bt_message *msg);
125
126#ifdef BT_DEV_MODE
127# define bt_message_freeze _bt_message_freeze
128#else
129# define bt_message_freeze(_x)
130#endif /* BT_DEV_MODE */
131
132static inline
133const char *bt_message_type_string(enum bt_message_type type)
134{
135 switch (type) {
136 case BT_MESSAGE_TYPE_EVENT:
8a432889 137 return "EVENT";
b9fd9cbb 138 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
8a432889 139 return "MESSAGE_ITERATOR_INACTIVITY";
d6e69534 140 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
8a432889 141 return "STREAM_BEGINNING";
d6e69534 142 case BT_MESSAGE_TYPE_STREAM_END:
8a432889 143 return "STREAM_END";
d6e69534 144 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
8a432889 145 return "PACKET_BEGINNING";
d6e69534 146 case BT_MESSAGE_TYPE_PACKET_END:
8a432889 147 return "PACKET_END";
4c833281 148 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
8a432889 149 return "DISCARDED_EVENTS";
d6e69534
PP
150 default:
151 return "(unknown)";
152 }
153}
154
155#endif /* BABELTRACE_GRAPH_MESSAGE_MESSAGE_INTERNAL_H */
This page took 0.064541 seconds and 4 git commands to generate.