| 1 | /* |
| 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef LTTNG_BUFFER_VIEW_H |
| 9 | #define LTTNG_BUFFER_VIEW_H |
| 10 | |
| 11 | #include <common/macros.hpp> |
| 12 | #include <stdbool.h> |
| 13 | #include <stddef.h> |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | struct lttng_dynamic_buffer; |
| 17 | |
| 18 | struct lttng_buffer_view { |
| 19 | const char *data; |
| 20 | size_t size; |
| 21 | }; |
| 22 | |
| 23 | /** |
| 24 | * Return a buffer view referencing a subset of the memory referenced by a raw |
| 25 | * pointer. |
| 26 | * |
| 27 | * @src Source buffer to reference |
| 28 | * @offset Offset to apply to the source memory buffer |
| 29 | * @len Length of the memory contents to reference. |
| 30 | * |
| 31 | * Note that a buffer view never assumes the ownership of the memory it |
| 32 | * references. |
| 33 | */ |
| 34 | struct lttng_buffer_view lttng_buffer_view_init( |
| 35 | const char *src, size_t offset, ptrdiff_t len); |
| 36 | |
| 37 | /** |
| 38 | * Checks if a buffer view is safe to access. |
| 39 | * |
| 40 | * After calling the buffer view creation functions, callers should verify |
| 41 | * if the resquested length (if any is explicitly provided) could be mapped |
| 42 | * to a new view. |
| 43 | * |
| 44 | * @view Buffer view to validate |
| 45 | */ |
| 46 | bool lttng_buffer_view_is_valid(const struct lttng_buffer_view *view); |
| 47 | |
| 48 | /** |
| 49 | * Return a buffer view referencing a subset of the memory referenced by another |
| 50 | * view. |
| 51 | * |
| 52 | * @src Source view to reference |
| 53 | * @offset Offset to apply to the source memory content |
| 54 | * @len Length of the memory contents to reference. Passing -1 will |
| 55 | * cause the view to reference the whole view from the offset |
| 56 | * provided. |
| 57 | * |
| 58 | * Note that a buffer view never assumes the ownership of the memory it |
| 59 | * references. |
| 60 | */ |
| 61 | struct lttng_buffer_view lttng_buffer_view_from_view( |
| 62 | const struct lttng_buffer_view *src, size_t offset, |
| 63 | ptrdiff_t len); |
| 64 | |
| 65 | /** |
| 66 | * Return a buffer view referencing a subset of the memory referenced by a |
| 67 | * dynamic buffer. |
| 68 | * |
| 69 | * @src Source dynamic buffer to reference |
| 70 | * @offset Offset to apply to the source memory content |
| 71 | * @len Length of the memory contents to reference. Passing -1 will |
| 72 | * cause the view to reference the whole dynamic buffer from the |
| 73 | * offset provided. |
| 74 | * |
| 75 | * Note that a buffer view never assumes the ownership of the memory it |
| 76 | * references. |
| 77 | */ |
| 78 | struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer( |
| 79 | const struct lttng_dynamic_buffer *src, size_t offset, |
| 80 | ptrdiff_t len); |
| 81 | |
| 82 | /** |
| 83 | * Verify that `buf` contains a string starting at `str` of length |
| 84 | * `len_with_null_terminator`. |
| 85 | * |
| 86 | * @buf The buffer view |
| 87 | * @str The start of the string |
| 88 | * @len_with_null_terminator Expected length of the string, including the |
| 89 | * NULL terminator. |
| 90 | */ |
| 91 | bool lttng_buffer_view_contains_string(const struct lttng_buffer_view *buf, |
| 92 | const char *str, |
| 93 | size_t len_with_null_terminator); |
| 94 | |
| 95 | #endif /* LTTNG_BUFFER_VIEW_H */ |