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