payload: use fd_handle instead of raw file descriptors
[lttng-tools.git] / src / common / payload-view.c
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
5a2f5f00 8#include <common/dynamic-array.h>
c0a66c84
JG
9#include <common/buffer-view.h>
10#include "payload-view.h"
11#include "payload.h"
12#include <stddef.h>
13
14LTTNG_HIDDEN
15struct lttng_payload_view lttng_payload_view_from_payload(
16 const struct lttng_payload *payload, size_t offset,
17 ptrdiff_t len)
18{
19 return (struct lttng_payload_view) {
20 .buffer = lttng_buffer_view_from_dynamic_buffer(
21 &payload->buffer, offset, len),
fe489250 22 ._fd_handles = payload->_fd_handles,
c0a66c84
JG
23 };
24}
25
26LTTNG_HIDDEN
27struct lttng_payload_view lttng_payload_view_from_view(
28 struct lttng_payload_view *view, size_t offset,
29 ptrdiff_t len)
30{
31 return (struct lttng_payload_view) {
32 .buffer = lttng_buffer_view_from_view(
ddab38d1 33 &view->buffer, offset, len),
fe489250
JG
34 ._fd_handles = view->_fd_handles,
35 ._iterator.p_fd_handles_position = view->_iterator.p_fd_handles_position ?:
36 &view->_iterator.fd_handles_position,
c0a66c84
JG
37 };
38}
39
40LTTNG_HIDDEN
41struct lttng_payload_view lttng_payload_view_from_dynamic_buffer(
42 const struct lttng_dynamic_buffer *buffer, size_t offset,
43 ptrdiff_t len)
44{
45 return (struct lttng_payload_view) {
46 .buffer = lttng_buffer_view_from_dynamic_buffer(
47 buffer, offset, len)
48 };
49}
50
5a2f5f00
JG
51LTTNG_HIDDEN
52struct lttng_payload_view lttng_payload_view_from_buffer_view(
53 const struct lttng_buffer_view *view, size_t offset,
54 ptrdiff_t len)
55{
56 return (struct lttng_payload_view) {
57 .buffer = lttng_buffer_view_from_view(
58 view, offset, len)
59 };
60}
61
e368fb43
JG
62LTTNG_HIDDEN
63struct lttng_payload_view lttng_payload_view_init_from_buffer(
64 const char *src, size_t offset, ptrdiff_t len)
65{
66 return (struct lttng_payload_view) {
67 .buffer = lttng_buffer_view_init(
68 src, offset, len)
69 };
70}
71
5a2f5f00 72LTTNG_HIDDEN
fe489250 73int lttng_payload_view_get_fd_handle_count(struct lttng_payload_view *payload_view)
5a2f5f00
JG
74{
75 int ret;
76 size_t position;
77
78 if (!payload_view) {
79 ret = -1;
80 goto end;
81 }
82
fe489250 83 ret = lttng_dynamic_pointer_array_get_count(&payload_view->_fd_handles);
5a2f5f00
JG
84 if (ret < 0) {
85 goto end;
86 }
87
fe489250
JG
88 position = payload_view->_iterator.p_fd_handles_position ?
89 *payload_view->_iterator.p_fd_handles_position :
90 payload_view->_iterator.fd_handles_position;
5a2f5f00
JG
91 ret = ret - (int) position;
92end:
93 return ret;
94}
95
c0a66c84 96LTTNG_HIDDEN
fe489250
JG
97struct fd_handle *lttng_payload_view_pop_fd_handle(
98 struct lttng_payload_view *view)
c0a66c84 99{
fe489250
JG
100 struct fd_handle *handle = NULL;
101 size_t fd_handle_count;
c0a66c84
JG
102 size_t *pos;
103
104 if (!view) {
c0a66c84
JG
105 goto end;
106 }
107
fe489250
JG
108 fd_handle_count = lttng_payload_view_get_fd_handle_count(view);
109 if (fd_handle_count == 0) {
c0a66c84
JG
110 goto end;
111 }
112
fe489250
JG
113 pos = view->_iterator.p_fd_handles_position ?
114 view->_iterator.p_fd_handles_position :
115 &view->_iterator.fd_handles_position;
116 handle = lttng_dynamic_pointer_array_get_pointer(&view->_fd_handles,
117 *pos);
c0a66c84 118 (*pos)++;
fe489250 119 fd_handle_get(handle);
c0a66c84 120end:
fe489250 121 return handle;
c0a66c84 122}
This page took 0.040996 seconds and 4 git commands to generate.