ust registry: Refactor representation of nested types
[lttng-tools.git] / src / common / action.c
CommitLineData
a58c490f 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
a58c490f 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
a58c490f 5 *
a58c490f
JG
6 */
7
8#include <lttng/action/action-internal.h>
9#include <lttng/action/notify-internal.h>
10#include <common/error.h>
11#include <assert.h>
12
13enum lttng_action_type lttng_action_get_type(struct lttng_action *action)
14{
15 return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN;
16}
17
9b63a4aa
JG
18LTTNG_HIDDEN
19enum lttng_action_type lttng_action_get_type_const(
20 const struct lttng_action *action)
21{
22 return action->type;
23}
24
a58c490f
JG
25void lttng_action_destroy(struct lttng_action *action)
26{
27 if (!action) {
28 return;
29 }
30
31 assert(action->destroy);
32 action->destroy(action);
33}
34
35LTTNG_HIDDEN
36bool lttng_action_validate(struct lttng_action *action)
37{
38 bool valid;
39
40 if (!action) {
41 valid = false;
42 goto end;
43 }
44
45 if (!action->validate) {
46 /* Sub-class guarantees that it can never be invalid. */
47 valid = true;
48 goto end;
49 }
50
51 valid = action->validate(action);
52end:
53 return valid;
54}
55
56LTTNG_HIDDEN
3647288f
JG
57int lttng_action_serialize(struct lttng_action *action,
58 struct lttng_dynamic_buffer *buf)
a58c490f 59{
3647288f
JG
60 int ret;
61 struct lttng_action_comm action_comm = {
62 .action_type = (int8_t) action->type,
63 };
64
65 ret = lttng_dynamic_buffer_append(buf, &action_comm,
66 sizeof(action_comm));
67 if (ret) {
a58c490f
JG
68 goto end;
69 }
70
3647288f
JG
71 ret = action->serialize(action, buf);
72 if (ret) {
a58c490f
JG
73 goto end;
74 }
a58c490f
JG
75end:
76 return ret;
77}
78
79LTTNG_HIDDEN
80ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view,
81 struct lttng_action **_action)
82{
83 ssize_t ret, action_size = sizeof(struct lttng_action_comm);
84 struct lttng_action *action;
85 const struct lttng_action_comm *action_comm;
86
87 if (!view || !_action) {
88 ret = -1;
89 goto end;
90 }
91
92 action_comm = (const struct lttng_action_comm *) view->data;
93 DBG("Deserializing action from buffer");
94 switch (action_comm->action_type) {
95 case LTTNG_ACTION_TYPE_NOTIFY:
96 action = lttng_action_notify_create();
97 break;
98 default:
99 ret = -1;
100 goto end;
101 }
102
103 if (!action) {
104 ret = -1;
105 goto end;
106 }
107 ret = action_size;
108 *_action = action;
109end:
110 return ret;
111}
This page took 0.056796 seconds and 5 git commands to generate.