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
);
32 ret
= lttng_dynamic_array_set_count(&path
->indexes
, index_count
);
37 for (i
= 0; i
< index_count
; i
++) {
38 ret
= lttng_dynamic_array_add_element(
39 &path
->indexes
, &indexes
[i
]);
47 lttng_action_path_destroy(path
);
53 enum lttng_action_path_status
lttng_action_path_get_index_count(
54 const struct lttng_action_path
*path
, size_t *index_count
)
56 enum lttng_action_path_status status
;
58 if (!path
|| !index_count
) {
59 status
= LTTNG_ACTION_PATH_STATUS_INVALID
;
63 *index_count
= lttng_dynamic_array_get_count(&path
->indexes
);
64 status
= LTTNG_ACTION_PATH_STATUS_OK
;
69 enum lttng_action_path_status
lttng_action_path_get_index_at_index(
70 const struct lttng_action_path
*path
,
74 enum lttng_action_path_status status
;
76 if (!path
|| !out_index
||
77 path_index
>= lttng_dynamic_array_get_count(
79 status
= LTTNG_ACTION_PATH_STATUS_INVALID
;
83 *out_index
= *((typeof(out_index
)) lttng_dynamic_array_get_element(
84 &path
->indexes
, path_index
));
85 status
= LTTNG_ACTION_PATH_STATUS_OK
;
90 void lttng_action_path_destroy(struct lttng_action_path
*action_path
)
96 lttng_dynamic_array_reset(&action_path
->indexes
);
103 int lttng_action_path_copy(const struct lttng_action_path
*src
,
104 struct lttng_action_path
*dst
)
112 lttng_dynamic_array_init(&dst
->indexes
, sizeof(uint64_t), NULL
);
113 src_count
= lttng_dynamic_array_get_count(&src
->indexes
);
115 ret
= lttng_dynamic_array_set_count(&dst
->indexes
, src_count
);
120 for (i
= 0; i
< src_count
; i
++) {
121 const void *index
= lttng_dynamic_array_get_element(
124 ret
= lttng_dynamic_array_add_element(&dst
->indexes
, index
);
133 lttng_dynamic_array_reset(&dst
->indexes
);
139 ssize_t
lttng_action_path_create_from_payload(
140 struct lttng_payload_view
*view
,
141 struct lttng_action_path
**_action_path
)
143 ssize_t consumed_size
= 0, ret
= -1;
144 const struct lttng_action_path_comm
*header
;
145 struct lttng_action_path
*action_path
= NULL
;
146 const struct lttng_payload_view header_view
=
147 lttng_payload_view_from_view(view
, 0, sizeof(*header
));
149 if (!lttng_payload_view_is_valid(&header_view
)) {
153 header
= (typeof(header
)) header_view
.buffer
.data
;
154 consumed_size
+= header_view
.buffer
.size
;
156 const struct lttng_payload_view indexes_view
=
157 lttng_payload_view_from_view(view
,
159 header
->index_count
*
162 if (!lttng_payload_view_is_valid(&indexes_view
)) {
166 consumed_size
+= indexes_view
.buffer
.size
;
167 action_path
= lttng_action_path_create(
168 (const uint64_t *) indexes_view
.buffer
.data
,
169 header
->index_count
);
181 int lttng_action_path_serialize(const struct lttng_action_path
*action_path
,
182 struct lttng_payload
*payload
)
185 size_t index_count
, i
;
186 enum lttng_action_path_status status
;
188 status
= lttng_action_path_get_index_count(action_path
, &index_count
);
189 if (status
!= LTTNG_ACTION_PATH_STATUS_OK
) {
194 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
195 &((struct lttng_action_path_comm
) {
196 .index_count
= index_count
198 sizeof(struct lttng_action_path_comm
));
200 for (i
= 0; i
< index_count
; i
++) {
203 status
= lttng_action_path_get_index_at_index(
204 action_path
, i
, &path_index
);
205 if (status
!= LTTNG_ACTION_PATH_STATUS_OK
) {
210 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &path_index
,