|
| 1 | +From 876b5118fa521f62e31a5bec7ec0be27da3bb7ab Mon Sep 17 00:00:00 2001 |
| 2 | +From: Thierry Treyer < [email protected]> |
| 3 | +Date: Fri, 11 Apr 2025 09:04:29 -0700 |
| 4 | +Subject: [PATCH] Remove 'cstring_view' |
| 5 | + |
| 6 | +The `cstring_view` class was a `std::string_view` with the added |
| 7 | +guarantee that it is NULL-terminated. It was only used by BpfMap. |
| 8 | +This commit replaces it by a `std::string` |
| 9 | + |
| 10 | +Fixes: #4001 |
| 11 | + |
| 12 | +Signed-off-by: Thierry Treyer < [email protected]> |
| 13 | +--- |
| 14 | + src/bpfmap.cpp | 2 +- |
| 15 | + src/bpfmap.h | 10 +++--- |
| 16 | + src/container/cstring_view.h | 39 ----------------------- |
| 17 | + tests/CMakeLists.txt | 1 - |
| 18 | + tests/cstring_view.cpp | 60 ------------------------------------ |
| 19 | + 5 files changed, 5 insertions(+), 107 deletions(-) |
| 20 | + delete mode 100644 src/container/cstring_view.h |
| 21 | + delete mode 100644 tests/cstring_view.cpp |
| 22 | + |
| 23 | +diff --git a/src/bpfmap.cpp b/src/bpfmap.cpp |
| 24 | +index 9464e8ed..eb65621e 100644 |
| 25 | +--- a/src/bpfmap.cpp |
| 26 | ++++ b/src/bpfmap.cpp |
| 27 | +@@ -12,7 +12,7 @@ libbpf::bpf_map_type BpfMap::type() const |
| 28 | + return type_; |
| 29 | + } |
| 30 | + |
| 31 | +-cstring_view BpfMap::bpf_name() const |
| 32 | ++const std::string &BpfMap::bpf_name() const |
| 33 | + { |
| 34 | + return name_; |
| 35 | + } |
| 36 | +diff --git a/src/bpfmap.h b/src/bpfmap.h |
| 37 | +index 09153764..d48763ed 100644 |
| 38 | +--- a/src/bpfmap.h |
| 39 | ++++ b/src/bpfmap.h |
| 40 | +@@ -11,8 +11,6 @@ namespace libbpf { |
| 41 | + #include "libbpf/bpf.h" |
| 42 | + } // namespace libbpf |
| 43 | + |
| 44 | +-#include "container/cstring_view.h" |
| 45 | +- |
| 46 | + namespace bpftrace { |
| 47 | + |
| 48 | + class BpfMap { |
| 49 | +@@ -28,12 +26,12 @@ public: |
| 50 | + } |
| 51 | + |
| 52 | + BpfMap(libbpf::bpf_map_type type, |
| 53 | +- cstring_view name, |
| 54 | ++ std::string name, |
| 55 | + uint32_t key_size, |
| 56 | + uint32_t value_size, |
| 57 | + uint32_t max_entries) |
| 58 | + : type_(type), |
| 59 | +- name_(name), |
| 60 | ++ name_(std::move(name)), |
| 61 | + key_size_(key_size), |
| 62 | + value_size_(value_size), |
| 63 | + max_entries_(max_entries) |
| 64 | +@@ -42,7 +40,7 @@ public: |
| 65 | + |
| 66 | + int fd() const; |
| 67 | + libbpf::bpf_map_type type() const; |
| 68 | +- cstring_view bpf_name() const; |
| 69 | ++ const std::string &bpf_name() const; |
| 70 | + std::string name() const; |
| 71 | + uint32_t key_size() const; |
| 72 | + uint32_t value_size() const; |
| 73 | +@@ -56,7 +54,7 @@ public: |
| 74 | + private: |
| 75 | + struct bpf_map *bpf_map_; |
| 76 | + libbpf::bpf_map_type type_; |
| 77 | +- cstring_view name_; |
| 78 | ++ std::string name_; |
| 79 | + uint32_t key_size_; |
| 80 | + uint32_t value_size_; |
| 81 | + uint32_t max_entries_; |
| 82 | +diff --git a/src/container/cstring_view.h b/src/container/cstring_view.h |
| 83 | +deleted file mode 100644 |
| 84 | +index 2e1c4602..00000000 |
| 85 | +--- a/src/container/cstring_view.h |
| 86 | ++++ /dev/null |
| 87 | +@@ -1,39 +0,0 @@ |
| 88 | +-#pragma once |
| 89 | +- |
| 90 | +-#include <string> |
| 91 | +-#include <string_view> |
| 92 | +- |
| 93 | +-namespace bpftrace { |
| 94 | +- |
| 95 | +-// cstring_view |
| 96 | +-// |
| 97 | +-// A restricted version of std::string_view which guarantees that the underlying |
| 98 | +-// string buffer will be null-terminated. This can be useful when interacting |
| 99 | +-// with C APIs while avoiding the use of char* and unnecessary copies from using |
| 100 | +-// std::string. |
| 101 | +-// |
| 102 | +-// We only allow constructing cstring_view from types which are guaranteed to |
| 103 | +-// store null-terminated strings. All modifiers or operations on cstring_view |
| 104 | +-// will also maintain the null-terminated property. |
| 105 | +-class cstring_view : public std::string_view { |
| 106 | +-public: |
| 107 | +- constexpr cstring_view(const char *str) noexcept : std::string_view{ str } |
| 108 | +- { |
| 109 | +- } |
| 110 | +- constexpr cstring_view(const std::string &str) noexcept |
| 111 | +- : std::string_view{ str } |
| 112 | +- { |
| 113 | +- } |
| 114 | +- constexpr const char *c_str() const noexcept |
| 115 | +- { |
| 116 | +- return data(); |
| 117 | +- } |
| 118 | +- |
| 119 | +-private: |
| 120 | +- // Disallow use of functions which can break the null-termination invariant |
| 121 | +- using std::string_view::copy; |
| 122 | +- using std::string_view::remove_suffix; |
| 123 | +- using std::string_view::substr; |
| 124 | +-}; |
| 125 | +- |
| 126 | +-} // namespace bpftrace |
| 127 | +diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt |
| 128 | +index c5d10f9c..d012cad7 100644 |
| 129 | +--- a/tests/CMakeLists.txt |
| 130 | ++++ b/tests/CMakeLists.txt |
| 131 | +@@ -33,7 +33,6 @@ add_executable(bpftrace_test |
| 132 | + clang_parser.cpp |
| 133 | + config.cpp |
| 134 | + collect_nodes.cpp |
| 135 | +- cstring_view.cpp |
| 136 | + field_analyser.cpp |
| 137 | + function_registry.cpp |
| 138 | + log.cpp |
| 139 | +diff --git a/tests/cstring_view.cpp b/tests/cstring_view.cpp |
| 140 | +deleted file mode 100644 |
| 141 | +index 5b82a990..00000000 |
| 142 | +--- a/tests/cstring_view.cpp |
| 143 | ++++ /dev/null |
| 144 | +@@ -1,60 +0,0 @@ |
| 145 | +-#include "container/cstring_view.h" |
| 146 | +-#include "gtest/gtest.h" |
| 147 | +- |
| 148 | +-#include <type_traits> |
| 149 | +- |
| 150 | +-namespace bpftrace::test::cstring_view { |
| 151 | +- |
| 152 | +-using bpftrace::cstring_view; |
| 153 | +- |
| 154 | +-TEST(cstring_view, c_string) |
| 155 | +-{ |
| 156 | +- const char *str = "abc"; |
| 157 | +- cstring_view sv{ str }; |
| 158 | +- |
| 159 | +- EXPECT_EQ("abc", sv); |
| 160 | +- |
| 161 | +- EXPECT_EQ('a', sv[0]); |
| 162 | +- EXPECT_EQ('b', sv[1]); |
| 163 | +- EXPECT_EQ('c', sv[2]); |
| 164 | +- EXPECT_EQ('\0', sv[3]); |
| 165 | +-} |
| 166 | +- |
| 167 | +-TEST(cstring_view, std_string) |
| 168 | +-{ |
| 169 | +- std::string str = "abc"; |
| 170 | +- cstring_view sv{ str }; |
| 171 | +- |
| 172 | +- EXPECT_EQ("abc", sv); |
| 173 | +- |
| 174 | +- EXPECT_EQ('a', sv[0]); |
| 175 | +- EXPECT_EQ('b', sv[1]); |
| 176 | +- EXPECT_EQ('c', sv[2]); |
| 177 | +- EXPECT_EQ('\0', sv[3]); |
| 178 | +-} |
| 179 | +- |
| 180 | +-TEST(cstring_view, std_string_view) |
| 181 | +-{ |
| 182 | +- EXPECT_FALSE((std::is_constructible_v<cstring_view, std::string_view>)); |
| 183 | +- |
| 184 | +- // Sanity checks: |
| 185 | +- EXPECT_TRUE((std::is_constructible_v<cstring_view, std::string>)); |
| 186 | +- EXPECT_TRUE((std::is_constructible_v<cstring_view, const char *>)); |
| 187 | +-} |
| 188 | +- |
| 189 | +-TEST(cstring_view, length) |
| 190 | +-{ |
| 191 | +- cstring_view sv{ "abc" }; |
| 192 | +- |
| 193 | +- EXPECT_EQ("abc", sv); |
| 194 | +- EXPECT_EQ(3, sv.size()); |
| 195 | +- EXPECT_EQ(3, sv.length()); |
| 196 | +-} |
| 197 | +- |
| 198 | +-TEST(cstring_view, c_str) |
| 199 | +-{ |
| 200 | +- cstring_view sv{ "abc" }; |
| 201 | +- EXPECT_EQ(0, strcmp(sv.c_str(), "abc")); |
| 202 | +-} |
| 203 | +- |
| 204 | +-} // namespace bpftrace::test::cstring_view |
| 205 | +-- |
| 206 | +2.45.4 |
| 207 | + |
0 commit comments