payload-view: mark lttng_payload_view_get_fd_handle_count as const
[lttng-tools.git] / src / common / payload-view.c
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 <common/dynamic-array.h>
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),
22 ._fd_handles = payload->_fd_handles,
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(
33 &view->buffer, offset, len),
34 ._fd_handles = view->_fd_handles,
35 ._iterator.p_fd_handles_position = view->_iterator.p_fd_handles_position ?:
36 &view->_iterator.fd_handles_position,
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
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
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
72 LTTNG_HIDDEN
73 int lttng_payload_view_get_fd_handle_count(
74 const struct lttng_payload_view *payload_view)
75 {
76 int ret;
77 size_t position;
78
79 if (!payload_view) {
80 ret = -1;
81 goto end;
82 }
83
84 ret = lttng_dynamic_pointer_array_get_count(&payload_view->_fd_handles);
85 if (ret < 0) {
86 goto end;
87 }
88
89 position = payload_view->_iterator.p_fd_handles_position ?
90 *payload_view->_iterator.p_fd_handles_position :
91 payload_view->_iterator.fd_handles_position;
92 ret = ret - (int) position;
93 end:
94 return ret;
95 }
96
97 LTTNG_HIDDEN
98 struct fd_handle *lttng_payload_view_pop_fd_handle(
99 struct lttng_payload_view *view)
100 {
101 struct fd_handle *handle = NULL;
102 size_t fd_handle_count;
103 size_t *pos;
104
105 if (!view) {
106 goto end;
107 }
108
109 fd_handle_count = lttng_payload_view_get_fd_handle_count(view);
110 if (fd_handle_count == 0) {
111 goto end;
112 }
113
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);
119 (*pos)++;
120 fd_handle_get(handle);
121 end:
122 return handle;
123 }
This page took 0.046955 seconds and 5 git commands to generate.