Commit | Line | Data |
---|---|---|
fabb0b6c 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> | |
3c47cdaf | 19 | #include <lttng/condition/buffer-usage-internal.h> |
fabb0b6c | 20 | #include <common/macros.h> |
3c47cdaf | 21 | #include <common/error.h> |
fabb0b6c JG |
22 | #include <stdbool.h> |
23 | #include <assert.h> | |
24 | ||
3c47cdaf JG |
25 | LTTNG_HIDDEN |
26 | ssize_t lttng_evaluation_serialize(struct lttng_evaluation *evaluation, | |
27 | char *buf) | |
28 | { | |
29 | ssize_t ret, offset = 0; | |
30 | struct lttng_evaluation_comm evaluation_comm; | |
31 | ||
32 | evaluation_comm.type = (int8_t) evaluation->type; | |
33 | if (buf) { | |
34 | memcpy(buf, &evaluation_comm, sizeof(evaluation_comm)); | |
35 | } | |
36 | offset += sizeof(evaluation_comm); | |
37 | ||
38 | if (evaluation->serialize) { | |
39 | ret = evaluation->serialize(evaluation, | |
40 | buf ? (buf + offset) : NULL); | |
41 | if (ret < 0) { | |
42 | goto end; | |
43 | } | |
44 | offset += ret; | |
45 | } | |
46 | ||
47 | ret = offset; | |
48 | end: | |
49 | return ret; | |
50 | } | |
51 | ||
52 | LTTNG_HIDDEN | |
53 | ssize_t lttng_evaluation_create_from_buffer(const char *buf, | |
54 | struct lttng_evaluation **evaluation) | |
55 | { | |
56 | ssize_t ret, evaluation_size = 0; | |
57 | struct lttng_evaluation_comm *evaluation_comm = | |
58 | (struct lttng_evaluation_comm *) buf; | |
59 | ||
60 | if (!buf || !evaluation) { | |
61 | ret = -1; | |
62 | goto end; | |
63 | } | |
64 | ||
65 | evaluation_size += sizeof(*evaluation_comm); | |
66 | buf += evaluation_size; | |
67 | ||
68 | switch ((enum lttng_condition_type) evaluation_comm->type) { | |
69 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
70 | ret = lttng_evaluation_buffer_usage_low_create_from_buffer(buf, | |
71 | evaluation); | |
72 | if (ret < 0) { | |
73 | goto end; | |
74 | } | |
75 | evaluation_size += ret; | |
76 | break; | |
77 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
78 | ret = lttng_evaluation_buffer_usage_high_create_from_buffer(buf, | |
79 | evaluation); | |
80 | if (ret < 0) { | |
81 | goto end; | |
82 | } | |
83 | evaluation_size += ret; | |
84 | break; | |
85 | default: | |
86 | ERR("Attempted to create evaluation of unknown type (%i)", | |
87 | (int) evaluation_comm->type); | |
88 | ret = -1; | |
89 | goto end; | |
90 | } | |
91 | ret = evaluation_size; | |
92 | end: | |
93 | return ret; | |
94 | } | |
95 | ||
fabb0b6c JG |
96 | enum lttng_condition_type lttng_evaluation_get_type( |
97 | struct lttng_evaluation *evaluation) | |
98 | { | |
99 | return evaluation ? evaluation->type : LTTNG_CONDITION_TYPE_UNKNOWN; | |
100 | } | |
101 | ||
102 | void lttng_evaluation_destroy(struct lttng_evaluation *evaluation) | |
103 | { | |
104 | if (!evaluation) { | |
105 | return; | |
106 | } | |
107 | ||
108 | assert(evaluation->destroy); | |
109 | evaluation->destroy(evaluation); | |
110 | } |