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 <common/macros.h>
21 #include <common/error.h>
22 #include <common/dynamic-buffer.h>
23 #include <common/buffer-view.h>
27 enum lttng_condition_type
lttng_condition_get_type(
28 const struct lttng_condition
*condition
)
30 return condition
? condition
->type
: LTTNG_CONDITION_TYPE_UNKNOWN
;
33 void lttng_condition_destroy(struct lttng_condition
*condition
)
39 assert(condition
->destroy
);
40 condition
->destroy(condition
);
44 bool lttng_condition_validate(const struct lttng_condition
*condition
)
53 if (!condition
->validate
) {
54 /* Sub-class guarantees that it can never be invalid. */
59 valid
= condition
->validate(condition
);
65 ssize_t
lttng_condition_serialize(const struct lttng_condition
*condition
,
68 ssize_t ret
, condition_size
;
69 struct lttng_condition_comm condition_comm
= {
70 .condition_type
= (int8_t) (condition
?
71 condition
->type
: LTTNG_CONDITION_TYPE_UNKNOWN
)
79 ret
= sizeof(struct lttng_condition_comm
);
81 memcpy(buf
, &condition_comm
, ret
);
85 condition_size
= condition
->serialize(condition
, buf
);
86 if (condition_size
< 0) {
90 ret
+= condition_size
;
96 bool lttng_condition_is_equal(const struct lttng_condition
*a
,
97 const struct lttng_condition
*b
)
99 bool is_equal
= false;
105 if (a
->type
!= b
->type
) {
109 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
115 ssize_t
lttng_condition_create_from_buffer(
116 const struct lttng_buffer_view
*buffer
,
117 struct lttng_condition
**condition
)
119 ssize_t ret
, condition_size
= 0;
120 const struct lttng_condition_comm
*condition_comm
;
121 condition_create_from_buffer_cb create_from_buffer
= NULL
;
123 if (!buffer
|| !condition
) {
128 DBG("Deserializing condition from buffer");
129 condition_comm
= (const struct lttng_condition_comm
*) buffer
->data
;
130 condition_size
+= sizeof(*condition_comm
);
132 switch ((enum lttng_condition_type
) condition_comm
->condition_type
) {
133 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
134 create_from_buffer
= lttng_condition_buffer_usage_low_create_from_buffer
;
136 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
137 create_from_buffer
= lttng_condition_buffer_usage_high_create_from_buffer
;
140 ERR("Attempted to create condition of unknown type (%i)",
141 (int) condition_comm
->condition_type
);
146 if (create_from_buffer
) {
147 const struct lttng_buffer_view view
=
148 lttng_buffer_view_from_view(buffer
,
149 sizeof(*condition_comm
), -1);
151 ret
= create_from_buffer(&view
, condition
);
155 condition_size
+= ret
;
161 ret
= condition_size
;
167 void lttng_condition_init(struct lttng_condition
*condition
,
168 enum lttng_condition_type type
)
170 condition
->type
= type
;