xref: /aosp_15_r20/external/grpc-grpc/include/grpc/event_engine/slice_buffer.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 
26 #include <grpc/event_engine/internal/slice_cast.h>
27 #include <grpc/event_engine/slice.h>
28 #include <grpc/impl/codegen/slice.h>
29 #include <grpc/slice.h>
30 #include <grpc/slice_buffer.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/port_platform.h>
33 
34 namespace grpc_event_engine {
35 namespace experimental {
36 
37 /// A Wrapper around \a grpc_slice_buffer pointer.
38 ///
39 /// A slice buffer holds the memory for a collection of slices.
40 /// The SliceBuffer object itself is meant to only hide the C-style API,
41 /// and won't hold the data itself. In terms of lifespan, the
42 /// grpc_slice_buffer ought to be kept somewhere inside the caller's objects,
43 /// like a transport or an endpoint.
44 ///
45 /// This lifespan rule is likely to change in the future, as we may
46 /// collapse the grpc_slice_buffer structure straight into this class.
47 ///
48 /// The SliceBuffer API is basically a replica of the grpc_slice_buffer's,
49 /// and its documentation will move here once we remove the C structure,
50 /// which should happen before the EventEngine's API is no longer
51 /// an experimental API.
52 class SliceBuffer {
53  public:
SliceBuffer()54   SliceBuffer() { grpc_slice_buffer_init(&slice_buffer_); }
55   SliceBuffer(const SliceBuffer& other) = delete;
SliceBuffer(SliceBuffer && other)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   }
61   /// Upon destruction, the underlying raw slice buffer is cleaned out and all
62   /// slices are unreffed.
~SliceBuffer()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 
71   /// Swap the contents of this SliceBuffer with the contents of another.
Swap(SliceBuffer & other)72   void Swap(SliceBuffer& other) {
73     grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
74   }
75 
76   /// Appends a new slice into the SliceBuffer and makes an attempt to merge
77   /// this slice with the last slice in the SliceBuffer.
78   void Append(Slice slice);
79 
80   /// Adds a new slice into the SliceBuffer at the next available index.
81   /// Returns the index at which the new slice is added.
82   size_t AppendIndexed(Slice slice);
83 
84   /// Returns the number of slices held by the SliceBuffer.
Count()85   size_t Count() { return slice_buffer_.count; }
86 
87   /// Removes/deletes the last n bytes in the SliceBuffer.
RemoveLastNBytes(size_t n)88   void RemoveLastNBytes(size_t n) {
89     grpc_slice_buffer_trim_end(&slice_buffer_, n, nullptr);
90   }
91 
92   /// Move the first n bytes of the SliceBuffer into a memory pointed to by dst.
MoveFirstNBytesIntoBuffer(size_t n,void * dst)93   void MoveFirstNBytesIntoBuffer(size_t n, void* dst) {
94     grpc_slice_buffer_move_first_into_buffer(&slice_buffer_, n, dst);
95   }
96 
97   /// Removes/deletes the last n bytes in the SliceBuffer and add it to the
98   /// other SliceBuffer
MoveLastNBytesIntoSliceBuffer(size_t n,SliceBuffer & other)99   void MoveLastNBytesIntoSliceBuffer(size_t n, SliceBuffer& other) {
100     grpc_slice_buffer_trim_end(&slice_buffer_, n, &other.slice_buffer_);
101   }
102 
103   /// Move the first n bytes of the SliceBuffer into the other SliceBuffer
MoveFirstNBytesIntoSliceBuffer(size_t n,SliceBuffer & other)104   void MoveFirstNBytesIntoSliceBuffer(size_t n, SliceBuffer& other) {
105     grpc_slice_buffer_move_first(&slice_buffer_, n, &other.slice_buffer_);
106   }
107 
108   /// Removes and unrefs all slices in the SliceBuffer.
Clear()109   void Clear() { grpc_slice_buffer_reset_and_unref(&slice_buffer_); }
110 
111   /// Removes the first slice in the SliceBuffer and returns it.
112   Slice TakeFirst();
113 
114   /// Prepends the slice to the the front of the SliceBuffer.
115   void Prepend(Slice slice);
116 
117   /// Increased the ref-count of slice at the specified index and returns the
118   /// associated slice.
119   Slice RefSlice(size_t index);
120 
121   /// Array access into the SliceBuffer. It returns a non mutable reference to
122   /// the slice at the specified index
123   const Slice& operator[](size_t index) const {
124     return internal::SliceCast<Slice>(slice_buffer_.slices[index]);
125   }
126 
127   /// Return mutable reference to the slice at the specified index
MutableSliceAt(size_t index)128   Slice& MutableSliceAt(size_t index) const {
129     return internal::SliceCast<Slice>(slice_buffer_.slices[index]);
130   }
131 
132   /// The total number of bytes held by the SliceBuffer
Length()133   size_t Length() const { return slice_buffer_.length; }
134 
135   /// Return a pointer to the back raw grpc_slice_buffer
c_slice_buffer()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.
TakeCSliceBuffer(grpc_slice_buffer & slice_buffer)140   static SliceBuffer TakeCSliceBuffer(grpc_slice_buffer& slice_buffer) {
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.
SliceBuffer(grpc_slice_buffer * slice_buffer)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   }
151   /// The backing raw slice buffer.
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
159