ef095554585c934569b04cf7da76f512d51f195d
[lttng-tools.git] / src / common / payload.cpp
1 /*
2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include "payload.hpp"
9
10 #include <common/dynamic-array.hpp>
11 #include <common/dynamic-buffer.hpp>
12 #include <common/error.hpp>
13
14 static void release_fd_handle_ref(void *ptr)
15 {
16 struct fd_handle *fd_handle = (struct fd_handle *) ptr;
17
18 fd_handle_put(fd_handle);
19 }
20
21 void lttng_payload_init(struct lttng_payload *payload)
22 {
23 LTTNG_ASSERT(payload);
24 lttng_dynamic_buffer_init(&payload->buffer);
25 lttng_dynamic_pointer_array_init(&payload->_fd_handles, release_fd_handle_ref);
26 }
27
28 int lttng_payload_copy(const struct lttng_payload *src_payload, struct lttng_payload *dst_payload)
29 {
30 int ret;
31 size_t i;
32
33 ret = lttng_dynamic_buffer_append_buffer(&dst_payload->buffer, &src_payload->buffer);
34 if (ret) {
35 goto end;
36 }
37
38 for (i = 0; i < lttng_dynamic_pointer_array_get_count(&src_payload->_fd_handles); i++) {
39 struct fd_handle *new_fd_handle;
40 const struct fd_handle *src_fd_handle =
41 (fd_handle *) lttng_dynamic_pointer_array_get_pointer(
42 &src_payload->_fd_handles, i);
43
44 new_fd_handle = fd_handle_copy(src_fd_handle);
45 if (!new_fd_handle) {
46 PERROR("Failed to copy fd_handle while copying a payload");
47 ret = -1;
48 goto end;
49 }
50
51 ret = lttng_payload_push_fd_handle(dst_payload, new_fd_handle);
52 fd_handle_put(new_fd_handle);
53 if (ret) {
54 goto end;
55 }
56 }
57
58 end:
59 return ret;
60 }
61
62 void lttng_payload_reset(struct lttng_payload *payload)
63 {
64 if (!payload) {
65 return;
66 }
67
68 lttng_dynamic_buffer_reset(&payload->buffer);
69 lttng_dynamic_pointer_array_reset(&payload->_fd_handles);
70 }
71
72 void lttng_payload_clear(struct lttng_payload *payload)
73 {
74 (void) lttng_dynamic_buffer_set_size(&payload->buffer, 0);
75 lttng_dynamic_pointer_array_clear(&payload->_fd_handles);
76 }
77
78 int lttng_payload_push_fd_handle(struct lttng_payload *payload, struct fd_handle *fd_handle)
79 {
80 int ret;
81
82 if (!payload) {
83 ret = -1;
84 goto end;
85 }
86
87 ret = lttng_dynamic_pointer_array_add_pointer(&payload->_fd_handles, fd_handle);
88 if (ret) {
89 goto end;
90 }
91
92 fd_handle_get(fd_handle);
93 end:
94 return ret;
95 }
This page took 0.036536 seconds and 5 git commands to generate.