Cleanup: enforce const-correctness in notification thread
[lttng-tools.git] / src / common / evaluation.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/evaluation-internal.h>
19#include <lttng/condition/buffer-usage-internal.h>
e8360425 20#include <lttng/condition/session-consumed-size-internal.h>
c19092cd 21#include <lttng/condition/session-rotation-internal.h>
a58c490f
JG
22#include <common/macros.h>
23#include <common/error.h>
24#include <stdbool.h>
25#include <assert.h>
26
c19092cd
JG
27LTTNG_HIDDEN
28void lttng_evaluation_init(struct lttng_evaluation *evaluation,
29 enum lttng_condition_type type)
30{
31 evaluation->type = type;
32}
33
a58c490f 34LTTNG_HIDDEN
9b63a4aa 35int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation,
3647288f 36 struct lttng_dynamic_buffer *buf)
a58c490f 37{
3647288f 38 int ret;
db315d75
JG
39 struct lttng_evaluation_comm evaluation_comm = {
40 .type = (int8_t) evaluation->type
41 };
a58c490f 42
3647288f
JG
43 ret = lttng_dynamic_buffer_append(buf, &evaluation_comm,
44 sizeof(evaluation_comm));
45 if (ret) {
46 goto end;
a58c490f 47 }
a58c490f
JG
48
49 if (evaluation->serialize) {
3647288f
JG
50 ret = evaluation->serialize(evaluation, buf);
51 if (ret) {
a58c490f
JG
52 goto end;
53 }
a58c490f 54 }
a58c490f
JG
55end:
56 return ret;
57}
58
59LTTNG_HIDDEN
60ssize_t lttng_evaluation_create_from_buffer(
61 const struct lttng_buffer_view *src_view,
62 struct lttng_evaluation **evaluation)
63{
64 ssize_t ret, evaluation_size = 0;
65 const struct lttng_evaluation_comm *evaluation_comm;
66 const struct lttng_buffer_view evaluation_view =
67 lttng_buffer_view_from_view(src_view,
68 sizeof(*evaluation_comm), -1);
69
70 if (!src_view || !evaluation) {
71 ret = -1;
72 goto end;
73 }
74
75 evaluation_comm = (const struct lttng_evaluation_comm *) src_view->data;
76 evaluation_size += sizeof(*evaluation_comm);
77
78 switch ((enum lttng_condition_type) evaluation_comm->type) {
79 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
80 ret = lttng_evaluation_buffer_usage_low_create_from_buffer(
81 &evaluation_view, evaluation);
82 if (ret < 0) {
83 goto end;
84 }
85 evaluation_size += ret;
86 break;
87 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
88 ret = lttng_evaluation_buffer_usage_high_create_from_buffer(
89 &evaluation_view, evaluation);
90 if (ret < 0) {
91 goto end;
92 }
93 evaluation_size += ret;
94 break;
e8360425
JD
95 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
96 ret = lttng_evaluation_session_consumed_size_create_from_buffer(
97 &evaluation_view, evaluation);
98 if (ret < 0) {
99 goto end;
100 }
101 evaluation_size += ret;
102 break;
c19092cd
JG
103 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
104 ret = lttng_evaluation_session_rotation_ongoing_create_from_buffer(
105 &evaluation_view, evaluation);
106 if (ret < 0) {
107 goto end;
108 }
109 evaluation_size += ret;
110 break;
111 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
112 ret = lttng_evaluation_session_rotation_completed_create_from_buffer(
113 &evaluation_view, evaluation);
114 if (ret < 0) {
115 goto end;
116 }
117 evaluation_size += ret;
118 break;
a58c490f
JG
119 default:
120 ERR("Attempted to create evaluation of unknown type (%i)",
121 (int) evaluation_comm->type);
122 ret = -1;
123 goto end;
124 }
125 ret = evaluation_size;
126end:
127 return ret;
128}
129
130enum lttng_condition_type lttng_evaluation_get_type(
131 const struct lttng_evaluation *evaluation)
132{
133 return evaluation ? evaluation->type : LTTNG_CONDITION_TYPE_UNKNOWN;
134}
135
136void lttng_evaluation_destroy(struct lttng_evaluation *evaluation)
137{
138 if (!evaluation) {
139 return;
140 }
141
142 assert(evaluation->destroy);
143 evaluation->destroy(evaluation);
144}
This page took 0.030486 seconds and 4 git commands to generate.