tests: Use `--no-wait` when destroying sessions in relayd-grouping
[lttng-tools.git] / src / common / payload-view.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-view.hpp"
9 #include "payload.hpp"
10
11 #include <common/buffer-view.hpp>
12 #include <common/dynamic-array.hpp>
13
14 #include <stddef.h>
15
16 bool lttng_payload_view_is_valid(const struct lttng_payload_view *view)
17 {
18 return view && lttng_buffer_view_is_valid(&view->buffer);
19 }
20
21 struct lttng_payload_view
22 lttng_payload_view_from_payload(const struct lttng_payload *payload, size_t offset, ptrdiff_t len)
23 {
24 return payload ?
25 (struct lttng_payload_view){
26 .buffer = lttng_buffer_view_from_dynamic_buffer(
27 &payload->buffer, offset, len),
28 ._fd_handles = payload->_fd_handles,
29 ._iterator = {},
30 } :
31 (struct lttng_payload_view){
32 .buffer = {},
33 ._fd_handles = {},
34 ._iterator = {},
35 };
36 }
37
38 struct lttng_payload_view
39 lttng_payload_view_from_view(struct lttng_payload_view *view, size_t offset, ptrdiff_t len)
40 {
41 return view ? (struct lttng_payload_view) {
42 .buffer = lttng_buffer_view_from_view(
43 &view->buffer, offset, len),
44 ._fd_handles = view->_fd_handles,
45 ._iterator = {
46 .p_fd_handles_position = view->_iterator.p_fd_handles_position ?:
47 &view->_iterator.fd_handles_position,
48 .fd_handles_position = 0,
49 }
50 } : (struct lttng_payload_view) {
51 .buffer = {},
52 ._fd_handles = {},
53 ._iterator = {},
54 };
55 }
56
57 struct lttng_payload_view lttng_payload_view_from_dynamic_buffer(
58 const struct lttng_dynamic_buffer *buffer, size_t offset, ptrdiff_t len)
59 {
60 return buffer ?
61 (struct lttng_payload_view){
62 .buffer = lttng_buffer_view_from_dynamic_buffer(buffer, offset, len),
63 ._fd_handles = {},
64 ._iterator = {},
65 } :
66 (struct lttng_payload_view){
67 .buffer = {},
68 ._fd_handles = {},
69 ._iterator = {},
70 };
71 }
72
73 struct lttng_payload_view lttng_payload_view_from_buffer_view(const struct lttng_buffer_view *view,
74 size_t offset,
75 ptrdiff_t len)
76 {
77 return view ?
78 (struct lttng_payload_view){
79 .buffer = lttng_buffer_view_from_view(view, offset, len),
80 ._fd_handles = {},
81 ._iterator = {},
82 } :
83 (struct lttng_payload_view){
84 .buffer = {},
85 ._fd_handles = {},
86 ._iterator = {},
87 };
88 }
89
90 struct lttng_payload_view
91 lttng_payload_view_init_from_buffer(const char *src, size_t offset, ptrdiff_t len)
92 {
93 return (struct lttng_payload_view){
94 .buffer = lttng_buffer_view_init(src, offset, len),
95 ._fd_handles = {},
96 ._iterator = {},
97 };
98 }
99
100 int lttng_payload_view_get_fd_handle_count(const struct lttng_payload_view *payload_view)
101 {
102 int ret;
103 size_t position;
104
105 if (!payload_view) {
106 ret = -1;
107 goto end;
108 }
109
110 ret = lttng_dynamic_pointer_array_get_count(&payload_view->_fd_handles);
111 if (ret < 0) {
112 goto end;
113 }
114
115 position = payload_view->_iterator.p_fd_handles_position ?
116 *payload_view->_iterator.p_fd_handles_position :
117 payload_view->_iterator.fd_handles_position;
118 ret = ret - (int) position;
119 end:
120 return ret;
121 }
122
123 struct fd_handle *lttng_payload_view_pop_fd_handle(struct lttng_payload_view *view)
124 {
125 struct fd_handle *handle = nullptr;
126 size_t fd_handle_count;
127 size_t *pos;
128
129 if (!view) {
130 goto end;
131 }
132
133 fd_handle_count = lttng_payload_view_get_fd_handle_count(view);
134 if (fd_handle_count == 0) {
135 goto end;
136 }
137
138 pos = view->_iterator.p_fd_handles_position ? view->_iterator.p_fd_handles_position :
139 &view->_iterator.fd_handles_position;
140 handle = (fd_handle *) lttng_dynamic_pointer_array_get_pointer(&view->_fd_handles, *pos);
141 (*pos)++;
142 fd_handle_get(handle);
143 end:
144 return handle;
145 }
This page took 0.031468 seconds and 4 git commands to generate.