Commit | Line | Data |
---|---|---|
4c5c8fc8 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> | |
a365f2d0 | 19 | #include <lttng/condition/buffer-usage-internal.h> |
4c5c8fc8 JG |
20 | #include <common/macros.h> |
21 | #include <stdbool.h> | |
22 | #include <assert.h> | |
23 | ||
24 | enum lttng_condition_type lttng_condition_get_type( | |
25 | struct lttng_condition *condition) | |
26 | { | |
27 | return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN; | |
28 | } | |
29 | ||
30 | void lttng_condition_destroy(struct lttng_condition *condition) | |
31 | { | |
32 | if (!condition) { | |
33 | return; | |
34 | } | |
35 | ||
36 | assert(condition->destroy); | |
37 | condition->destroy(condition); | |
38 | } | |
39 | ||
40 | LTTNG_HIDDEN | |
41 | bool lttng_condition_validate(struct lttng_condition *condition) | |
42 | { | |
43 | bool valid; | |
44 | ||
45 | if (!condition) { | |
46 | valid = false; | |
47 | goto end; | |
48 | } | |
49 | ||
50 | if (!condition->validate) { | |
51 | /* Sub-class guarantees that it can never be invalid. */ | |
52 | valid = true; | |
53 | goto end; | |
54 | } | |
55 | ||
56 | valid = condition->validate(condition); | |
57 | end: | |
58 | return valid; | |
59 | } | |
2a38b722 JG |
60 | |
61 | LTTNG_HIDDEN | |
62 | ssize_t lttng_condition_serialize(struct lttng_condition *condition, char *buf) | |
63 | { | |
64 | ssize_t ret, condition_size; | |
65 | struct lttng_condition_comm condition_comm; | |
66 | ||
67 | if (!condition) { | |
68 | ret = -1; | |
69 | goto end; | |
70 | } | |
71 | ||
72 | condition_comm.condition_type = (int8_t) condition->type; | |
73 | ret = sizeof(struct lttng_condition_comm); | |
74 | if (buf) { | |
75 | memcpy(buf, &condition_comm, ret); | |
76 | buf += ret; | |
77 | } | |
78 | ||
79 | condition_size = condition->serialize(condition, buf); | |
80 | if (condition_size < 0) { | |
81 | ret = condition_size; | |
82 | goto end; | |
83 | } | |
84 | ret += condition_size; | |
85 | end: | |
86 | return ret; | |
87 | } | |
a365f2d0 JG |
88 | |
89 | LTTNG_HIDDEN | |
90 | ssize_t lttng_condition_create_from_buffer(const char *buf, | |
91 | struct lttng_condition **condition) | |
92 | { | |
93 | ssize_t ret, condition_size = 0; | |
94 | struct lttng_condition_comm *condition_comm = | |
95 | (struct lttng_condition_comm *) buf; | |
96 | ||
97 | if (!buf || !condition) { | |
98 | ret = -1; | |
99 | goto end; | |
100 | } | |
101 | ||
102 | condition_size += sizeof(*condition_comm); | |
103 | buf += condition_size; | |
104 | ||
105 | switch (condition_comm->condition_type) { | |
106 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
107 | ret = lttng_condition_buffer_usage_low_create_from_buffer(buf, | |
108 | condition); | |
109 | if (ret < 0) { | |
110 | goto end; | |
111 | } | |
112 | condition_size += ret; | |
113 | break; | |
114 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
115 | ret = lttng_condition_buffer_usage_high_create_from_buffer(buf, | |
116 | condition); | |
117 | if (ret < 0) { | |
118 | goto end; | |
119 | } | |
120 | condition_size += ret; | |
121 | break; | |
122 | default: | |
123 | ret = -1; | |
124 | goto end; | |
125 | } | |
126 | ret = condition_size; | |
127 | end: | |
128 | return ret; | |
129 | } |