• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/core/lib/slice/slice.h"
16 
17 #include <grpc/event_engine/slice.h>
18 #include <grpc/slice.h>
19 #include <grpc/support/port_platform.h>
20 #include <stdint.h>
21 
22 #include <string>
23 #include <utility>
24 
25 #include "absl/log/check.h"
26 #include "src/core/lib/slice/slice_internal.h"
27 #include "src/core/lib/slice/slice_refcount.h"
28 
29 namespace grpc_event_engine {
30 namespace experimental {
31 
32 namespace slice_detail {
33 
Hash() const34 uint32_t BaseSlice::Hash() const { return grpc_slice_hash(slice_); }
35 
36 template <>
FromCopiedString(std::string s)37 MutableSlice CopyConstructors<MutableSlice>::FromCopiedString(std::string s) {
38   return MutableSlice(grpc_slice_from_cpp_string(std::move(s)));
39 }
40 
41 template <>
FromCopiedString(std::string s)42 Slice CopyConstructors<Slice>::FromCopiedString(std::string s) {
43   return Slice(grpc_slice_from_cpp_string(std::move(s)));
44 }
45 
46 }  // namespace slice_detail
47 
MutableSlice(const grpc_slice & slice)48 MutableSlice::MutableSlice(const grpc_slice& slice)
49     : slice_detail::BaseSlice(slice) {
50   DCHECK(slice.refcount == nullptr || slice.refcount->IsUnique());
51 }
52 
~MutableSlice()53 MutableSlice::~MutableSlice() { grpc_core::CSliceUnref(c_slice()); }
54 
TakeOwned()55 Slice Slice::TakeOwned() {
56   if (c_slice().refcount == nullptr) {
57     return Slice(c_slice());
58   }
59   if (c_slice().refcount == grpc_slice_refcount::NoopRefcount()) {
60     return Slice(grpc_slice_copy(c_slice()));
61   }
62   return Slice(TakeCSlice());
63 }
64 
AsOwned() const65 Slice Slice::AsOwned() const {
66   if (c_slice().refcount == nullptr) {
67     return Slice(c_slice());
68   }
69   if (c_slice().refcount == grpc_slice_refcount::NoopRefcount()) {
70     return Slice(grpc_slice_copy(c_slice()));
71   }
72   return Slice(grpc_core::CSliceRef(c_slice()));
73 }
74 
TakeMutable()75 MutableSlice Slice::TakeMutable() {
76   if (c_slice().refcount == nullptr) {
77     return MutableSlice(c_slice());
78   }
79   if (c_slice().refcount != grpc_slice_refcount::NoopRefcount() &&
80       c_slice().refcount->IsUnique()) {
81     return MutableSlice(TakeCSlice());
82   }
83   return MutableSlice(grpc_slice_copy(c_slice()));
84 }
85 
~Slice()86 Slice::~Slice() { grpc_core::CSliceUnref(c_slice()); }
87 
Ref() const88 Slice Slice::Ref() const { return Slice(grpc_core::CSliceRef(c_slice())); }
89 
FromRefcountAndBytes(grpc_slice_refcount * r,const uint8_t * begin,const uint8_t * end)90 Slice Slice::FromRefcountAndBytes(grpc_slice_refcount* r, const uint8_t* begin,
91                                   const uint8_t* end) {
92   grpc_slice out;
93   out.refcount = r;
94   if (r != grpc_slice_refcount::NoopRefcount()) r->Ref({});
95   out.data.refcounted.bytes = const_cast<uint8_t*>(begin);
96   out.data.refcounted.length = end - begin;
97   return Slice(out);
98 }
99 
100 }  // namespace experimental
101 }  // namespace grpc_event_engine
102