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