Fix: payload view: payload view always refers to parent's position
[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),
22 ._fds = payload->_fds,
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),
c0a66c84 34 ._fds = view->_fds,
ddab38d1
JG
35 ._iterator.p_fds_position = view->_iterator.p_fds_position ?:
36 &view->_iterator.fds_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
62LTTNG_HIDDEN
63int lttng_payload_view_get_fd_count(struct lttng_payload_view *payload_view)
64{
65 int ret;
66 size_t position;
67
68 if (!payload_view) {
69 ret = -1;
70 goto end;
71 }
72
73 ret = lttng_dynamic_array_get_count(&payload_view->_fds);
74 if (ret < 0) {
75 goto end;
76 }
77
78 position = payload_view->_iterator.p_fds_position ?
79 *payload_view->_iterator.p_fds_position :
80 payload_view->_iterator.fds_position;
81 ret = ret - (int) position;
82end:
83 return ret;
84}
85
c0a66c84
JG
86LTTNG_HIDDEN
87int lttng_payload_view_pop_fd(struct lttng_payload_view *view)
88{
89 int ret = 0;
90 size_t fd_count;
91 size_t *pos;
92
93 if (!view) {
94 ret = -1;
95 goto end;
96 }
97
98 fd_count = lttng_dynamic_array_get_count(&view->_fds);
99 pos = view->_iterator.p_fds_position ? view->_iterator.p_fds_position :
100 &view->_iterator.fds_position;
101
102 if (*pos >= fd_count) {
103 ret = -1;
104 goto end;
105 }
106
107 ret = *((int *) lttng_dynamic_array_get_element(
108 &view->_fds, *pos));
109 (*pos)++;
110end:
111 return ret;
112}
This page took 0.037707 seconds and 4 git commands to generate.