Commit | Line | Data |
---|---|---|
a58c490f JG |
1 | /* |
2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
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. | |
7 | * | |
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 | |
11 | * for more details. | |
12 | * | |
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 | |
16 | */ | |
17 | ||
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> | |
24 | #include <stdbool.h> | |
25 | #include <assert.h> | |
26 | ||
27 | enum lttng_condition_type lttng_condition_get_type( | |
28 | const struct lttng_condition *condition) | |
29 | { | |
30 | return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN; | |
31 | } | |
32 | ||
33 | void lttng_condition_destroy(struct lttng_condition *condition) | |
34 | { | |
35 | if (!condition) { | |
36 | return; | |
37 | } | |
38 | ||
39 | assert(condition->destroy); | |
40 | condition->destroy(condition); | |
41 | } | |
42 | ||
43 | LTTNG_HIDDEN | |
44 | bool lttng_condition_validate(const struct lttng_condition *condition) | |
45 | { | |
46 | bool valid; | |
47 | ||
48 | if (!condition) { | |
49 | valid = false; | |
50 | goto end; | |
51 | } | |
52 | ||
53 | if (!condition->validate) { | |
54 | /* Sub-class guarantees that it can never be invalid. */ | |
55 | valid = true; | |
56 | goto end; | |
57 | } | |
58 | ||
59 | valid = condition->validate(condition); | |
60 | end: | |
61 | return valid; | |
62 | } | |
63 | ||
64 | LTTNG_HIDDEN | |
65 | ssize_t lttng_condition_serialize(const struct lttng_condition *condition, | |
66 | char *buf) | |
67 | { | |
68 | ssize_t ret, condition_size; | |
09e58909 JG |
69 | struct lttng_condition_comm condition_comm = { |
70 | .condition_type = (int8_t) condition->type | |
71 | }; | |
a58c490f JG |
72 | |
73 | if (!condition) { | |
74 | ret = -1; | |
75 | goto end; | |
76 | } | |
77 | ||
a58c490f JG |
78 | ret = sizeof(struct lttng_condition_comm); |
79 | if (buf) { | |
80 | memcpy(buf, &condition_comm, ret); | |
81 | buf += ret; | |
82 | } | |
83 | ||
84 | condition_size = condition->serialize(condition, buf); | |
85 | if (condition_size < 0) { | |
86 | ret = condition_size; | |
87 | goto end; | |
88 | } | |
89 | ret += condition_size; | |
90 | end: | |
91 | return ret; | |
92 | } | |
93 | ||
94 | LTTNG_HIDDEN | |
95 | bool lttng_condition_is_equal(const struct lttng_condition *a, | |
96 | const struct lttng_condition *b) | |
97 | { | |
98 | bool is_equal = false; | |
99 | ||
100 | if (!a || !b) { | |
101 | goto end; | |
102 | } | |
103 | ||
104 | if (a->type != b->type) { | |
105 | goto end; | |
106 | } | |
107 | ||
108 | is_equal = a->equal ? a->equal(a, b) : true; | |
109 | end: | |
110 | return is_equal; | |
111 | } | |
112 | ||
113 | LTTNG_HIDDEN | |
114 | ssize_t lttng_condition_create_from_buffer( | |
115 | const struct lttng_buffer_view *buffer, | |
116 | struct lttng_condition **condition) | |
117 | { | |
118 | ssize_t ret, condition_size = 0; | |
119 | const struct lttng_condition_comm *condition_comm; | |
120 | condition_create_from_buffer_cb create_from_buffer = NULL; | |
121 | ||
122 | if (!buffer || !condition) { | |
123 | ret = -1; | |
124 | goto end; | |
125 | } | |
126 | ||
127 | DBG("Deserializing condition from buffer"); | |
128 | condition_comm = (const struct lttng_condition_comm *) buffer->data; | |
129 | condition_size += sizeof(*condition_comm); | |
130 | ||
131 | switch ((enum lttng_condition_type) condition_comm->condition_type) { | |
132 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
133 | create_from_buffer = lttng_condition_buffer_usage_low_create_from_buffer; | |
134 | break; | |
135 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
136 | create_from_buffer = lttng_condition_buffer_usage_high_create_from_buffer; | |
137 | break; | |
138 | default: | |
139 | ERR("Attempted to create condition of unknown type (%i)", | |
140 | (int) condition_comm->condition_type); | |
141 | ret = -1; | |
142 | goto end; | |
143 | } | |
144 | ||
145 | if (create_from_buffer) { | |
146 | const struct lttng_buffer_view view = | |
147 | lttng_buffer_view_from_view(buffer, | |
148 | sizeof(*condition_comm), -1); | |
149 | ||
150 | ret = create_from_buffer(&view, condition); | |
151 | if (ret < 0) { | |
152 | goto end; | |
153 | } | |
154 | condition_size += ret; | |
155 | ||
156 | } else { | |
157 | abort(); | |
158 | } | |
159 | ||
160 | ret = condition_size; | |
161 | end: | |
162 | return ret; | |
163 | } | |
164 | ||
165 | LTTNG_HIDDEN | |
166 | void lttng_condition_init(struct lttng_condition *condition, | |
167 | enum lttng_condition_type type) | |
168 | { | |
169 | condition->type = type; | |
170 | } |