2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <lttng/condition/condition-internal.h>
19 #include <lttng/condition/buffer-usage-internal.h>
20 #include <lttng/condition/session-consumed-size-internal.h>
21 #include <common/macros.h>
22 #include <common/error.h>
23 #include <common/dynamic-buffer.h>
24 #include <common/buffer-view.h>
28 enum lttng_condition_type
lttng_condition_get_type(
29 const struct lttng_condition
*condition
)
31 return condition
? condition
->type
: LTTNG_CONDITION_TYPE_UNKNOWN
;
34 void lttng_condition_destroy(struct lttng_condition
*condition
)
40 assert(condition
->destroy
);
41 condition
->destroy(condition
);
45 bool lttng_condition_validate(const struct lttng_condition
*condition
)
54 if (!condition
->validate
) {
55 /* Sub-class guarantees that it can never be invalid. */
60 valid
= condition
->validate(condition
);
66 int lttng_condition_serialize(const struct lttng_condition
*condition
,
67 struct lttng_dynamic_buffer
*buf
)
70 struct lttng_condition_comm condition_comm
= { 0 };
77 condition_comm
.condition_type
= (int8_t) condition
->type
;
79 ret
= lttng_dynamic_buffer_append(buf
, &condition_comm
,
80 sizeof(condition_comm
));
85 ret
= condition
->serialize(condition
, buf
);
94 bool lttng_condition_is_equal(const struct lttng_condition
*a
,
95 const struct lttng_condition
*b
)
97 bool is_equal
= false;
103 if (a
->type
!= b
->type
) {
107 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
113 ssize_t
lttng_condition_create_from_buffer(
114 const struct lttng_buffer_view
*buffer
,
115 struct lttng_condition
**condition
)
117 ssize_t ret
, condition_size
= 0;
118 const struct lttng_condition_comm
*condition_comm
;
119 condition_create_from_buffer_cb create_from_buffer
= NULL
;
121 if (!buffer
|| !condition
) {
126 DBG("Deserializing condition from buffer");
127 condition_comm
= (const struct lttng_condition_comm
*) buffer
->data
;
128 condition_size
+= sizeof(*condition_comm
);
130 switch ((enum lttng_condition_type
) condition_comm
->condition_type
) {
131 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
132 create_from_buffer
= lttng_condition_buffer_usage_low_create_from_buffer
;
134 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
135 create_from_buffer
= lttng_condition_buffer_usage_high_create_from_buffer
;
137 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
138 create_from_buffer
= lttng_condition_session_consumed_size_create_from_buffer
;
141 ERR("Attempted to create condition of unknown type (%i)",
142 (int) condition_comm
->condition_type
);
147 if (create_from_buffer
) {
148 const struct lttng_buffer_view view
=
149 lttng_buffer_view_from_view(buffer
,
150 sizeof(*condition_comm
), -1);
152 ret
= create_from_buffer(&view
, condition
);
156 condition_size
+= ret
;
162 ret
= condition_size
;
168 void lttng_condition_init(struct lttng_condition
*condition
,
169 enum lttng_condition_type type
)
171 condition
->type
= type
;