Commit | Line | Data |
---|---|---|
b09a5592 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 | ||
85e7137b | 27 | #include "common/macros.h" |
57952005 MJ |
28 | #include "lib/object.h" |
29 | #include "common/assert.h" | |
71c5da58 MJ |
30 | #include <babeltrace2/graph/graph.h> |
31 | #include <babeltrace2/graph/message-const.h> | |
32 | #include <babeltrace2/trace-ir/stream.h> | |
57952005 | 33 | #include "lib/object-pool.h" |
71c5da58 | 34 | #include <babeltrace2/types.h> |
b09a5592 | 35 | |
8a14e80e PP |
36 | /* Protection: this file uses BT_LIB_LOG*() macros directly */ |
37 | #ifndef BT_LIB_LOG_SUPPORTED | |
38 | # error Please include "lib/logging.h" before including this file. | |
39 | #endif | |
40 | ||
b09a5592 PP |
41 | typedef struct bt_stream *(*get_stream_func)( |
42 | struct bt_message *message); | |
43 | ||
44 | struct bt_message { | |
45 | struct bt_object base; | |
46 | enum bt_message_type type; | |
b09a5592 PP |
47 | bt_bool frozen; |
48 | ||
49 | /* Owned by this; keeps the graph alive while the msg. is alive */ | |
50 | struct bt_graph *graph; | |
51 | }; | |
52 | ||
53 | #define BT_ASSERT_PRE_MSG_IS_TYPE(_msg, _type) \ | |
54 | BT_ASSERT_PRE(((struct bt_message *) (_msg))->type == (_type), \ | |
55 | "Message has the wrong type: expected-type=%s, " \ | |
56 | "%![msg-]+n", bt_message_type_string(_type), \ | |
57 | (_msg)) | |
58 | ||
59 | BT_HIDDEN | |
60 | void bt_message_init(struct bt_message *message, | |
61 | enum bt_message_type type, | |
62 | bt_object_release_func release, | |
63 | struct bt_graph *graph); | |
64 | ||
65 | static inline | |
66 | void bt_message_reset(struct bt_message *message) | |
67 | { | |
68 | BT_ASSERT(message); | |
69 | ||
70 | #ifdef BT_DEV_MODE | |
71 | message->frozen = BT_FALSE; | |
b09a5592 PP |
72 | #endif |
73 | } | |
74 | ||
75 | static inline | |
76 | struct bt_message *bt_message_create_from_pool( | |
77 | struct bt_object_pool *pool, struct bt_graph *graph) | |
78 | { | |
79 | struct bt_message *msg = bt_object_pool_create_object(pool); | |
80 | ||
85e7137b | 81 | if (G_UNLIKELY(!msg)) { |
b09a5592 PP |
82 | BT_LIB_LOGE("Cannot allocate one message from message pool: " |
83 | "%![pool-]+o, %![graph-]+g", pool, graph); | |
b09a5592 PP |
84 | goto error; |
85 | } | |
86 | ||
85e7137b | 87 | if (G_LIKELY(!msg->graph)) { |
b09a5592 PP |
88 | msg->graph = graph; |
89 | } | |
90 | ||
91 | goto end; | |
92 | ||
93 | error: | |
94 | BT_ASSERT(!msg); | |
95 | ||
96 | end: | |
97 | return msg; | |
98 | } | |
99 | ||
100 | static inline void _bt_message_freeze(struct bt_message *message) | |
101 | { | |
102 | message->frozen = BT_TRUE; | |
103 | } | |
104 | ||
105 | BT_HIDDEN | |
106 | void bt_message_unlink_graph(struct bt_message *msg); | |
107 | ||
108 | #ifdef BT_DEV_MODE | |
109 | # define bt_message_freeze _bt_message_freeze | |
110 | #else | |
111 | # define bt_message_freeze(_x) | |
112 | #endif /* BT_DEV_MODE */ | |
113 | ||
114 | static inline | |
115 | const char *bt_message_type_string(enum bt_message_type type) | |
116 | { | |
117 | switch (type) { | |
118 | case BT_MESSAGE_TYPE_EVENT: | |
119 | return "BT_MESSAGE_TYPE_EVENT"; | |
40bf6fd0 FD |
120 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
121 | return "BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY"; | |
b09a5592 PP |
122 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
123 | return "BT_MESSAGE_TYPE_STREAM_BEGINNING"; | |
124 | case BT_MESSAGE_TYPE_STREAM_END: | |
125 | return "BT_MESSAGE_TYPE_STREAM_END"; | |
126 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
127 | return "BT_MESSAGE_TYPE_PACKET_BEGINNING"; | |
128 | case BT_MESSAGE_TYPE_PACKET_END: | |
129 | return "BT_MESSAGE_TYPE_PACKET_END"; | |
a1f053a9 PP |
130 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: |
131 | return "BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING"; | |
132 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: | |
133 | return "BT_MESSAGE_TYPE_STREAM_ACTIVITY_END"; | |
8f1a1bbb PP |
134 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
135 | return "BT_MESSAGE_TYPE_DISCARDED_EVENTS"; | |
b09a5592 PP |
136 | default: |
137 | return "(unknown)"; | |
138 | } | |
139 | } | |
140 | ||
141 | #endif /* BABELTRACE_GRAPH_MESSAGE_MESSAGE_INTERNAL_H */ |