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>
25 enum lttng_condition_type
lttng_condition_get_type(
26 struct lttng_condition
*condition
)
28 return condition
? condition
->type
: LTTNG_CONDITION_TYPE_UNKNOWN
;
31 void lttng_condition_destroy(struct lttng_condition
*condition
)
37 assert(condition
->destroy
);
38 condition
->destroy(condition
);
42 bool lttng_condition_validate(struct lttng_condition
*condition
)
51 if (!condition
->validate
) {
52 /* Sub-class guarantees that it can never be invalid. */
57 valid
= condition
->validate(condition
);
63 ssize_t
lttng_condition_serialize(struct lttng_condition
*condition
, char *buf
)
65 ssize_t ret
, condition_size
;
66 struct lttng_condition_comm condition_comm
;
73 condition_comm
.condition_type
= (int8_t) condition
->type
;
74 ret
= sizeof(struct lttng_condition_comm
);
76 memcpy(buf
, &condition_comm
, ret
);
80 condition_size
= condition
->serialize(condition
, buf
);
81 if (condition_size
< 0) {
85 ret
+= condition_size
;
91 ssize_t
lttng_condition_create_from_buffer(const char *buf
,
92 struct lttng_condition
**condition
)
94 ssize_t ret
, condition_size
= 0;
95 struct lttng_condition_comm
*condition_comm
=
96 (struct lttng_condition_comm
*) buf
;
98 if (!buf
|| !condition
) {
103 DBG("Deserializing condition from buffer");
104 condition_size
+= sizeof(*condition_comm
);
105 buf
+= condition_size
;
107 switch (condition_comm
->condition_type
) {
108 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
109 ret
= lttng_condition_buffer_usage_low_create_from_buffer(buf
,
114 condition_size
+= ret
;
116 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
117 ret
= lttng_condition_buffer_usage_high_create_from_buffer(buf
,
122 condition_size
+= ret
;
125 ERR("Attempted to create condition of unknown type (%i)",
126 (int) condition_comm
->condition_type
);
130 ret
= condition_size
;