xref: /aosp_15_r20/external/grpc-grpc/src/core/lib/slice/slice.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/slice/slice.h"
22 
23 #include <string.h>
24 
25 #include <new>
26 
27 #include <grpc/slice.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 
31 #include "src/core/lib/gprpp/memory.h"
32 #include "src/core/lib/slice/slice_internal.h"
33 #include "src/core/lib/slice/slice_refcount.h"
34 
grpc_slice_to_c_string(grpc_slice slice)35 char* grpc_slice_to_c_string(grpc_slice slice) {
36   char* out = static_cast<char*>(gpr_malloc(GRPC_SLICE_LENGTH(slice) + 1));
37   memcpy(out, GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice));
38   out[GRPC_SLICE_LENGTH(slice)] = 0;
39   return out;
40 }
41 
grpc_empty_slice(void)42 grpc_slice grpc_empty_slice(void) {
43   return grpc_core::slice_detail::EmptySlice();
44 }
45 
grpc_slice_copy(grpc_slice s)46 grpc_slice grpc_slice_copy(grpc_slice s) {
47   grpc_slice out = GRPC_SLICE_MALLOC(GRPC_SLICE_LENGTH(s));
48   memcpy(GRPC_SLICE_START_PTR(out), GRPC_SLICE_START_PTR(s),
49          GRPC_SLICE_LENGTH(s));
50   return out;
51 }
52 
53 namespace grpc_core {
54 
55 // grpc_slice_new support structures - we create a refcount object extended
56 // with the user provided data pointer & destroy function
57 class NewSliceRefcount : public grpc_slice_refcount {
58  public:
NewSliceRefcount(void (* destroy)(void *),void * user_data)59   NewSliceRefcount(void (*destroy)(void*), void* user_data)
60       : grpc_slice_refcount(Destroy),
61         user_destroy_(destroy),
62         user_data_(user_data) {}
~NewSliceRefcount()63   ~NewSliceRefcount() { user_destroy_(user_data_); }
64 
65  private:
Destroy(grpc_slice_refcount * arg)66   static void Destroy(grpc_slice_refcount* arg) {
67     delete static_cast<NewSliceRefcount*>(arg);
68   }
69 
70   void (*user_destroy_)(void*);
71   void* user_data_;
72 };
73 
74 }  // namespace grpc_core
75 
grpc_slice_memory_usage(grpc_slice s)76 size_t grpc_slice_memory_usage(grpc_slice s) {
77   if (s.refcount == nullptr ||
78       s.refcount == grpc_slice_refcount::NoopRefcount()) {
79     return 0;
80   } else {
81     return s.data.refcounted.length;
82   }
83 }
84 
grpc_slice_from_static_buffer(const void * s,size_t len)85 grpc_slice grpc_slice_from_static_buffer(const void* s, size_t len) {
86   return grpc_core::StaticSlice::FromStaticBuffer(s, len).TakeCSlice();
87 }
88 
grpc_slice_from_static_string(const char * s)89 grpc_slice grpc_slice_from_static_string(const char* s) {
90   return grpc_core::StaticSlice::FromStaticString(s).TakeCSlice();
91 }
92 
grpc_slice_new_with_user_data(void * p,size_t len,void (* destroy)(void *),void * user_data)93 grpc_slice grpc_slice_new_with_user_data(void* p, size_t len,
94                                          void (*destroy)(void*),
95                                          void* user_data) {
96   grpc_slice slice;
97   slice.refcount = new grpc_core::NewSliceRefcount(destroy, user_data);
98   slice.data.refcounted.bytes = static_cast<uint8_t*>(p);
99   slice.data.refcounted.length = len;
100   return slice;
101 }
102 
grpc_slice_new(void * p,size_t len,void (* destroy)(void *))103 grpc_slice grpc_slice_new(void* p, size_t len, void (*destroy)(void*)) {
104   // Pass "p" to *destroy when the slice is no longer needed.
105   return grpc_slice_new_with_user_data(p, len, destroy, p);
106 }
107 
108 namespace grpc_core {
109 // grpc_slice_new_with_len support structures - we create a refcount object
110 // extended with the user provided data pointer & destroy function
111 class NewWithLenSliceRefcount : public grpc_slice_refcount {
112  public:
NewWithLenSliceRefcount(void (* destroy)(void *,size_t),void * user_data,size_t user_length)113   NewWithLenSliceRefcount(void (*destroy)(void*, size_t), void* user_data,
114                           size_t user_length)
115       : grpc_slice_refcount(Destroy),
116         user_data_(user_data),
117         user_length_(user_length),
118         user_destroy_(destroy) {}
~NewWithLenSliceRefcount()119   ~NewWithLenSliceRefcount() { user_destroy_(user_data_, user_length_); }
120 
121  private:
Destroy(grpc_slice_refcount * arg)122   static void Destroy(grpc_slice_refcount* arg) {
123     delete static_cast<NewWithLenSliceRefcount*>(arg);
124   }
125 
126   void* user_data_;
127   size_t user_length_;
128   void (*user_destroy_)(void*, size_t);
129 };
130 
131 /// grpc_slice_from_moved_(string|buffer) ref count .
132 class MovedStringSliceRefCount : public grpc_slice_refcount {
133  public:
MovedStringSliceRefCount(UniquePtr<char> && str)134   explicit MovedStringSliceRefCount(UniquePtr<char>&& str)
135       : grpc_slice_refcount(Destroy), str_(std::move(str)) {}
136 
137  private:
Destroy(grpc_slice_refcount * arg)138   static void Destroy(grpc_slice_refcount* arg) {
139     delete static_cast<MovedStringSliceRefCount*>(arg);
140   }
141 
142   UniquePtr<char> str_;
143 };
144 
145 // grpc_slice_from_cpp_string() ref count.
146 class MovedCppStringSliceRefCount : public grpc_slice_refcount {
147  public:
MovedCppStringSliceRefCount(std::string && str)148   explicit MovedCppStringSliceRefCount(std::string&& str)
149       : grpc_slice_refcount(Destroy), str_(std::move(str)) {}
150 
data()151   uint8_t* data() {
152     return reinterpret_cast<uint8_t*>(const_cast<char*>(str_.data()));
153   }
154 
size() const155   size_t size() const { return str_.size(); }
156 
157  private:
Destroy(grpc_slice_refcount * arg)158   static void Destroy(grpc_slice_refcount* arg) {
159     delete static_cast<MovedCppStringSliceRefCount*>(arg);
160   }
161 
162   std::string str_;
163 };
164 
165 }  // namespace grpc_core
166 
grpc_slice_new_with_len(void * p,size_t len,void (* destroy)(void *,size_t))167 grpc_slice grpc_slice_new_with_len(void* p, size_t len,
168                                    void (*destroy)(void*, size_t)) {
169   grpc_slice slice;
170   slice.refcount = new grpc_core::NewWithLenSliceRefcount(destroy, p, len);
171   slice.data.refcounted.bytes = static_cast<uint8_t*>(p);
172   slice.data.refcounted.length = len;
173   return slice;
174 }
175 
grpc_slice_from_copied_buffer(const char * source,size_t len)176 grpc_slice grpc_slice_from_copied_buffer(const char* source, size_t len) {
177   if (len == 0) return grpc_empty_slice();
178   grpc_slice out = grpc_slice_malloc(len);
179   memcpy(GRPC_SLICE_START_PTR(out), source, len);
180   return out;
181 }
182 
grpc_slice_from_copied_string(const char * source)183 grpc_slice grpc_slice_from_copied_string(const char* source) {
184   return grpc_slice_from_copied_buffer(source, strlen(source));
185 }
186 
grpc_slice_from_moved_buffer(grpc_core::UniquePtr<char> p,size_t len)187 grpc_slice grpc_slice_from_moved_buffer(grpc_core::UniquePtr<char> p,
188                                         size_t len) {
189   uint8_t* ptr = reinterpret_cast<uint8_t*>(p.get());
190   grpc_slice slice;
191   if (len <= sizeof(slice.data.inlined.bytes)) {
192     slice.refcount = nullptr;
193     slice.data.inlined.length = len;
194     memcpy(GRPC_SLICE_START_PTR(slice), ptr, len);
195   } else {
196     slice.refcount = new grpc_core::MovedStringSliceRefCount(std::move(p));
197     slice.data.refcounted.bytes = ptr;
198     slice.data.refcounted.length = len;
199   }
200   return slice;
201 }
202 
grpc_slice_from_moved_string(grpc_core::UniquePtr<char> p)203 grpc_slice grpc_slice_from_moved_string(grpc_core::UniquePtr<char> p) {
204   const size_t len = strlen(p.get());
205   return grpc_slice_from_moved_buffer(std::move(p), len);
206 }
207 
grpc_slice_from_cpp_string(std::string str)208 grpc_slice grpc_slice_from_cpp_string(std::string str) {
209   grpc_slice slice;
210   if (str.size() <= sizeof(slice.data.inlined.bytes)) {
211     slice.refcount = nullptr;
212     slice.data.inlined.length = str.size();
213     memcpy(GRPC_SLICE_START_PTR(slice), str.data(), str.size());
214   } else {
215     auto* refcount = new grpc_core::MovedCppStringSliceRefCount(std::move(str));
216     slice.data.refcounted.bytes = refcount->data();
217     slice.data.refcounted.length = refcount->size();
218     slice.refcount = refcount;
219   }
220   return slice;
221 }
222 
grpc_slice_malloc_large(size_t length)223 grpc_slice grpc_slice_malloc_large(size_t length) {
224   grpc_slice slice;
225   uint8_t* memory = new uint8_t[sizeof(grpc_slice_refcount) + length];
226   slice.refcount = new (memory) grpc_slice_refcount(
227       [](grpc_slice_refcount* p) { delete[] reinterpret_cast<uint8_t*>(p); });
228   slice.data.refcounted.bytes = memory + sizeof(grpc_slice_refcount);
229   slice.data.refcounted.length = length;
230   return slice;
231 }
232 
grpc_slice_malloc(size_t length)233 grpc_slice grpc_slice_malloc(size_t length) {
234   if (length <= GRPC_SLICE_INLINED_SIZE) {
235     grpc_slice slice;
236     slice.refcount = nullptr;
237     slice.data.inlined.length = length;
238     return slice;
239   } else {
240     return grpc_slice_malloc_large(length);
241   }
242 }
243 
sub_no_ref(const grpc_slice & source,size_t begin,size_t end)244 static grpc_slice sub_no_ref(const grpc_slice& source, size_t begin,
245                              size_t end) {
246   grpc_slice subset;
247 
248   GPR_ASSERT(end >= begin);
249 
250   if (source.refcount != nullptr) {
251     // Enforce preconditions
252     GPR_ASSERT(source.data.refcounted.length >= end);
253 
254     // Build the result
255     subset.refcount = source.refcount;
256     // Point into the source array
257     subset.data.refcounted.bytes = source.data.refcounted.bytes + begin;
258     subset.data.refcounted.length = end - begin;
259   } else {
260     // Enforce preconditions
261     GPR_ASSERT(source.data.inlined.length >= end);
262     subset.refcount = nullptr;
263     subset.data.inlined.length = static_cast<uint8_t>(end - begin);
264     memcpy(subset.data.inlined.bytes, source.data.inlined.bytes + begin,
265            end - begin);
266   }
267   return subset;
268 }
269 
grpc_slice_sub_no_ref(grpc_slice source,size_t begin,size_t end)270 grpc_slice grpc_slice_sub_no_ref(grpc_slice source, size_t begin, size_t end) {
271   return sub_no_ref(source, begin, end);
272 }
273 
grpc_slice_sub(grpc_slice source,size_t begin,size_t end)274 grpc_slice grpc_slice_sub(grpc_slice source, size_t begin, size_t end) {
275   grpc_slice subset;
276 
277   if (end - begin <= sizeof(subset.data.inlined.bytes)) {
278     subset.refcount = nullptr;
279     subset.data.inlined.length = static_cast<uint8_t>(end - begin);
280     memcpy(subset.data.inlined.bytes, GRPC_SLICE_START_PTR(source) + begin,
281            end - begin);
282   } else {
283     subset = grpc_slice_sub_no_ref(source, begin, end);
284     // Bump the refcount
285     if (subset.refcount != grpc_slice_refcount::NoopRefcount()) {
286       subset.refcount->Ref({});
287     }
288   }
289   return subset;
290 }
291 
292 template <bool allow_inline>
grpc_slice_split_tail_maybe_ref_impl(grpc_slice * source,size_t split,grpc_slice_ref_whom ref_whom)293 grpc_slice grpc_slice_split_tail_maybe_ref_impl(grpc_slice* source,
294                                                 size_t split,
295                                                 grpc_slice_ref_whom ref_whom) {
296   grpc_slice tail;
297 
298   if (source->refcount == nullptr) {
299     // inlined data, copy it out
300     GPR_ASSERT(source->data.inlined.length >= split);
301     tail.refcount = nullptr;
302     tail.data.inlined.length =
303         static_cast<uint8_t>(source->data.inlined.length - split);
304     memcpy(tail.data.inlined.bytes, source->data.inlined.bytes + split,
305            tail.data.inlined.length);
306     source->data.inlined.length = static_cast<uint8_t>(split);
307   } else if (source->refcount == grpc_slice_refcount::NoopRefcount()) {
308     // refcount == NoopRefcount(), so we can just split in-place
309     tail.refcount = grpc_slice_refcount::NoopRefcount();
310     tail.data.refcounted.bytes = source->data.refcounted.bytes + split;
311     tail.data.refcounted.length = source->data.refcounted.length - split;
312     source->data.refcounted.length = split;
313   } else {
314     size_t tail_length = source->data.refcounted.length - split;
315     GPR_ASSERT(source->data.refcounted.length >= split);
316     if (allow_inline && tail_length < sizeof(tail.data.inlined.bytes) &&
317         ref_whom != GRPC_SLICE_REF_TAIL) {
318       // Copy out the bytes - it'll be cheaper than refcounting
319       tail.refcount = nullptr;
320       tail.data.inlined.length = static_cast<uint8_t>(tail_length);
321       memcpy(tail.data.inlined.bytes, source->data.refcounted.bytes + split,
322              tail_length);
323     } else {
324       // Build the result
325       switch (ref_whom) {
326         case GRPC_SLICE_REF_TAIL:
327           tail.refcount = source->refcount;
328           source->refcount = grpc_slice_refcount::NoopRefcount();
329           break;
330         case GRPC_SLICE_REF_HEAD:
331           tail.refcount = grpc_slice_refcount::NoopRefcount();
332           break;
333         case GRPC_SLICE_REF_BOTH:
334           tail.refcount = source->refcount;
335           // Bump the refcount
336           if (tail.refcount != grpc_slice_refcount::NoopRefcount()) {
337             tail.refcount->Ref({});
338           }
339           break;
340       }
341       // Point into the source array
342       tail.data.refcounted.bytes = source->data.refcounted.bytes + split;
343       tail.data.refcounted.length = tail_length;
344     }
345     source->data.refcounted.length = split;
346   }
347 
348   return tail;
349 }
350 
grpc_slice_split_tail_maybe_ref(grpc_slice * source,size_t split,grpc_slice_ref_whom ref_whom)351 grpc_slice grpc_slice_split_tail_maybe_ref(grpc_slice* source, size_t split,
352                                            grpc_slice_ref_whom ref_whom) {
353   return grpc_slice_split_tail_maybe_ref_impl<true>(source, split, ref_whom);
354 }
355 
grpc_slice_split_tail_maybe_ref_no_inline(grpc_slice * source,size_t split,grpc_slice_ref_whom ref_whom)356 grpc_slice grpc_slice_split_tail_maybe_ref_no_inline(
357     grpc_slice* source, size_t split, grpc_slice_ref_whom ref_whom) {
358   return grpc_slice_split_tail_maybe_ref_impl<false>(source, split, ref_whom);
359 }
360 
grpc_slice_split_tail(grpc_slice * source,size_t split)361 grpc_slice grpc_slice_split_tail(grpc_slice* source, size_t split) {
362   return grpc_slice_split_tail_maybe_ref(source, split, GRPC_SLICE_REF_BOTH);
363 }
364 
grpc_slice_split_tail_no_inline(grpc_slice * source,size_t split)365 grpc_slice grpc_slice_split_tail_no_inline(grpc_slice* source, size_t split) {
366   return grpc_slice_split_tail_maybe_ref_no_inline(source, split,
367                                                    GRPC_SLICE_REF_BOTH);
368 }
369 
370 template <bool allow_inline>
grpc_slice_split_head_impl(grpc_slice * source,size_t split)371 grpc_slice grpc_slice_split_head_impl(grpc_slice* source, size_t split) {
372   grpc_slice head;
373 
374   if (source->refcount == nullptr) {
375     GPR_ASSERT(source->data.inlined.length >= split);
376 
377     head.refcount = nullptr;
378     head.data.inlined.length = static_cast<uint8_t>(split);
379     memcpy(head.data.inlined.bytes, source->data.inlined.bytes, split);
380     source->data.inlined.length =
381         static_cast<uint8_t>(source->data.inlined.length - split);
382     memmove(source->data.inlined.bytes, source->data.inlined.bytes + split,
383             source->data.inlined.length);
384   } else if (allow_inline && split < sizeof(head.data.inlined.bytes)) {
385     GPR_ASSERT(source->data.refcounted.length >= split);
386 
387     head.refcount = nullptr;
388     head.data.inlined.length = static_cast<uint8_t>(split);
389     memcpy(head.data.inlined.bytes, source->data.refcounted.bytes, split);
390     source->data.refcounted.bytes += split;
391     source->data.refcounted.length -= split;
392   } else {
393     GPR_ASSERT(source->data.refcounted.length >= split);
394 
395     // Build the result
396     head.refcount = source->refcount;
397     // Bump the refcount
398     if (head.refcount != grpc_slice_refcount::NoopRefcount()) {
399       head.refcount->Ref({});
400     }
401     // Point into the source array
402     head.data.refcounted.bytes = source->data.refcounted.bytes;
403     head.data.refcounted.length = split;
404     source->data.refcounted.bytes += split;
405     source->data.refcounted.length -= split;
406   }
407 
408   return head;
409 }
410 
grpc_slice_split_head(grpc_slice * source,size_t split)411 grpc_slice grpc_slice_split_head(grpc_slice* source, size_t split) {
412   return grpc_slice_split_head_impl<true>(source, split);
413 }
414 
grpc_slice_split_head_no_inline(grpc_slice * source,size_t split)415 grpc_slice grpc_slice_split_head_no_inline(grpc_slice* source, size_t split) {
416   return grpc_slice_split_head_impl<false>(source, split);
417 }
418 
grpc_slice_eq(grpc_slice a,grpc_slice b)419 int grpc_slice_eq(grpc_slice a, grpc_slice b) {
420   if (GRPC_SLICE_LENGTH(a) != GRPC_SLICE_LENGTH(b)) return false;
421   if (GRPC_SLICE_LENGTH(a) == 0) return true;
422   return 0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
423                      GRPC_SLICE_LENGTH(a));
424 }
425 
grpc_slice_differs_refcounted(const grpc_slice & a,const grpc_slice & b_not_inline)426 int grpc_slice_differs_refcounted(const grpc_slice& a,
427                                   const grpc_slice& b_not_inline) {
428   size_t a_len;
429   const uint8_t* a_ptr;
430   if (a.refcount) {
431     a_len = a.data.refcounted.length;
432     a_ptr = a.data.refcounted.bytes;
433   } else {
434     a_len = a.data.inlined.length;
435     a_ptr = &a.data.inlined.bytes[0];
436   }
437   if (a_len != b_not_inline.data.refcounted.length) {
438     return true;
439   }
440   if (a_len == 0) {
441     return false;
442   }
443   // This check *must* occur after the a_len == 0 check
444   // to retain compatibility with grpc_slice_eq.
445   if (a_ptr == nullptr) {
446     return true;
447   }
448   return memcmp(a_ptr, b_not_inline.data.refcounted.bytes, a_len);
449 }
450 
grpc_slice_cmp(grpc_slice a,grpc_slice b)451 int grpc_slice_cmp(grpc_slice a, grpc_slice b) {
452   int d = static_cast<int>(GRPC_SLICE_LENGTH(a) - GRPC_SLICE_LENGTH(b));
453   if (d != 0) return d;
454   return memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
455                 GRPC_SLICE_LENGTH(a));
456 }
457 
grpc_slice_str_cmp(grpc_slice a,const char * b)458 int grpc_slice_str_cmp(grpc_slice a, const char* b) {
459   size_t b_length = strlen(b);
460   int d = static_cast<int>(GRPC_SLICE_LENGTH(a) - b_length);
461   if (d != 0) return d;
462   return memcmp(GRPC_SLICE_START_PTR(a), b, b_length);
463 }
464 
grpc_slice_is_equivalent(grpc_slice a,grpc_slice b)465 int grpc_slice_is_equivalent(grpc_slice a, grpc_slice b) {
466   if (a.refcount == nullptr || b.refcount == nullptr) {
467     return grpc_slice_eq(a, b);
468   }
469   return a.data.refcounted.length == b.data.refcounted.length &&
470          a.data.refcounted.bytes == b.data.refcounted.bytes;
471 }
472 
grpc_slice_buf_start_eq(grpc_slice a,const void * b,size_t len)473 int grpc_slice_buf_start_eq(grpc_slice a, const void* b, size_t len) {
474   if (GRPC_SLICE_LENGTH(a) < len) return 0;
475   return 0 == memcmp(GRPC_SLICE_START_PTR(a), b, len);
476 }
477 
grpc_slice_rchr(grpc_slice s,char c)478 int grpc_slice_rchr(grpc_slice s, char c) {
479   const char* b = reinterpret_cast<const char*> GRPC_SLICE_START_PTR(s);
480   int i;
481   for (i = static_cast<int> GRPC_SLICE_LENGTH(s) - 1; i != -1 && b[i] != c;
482        i--) {
483   }
484   return i;
485 }
486 
grpc_slice_chr(grpc_slice s,char c)487 int grpc_slice_chr(grpc_slice s, char c) {
488   const char* b = reinterpret_cast<const char*> GRPC_SLICE_START_PTR(s);
489   const char* p = static_cast<const char*>(memchr(b, c, GRPC_SLICE_LENGTH(s)));
490   return p == nullptr ? -1 : static_cast<int>(p - b);
491 }
492 
grpc_slice_slice(grpc_slice haystack,grpc_slice needle)493 int grpc_slice_slice(grpc_slice haystack, grpc_slice needle) {
494   size_t haystack_len = GRPC_SLICE_LENGTH(haystack);
495   const uint8_t* haystack_bytes = GRPC_SLICE_START_PTR(haystack);
496   size_t needle_len = GRPC_SLICE_LENGTH(needle);
497   const uint8_t* needle_bytes = GRPC_SLICE_START_PTR(needle);
498 
499   if (haystack_len == 0 || needle_len == 0) return -1;
500   if (haystack_len < needle_len) return -1;
501   if (haystack_len == needle_len) {
502     return grpc_slice_eq(haystack, needle) ? 0 : -1;
503   }
504   if (needle_len == 1) {
505     return grpc_slice_chr(haystack, static_cast<char>(*needle_bytes));
506   }
507 
508   const uint8_t* last = haystack_bytes + haystack_len - needle_len;
509   for (const uint8_t* cur = haystack_bytes; cur <= last; ++cur) {
510     if (0 == memcmp(cur, needle_bytes, needle_len)) {
511       return static_cast<int>(cur - haystack_bytes);
512     }
513   }
514   return -1;
515 }
516 
grpc_slice_dup(grpc_slice a)517 grpc_slice grpc_slice_dup(grpc_slice a) {
518   grpc_slice copy = GRPC_SLICE_MALLOC(GRPC_SLICE_LENGTH(a));
519   memcpy(GRPC_SLICE_START_PTR(copy), GRPC_SLICE_START_PTR(a),
520          GRPC_SLICE_LENGTH(a));
521   return copy;
522 }
523 
grpc_slice_ref(grpc_slice slice)524 grpc_slice grpc_slice_ref(grpc_slice slice) {
525   return grpc_core::CSliceRef(slice);
526 }
527 
grpc_slice_unref(grpc_slice slice)528 void grpc_slice_unref(grpc_slice slice) { grpc_core::CSliceUnref(slice); }
529