1 // 2 // Copyright 2023 gRPC authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include "src/core/util/ref_counted_string.h" 18 19 #include <grpc/support/alloc.h> 20 #include <grpc/support/port_platform.h> 21 #include <string.h> 22 23 #include <new> 24 25 namespace grpc_core { 26 Make(absl::string_view src)27RefCountedPtr<RefCountedString> RefCountedString::Make(absl::string_view src) { 28 void* p = gpr_malloc(sizeof(Header) + src.length() + 1); 29 return RefCountedPtr<RefCountedString>(new (p) RefCountedString(src)); 30 } 31 RefCountedString(absl::string_view src)32RefCountedString::RefCountedString(absl::string_view src) 33 : header_{{}, src.length()} { 34 memcpy(payload_, src.data(), header_.length); 35 // Null terminate because we frequently need to convert to char* still to go 36 // back and forth to the old c-style api. 37 payload_[header_.length] = 0; 38 } 39 Destroy()40void RefCountedString::Destroy() { gpr_free(this); } 41 42 } // namespace grpc_core 43