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