Use the dynamic buffer to serialize notification objects
[lttng-tools.git] / src / common / condition.c
CommitLineData
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>
e8360425 20#include <lttng/condition/session-consumed-size-internal.h>
a58c490f
JG
21#include <common/macros.h>
22#include <common/error.h>
23#include <common/dynamic-buffer.h>
24#include <common/buffer-view.h>
25#include <stdbool.h>
26#include <assert.h>
27
28enum lttng_condition_type lttng_condition_get_type(
29 const struct lttng_condition *condition)
30{
31 return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN;
32}
33
34void lttng_condition_destroy(struct lttng_condition *condition)
35{
36 if (!condition) {
37 return;
38 }
39
40 assert(condition->destroy);
41 condition->destroy(condition);
42}
43
44LTTNG_HIDDEN
45bool lttng_condition_validate(const struct lttng_condition *condition)
46{
47 bool valid;
48
49 if (!condition) {
50 valid = false;
51 goto end;
52 }
53
54 if (!condition->validate) {
55 /* Sub-class guarantees that it can never be invalid. */
56 valid = true;
57 goto end;
58 }
59
60 valid = condition->validate(condition);
61end:
62 return valid;
63}
64
65LTTNG_HIDDEN
3647288f
JG
66int lttng_condition_serialize(const struct lttng_condition *condition,
67 struct lttng_dynamic_buffer *buf)
a58c490f 68{
3647288f
JG
69 int ret;
70 struct lttng_condition_comm condition_comm = { 0 };
a58c490f
JG
71
72 if (!condition) {
73 ret = -1;
74 goto end;
75 }
76
3647288f
JG
77 condition_comm.condition_type = (int8_t) condition->type;
78
79 ret = lttng_dynamic_buffer_append(buf, &condition_comm,
80 sizeof(condition_comm));
81 if (ret) {
82 goto end;
a58c490f
JG
83 }
84
3647288f
JG
85 ret = condition->serialize(condition, buf);
86 if (ret) {
a58c490f
JG
87 goto end;
88 }
a58c490f
JG
89end:
90 return ret;
91}
92
93LTTNG_HIDDEN
94bool lttng_condition_is_equal(const struct lttng_condition *a,
95 const struct lttng_condition *b)
96{
97 bool is_equal = false;
98
99 if (!a || !b) {
100 goto end;
101 }
102
103 if (a->type != b->type) {
104 goto end;
105 }
106
107 is_equal = a->equal ? a->equal(a, b) : true;
108end:
109 return is_equal;
110}
111
112LTTNG_HIDDEN
113ssize_t lttng_condition_create_from_buffer(
114 const struct lttng_buffer_view *buffer,
115 struct lttng_condition **condition)
116{
117 ssize_t ret, condition_size = 0;
118 const struct lttng_condition_comm *condition_comm;
119 condition_create_from_buffer_cb create_from_buffer = NULL;
120
121 if (!buffer || !condition) {
122 ret = -1;
123 goto end;
124 }
125
126 DBG("Deserializing condition from buffer");
127 condition_comm = (const struct lttng_condition_comm *) buffer->data;
128 condition_size += sizeof(*condition_comm);
129
130 switch ((enum lttng_condition_type) condition_comm->condition_type) {
131 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
132 create_from_buffer = lttng_condition_buffer_usage_low_create_from_buffer;
133 break;
134 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
135 create_from_buffer = lttng_condition_buffer_usage_high_create_from_buffer;
136 break;
e8360425
JD
137 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
138 create_from_buffer = lttng_condition_session_consumed_size_create_from_buffer;
139 break;
a58c490f
JG
140 default:
141 ERR("Attempted to create condition of unknown type (%i)",
142 (int) condition_comm->condition_type);
143 ret = -1;
144 goto end;
145 }
146
147 if (create_from_buffer) {
148 const struct lttng_buffer_view view =
149 lttng_buffer_view_from_view(buffer,
150 sizeof(*condition_comm), -1);
151
152 ret = create_from_buffer(&view, condition);
153 if (ret < 0) {
154 goto end;
155 }
156 condition_size += ret;
157
158 } else {
159 abort();
160 }
161
162 ret = condition_size;
163end:
164 return ret;
165}
166
167LTTNG_HIDDEN
168void lttng_condition_init(struct lttng_condition *condition,
169 enum lttng_condition_type type)
170{
171 condition->type = type;
172}
This page took 0.030874 seconds and 4 git commands to generate.