lib: add precond. check for begin <= end on pkt./ev. disc. msg. creation
[babeltrace.git] / src / lib / graph / message / message.h
CommitLineData
d6e69534 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
d6e69534
PP
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
d6e69534
PP
6 */
7
0235b0db
MJ
8#ifndef BABELTRACE_GRAPH_MESSAGE_MESSAGE_INTERNAL_H
9#define BABELTRACE_GRAPH_MESSAGE_MESSAGE_INTERNAL_H
10
870631a2
PP
11/* Protection: this file uses BT_LIB_LOG*() macros directly */
12#ifndef BT_LIB_LOG_SUPPORTED
13# error Please include "lib/logging.h" before including this file.
14#endif
15
91d81473 16#include "common/macros.h"
578e048b
MJ
17#include "lib/object.h"
18#include "common/assert.h"
3fadfbc0 19#include <babeltrace2/graph/graph.h>
43c59509 20#include <babeltrace2/graph/message.h>
3fadfbc0 21#include <babeltrace2/trace-ir/stream.h>
578e048b 22#include "lib/object-pool.h"
3fadfbc0 23#include <babeltrace2/types.h>
d6e69534 24
f137d351
PP
25/* Protection: this file uses BT_LIB_LOG*() macros directly */
26#ifndef BT_LIB_LOG_SUPPORTED
27# error Please include "lib/logging.h" before including this file.
28#endif
29
d6e69534
PP
30typedef struct bt_stream *(*get_stream_func)(
31 struct bt_message *message);
32
33struct bt_message {
34 struct bt_object base;
35 enum bt_message_type type;
d6e69534
PP
36 bt_bool frozen;
37
38 /* Owned by this; keeps the graph alive while the msg. is alive */
39 struct bt_graph *graph;
40};
41
bdb288b3
PP
42#define _BT_ASSERT_PRE_MSG_IS_TYPE_COND(_msg, _type) \
43 (((struct bt_message *) (_msg))->type == (_type))
44
45#define _BT_ASSERT_PRE_MSG_IS_TYPE_FMT \
46 "Message has the wrong type: expected-type=%s, %![msg-]+n"
47
48#define BT_ASSERT_PRE_MSG_IS_TYPE(_msg, _type) \
49 BT_ASSERT_PRE( \
50 _BT_ASSERT_PRE_MSG_IS_TYPE_COND((_msg), (_type)), \
51 _BT_ASSERT_PRE_MSG_IS_TYPE_FMT, \
52 bt_message_type_string(_type), (_msg))
53
54#define BT_ASSERT_PRE_DEV_MSG_IS_TYPE(_msg, _type) \
55 BT_ASSERT_PRE_DEV( \
56 _BT_ASSERT_PRE_MSG_IS_TYPE_COND((_msg), (_type)), \
57 _BT_ASSERT_PRE_MSG_IS_TYPE_FMT, \
58 bt_message_type_string(_type), (_msg))
d6e69534 59
5d9ef4cb
SM
60#define BT_ASSERT_PRE_BEGIN_LE_END(_msg_iter, _begin, _end) \
61 BT_ASSERT_PRE( \
62 _begin <= _end, \
63 "Beginning default clock snapshot value is greater " \
64 "than end default clock snapshot value: " \
65 "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64 ", " \
66 "%![msg-iter-]i", \
67 _begin, _end, _msg_iter);
68
d6e69534
PP
69BT_HIDDEN
70void bt_message_init(struct bt_message *message,
71 enum bt_message_type type,
72 bt_object_release_func release,
73 struct bt_graph *graph);
74
75static inline
76void bt_message_reset(struct bt_message *message)
77{
98b15851 78 BT_ASSERT_DBG(message);
d6e69534
PP
79
80#ifdef BT_DEV_MODE
81 message->frozen = BT_FALSE;
d6e69534
PP
82#endif
83}
84
85static inline
86struct bt_message *bt_message_create_from_pool(
87 struct bt_object_pool *pool, struct bt_graph *graph)
88{
89 struct bt_message *msg = bt_object_pool_create_object(pool);
90
91d81473 91 if (G_UNLIKELY(!msg)) {
870631a2
PP
92 BT_LIB_LOGE_APPEND_CAUSE(
93 "Cannot allocate one message from message pool: "
d6e69534 94 "%![pool-]+o, %![graph-]+g", pool, graph);
d6e69534
PP
95 goto error;
96 }
97
91d81473 98 if (G_LIKELY(!msg->graph)) {
d6e69534
PP
99 msg->graph = graph;
100 }
101
102 goto end;
103
104error:
105 BT_ASSERT(!msg);
106
107end:
108 return msg;
109}
110
111static inline void _bt_message_freeze(struct bt_message *message)
112{
113 message->frozen = BT_TRUE;
114}
115
116BT_HIDDEN
117void bt_message_unlink_graph(struct bt_message *msg);
118
119#ifdef BT_DEV_MODE
120# define bt_message_freeze _bt_message_freeze
121#else
122# define bt_message_freeze(_x)
123#endif /* BT_DEV_MODE */
124
125static inline
126const char *bt_message_type_string(enum bt_message_type type)
127{
128 switch (type) {
129 case BT_MESSAGE_TYPE_EVENT:
8a432889 130 return "EVENT";
b9fd9cbb 131 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
8a432889 132 return "MESSAGE_ITERATOR_INACTIVITY";
d6e69534 133 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
8a432889 134 return "STREAM_BEGINNING";
d6e69534 135 case BT_MESSAGE_TYPE_STREAM_END:
8a432889 136 return "STREAM_END";
d6e69534 137 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
8a432889 138 return "PACKET_BEGINNING";
d6e69534 139 case BT_MESSAGE_TYPE_PACKET_END:
8a432889 140 return "PACKET_END";
4c833281 141 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
8a432889 142 return "DISCARDED_EVENTS";
d6e69534
PP
143 default:
144 return "(unknown)";
145 }
146}
147
148#endif /* BABELTRACE_GRAPH_MESSAGE_MESSAGE_INTERNAL_H */
This page took 0.068091 seconds and 4 git commands to generate.