GRPC Core  43.0.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 
18 #include <string.h>
19 
20 #include <cstdint>
21 #include <string>
22 
23 #include "absl/strings/string_view.h"
24 #include "absl/utility/utility.h"
25 
29 #include <grpc/slice.h>
30 #include <grpc/slice_buffer.h>
31 #include <grpc/support/log.h>
33 
34 namespace grpc_event_engine {
35 namespace experimental {
36 
52 class SliceBuffer {
53  public:
54  SliceBuffer() { grpc_slice_buffer_init(&slice_buffer_); }
55  SliceBuffer(const SliceBuffer& other) = delete;
56  SliceBuffer(SliceBuffer&& other) noexcept
57  : slice_buffer_(other.slice_buffer_) {
58  grpc_slice_buffer_init(&slice_buffer_);
59  grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
60  }
63  ~SliceBuffer() { grpc_slice_buffer_destroy(&slice_buffer_); }
64 
65  SliceBuffer& operator=(const SliceBuffer&) = delete;
66  SliceBuffer& operator=(SliceBuffer&& other) noexcept {
67  grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
68  return *this;
69  }
70 
72  void Swap(SliceBuffer& other) {
73  grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
74  }
75 
78  void Append(Slice slice);
79 
82  size_t AppendIndexed(Slice slice);
83 
85  size_t Count() { return slice_buffer_.count; }
86 
88  void RemoveLastNBytes(size_t n) {
89  grpc_slice_buffer_trim_end(&slice_buffer_, n, nullptr);
90  }
91 
93  void MoveFirstNBytesIntoBuffer(size_t n, void* dst) {
94  grpc_slice_buffer_move_first_into_buffer(&slice_buffer_, n, dst);
95  }
96 
99  void MoveLastNBytesIntoSliceBuffer(size_t n, SliceBuffer& other) {
100  grpc_slice_buffer_trim_end(&slice_buffer_, n, &other.slice_buffer_);
101  }
102 
105  grpc_slice_buffer_move_first(&slice_buffer_, n, &other.slice_buffer_);
106  }
107 
109  void Clear() { grpc_slice_buffer_reset_and_unref(&slice_buffer_); }
110 
112  Slice TakeFirst();
113 
115  void Prepend(Slice slice);
116 
119  Slice RefSlice(size_t index);
120 
123  const Slice& operator[](size_t index) const {
124  return internal::SliceCast<Slice>(slice_buffer_.slices[index]);
125  }
126 
128  Slice& MutableSliceAt(size_t index) const {
129  return internal::SliceCast<Slice>(slice_buffer_.slices[index]);
130  }
131 
133  size_t Length() const { return slice_buffer_.length; }
134 
136  grpc_slice_buffer* c_slice_buffer() { return &slice_buffer_; }
137 
138  // Returns a SliceBuffer that transfers slices into this new SliceBuffer,
139  // leaving the input parameter empty.
141  return SliceBuffer(&slice_buffer);
142  }
143 
144  private:
145  // Transfers slices into this new SliceBuffer, leaving the parameter empty.
146  // Does not take ownership of the slice_buffer argument.
147  explicit SliceBuffer(grpc_slice_buffer* slice_buffer) {
148  grpc_slice_buffer_init(&slice_buffer_);
149  grpc_slice_buffer_swap(&slice_buffer_, slice_buffer);
150  }
152  grpc_slice_buffer slice_buffer_;
153 };
154 
155 } // namespace experimental
156 } // namespace grpc_event_engine
157 
158 #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:140
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:72
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:221
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:123
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:136
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:109
grpc_event_engine::experimental::SliceBuffer
A Wrapper around grpc_slice_buffer pointer.
Definition: slice_buffer.h:52
grpc_event_engine::experimental::SliceBuffer::RemoveLastNBytes
void RemoveLastNBytes(size_t n)
Removes/deletes the last n bytes in the SliceBuffer.
Definition: slice_buffer.h:88
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:104
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.h
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:133
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:128
grpc_event_engine::experimental::SliceBuffer::Count
size_t Count()
Returns the number of slices held by the SliceBuffer.
Definition: slice_buffer.h:85
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:93
grpc_event_engine::experimental::SliceBuffer::operator=
SliceBuffer & operator=(SliceBuffer &&other) noexcept
Definition: slice_buffer.h:66
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:56
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:63
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:54
grpc_event_engine::experimental::SliceBuffer::Prepend
void Prepend(Slice slice)
Prepends the slice to the the front of the SliceBuffer.
slice.h
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:99