GRPC Core  44.2.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 
21 #include <grpc/slice.h>
22 #include <grpc/slice_buffer.h>
24 #include <string.h>
25 
26 #include <cstdint>
27 #include <string>
28 
29 #include "absl/strings/string_view.h"
30 #include "absl/utility/utility.h"
31 
32 namespace grpc_event_engine {
33 namespace experimental {
34 
50 class SliceBuffer {
51  public:
52  SliceBuffer() { grpc_slice_buffer_init(&slice_buffer_); }
53  SliceBuffer(const SliceBuffer& other) = delete;
54  SliceBuffer(SliceBuffer&& other) noexcept
55  : slice_buffer_(other.slice_buffer_) {
56  grpc_slice_buffer_init(&slice_buffer_);
57  grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
58  }
61  ~SliceBuffer() { grpc_slice_buffer_destroy(&slice_buffer_); }
62 
63  SliceBuffer& operator=(const SliceBuffer&) = delete;
64  SliceBuffer& operator=(SliceBuffer&& other) noexcept {
65  grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
66  return *this;
67  }
68 
70  void Swap(SliceBuffer& other) {
71  grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
72  }
73 
76  void Append(Slice slice);
77 
80  size_t AppendIndexed(Slice slice);
81 
83  size_t Count() { return slice_buffer_.count; }
84 
86  void RemoveLastNBytes(size_t n) {
87  grpc_slice_buffer_trim_end(&slice_buffer_, n, nullptr);
88  }
89 
91  void MoveFirstNBytesIntoBuffer(size_t n, void* dst) {
92  grpc_slice_buffer_move_first_into_buffer(&slice_buffer_, n, dst);
93  }
94 
97  void MoveLastNBytesIntoSliceBuffer(size_t n, SliceBuffer& other) {
98  grpc_slice_buffer_trim_end(&slice_buffer_, n, &other.slice_buffer_);
99  }
100 
103  grpc_slice_buffer_move_first(&slice_buffer_, n, &other.slice_buffer_);
104  }
105 
107  void Clear() { grpc_slice_buffer_reset_and_unref(&slice_buffer_); }
108 
110  Slice TakeFirst();
111 
113  void Prepend(Slice slice);
114 
117  Slice RefSlice(size_t index);
118 
121  const Slice& operator[](size_t index) const {
122  return internal::SliceCast<Slice>(slice_buffer_.slices[index]);
123  }
124 
126  Slice& MutableSliceAt(size_t index) const {
127  return internal::SliceCast<Slice>(slice_buffer_.slices[index]);
128  }
129 
131  size_t Length() const { return slice_buffer_.length; }
132 
134  grpc_slice_buffer* c_slice_buffer() { return &slice_buffer_; }
135 
136  // Returns a SliceBuffer that transfers slices into this new SliceBuffer,
137  // leaving the input parameter empty.
139  return SliceBuffer(&slice_buffer);
140  }
141 
142  private:
143  // Transfers slices into this new SliceBuffer, leaving the parameter empty.
144  // Does not take ownership of the slice_buffer argument.
145  explicit SliceBuffer(grpc_slice_buffer* slice_buffer) {
146  grpc_slice_buffer_init(&slice_buffer_);
147  grpc_slice_buffer_swap(&slice_buffer_, slice_buffer);
148  }
150  grpc_slice_buffer slice_buffer_;
151 };
152 
153 } // namespace experimental
154 } // namespace grpc_event_engine
155 
156 #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:138
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:70
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:219
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:86
grpc_event_engine::experimental::SliceBuffer::operator[]
const Slice & operator[](size_t index) const
Array access into the SliceBuffer.
Definition: slice_buffer.h:121
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:134
grpc_slice_buffer::count
size_t count
the number of slices in the array
Definition: slice_type.h:88
grpc_event_engine::experimental::SliceBuffer::Clear
void Clear()
Removes and unrefs all slices in the SliceBuffer.
Definition: slice_buffer.h:107
grpc_event_engine::experimental::SliceBuffer
A Wrapper around grpc_slice_buffer pointer.
Definition: slice_buffer.h:50
grpc_event_engine::experimental::SliceBuffer::RemoveLastNBytes
void RemoveLastNBytes(size_t n)
Removes/deletes the last n bytes in the SliceBuffer.
Definition: slice_buffer.h:86
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:93
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:102
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:131
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:126
grpc_event_engine::experimental::SliceBuffer::Count
size_t Count()
Returns the number of slices held by the SliceBuffer.
Definition: slice_buffer.h:83
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:91
grpc_event_engine::experimental::SliceBuffer::operator=
SliceBuffer & operator=(SliceBuffer &&other) noexcept
Definition: slice_buffer.h:64
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:54
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:61
grpc_slice_buffer
Represents an expandable array of slices, to be interpreted as a single item.
Definition: slice_type.h:80
grpc_event_engine::experimental::SliceBuffer::SliceBuffer
SliceBuffer()
Definition: slice_buffer.h:52
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:97