Clean-up: location lookup destroy can be type-agnostic
[lttng-tools.git] / src / common / notification.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/notification/notification-internal.h>
19#include <lttng/condition/condition-internal.h>
20#include <lttng/condition/evaluation-internal.h>
21#include <lttng/condition/condition.h>
22#include <lttng/condition/evaluation.h>
23#include <assert.h>
24
25LTTNG_HIDDEN
26struct lttng_notification *lttng_notification_create(
27 struct lttng_condition *condition,
28 struct lttng_evaluation *evaluation)
29{
30 struct lttng_notification *notification = NULL;
31
32 if (!condition || !evaluation) {
33 goto end;
34 }
35
36 notification = zmalloc(sizeof(struct lttng_notification));
37 if (!notification) {
38 goto end;
39 }
40
41 notification->condition = condition;
42 notification->evaluation = evaluation;
a58c490f
JG
43end:
44 return notification;
45}
46
47LTTNG_HIDDEN
9b63a4aa 48int lttng_notification_serialize(const struct lttng_notification *notification,
3647288f 49 struct lttng_dynamic_buffer *buf)
a58c490f 50{
3647288f
JG
51 int ret;
52 size_t header_offset, size_before_payload;
1e9e2705 53 struct lttng_notification_comm notification_comm = { 0 };
3647288f 54 struct lttng_notification_comm *header;
a58c490f 55
3647288f
JG
56 header_offset = buf->size;
57 ret = lttng_dynamic_buffer_append(buf, &notification_comm,
58 sizeof(notification_comm));
a58c490f 59
3647288f
JG
60 size_before_payload = buf->size;
61 ret = lttng_condition_serialize(notification->condition,
62 buf);
63 if (ret) {
a58c490f
JG
64 goto end;
65 }
a58c490f 66
3647288f
JG
67 ret = lttng_evaluation_serialize(notification->evaluation, buf);
68 if (ret) {
a58c490f
JG
69 goto end;
70 }
a58c490f 71
3647288f
JG
72 /* Update payload size. */
73 header = (struct lttng_notification_comm *) ((char *) buf->data + header_offset);
74 header->length = (uint32_t) (buf->size - size_before_payload);
a58c490f
JG
75end:
76 return ret;
77
78}
79
80LTTNG_HIDDEN
81ssize_t lttng_notification_create_from_buffer(
82 const struct lttng_buffer_view *src_view,
83 struct lttng_notification **notification)
84{
85 ssize_t ret, notification_size = 0, condition_size, evaluation_size;
86 const struct lttng_notification_comm *notification_comm;
87 struct lttng_condition *condition;
88 struct lttng_evaluation *evaluation;
89 struct lttng_buffer_view condition_view;
90 struct lttng_buffer_view evaluation_view;
91
92 if (!src_view || !notification) {
93 ret = -1;
94 goto end;
95 }
96
97 notification_comm =
98 (const struct lttng_notification_comm *) src_view->data;
99 notification_size += sizeof(*notification_comm);
100
101 /* struct lttng_condition */
102 condition_view = lttng_buffer_view_from_view(src_view,
103 sizeof(*notification_comm), -1);
104 condition_size = lttng_condition_create_from_buffer(&condition_view,
105 &condition);
106 if (condition_size < 0) {
107 ret = condition_size;
108 goto end;
109 }
110 notification_size += condition_size;
111
112 /* struct lttng_evaluation */
113 evaluation_view = lttng_buffer_view_from_view(&condition_view,
114 condition_size, -1);
115 evaluation_size = lttng_evaluation_create_from_buffer(&evaluation_view,
116 &evaluation);
117 if (evaluation_size < 0) {
118 ret = evaluation_size;
119 goto end;
120 }
121 notification_size += evaluation_size;
122
123 /* Unexpected size of inner-elements; the buffer is corrupted. */
124 if ((ssize_t) notification_comm->length !=
125 condition_size + evaluation_size) {
126 ret = -1;
127 goto error;
128 }
129
130 *notification = lttng_notification_create(condition, evaluation);
131 if (!*notification) {
132 ret = -1;
133 goto error;
134 }
135 ret = notification_size;
a58c490f
JG
136end:
137 return ret;
138error:
139 lttng_condition_destroy(condition);
140 lttng_evaluation_destroy(evaluation);
141 return ret;
142}
143
144void lttng_notification_destroy(struct lttng_notification *notification)
145{
146 if (!notification) {
147 return;
148 }
149
9b63a4aa
JG
150 lttng_condition_destroy(notification->condition);
151 lttng_evaluation_destroy(notification->evaluation);
a58c490f
JG
152 free(notification);
153}
154
155const struct lttng_condition *lttng_notification_get_condition(
156 struct lttng_notification *notification)
157{
158 return notification ? notification->condition : NULL;
159}
160
161const struct lttng_evaluation *lttng_notification_get_evaluation(
162 struct lttng_notification *notification)
163{
164 return notification ? notification->evaluation : NULL;
165}
This page took 0.049788 seconds and 4 git commands to generate.