Debian Codegraph @

src/core/lib/slice/slice_buffer.h

raw
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_CORE_LIB_SLICE_SLICE_BUFFER_Habc_SLICE_BUFFER_H
16 #define GRPC_CORE_LIB_SLICE_SLICE_BUFFER_Habc_SLICE_BUFFER_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <string.h>
21
22 #include <string>
23
24 #include <grpc/slice.h>
25 #include <grpc/slice_buffer.h>
26
27 #include "src/core/lib/slice/slice.h"
28
29 namespace grpc_core {
30
31 /// A slice buffer holds the memory for a collection of slices.
32 /// The SliceBuffer object itself is meant to only hide the C-style API,
33 /// and won't hold the data itself. In terms of lifespan, the
34 /// grpc_slice_buffer ought to be kept somewhere inside the caller's objects,
35 /// like a transport or an endpoint.
36 ///
37 /// This lifespan rule is likely to change in the future, as we may
38 /// collapse the grpc_slice_buffer structure straight into this class.
39 ///
40 /// The SliceBuffer API is basically a replica of the grpc_slice_buffer's,
41 /// and its documentation will move here once we remove the C structure,
42 /// which should happen before the Event Engine's API is no longer
43 /// an experimental API.
44 class SliceBuffer {
45 public:
46 explicit SliceBuffer() { grpc_slice_buffer_init(&slice_buffer_); }
47 SliceBuffer(const SliceBuffer& other) = delete;
48 SliceBuffer(SliceBuffer&& other) noexcept {
49 grpc_slice_buffer_init(&slice_buffer_);
50 grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
51 }
52 /// Upon destruction, the underlying raw slice buffer is cleaned out and all
53 /// slices are unreffed.
54 ~SliceBuffer() { grpc_slice_buffer_destroy(&slice_buffer_); }
55
56 SliceBuffer& operator=(const SliceBuffer&) = delete;
57 SliceBuffer& operator=(SliceBuffer&& other) noexcept {
58 grpc_slice_buffer_swap(&slice_buffer_, &other.slice_buffer_);
59 return *this;
60 }
61
62 /// Appends a new slice into the SliceBuffer and makes an attempt to merge
63 /// this slice with the last slice in the SliceBuffer.
64 void Append(Slice slice);
65
66 /// Adds a new slice into the SliceBuffer at the next available index.
67 /// Returns the index at which the new slice is added.
68 size_t AppendIndexed(Slice slice);
69
70 /// Returns the number of slices held by the SliceBuffer.
71 size_t Count() const { return slice_buffer_.count; }
72
73 /// Removes/deletes the last n bytes in the SliceBuffer.
74 void RemoveLastNBytes(size_t n) {
75 grpc_slice_buffer_trim_end(&slice_buffer_, n, nullptr);
76 }
77
78 /// Move the first n bytes of the SliceBuffer into a memory pointed to by dst.
79 void MoveFirstNBytesIntoBuffer(size_t n, void* dst) {
80 grpc_slice_buffer_move_first_into_buffer(&slice_buffer_, n, dst);
81 }
82
83 /// Removes and unrefs all slices in the SliceBuffer.
84 void Clear() { grpc_slice_buffer_reset_and_unref(&slice_buffer_); }
85
86 /// Removes the first slice in the SliceBuffer and returns it.
87 Slice TakeFirst();
88
89 /// Prepends the slice to the the front of the SliceBuffer.
90 void Prepend(Slice slice);
91
92 /// Increased the ref-count of slice at the specified index and returns the
93 /// associated slice.
94 Slice RefSlice(size_t index) const;
95
96 /// The total number of bytes held by the SliceBuffer
97 size_t Length() const { return slice_buffer_.length; }
98
99 /// Swap with another slice buffer
100 void Swap(SliceBuffer* other) {
101 grpc_slice_buffer_swap(c_slice_buffer(), other->c_slice_buffer());
102 }
103
104 /// Concatenate all slices and return the resulting string.
105 std::string JoinIntoString() const;
106
107 // Return a copy of the slice buffer
108 SliceBuffer Copy() const {
109 SliceBuffer copy;
110 for (size_t i = 0; i < Count(); i++) {
111 copy.Append(RefSlice(i));
112 }
113 return copy;
114 }
115
116 /// Return a pointer to the back raw grpc_slice_buffer
117 grpc_slice_buffer* c_slice_buffer() { return &slice_buffer_; }
118
119 /// Return a pointer to the back raw grpc_slice_buffer
120 const grpc_slice_buffer* c_slice_buffer() const { return &slice_buffer_; }
121
122 const grpc_slice& c_slice_at(size_t index) {
123 return slice_buffer_.slices[index];
124 }
125
126 private:
127 /// The backing raw slice buffer.
128 grpc_slice_buffer slice_buffer_;
129 };
130
131 } // namespace grpc_core
132
133 // Copy the first n bytes of src into memory pointed to by dst.
134 void grpc_slice_buffer_copy_first_into_buffer(grpc_slice_buffer* src, size_t n,
135 void* dst);
136
137 #endif // GRPC_CORE_LIB_SLICE_SLICE_BUFFER_H