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