Fix: waiter: futex wait: handle spurious futex wakeups
[lttng-tools.git] / src / common / buffer-view.cpp
CommitLineData
01dc0eed 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
01dc0eed 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
01dc0eed 5 *
01dc0eed
JG
6 */
7
c9e313bc
SM
8#include <common/buffer-view.hpp>
9#include <common/dynamic-buffer.hpp>
10#include <common/error.hpp>
01dc0eed 11
b35aac84
JG
12struct lttng_buffer_view lttng_buffer_view_init(
13 const char *src, size_t offset, ptrdiff_t len)
14{
a6bc4ca9 15 struct lttng_buffer_view view = { .data = src + offset, .size = (size_t) len };
b35aac84
JG
16 return view;
17}
18
3e6e0df2
JG
19bool lttng_buffer_view_is_valid(const struct lttng_buffer_view *view)
20{
21 return view && view->data && view->size > 0;
22}
23
01dc0eed
JG
24struct lttng_buffer_view lttng_buffer_view_from_view(
25 const struct lttng_buffer_view *src, size_t offset,
26 ptrdiff_t len)
27{
28 struct lttng_buffer_view view = { .data = NULL, .size = 0 };
29
a0377dfe 30 LTTNG_ASSERT(src);
01dc0eed
JG
31
32 if (offset > src->size) {
df62dbcd
JG
33 ERR("Attempt to create buffer view from another view with invalid offset (offset > source size): source size = %zu, offset in source = %zu, length = %zd",
34 src->size, offset, len);
01dc0eed
JG
35 goto end;
36 }
37
38 if (len != -1 && len > (src->size - offset)) {
df62dbcd
JG
39 ERR("Attempt to create buffer view from another view with invalid length (length > space left after offset in source): source size = %zu, offset in source = %zu, length = %zd",
40 src->size, offset, len);
01dc0eed
JG
41 goto end;
42 }
43
44 view.data = src->data + offset;
45 view.size = len == -1 ? (src->size - offset) : len;
46end:
47 return view;
48}
49
50struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer(
51 const struct lttng_dynamic_buffer *src, size_t offset,
52 ptrdiff_t len)
53{
54 struct lttng_buffer_view view = { .data = NULL, .size = 0 };
55
a0377dfe 56 LTTNG_ASSERT(src);
01dc0eed
JG
57
58 if (offset > src->size) {
df62dbcd
JG
59 ERR("Attempt to create buffer view from a dynamic buffer with invalid offset (offset > source size): source size = %zu, offset in source = %zu, length = %zd",
60 src->size, offset, len);
01dc0eed
JG
61 goto end;
62 }
63
64 if (len != -1 && len > (src->size - offset)) {
df62dbcd
JG
65 ERR("Attempt to create buffer view from a dynamic buffer with invalid length (length > space left after offset in source): source size = %zu, offset in source = %zu, length = %zd",
66 src->size, offset, len);
01dc0eed
JG
67 goto end;
68 }
69
70 view.data = src->data + offset;
71 view.size = len == -1 ? (src->size - offset) : len;
72end:
73 return view;
74}
67d8e2ef 75
67d8e2ef
SM
76bool lttng_buffer_view_contains_string(const struct lttng_buffer_view *buf,
77 const char *str,
78 size_t len_with_null_terminator)
79{
80 const char *past_buf_end;
81 size_t max_str_len_with_null_terminator;
82 size_t str_len;
83 bool ret;
84
85 past_buf_end = buf->data + buf->size;
86
87 /* Is the start of the string in the buffer view? */
88 if (str < buf->data || str >= past_buf_end) {
89 ret = false;
90 goto end;
91 }
92
93 /*
94 * Max length the string could have to fit in the buffer, including
95 * NULL terminator.
96 */
97 max_str_len_with_null_terminator = past_buf_end - str;
98
99 /* Could the string even fit in the buffer? */
100 if (len_with_null_terminator > max_str_len_with_null_terminator) {
101 ret = false;
102 goto end;
103 }
104
105 str_len = lttng_strnlen(str, max_str_len_with_null_terminator);
106 if (str_len != (len_with_null_terminator - 1)) {
107 ret = false;
108 goto end;
109 }
110
111 ret = true;
112
113end:
114 return ret;
115}
This page took 0.051079 seconds and 4 git commands to generate.