Commit | Line | Data |
---|---|---|
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 | ||
14 | LTTNG_HIDDEN | |
15 | struct 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 | ||
26 | LTTNG_HIDDEN | |
27 | struct 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 | ||
40 | LTTNG_HIDDEN | |
41 | struct 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 |
51 | LTTNG_HIDDEN |
52 | struct 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 |
62 | LTTNG_HIDDEN |
63 | struct 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 | 72 | LTTNG_HIDDEN |
18eec1c9 JG |
73 | int lttng_payload_view_get_fd_handle_count( |
74 | const struct lttng_payload_view *payload_view) | |
5a2f5f00 JG |
75 | { |
76 | int ret; | |
77 | size_t position; | |
78 | ||
79 | if (!payload_view) { | |
80 | ret = -1; | |
81 | goto end; | |
82 | } | |
83 | ||
fe489250 | 84 | ret = lttng_dynamic_pointer_array_get_count(&payload_view->_fd_handles); |
5a2f5f00 JG |
85 | if (ret < 0) { |
86 | goto end; | |
87 | } | |
88 | ||
fe489250 JG |
89 | position = payload_view->_iterator.p_fd_handles_position ? |
90 | *payload_view->_iterator.p_fd_handles_position : | |
91 | payload_view->_iterator.fd_handles_position; | |
5a2f5f00 JG |
92 | ret = ret - (int) position; |
93 | end: | |
94 | return ret; | |
95 | } | |
96 | ||
c0a66c84 | 97 | LTTNG_HIDDEN |
fe489250 JG |
98 | struct fd_handle *lttng_payload_view_pop_fd_handle( |
99 | struct lttng_payload_view *view) | |
c0a66c84 | 100 | { |
fe489250 JG |
101 | struct fd_handle *handle = NULL; |
102 | size_t fd_handle_count; | |
c0a66c84 JG |
103 | size_t *pos; |
104 | ||
105 | if (!view) { | |
c0a66c84 JG |
106 | goto end; |
107 | } | |
108 | ||
fe489250 JG |
109 | fd_handle_count = lttng_payload_view_get_fd_handle_count(view); |
110 | if (fd_handle_count == 0) { | |
c0a66c84 JG |
111 | goto end; |
112 | } | |
113 | ||
fe489250 JG |
114 | pos = view->_iterator.p_fd_handles_position ? |
115 | view->_iterator.p_fd_handles_position : | |
116 | &view->_iterator.fd_handles_position; | |
117 | handle = lttng_dynamic_pointer_array_get_pointer(&view->_fd_handles, | |
118 | *pos); | |
c0a66c84 | 119 | (*pos)++; |
fe489250 | 120 | fd_handle_get(handle); |
c0a66c84 | 121 | end: |
fe489250 | 122 | return handle; |
c0a66c84 | 123 | } |