include/grpc/event_engine/slice.h
Go to the documentation of this file.
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_EVENT_ENGINE_SLICE_H
16 #define GRPC_EVENT_ENGINE_SLICE_H
17 
19 
20 #include <string.h>
21 
22 #include <cstdint>
23 #include <string>
24 #include <utility>
25 
26 #include "absl/strings/string_view.h"
27 
28 #include <grpc/slice.h>
29 #include <grpc/support/log.h>
30 
31 // This public slice definition largely based of the internal grpc_core::Slice
32 // implementation. Changes to this implementation might warrant changes to the
33 // internal grpc_core::Slice type as well.
34 
35 namespace grpc_event_engine {
36 namespace experimental {
37 
38 // Forward declarations
39 class Slice;
40 class MutableSlice;
41 
42 namespace slice_detail {
43 
44 // Returns an empty slice.
45 static constexpr grpc_slice EmptySlice() { return {nullptr, {}}; }
46 
47 // BaseSlice holds the grpc_slice object, but does not apply refcounting policy.
48 // It does export immutable access into the slice, such that this can be shared
49 // by all storage policies.
50 class BaseSlice {
51  public:
52  BaseSlice(const BaseSlice&) = delete;
53  BaseSlice& operator=(const BaseSlice&) = delete;
54  BaseSlice(BaseSlice&& other) = delete;
55  BaseSlice& operator=(BaseSlice&& other) = delete;
56 
57  // Iterator access to the underlying bytes
58  const uint8_t* begin() const { return GRPC_SLICE_START_PTR(c_slice()); }
59  const uint8_t* end() const { return GRPC_SLICE_END_PTR(c_slice()); }
60  const uint8_t* cbegin() const { return GRPC_SLICE_START_PTR(c_slice()); }
61  const uint8_t* cend() const { return GRPC_SLICE_END_PTR(c_slice()); }
62 
63  // Retrieve a borrowed reference to the underlying grpc_slice.
64  const grpc_slice& c_slice() const { return slice_; }
65 
66  // Retrieve the underlying grpc_slice, and replace the one in this object with
67  // EmptySlice().
70  slice_ = EmptySlice();
71  return out;
72  }
73 
74  // As other things... borrowed references.
76  return absl::string_view(reinterpret_cast<const char*>(data()), size());
77  }
78 
79  // Array access
80  uint8_t operator[](size_t i) const {
81  return GRPC_SLICE_START_PTR(c_slice())[i];
82  }
83 
84  // Access underlying data
85  const uint8_t* data() const { return GRPC_SLICE_START_PTR(c_slice()); }
86 
87  // Size of the slice
88  size_t size() const { return GRPC_SLICE_LENGTH(c_slice()); }
89  size_t length() const { return size(); }
90  bool empty() const { return size() == 0; }
91 
92  // For inlined slices - are these two slices equal?
93  // For non-inlined slices - do these two slices refer to the same block of
94  // memory?
95  bool is_equivalent(const BaseSlice& other) const {
96  return grpc_slice_is_equivalent(slice_, other.slice_);
97  }
98 
99  uint32_t Hash() const;
100 
101  protected:
103  explicit BaseSlice(const grpc_slice& slice) : slice_(slice) {}
104  ~BaseSlice() = default;
105 
106  void Swap(BaseSlice* other) { std::swap(slice_, other->slice_); }
107  void SetCSlice(const grpc_slice& slice) { slice_ = slice; }
108 
110 
111  grpc_slice* c_slice_ptr() { return &slice_; }
112 
113  private:
115 };
116 
117 inline bool operator==(const BaseSlice& a, const BaseSlice& b) {
118  return grpc_slice_eq(a.c_slice(), b.c_slice()) != 0;
119 }
120 
121 inline bool operator!=(const BaseSlice& a, const BaseSlice& b) {
122  return grpc_slice_eq(a.c_slice(), b.c_slice()) == 0;
123 }
124 
125 inline bool operator==(const BaseSlice& a, absl::string_view b) {
126  return a.as_string_view() == b;
127 }
128 
129 inline bool operator!=(const BaseSlice& a, absl::string_view b) {
130  return a.as_string_view() != b;
131 }
132 
133 inline bool operator==(absl::string_view a, const BaseSlice& b) {
134  return a == b.as_string_view();
135 }
136 
137 inline bool operator!=(absl::string_view a, const BaseSlice& b) {
138  return a != b.as_string_view();
139 }
140 
141 inline bool operator==(const BaseSlice& a, const grpc_slice& b) {
142  return grpc_slice_eq(a.c_slice(), b) != 0;
143 }
144 
145 inline bool operator!=(const BaseSlice& a, const grpc_slice& b) {
146  return grpc_slice_eq(a.c_slice(), b) == 0;
147 }
148 
149 inline bool operator==(const grpc_slice& a, const BaseSlice& b) {
150  return grpc_slice_eq(a, b.c_slice()) != 0;
151 }
152 
153 inline bool operator!=(const grpc_slice& a, const BaseSlice& b) {
154  return grpc_slice_eq(a, b.c_slice()) == 0;
155 }
156 
157 template <typename Out>
159  static Out FromCopiedString(const char* s) {
160  return FromCopiedBuffer(s, strlen(s));
161  }
163  return FromCopiedBuffer(s.data(), s.size());
164  }
165  static Out FromCopiedString(std::string s);
166 
167  static Out FromCopiedBuffer(const char* p, size_t len) {
168  return Out(grpc_slice_from_copied_buffer(p, len));
169  }
170 
171  template <typename Buffer>
172  static Out FromCopiedBuffer(const Buffer& buffer) {
173  return FromCopiedBuffer(reinterpret_cast<const char*>(buffer.data()),
174  buffer.size());
175  }
176 };
177 
178 } // namespace slice_detail
179 
181  public slice_detail::CopyConstructors<MutableSlice> {
182  public:
183  MutableSlice() = default;
184  explicit MutableSlice(const grpc_slice& slice);
185  ~MutableSlice();
186 
187  MutableSlice(const MutableSlice&) = delete;
188  MutableSlice& operator=(const MutableSlice&) = delete;
189  MutableSlice(MutableSlice&& other) noexcept
190  : slice_detail::BaseSlice(other.TakeCSlice()) {}
191  MutableSlice& operator=(MutableSlice&& other) noexcept {
192  Swap(&other);
193  return *this;
194  }
195 
198  }
199 
200  // Return a sub slice of this one. Leaves this slice in an indeterminate but
201  // valid state.
202  MutableSlice TakeSubSlice(size_t pos, size_t n) {
204  }
205 
206  // Iterator access to the underlying bytes
207  uint8_t* begin() { return mutable_data(); }
208  uint8_t* end() { return mutable_data() + size(); }
209  uint8_t* data() { return mutable_data(); }
210 
211  // Array access
212  uint8_t& operator[](size_t i) { return mutable_data()[i]; }
213 };
214 
216  public slice_detail::CopyConstructors<Slice> {
217  public:
218  Slice() = default;
219  ~Slice();
220  explicit Slice(const grpc_slice& slice) : slice_detail::BaseSlice(slice) {}
221  explicit Slice(slice_detail::BaseSlice&& other)
222  : slice_detail::BaseSlice(other.TakeCSlice()) {}
223 
224  Slice(const Slice&) = delete;
225  Slice& operator=(const Slice&) = delete;
226  Slice(Slice&& other) noexcept : slice_detail::BaseSlice(other.TakeCSlice()) {}
227  Slice& operator=(Slice&& other) noexcept {
228  Swap(&other);
229  return *this;
230  }
231 
232  // A slice might refer to some memory that we keep a refcount to (this is
233  // owned), or some memory that's inlined into the slice (also owned), or some
234  // other block of memory that we know will be available for the lifetime of
235  // some operation in the common case (not owned). In the *less common* case
236  // that we need to keep that slice text for longer than our API's guarantee us
237  // access, we need to take a copy and turn this into something that we do own.
238 
239  // TakeOwned returns an owned slice regardless of current ownership, and
240  // leaves the current slice in a valid but externally unpredictable state - in
241  // doing so it can avoid adding a ref to the underlying slice.
242  Slice TakeOwned();
243 
244  // AsOwned returns an owned slice but does not mutate the current slice,
245  // meaning that it may add a reference to the underlying slice.
246  Slice AsOwned() const;
247 
248  // TakeMutable returns a MutableSlice, and leaves the current slice in an
249  // indeterminate but valid state.
250  // A mutable slice requires only one reference to the bytes of the slice -
251  // this can be achieved either with inlined storage or with a single
252  // reference.
253  // If the current slice is refcounted and there are more than one references
254  // to that slice, then the slice is copied in order to achieve a mutable
255  // version.
257 
258  // Return a sub slice of this one. Leaves this slice in an indeterminate but
259  // valid state.
260  Slice TakeSubSlice(size_t pos, size_t n) {
262  }
263 
264  // Return a sub slice of this one. Adds a reference to the underlying slice.
265  Slice RefSubSlice(size_t pos, size_t n) const {
266  return Slice(grpc_slice_sub(c_slice(), pos, pos + n));
267  }
268 
269  // Split this slice, returning a new slice containing (split:end] and
270  // leaving this slice with [begin:split).
271  Slice Split(size_t split) {
273  }
274 
275  Slice Ref() const;
276 
277  Slice Copy() const { return Slice(grpc_slice_copy(c_slice())); }
278 
280  const uint8_t* begin, const uint8_t* end);
281 };
282 
283 } // namespace experimental
284 } // namespace grpc_event_engine
285 
286 #endif // GRPC_EVENT_ENGINE_SLICE_H
grpc_event_engine::experimental::Slice::Slice
Slice()=default
grpc_event_engine::experimental::slice_detail::BaseSlice::is_equivalent
bool is_equivalent(const BaseSlice &other) const
Definition: include/grpc/event_engine/slice.h:95
grpc_event_engine::experimental::MutableSlice::operator=
MutableSlice & operator=(const MutableSlice &)=delete
grpc_event_engine::experimental::MutableSlice::TakeSubSlice
MutableSlice TakeSubSlice(size_t pos, size_t n)
Definition: include/grpc/event_engine/slice.h:202
grpc_event_engine::experimental::slice_detail::BaseSlice::cend
const uint8_t * cend() const
Definition: include/grpc/event_engine/slice.h:61
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
grpc_event_engine::experimental::slice_detail::BaseSlice::BaseSlice
BaseSlice()
Definition: include/grpc/event_engine/slice.h:102
log.h
grpc_event_engine::experimental::slice_detail::CopyConstructors::FromCopiedString
static Out FromCopiedString(const char *s)
Definition: include/grpc/event_engine/slice.h:159
grpc_event_engine::experimental::slice_detail::operator==
bool operator==(const BaseSlice &a, const BaseSlice &b)
Definition: include/grpc/event_engine/slice.h:117
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
grpc_event_engine::experimental::slice_detail::BaseSlice::Swap
void Swap(BaseSlice *other)
Definition: include/grpc/event_engine/slice.h:106
grpc_event_engine::experimental::MutableSlice::end
uint8_t * end()
Definition: include/grpc/event_engine/slice.h:208
grpc_event_engine::experimental::slice_detail::CopyConstructors
Definition: include/grpc/event_engine/slice.h:158
slice.h
grpc_event_engine::experimental::Slice::Split
Slice Split(size_t split)
Definition: include/grpc/event_engine/slice.h:271
string.h
grpc_event_engine::experimental::Slice::Slice
Slice(slice_detail::BaseSlice &&other)
Definition: include/grpc/event_engine/slice.h:221
grpc_event_engine::experimental::Slice::RefSubSlice
Slice RefSubSlice(size_t pos, size_t n) const
Definition: include/grpc/event_engine/slice.h:265
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_event_engine::experimental::MutableSlice::~MutableSlice
~MutableSlice()
Definition: event_engine/slice.cc:54
grpc_event_engine::experimental::Slice::~Slice
~Slice()
Definition: event_engine/slice.cc:87
grpc_slice_sub
GPRAPI grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end)
Definition: slice/slice.cc:268
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
grpc_event_engine::experimental::slice_detail::BaseSlice::end
const uint8_t * end() const
Definition: include/grpc/event_engine/slice.h:59
grpc_event_engine::experimental::MutableSlice::MutableSlice
MutableSlice(MutableSlice &&other) noexcept
Definition: include/grpc/event_engine/slice.h:189
grpc_slice_sub_no_ref
GPRAPI grpc_slice grpc_slice_sub_no_ref(grpc_slice s, size_t begin, size_t end)
Definition: slice/slice.cc:264
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc_event_engine::experimental::slice_detail::BaseSlice::BaseSlice
BaseSlice(const grpc_slice &slice)
Definition: include/grpc/event_engine/slice.h:103
grpc_event_engine::experimental::MutableSlice::operator=
MutableSlice & operator=(MutableSlice &&other) noexcept
Definition: include/grpc/event_engine/slice.h:191
grpc_slice_malloc
GPRAPI grpc_slice grpc_slice_malloc(size_t length)
Definition: slice/slice.cc:227
grpc_event_engine::experimental::slice_detail::EmptySlice
static constexpr grpc_slice EmptySlice()
Definition: include/grpc/event_engine/slice.h:45
grpc_event_engine::experimental::MutableSlice::operator[]
uint8_t & operator[](size_t i)
Definition: include/grpc/event_engine/slice.h:212
grpc_event_engine::experimental::slice_detail::BaseSlice
Definition: include/grpc/event_engine/slice.h:50
grpc_event_engine::experimental::slice_detail::BaseSlice::cbegin
const uint8_t * cbegin() const
Definition: include/grpc/event_engine/slice.h:60
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_event_engine::experimental::slice_detail::BaseSlice::as_string_view
absl::string_view as_string_view() const
Definition: include/grpc/event_engine/slice.h:75
grpc_event_engine::experimental::Slice::Slice
Slice(const grpc_slice &slice)
Definition: include/grpc/event_engine/slice.h:220
grpc_event_engine::experimental::slice_detail::CopyConstructors::FromCopiedBuffer
static Out FromCopiedBuffer(const char *p, size_t len)
Definition: include/grpc/event_engine/slice.h:167
grpc_event_engine::experimental::slice_detail::BaseSlice::empty
bool empty() const
Definition: include/grpc/event_engine/slice.h:90
grpc_event_engine::experimental::slice_detail::BaseSlice::SetCSlice
void SetCSlice(const grpc_slice &slice)
Definition: include/grpc/event_engine/slice.h:107
grpc_slice_is_equivalent
GPRAPI int grpc_slice_is_equivalent(grpc_slice a, grpc_slice b)
Definition: slice/slice.cc:433
grpc_event_engine::experimental::Slice::Slice
Slice(Slice &&other) noexcept
Definition: include/grpc/event_engine/slice.h:226
grpc_event_engine::experimental::slice_detail::CopyConstructors::FromCopiedString
static Out FromCopiedString(absl::string_view s)
Definition: include/grpc/event_engine/slice.h:162
grpc_event_engine::experimental::slice_detail::BaseSlice::c_slice_ptr
grpc_slice * c_slice_ptr()
Definition: include/grpc/event_engine/slice.h:111
grpc_event_engine::experimental::Slice::TakeOwned
Slice TakeOwned()
Definition: event_engine/slice.cc:56
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc_event_engine::experimental::slice_detail::operator!=
bool operator!=(const BaseSlice &a, const BaseSlice &b)
Definition: include/grpc/event_engine/slice.h:121
grpc_event_engine::experimental::Slice::TakeSubSlice
Slice TakeSubSlice(size_t pos, size_t n)
Definition: include/grpc/event_engine/slice.h:260
grpc_slice_refcount
Definition: slice_refcount_base.h:25
grpc_event_engine::experimental::Slice::operator=
Slice & operator=(Slice &&other) noexcept
Definition: include/grpc/event_engine/slice.h:227
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_event_engine::experimental::Slice::operator=
Slice & operator=(const Slice &)=delete
grpc_event_engine::experimental::Slice::TakeMutable
MutableSlice TakeMutable()
Definition: event_engine/slice.cc:76
std::swap
void swap(Json::Value &a, Json::Value &b)
Specialize std::swap() for Json::Value.
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:1226
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_event_engine::experimental::MutableSlice::MutableSlice
MutableSlice()=default
grpc_event_engine::experimental::MutableSlice
Definition: include/grpc/event_engine/slice.h:180
grpc_event_engine::experimental::slice_detail::BaseSlice::mutable_data
uint8_t * mutable_data()
Definition: include/grpc/event_engine/slice.h:109
grpc_event_engine::experimental::slice_detail::BaseSlice::size
size_t size() const
Definition: include/grpc/event_engine/slice.h:88
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
grpc_event_engine::experimental::slice_detail::BaseSlice::length
size_t length() const
Definition: include/grpc/event_engine/slice.h:89
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
GRPC_SLICE_END_PTR
#define GRPC_SLICE_END_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:110
grpc_event_engine::experimental::MutableSlice::data
uint8_t * data()
Definition: include/grpc/event_engine/slice.h:209
grpc_event_engine::experimental::slice_detail::BaseSlice::begin
const uint8_t * begin() const
Definition: include/grpc/event_engine/slice.h:58
grpc_event_engine::experimental::slice_detail::BaseSlice::data
const uint8_t * data() const
Definition: include/grpc/event_engine/slice.h:85
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
grpc_event_engine::experimental::slice_detail::BaseSlice::slice_
grpc_slice slice_
Definition: include/grpc/event_engine/slice.h:114
grpc_event_engine::experimental::Slice::AsOwned
Slice AsOwned() const
Definition: event_engine/slice.cc:66
grpc_slice_from_copied_buffer
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Definition: slice/slice.cc:170
grpc_event_engine::experimental::Slice::FromRefcountAndBytes
static Slice FromRefcountAndBytes(grpc_slice_refcount *r, const uint8_t *begin, const uint8_t *end)
Definition: event_engine/slice.cc:91
grpc_event_engine::experimental::slice_detail::BaseSlice::~BaseSlice
~BaseSlice()=default
grpc_event_engine::experimental::Slice::Copy
Slice Copy() const
Definition: include/grpc/event_engine/slice.h:277
fix_build_deps.r
r
Definition: fix_build_deps.py:491
string_view
absl::string_view string_view
Definition: attr.cc:22
grpc_event_engine
Definition: endpoint_config.h:24
grpc_event_engine::experimental::slice_detail::BaseSlice::Hash
uint32_t Hash() const
Definition: event_engine/slice.cc:35
grpc_event_engine::experimental::slice_detail::CopyConstructors::FromCopiedBuffer
static Out FromCopiedBuffer(const Buffer &buffer)
Definition: include/grpc/event_engine/slice.h:172
grpc_event_engine::experimental::MutableSlice::begin
uint8_t * begin()
Definition: include/grpc/event_engine/slice.h:207
grpc_event_engine::experimental::Slice::Ref
Slice Ref() const
Definition: event_engine/slice.cc:89
grpc_event_engine::experimental::Slice
Definition: include/grpc/event_engine/slice.h:215
grpc_event_engine::experimental::slice_detail::BaseSlice::TakeCSlice
grpc_slice TakeCSlice()
Definition: include/grpc/event_engine/slice.h:68
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
split
static void split(const char *s, char ***ss, size_t *ns)
Definition: debug/trace.cc:111
grpc_event_engine::experimental::MutableSlice::CreateUninitialized
static MutableSlice CreateUninitialized(size_t length)
Definition: include/grpc/event_engine/slice.h:196
grpc_event_engine::experimental::slice_detail::BaseSlice::operator[]
uint8_t operator[](size_t i) const
Definition: include/grpc/event_engine/slice.h:80
grpc_slice_eq
GPRAPI int grpc_slice_eq(grpc_slice a, grpc_slice b)
Definition: slice/slice.cc:387
grpc_event_engine::experimental::slice_detail::BaseSlice::c_slice
const grpc_slice & c_slice() const
Definition: include/grpc/event_engine/slice.h:64
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_slice_split_tail
GPRAPI grpc_slice grpc_slice_split_tail(grpc_slice *s, size_t split)
Definition: slice/slice.cc:343
grpc_event_engine::experimental::slice_detail::BaseSlice::operator=
BaseSlice & operator=(const BaseSlice &)=delete
grpc_slice_copy
GPRAPI grpc_slice grpc_slice_copy(grpc_slice s)
Definition: slice/slice.cc:46
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:13