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 | 20 | #include <common/macros.h> |
45fdf591 | 21 | #include <common/error.h> |
4c5c8fc8 JG |
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 | } | |
2a38b722 JG |
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 | } | |
a365f2d0 JG |
89 | |
90 | LTTNG_HIDDEN | |
91 | ssize_t lttng_condition_create_from_buffer(const char *buf, | |
92 | struct lttng_condition **condition) | |
93 | { | |
94 | ssize_t ret, condition_size = 0; | |
95 | struct lttng_condition_comm *condition_comm = | |
96 | (struct lttng_condition_comm *) buf; | |
97 | ||
98 | if (!buf || !condition) { | |
99 | ret = -1; | |
100 | goto end; | |
101 | } | |
102 | ||
45fdf591 | 103 | DBG("Deserializing condition from buffer"); |
a365f2d0 JG |
104 | condition_size += sizeof(*condition_comm); |
105 | buf += condition_size; | |
106 | ||
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, | |
110 | condition); | |
111 | if (ret < 0) { | |
112 | goto end; | |
113 | } | |
114 | condition_size += ret; | |
115 | break; | |
116 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
117 | ret = lttng_condition_buffer_usage_high_create_from_buffer(buf, | |
118 | condition); | |
119 | if (ret < 0) { | |
120 | goto end; | |
121 | } | |
122 | condition_size += ret; | |
123 | break; | |
124 | default: | |
45fdf591 JG |
125 | ERR("Attempted to create condition of unknown type (%i)", |
126 | (int) condition_comm->condition_type); | |
a365f2d0 JG |
127 | ret = -1; |
128 | goto end; | |
129 | } | |
130 | ret = condition_size; | |
131 | end: | |
132 | return ret; | |
133 | } | |
c254fb11 JG |
134 | |
135 | LTTNG_HIDDEN | |
136 | void lttng_condition_init(struct lttng_condition *condition, | |
137 | enum lttng_condition_type type) | |
138 | { | |
139 | condition->type = type; | |
140 | CDS_INIT_LIST_HEAD(&condition->list_node); | |
141 | } |