2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/condition/condition-internal.h>
9 #include <lttng/condition/buffer-usage-internal.h>
10 #include <lttng/condition/session-consumed-size-internal.h>
11 #include <lttng/condition/session-rotation-internal.h>
12 #include <common/macros.h>
13 #include <common/error.h>
14 #include <common/dynamic-buffer.h>
15 #include <common/buffer-view.h>
19 enum lttng_condition_type
lttng_condition_get_type(
20 const struct lttng_condition
*condition
)
22 return condition
? condition
->type
: LTTNG_CONDITION_TYPE_UNKNOWN
;
25 void lttng_condition_destroy(struct lttng_condition
*condition
)
31 assert(condition
->destroy
);
32 condition
->destroy(condition
);
36 bool lttng_condition_validate(const struct lttng_condition
*condition
)
45 if (!condition
->validate
) {
46 /* Sub-class guarantees that it can never be invalid. */
51 valid
= condition
->validate(condition
);
57 int lttng_condition_serialize(const struct lttng_condition
*condition
,
58 struct lttng_dynamic_buffer
*buf
)
61 struct lttng_condition_comm condition_comm
= { 0 };
68 condition_comm
.condition_type
= (int8_t) condition
->type
;
70 ret
= lttng_dynamic_buffer_append(buf
, &condition_comm
,
71 sizeof(condition_comm
));
76 ret
= condition
->serialize(condition
, buf
);
85 bool lttng_condition_is_equal(const struct lttng_condition
*a
,
86 const struct lttng_condition
*b
)
88 bool is_equal
= false;
94 if (a
->type
!= b
->type
) {
103 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
109 ssize_t
lttng_condition_create_from_buffer(
110 const struct lttng_buffer_view
*buffer
,
111 struct lttng_condition
**condition
)
113 ssize_t ret
, condition_size
= 0;
114 const struct lttng_condition_comm
*condition_comm
;
115 condition_create_from_buffer_cb create_from_buffer
= NULL
;
117 if (!buffer
|| !condition
) {
122 DBG("Deserializing condition from buffer");
123 condition_comm
= (const struct lttng_condition_comm
*) buffer
->data
;
124 condition_size
+= sizeof(*condition_comm
);
126 switch ((enum lttng_condition_type
) condition_comm
->condition_type
) {
127 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
128 create_from_buffer
= lttng_condition_buffer_usage_low_create_from_buffer
;
130 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
131 create_from_buffer
= lttng_condition_buffer_usage_high_create_from_buffer
;
133 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
134 create_from_buffer
= lttng_condition_session_consumed_size_create_from_buffer
;
136 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
137 create_from_buffer
= lttng_condition_session_rotation_ongoing_create_from_buffer
;
139 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
140 create_from_buffer
= lttng_condition_session_rotation_completed_create_from_buffer
;
143 ERR("Attempted to create condition of unknown type (%i)",
144 (int) condition_comm
->condition_type
);
149 if (create_from_buffer
) {
150 const struct lttng_buffer_view view
=
151 lttng_buffer_view_from_view(buffer
,
152 sizeof(*condition_comm
), -1);
154 ret
= create_from_buffer(&view
, condition
);
158 condition_size
+= ret
;
164 ret
= condition_size
;
170 void lttng_condition_init(struct lttng_condition
*condition
,
171 enum lttng_condition_type type
)
173 condition
->type
= type
;