| 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 | |
| 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 | |
| 32 | #include "common/macros.h" |
| 33 | #include "lib/object.h" |
| 34 | #include "common/assert.h" |
| 35 | #include <babeltrace2/graph/graph.h> |
| 36 | #include <babeltrace2/graph/message-const.h> |
| 37 | #include <babeltrace2/trace-ir/stream.h> |
| 38 | #include "lib/object-pool.h" |
| 39 | #include <babeltrace2/types.h> |
| 40 | |
| 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 | |
| 46 | typedef struct bt_stream *(*get_stream_func)( |
| 47 | struct bt_message *message); |
| 48 | |
| 49 | struct bt_message { |
| 50 | struct bt_object base; |
| 51 | enum bt_message_type type; |
| 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 | |
| 58 | #define BT_ASSERT_PRE_MSG_IS_TYPE(_msg, _type) \ |
| 59 | BT_ASSERT_PRE(((struct bt_message *) (_msg))->type == (_type), \ |
| 60 | "Message has the wrong type: expected-type=%s, " \ |
| 61 | "%![msg-]+n", bt_message_type_string(_type), \ |
| 62 | (_msg)) |
| 63 | |
| 64 | BT_HIDDEN |
| 65 | void bt_message_init(struct bt_message *message, |
| 66 | enum bt_message_type type, |
| 67 | bt_object_release_func release, |
| 68 | struct bt_graph *graph); |
| 69 | |
| 70 | static inline |
| 71 | void bt_message_reset(struct bt_message *message) |
| 72 | { |
| 73 | BT_ASSERT(message); |
| 74 | |
| 75 | #ifdef BT_DEV_MODE |
| 76 | message->frozen = BT_FALSE; |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | static inline |
| 81 | struct bt_message *bt_message_create_from_pool( |
| 82 | struct bt_object_pool *pool, struct bt_graph *graph) |
| 83 | { |
| 84 | struct bt_message *msg = bt_object_pool_create_object(pool); |
| 85 | |
| 86 | if (G_UNLIKELY(!msg)) { |
| 87 | BT_LIB_LOGE_APPEND_CAUSE( |
| 88 | "Cannot allocate one message from message pool: " |
| 89 | "%![pool-]+o, %![graph-]+g", pool, graph); |
| 90 | goto error; |
| 91 | } |
| 92 | |
| 93 | if (G_LIKELY(!msg->graph)) { |
| 94 | msg->graph = graph; |
| 95 | } |
| 96 | |
| 97 | goto end; |
| 98 | |
| 99 | error: |
| 100 | BT_ASSERT(!msg); |
| 101 | |
| 102 | end: |
| 103 | return msg; |
| 104 | } |
| 105 | |
| 106 | static inline void _bt_message_freeze(struct bt_message *message) |
| 107 | { |
| 108 | message->frozen = BT_TRUE; |
| 109 | } |
| 110 | |
| 111 | BT_HIDDEN |
| 112 | void bt_message_unlink_graph(struct bt_message *msg); |
| 113 | |
| 114 | #ifdef BT_DEV_MODE |
| 115 | # define bt_message_freeze _bt_message_freeze |
| 116 | #else |
| 117 | # define bt_message_freeze(_x) |
| 118 | #endif /* BT_DEV_MODE */ |
| 119 | |
| 120 | static inline |
| 121 | const char *bt_message_type_string(enum bt_message_type type) |
| 122 | { |
| 123 | switch (type) { |
| 124 | case BT_MESSAGE_TYPE_EVENT: |
| 125 | return "BT_MESSAGE_TYPE_EVENT"; |
| 126 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
| 127 | return "BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY"; |
| 128 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 129 | return "BT_MESSAGE_TYPE_STREAM_BEGINNING"; |
| 130 | case BT_MESSAGE_TYPE_STREAM_END: |
| 131 | return "BT_MESSAGE_TYPE_STREAM_END"; |
| 132 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 133 | return "BT_MESSAGE_TYPE_PACKET_BEGINNING"; |
| 134 | case BT_MESSAGE_TYPE_PACKET_END: |
| 135 | return "BT_MESSAGE_TYPE_PACKET_END"; |
| 136 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: |
| 137 | return "BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING"; |
| 138 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: |
| 139 | return "BT_MESSAGE_TYPE_STREAM_ACTIVITY_END"; |
| 140 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
| 141 | return "BT_MESSAGE_TYPE_DISCARDED_EVENTS"; |
| 142 | default: |
| 143 | return "(unknown)"; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #endif /* BABELTRACE_GRAPH_MESSAGE_MESSAGE_INTERNAL_H */ |