GRPC C++  1.62.0
slice_buffer.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_BUFFER_H
16 #define GRPC_EVENT_ENGINE_SLICE_BUFFER_H
17 
19 
20 #include <string.h>
21 
22 #include <cstdint>
23 #include <string>
24 
25 #include "absl/strings/string_view.h"
26 #include "absl/utility/utility.h"
27 
31 #include <grpc/slice.h>
32 #include <grpc/slice_buffer.h>
33 #include <grpc/support/log.h>
34 
35 namespace grpc_event_engine {
36 namespace experimental {
37 
53 class SliceBuffer {
54  public:
55  SliceBuffer() { grpc_slice_buffer_init(&slice_buffer_); }
56  SliceBuffer(const SliceBuffer& other) = delete;
57  SliceBuffer(SliceBuffer&& other) noexcept
58  : slice_buffer_(other.slice_buffer_) {
59  grpc_slice_buffer_init(&slice_buffer_);
60  grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
61  }
64  ~SliceBuffer() { grpc_slice_buffer_destroy(&slice_buffer_); }
65 
66  SliceBuffer& operator=(const SliceBuffer&) = delete;
67  SliceBuffer& operator=(SliceBuffer&& other) noexcept {
68  grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
69  return *this;
70  }
71 
73  void Swap(SliceBuffer& other) {
74  grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
75  }
76 
79  void Append(Slice slice);
80 
83  size_t AppendIndexed(Slice slice);
84 
86  size_t Count() { return slice_buffer_.count; }
87 
89  void RemoveLastNBytes(size_t n) {
90  grpc_slice_buffer_trim_end(&slice_buffer_, n, nullptr);
91  }
92 
94  void MoveFirstNBytesIntoBuffer(size_t n, void* dst) {
95  grpc_slice_buffer_move_first_into_buffer(&slice_buffer_, n, dst);
96  }
97 
101  grpc_slice_buffer_trim_end(&slice_buffer_, n, &other.slice_buffer_);
102  }
103 
106  grpc_slice_buffer_move_first(&slice_buffer_, n, &other.slice_buffer_);
107  }
108 
110  void Clear() { grpc_slice_buffer_reset_and_unref(&slice_buffer_); }
111 
113  Slice TakeFirst();
114 
116  void Prepend(Slice slice);
117 
120  Slice RefSlice(size_t index);
121 
124  const Slice& operator[](size_t index) const {
125  return internal::SliceCast<Slice>(slice_buffer_.slices[index]);
126  }
127 
129  Slice& MutableSliceAt(size_t index) const {
130  return internal::SliceCast<Slice>(slice_buffer_.slices[index]);
131  }
132 
134  size_t Length() const { return slice_buffer_.length; }
135 
137  grpc_slice_buffer* c_slice_buffer() { return &slice_buffer_; }
138 
139  // Returns a SliceBuffer that transfers slices into this new SliceBuffer,
140  // leaving the input parameter empty.
142  return SliceBuffer(&slice_buffer);
143  }
144 
145  private:
146  // Transfers slices into this new SliceBuffer, leaving the parameter empty.
147  // Does not take ownership of the slice_buffer argument.
148  explicit SliceBuffer(grpc_slice_buffer* slice_buffer) {
149  grpc_slice_buffer_init(&slice_buffer_);
150  grpc_slice_buffer_swap(&slice_buffer_, slice_buffer);
151  }
153  grpc_slice_buffer slice_buffer_;
154 };
155 
156 } // namespace experimental
157 } // namespace grpc_event_engine
158 
159 #endif // GRPC_EVENT_ENGINE_SLICE_BUFFER_H
grpc_event_engine::experimental::SliceBuffer::TakeCSliceBuffer
static SliceBuffer TakeCSliceBuffer(grpc_slice_buffer &slice_buffer)
Definition: slice_buffer.h:141
grpc_slice_buffer_destroy
GPRAPI void grpc_slice_buffer_destroy(grpc_slice_buffer *sb)
destroy a slice buffer - unrefs any held elements
grpc_event_engine::experimental::SliceBuffer::Swap
void Swap(SliceBuffer &other)
Swap the contents of this SliceBuffer with the contents of another.
Definition: slice_buffer.h:73
grpc_event_engine::experimental::SliceBuffer::Append
void Append(Slice slice)
Appends a new slice into the SliceBuffer and makes an attempt to merge this slice with the last slice...
grpc_event_engine::experimental::SliceBuffer::RefSlice
Slice RefSlice(size_t index)
Increased the ref-count of slice at the specified index and returns the associated slice.
grpc_event_engine::experimental::SliceBuffer::AppendIndexed
size_t AppendIndexed(Slice slice)
Adds a new slice into the SliceBuffer at the next available index.
grpc_event_engine::experimental::Slice
Definition: slice.h:222
grpc_slice_buffer::slices
grpc_slice * slices
slices in the array (Points to the first valid grpc_slice in the array)
Definition: slice_type.h:87
grpc_event_engine::experimental::SliceBuffer::operator[]
const Slice & operator[](size_t index) const
Array access into the SliceBuffer.
Definition: slice_buffer.h:124
slice.h
grpc_slice_buffer_move_first
GPRAPI void grpc_slice_buffer_move_first(grpc_slice_buffer *src, size_t n, grpc_slice_buffer *dst)
move the first n bytes of src into dst
grpc_event_engine::experimental::SliceBuffer::c_slice_buffer
grpc_slice_buffer * c_slice_buffer()
Return a pointer to the back raw grpc_slice_buffer.
Definition: slice_buffer.h:137
log.h
grpc_slice_buffer::count
size_t count
the number of slices in the array
Definition: slice_type.h:89
grpc_event_engine::experimental::SliceBuffer::Clear
void Clear()
Removes and unrefs all slices in the SliceBuffer.
Definition: slice_buffer.h:110
grpc_event_engine::experimental::SliceBuffer
A Wrapper around grpc_slice_buffer pointer.
Definition: slice_buffer.h:53
grpc_event_engine::experimental::SliceBuffer::RemoveLastNBytes
void RemoveLastNBytes(size_t n)
Removes/deletes the last n bytes in the SliceBuffer.
Definition: slice_buffer.h:89
grpc_event_engine::experimental::SliceBuffer::operator=
SliceBuffer & operator=(const SliceBuffer &)=delete
slice_cast.h
grpc_slice_buffer::length
size_t length
the combined length of all slices in the array
Definition: slice_type.h:94
grpc_event_engine::experimental::SliceBuffer::MoveFirstNBytesIntoSliceBuffer
void MoveFirstNBytesIntoSliceBuffer(size_t n, SliceBuffer &other)
Move the first n bytes of the SliceBuffer into the other SliceBuffer.
Definition: slice_buffer.h:105
grpc_slice_buffer_reset_and_unref
GPRAPI void grpc_slice_buffer_reset_and_unref(grpc_slice_buffer *sb)
clear a slice buffer, unref all elements
slice_buffer.h
grpc_event_engine::experimental::SliceBuffer::Length
size_t Length() const
The total number of bytes held by the SliceBuffer.
Definition: slice_buffer.h:134
grpc_slice_buffer_init
GPRAPI void grpc_slice_buffer_init(grpc_slice_buffer *sb)
initialize a slice buffer
grpc_slice_buffer_trim_end
GPRAPI void grpc_slice_buffer_trim_end(grpc_slice_buffer *sb, size_t n, grpc_slice_buffer *garbage)
remove n bytes from the end of a slice buffer
grpc_event_engine::experimental::SliceBuffer::MutableSliceAt
Slice & MutableSliceAt(size_t index) const
Return mutable reference to the slice at the specified index.
Definition: slice_buffer.h:129
slice.h
grpc_event_engine::experimental::SliceBuffer::Count
size_t Count()
Returns the number of slices held by the SliceBuffer.
Definition: slice_buffer.h:86
grpc_event_engine
Definition: endpoint_config.h:24
grpc_event_engine::experimental::SliceBuffer::MoveFirstNBytesIntoBuffer
void MoveFirstNBytesIntoBuffer(size_t n, void *dst)
Move the first n bytes of the SliceBuffer into a memory pointed to by dst.
Definition: slice_buffer.h:94
grpc_event_engine::experimental::SliceBuffer::operator=
SliceBuffer & operator=(SliceBuffer &&other) noexcept
Definition: slice_buffer.h:67
grpc_slice_buffer_move_first_into_buffer
GPRAPI void grpc_slice_buffer_move_first_into_buffer(grpc_slice_buffer *src, size_t n, void *dst)
move the first n bytes of src into dst (copying them)
grpc_event_engine::experimental::SliceBuffer::TakeFirst
Slice TakeFirst()
Removes the first slice in the SliceBuffer and returns it.
grpc_event_engine::experimental::SliceBuffer::SliceBuffer
SliceBuffer(SliceBuffer &&other) noexcept
Definition: slice_buffer.h:57
grpc_event_engine::experimental::SliceBuffer::~SliceBuffer
~SliceBuffer()
Upon destruction, the underlying raw slice buffer is cleaned out and all slices are unreffed.
Definition: slice_buffer.h:64
grpc_slice_buffer
Represents an expandable array of slices, to be interpreted as a single item.
Definition: slice_type.h:81
grpc_event_engine::experimental::SliceBuffer::SliceBuffer
SliceBuffer()
Definition: slice_buffer.h:55
grpc_event_engine::experimental::SliceBuffer::Prepend
void Prepend(Slice slice)
Prepends the slice to the the front of the SliceBuffer.
grpc_slice_buffer_swap
GPRAPI void grpc_slice_buffer_swap(grpc_slice_buffer *a, grpc_slice_buffer *b)
swap the contents of two slice buffers
slice.h
port_platform.h
grpc_event_engine::experimental::SliceBuffer::MoveLastNBytesIntoSliceBuffer
void MoveLastNBytesIntoSliceBuffer(size_t n, SliceBuffer &other)
Removes/deletes the last n bytes in the SliceBuffer and add it to the other SliceBuffer.
Definition: slice_buffer.h:100