| 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 | bool lttng_payload_view_is_valid(const struct lttng_payload_view *view) |
| 16 | { |
| 17 | return view && lttng_buffer_view_is_valid(&view->buffer); |
| 18 | } |
| 19 | |
| 20 | LTTNG_HIDDEN |
| 21 | struct lttng_payload_view lttng_payload_view_from_payload( |
| 22 | const struct lttng_payload *payload, size_t offset, |
| 23 | ptrdiff_t len) |
| 24 | { |
| 25 | return payload ? (struct lttng_payload_view) { |
| 26 | .buffer = lttng_buffer_view_from_dynamic_buffer( |
| 27 | &payload->buffer, offset, len), |
| 28 | ._fd_handles = payload->_fd_handles, |
| 29 | } : (struct lttng_payload_view) {}; |
| 30 | } |
| 31 | |
| 32 | LTTNG_HIDDEN |
| 33 | struct lttng_payload_view lttng_payload_view_from_view( |
| 34 | struct lttng_payload_view *view, size_t offset, |
| 35 | ptrdiff_t len) |
| 36 | { |
| 37 | return view ? (struct lttng_payload_view) { |
| 38 | .buffer = lttng_buffer_view_from_view( |
| 39 | &view->buffer, offset, len), |
| 40 | ._fd_handles = view->_fd_handles, |
| 41 | ._iterator.p_fd_handles_position = view->_iterator.p_fd_handles_position ?: |
| 42 | &view->_iterator.fd_handles_position, |
| 43 | } : (struct lttng_payload_view) {}; |
| 44 | } |
| 45 | |
| 46 | LTTNG_HIDDEN |
| 47 | struct lttng_payload_view lttng_payload_view_from_dynamic_buffer( |
| 48 | const struct lttng_dynamic_buffer *buffer, size_t offset, |
| 49 | ptrdiff_t len) |
| 50 | { |
| 51 | return buffer ? (struct lttng_payload_view) { |
| 52 | .buffer = lttng_buffer_view_from_dynamic_buffer( |
| 53 | buffer, offset, len) |
| 54 | } : (struct lttng_payload_view) {}; |
| 55 | } |
| 56 | |
| 57 | LTTNG_HIDDEN |
| 58 | struct lttng_payload_view lttng_payload_view_from_buffer_view( |
| 59 | const struct lttng_buffer_view *view, size_t offset, |
| 60 | ptrdiff_t len) |
| 61 | { |
| 62 | return view ? (struct lttng_payload_view) { |
| 63 | .buffer = lttng_buffer_view_from_view( |
| 64 | view, offset, len) |
| 65 | } : (struct lttng_payload_view) {}; |
| 66 | } |
| 67 | |
| 68 | LTTNG_HIDDEN |
| 69 | struct lttng_payload_view lttng_payload_view_init_from_buffer( |
| 70 | const char *src, size_t offset, ptrdiff_t len) |
| 71 | { |
| 72 | return (struct lttng_payload_view) { |
| 73 | .buffer = lttng_buffer_view_init( |
| 74 | src, offset, len) |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | LTTNG_HIDDEN |
| 79 | int lttng_payload_view_get_fd_handle_count( |
| 80 | const struct lttng_payload_view *payload_view) |
| 81 | { |
| 82 | int ret; |
| 83 | size_t position; |
| 84 | |
| 85 | if (!payload_view) { |
| 86 | ret = -1; |
| 87 | goto end; |
| 88 | } |
| 89 | |
| 90 | ret = lttng_dynamic_pointer_array_get_count(&payload_view->_fd_handles); |
| 91 | if (ret < 0) { |
| 92 | goto end; |
| 93 | } |
| 94 | |
| 95 | position = payload_view->_iterator.p_fd_handles_position ? |
| 96 | *payload_view->_iterator.p_fd_handles_position : |
| 97 | payload_view->_iterator.fd_handles_position; |
| 98 | ret = ret - (int) position; |
| 99 | end: |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | LTTNG_HIDDEN |
| 104 | struct fd_handle *lttng_payload_view_pop_fd_handle( |
| 105 | struct lttng_payload_view *view) |
| 106 | { |
| 107 | struct fd_handle *handle = NULL; |
| 108 | size_t fd_handle_count; |
| 109 | size_t *pos; |
| 110 | |
| 111 | if (!view) { |
| 112 | goto end; |
| 113 | } |
| 114 | |
| 115 | fd_handle_count = lttng_payload_view_get_fd_handle_count(view); |
| 116 | if (fd_handle_count == 0) { |
| 117 | goto end; |
| 118 | } |
| 119 | |
| 120 | pos = view->_iterator.p_fd_handles_position ? |
| 121 | view->_iterator.p_fd_handles_position : |
| 122 | &view->_iterator.fd_handles_position; |
| 123 | handle = lttng_dynamic_pointer_array_get_pointer(&view->_fd_handles, |
| 124 | *pos); |
| 125 | (*pos)++; |
| 126 | fd_handle_get(handle); |
| 127 | end: |
| 128 | return handle; |
| 129 | } |