xref: /aosp_15_r20/external/grpc-grpc/src/core/lib/gprpp/ref_counted_string.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <grpc/support/port_platform.h>
18 
19 #include "src/core/lib/gprpp/ref_counted_string.h"
20 
21 #include <string.h>
22 
23 #include <new>
24 
25 #include <grpc/support/alloc.h>
26 
27 namespace grpc_core {
28 
Make(absl::string_view src)29 RefCountedPtr<RefCountedString> RefCountedString::Make(absl::string_view src) {
30   void* p = gpr_malloc(sizeof(Header) + src.length() + 1);
31   return RefCountedPtr<RefCountedString>(new (p) RefCountedString(src));
32 }
33 
RefCountedString(absl::string_view src)34 RefCountedString::RefCountedString(absl::string_view src)
35     : header_{{}, src.length()} {
36   memcpy(payload_, src.data(), header_.length);
37   // Null terminate because we frequently need to convert to char* still to go
38   // back and forth to the old c-style api.
39   payload_[header_.length] = 0;
40 }
41 
Destroy()42 void RefCountedString::Destroy() { gpr_free(this); }
43 
44 }  // namespace grpc_core
45