2 * Copyright (C) 2021 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/action/path-internal.h>
10 struct lttng_action_path_comm
{
15 struct lttng_action_path
*lttng_action_path_create(
16 const uint64_t *indexes
, size_t index_count
)
20 struct lttng_action_path
*path
= NULL
;
22 if (!indexes
&& index_count
> 0) {
26 path
= zmalloc(sizeof(*path
));
31 lttng_dynamic_array_init(&path
->indexes
, sizeof(uint64_t), NULL
);
33 for (i
= 0; i
< index_count
; i
++) {
34 ret
= lttng_dynamic_array_add_element(
35 &path
->indexes
, &indexes
[i
]);
43 lttng_action_path_destroy(path
);
49 enum lttng_action_path_status
lttng_action_path_get_index_count(
50 const struct lttng_action_path
*path
, size_t *index_count
)
52 enum lttng_action_path_status status
;
54 if (!path
|| !index_count
) {
55 status
= LTTNG_ACTION_PATH_STATUS_INVALID
;
59 *index_count
= lttng_dynamic_array_get_count(&path
->indexes
);
60 status
= LTTNG_ACTION_PATH_STATUS_OK
;
65 enum lttng_action_path_status
lttng_action_path_get_index_at_index(
66 const struct lttng_action_path
*path
,
70 enum lttng_action_path_status status
;
72 if (!path
|| !out_index
||
73 path_index
>= lttng_dynamic_array_get_count(
75 status
= LTTNG_ACTION_PATH_STATUS_INVALID
;
79 *out_index
= *((typeof(out_index
)) lttng_dynamic_array_get_element(
80 &path
->indexes
, path_index
));
81 status
= LTTNG_ACTION_PATH_STATUS_OK
;
86 void lttng_action_path_destroy(struct lttng_action_path
*action_path
)
92 lttng_dynamic_array_reset(&action_path
->indexes
);
99 int lttng_action_path_copy(const struct lttng_action_path
*src
,
100 struct lttng_action_path
*dst
)
108 lttng_dynamic_array_init(&dst
->indexes
, sizeof(uint64_t), NULL
);
109 src_count
= lttng_dynamic_array_get_count(&src
->indexes
);
111 for (i
= 0; i
< src_count
; i
++) {
112 const void *index
= lttng_dynamic_array_get_element(
115 ret
= lttng_dynamic_array_add_element(&dst
->indexes
, index
);
124 lttng_dynamic_array_reset(&dst
->indexes
);
130 ssize_t
lttng_action_path_create_from_payload(
131 struct lttng_payload_view
*view
,
132 struct lttng_action_path
**_action_path
)
134 ssize_t consumed_size
= 0, ret
= -1;
135 const struct lttng_action_path_comm
*header
;
136 struct lttng_action_path
*action_path
= NULL
;
137 const struct lttng_payload_view header_view
=
138 lttng_payload_view_from_view(view
, 0, sizeof(*header
));
140 if (!lttng_payload_view_is_valid(&header_view
)) {
144 header
= (typeof(header
)) header_view
.buffer
.data
;
145 consumed_size
+= header_view
.buffer
.size
;
147 const struct lttng_payload_view indexes_view
=
148 lttng_payload_view_from_view(view
,
150 header
->index_count
*
153 if (!lttng_payload_view_is_valid(&indexes_view
)) {
157 consumed_size
+= indexes_view
.buffer
.size
;
158 action_path
= lttng_action_path_create(
159 (const uint64_t *) indexes_view
.buffer
.data
,
160 header
->index_count
);
167 *_action_path
= action_path
;
173 int lttng_action_path_serialize(const struct lttng_action_path
*action_path
,
174 struct lttng_payload
*payload
)
177 size_t index_count
, i
;
178 enum lttng_action_path_status status
;
180 status
= lttng_action_path_get_index_count(action_path
, &index_count
);
181 if (status
!= LTTNG_ACTION_PATH_STATUS_OK
) {
186 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
187 &((struct lttng_action_path_comm
) {
188 .index_count
= index_count
190 sizeof(struct lttng_action_path_comm
));
192 for (i
= 0; i
< index_count
; i
++) {
195 status
= lttng_action_path_get_index_at_index(
196 action_path
, i
, &path_index
);
197 if (status
!= LTTNG_ACTION_PATH_STATUS_OK
) {
202 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &path_index
,