• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_internal.h"
22 
23 #include <grpc/slice.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 
27 #include <string.h>
28 
29 #include "src/core/lib/iomgr/exec_ctx.h"
30 
grpc_slice_to_c_string(grpc_slice slice)31 char* grpc_slice_to_c_string(grpc_slice slice) {
32   char* out = static_cast<char*>(gpr_malloc(GRPC_SLICE_LENGTH(slice) + 1));
33   memcpy(out, GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice));
34   out[GRPC_SLICE_LENGTH(slice)] = 0;
35   return out;
36 }
37 
grpc_empty_slice(void)38 grpc_slice grpc_empty_slice(void) {
39   grpc_slice out;
40   out.refcount = nullptr;
41   out.data.inlined.length = 0;
42   return out;
43 }
44 
grpc_slice_copy(grpc_slice s)45 grpc_slice grpc_slice_copy(grpc_slice s) {
46   grpc_slice out = GRPC_SLICE_MALLOC(GRPC_SLICE_LENGTH(s));
47   memcpy(GRPC_SLICE_START_PTR(out), GRPC_SLICE_START_PTR(s),
48          GRPC_SLICE_LENGTH(s));
49   return out;
50 }
51 
grpc_slice_ref_internal(grpc_slice slice)52 grpc_slice grpc_slice_ref_internal(grpc_slice slice) {
53   if (slice.refcount) {
54     slice.refcount->vtable->ref(slice.refcount);
55   }
56   return slice;
57 }
58 
grpc_slice_unref_internal(grpc_slice slice)59 void grpc_slice_unref_internal(grpc_slice slice) {
60   if (slice.refcount) {
61     slice.refcount->vtable->unref(slice.refcount);
62   }
63 }
64 
65 /* Public API */
grpc_slice_ref(grpc_slice slice)66 grpc_slice grpc_slice_ref(grpc_slice slice) {
67   return grpc_slice_ref_internal(slice);
68 }
69 
70 /* Public API */
grpc_slice_unref(grpc_slice slice)71 void grpc_slice_unref(grpc_slice slice) {
72   if (grpc_core::ExecCtx::Get() == nullptr) {
73     grpc_core::ExecCtx exec_ctx;
74     grpc_slice_unref_internal(slice);
75   } else {
76     grpc_slice_unref_internal(slice);
77   }
78 }
79 
80 /* grpc_slice_from_static_string support structure - a refcount that does
81    nothing */
noop_ref(void * unused)82 static void noop_ref(void* unused) {}
noop_unref(void * unused)83 static void noop_unref(void* unused) {}
84 
85 static const grpc_slice_refcount_vtable noop_refcount_vtable = {
86     noop_ref, noop_unref, grpc_slice_default_eq_impl,
87     grpc_slice_default_hash_impl};
88 static grpc_slice_refcount noop_refcount = {&noop_refcount_vtable,
89                                             &noop_refcount};
90 
grpc_slice_from_static_buffer(const void * s,size_t len)91 grpc_slice grpc_slice_from_static_buffer(const void* s, size_t len) {
92   grpc_slice slice;
93   slice.refcount = &noop_refcount;
94   slice.data.refcounted.bytes = (uint8_t*)s;
95   slice.data.refcounted.length = len;
96   return slice;
97 }
98 
grpc_slice_from_static_string(const char * s)99 grpc_slice grpc_slice_from_static_string(const char* s) {
100   return grpc_slice_from_static_buffer(s, strlen(s));
101 }
102 
103 /* grpc_slice_new support structures - we create a refcount object extended
104    with the user provided data pointer & destroy function */
105 typedef struct new_slice_refcount {
106   grpc_slice_refcount rc;
107   gpr_refcount refs;
108   void (*user_destroy)(void*);
109   void* user_data;
110 } new_slice_refcount;
111 
new_slice_ref(void * p)112 static void new_slice_ref(void* p) {
113   new_slice_refcount* r = static_cast<new_slice_refcount*>(p);
114   gpr_ref(&r->refs);
115 }
116 
new_slice_unref(void * p)117 static void new_slice_unref(void* p) {
118   new_slice_refcount* r = static_cast<new_slice_refcount*>(p);
119   if (gpr_unref(&r->refs)) {
120     r->user_destroy(r->user_data);
121     gpr_free(r);
122   }
123 }
124 
125 static const grpc_slice_refcount_vtable new_slice_vtable = {
126     new_slice_ref, new_slice_unref, grpc_slice_default_eq_impl,
127     grpc_slice_default_hash_impl};
128 
grpc_slice_new_with_user_data(void * p,size_t len,void (* destroy)(void *),void * user_data)129 grpc_slice grpc_slice_new_with_user_data(void* p, size_t len,
130                                          void (*destroy)(void*),
131                                          void* user_data) {
132   grpc_slice slice;
133   new_slice_refcount* rc =
134       static_cast<new_slice_refcount*>(gpr_malloc(sizeof(new_slice_refcount)));
135   gpr_ref_init(&rc->refs, 1);
136   rc->rc.vtable = &new_slice_vtable;
137   rc->rc.sub_refcount = &rc->rc;
138   rc->user_destroy = destroy;
139   rc->user_data = user_data;
140 
141   slice.refcount = &rc->rc;
142   slice.data.refcounted.bytes = static_cast<uint8_t*>(p);
143   slice.data.refcounted.length = len;
144   return slice;
145 }
146 
grpc_slice_new(void * p,size_t len,void (* destroy)(void *))147 grpc_slice grpc_slice_new(void* p, size_t len, void (*destroy)(void*)) {
148   /* Pass "p" to *destroy when the slice is no longer needed. */
149   return grpc_slice_new_with_user_data(p, len, destroy, p);
150 }
151 
152 /* grpc_slice_new_with_len support structures - we create a refcount object
153    extended with the user provided data pointer & destroy function */
154 typedef struct new_with_len_slice_refcount {
155   grpc_slice_refcount rc;
156   gpr_refcount refs;
157   void* user_data;
158   size_t user_length;
159   void (*user_destroy)(void*, size_t);
160 } new_with_len_slice_refcount;
161 
new_with_len_ref(void * p)162 static void new_with_len_ref(void* p) {
163   new_with_len_slice_refcount* r = static_cast<new_with_len_slice_refcount*>(p);
164   gpr_ref(&r->refs);
165 }
166 
new_with_len_unref(void * p)167 static void new_with_len_unref(void* p) {
168   new_with_len_slice_refcount* r = static_cast<new_with_len_slice_refcount*>(p);
169   if (gpr_unref(&r->refs)) {
170     r->user_destroy(r->user_data, r->user_length);
171     gpr_free(r);
172   }
173 }
174 
175 static const grpc_slice_refcount_vtable new_with_len_vtable = {
176     new_with_len_ref, new_with_len_unref, grpc_slice_default_eq_impl,
177     grpc_slice_default_hash_impl};
178 
grpc_slice_new_with_len(void * p,size_t len,void (* destroy)(void *,size_t))179 grpc_slice grpc_slice_new_with_len(void* p, size_t len,
180                                    void (*destroy)(void*, size_t)) {
181   grpc_slice slice;
182   new_with_len_slice_refcount* rc = static_cast<new_with_len_slice_refcount*>(
183       gpr_malloc(sizeof(new_with_len_slice_refcount)));
184   gpr_ref_init(&rc->refs, 1);
185   rc->rc.vtable = &new_with_len_vtable;
186   rc->rc.sub_refcount = &rc->rc;
187   rc->user_destroy = destroy;
188   rc->user_data = p;
189   rc->user_length = len;
190 
191   slice.refcount = &rc->rc;
192   slice.data.refcounted.bytes = static_cast<uint8_t*>(p);
193   slice.data.refcounted.length = len;
194   return slice;
195 }
196 
grpc_slice_from_copied_buffer(const char * source,size_t length)197 grpc_slice grpc_slice_from_copied_buffer(const char* source, size_t length) {
198   if (length == 0) return grpc_empty_slice();
199   grpc_slice slice = GRPC_SLICE_MALLOC(length);
200   memcpy(GRPC_SLICE_START_PTR(slice), source, length);
201   return slice;
202 }
203 
grpc_slice_from_copied_string(const char * source)204 grpc_slice grpc_slice_from_copied_string(const char* source) {
205   return grpc_slice_from_copied_buffer(source, strlen(source));
206 }
207 
208 typedef struct {
209   grpc_slice_refcount base;
210   gpr_refcount refs;
211 } malloc_refcount;
212 
malloc_ref(void * p)213 static void malloc_ref(void* p) {
214   malloc_refcount* r = static_cast<malloc_refcount*>(p);
215   gpr_ref(&r->refs);
216 }
217 
malloc_unref(void * p)218 static void malloc_unref(void* p) {
219   malloc_refcount* r = static_cast<malloc_refcount*>(p);
220   if (gpr_unref(&r->refs)) {
221     gpr_free(r);
222   }
223 }
224 
225 static const grpc_slice_refcount_vtable malloc_vtable = {
226     malloc_ref, malloc_unref, grpc_slice_default_eq_impl,
227     grpc_slice_default_hash_impl};
228 
grpc_slice_malloc_large(size_t length)229 grpc_slice grpc_slice_malloc_large(size_t length) {
230   grpc_slice slice;
231 
232   /* Memory layout used by the slice created here:
233 
234      +-----------+----------------------------------------------------------+
235      | refcount  | bytes                                                    |
236      +-----------+----------------------------------------------------------+
237 
238      refcount is a malloc_refcount
239      bytes is an array of bytes of the requested length
240      Both parts are placed in the same allocation returned from gpr_malloc */
241   malloc_refcount* rc = static_cast<malloc_refcount*>(
242       gpr_malloc(sizeof(malloc_refcount) + length));
243 
244   /* Initial refcount on rc is 1 - and it's up to the caller to release
245      this reference. */
246   gpr_ref_init(&rc->refs, 1);
247 
248   rc->base.vtable = &malloc_vtable;
249   rc->base.sub_refcount = &rc->base;
250 
251   /* Build up the slice to be returned. */
252   /* The slices refcount points back to the allocated block. */
253   slice.refcount = &rc->base;
254   /* The data bytes are placed immediately after the refcount struct */
255   slice.data.refcounted.bytes = reinterpret_cast<uint8_t*>(rc + 1);
256   /* And the length of the block is set to the requested length */
257   slice.data.refcounted.length = length;
258   return slice;
259 }
260 
grpc_slice_malloc(size_t length)261 grpc_slice grpc_slice_malloc(size_t length) {
262   grpc_slice slice;
263 
264   if (length > sizeof(slice.data.inlined.bytes)) {
265     return grpc_slice_malloc_large(length);
266   } else {
267     /* small slice: just inline the data */
268     slice.refcount = nullptr;
269     slice.data.inlined.length = static_cast<uint8_t>(length);
270   }
271   return slice;
272 }
273 
grpc_slice_sub_no_ref(grpc_slice source,size_t begin,size_t end)274 grpc_slice grpc_slice_sub_no_ref(grpc_slice source, size_t begin, size_t end) {
275   grpc_slice subset;
276 
277   GPR_ASSERT(end >= begin);
278 
279   if (source.refcount) {
280     /* Enforce preconditions */
281     GPR_ASSERT(source.data.refcounted.length >= end);
282 
283     /* Build the result */
284     subset.refcount = source.refcount->sub_refcount;
285     /* Point into the source array */
286     subset.data.refcounted.bytes = source.data.refcounted.bytes + begin;
287     subset.data.refcounted.length = end - begin;
288   } else {
289     /* Enforce preconditions */
290     GPR_ASSERT(source.data.inlined.length >= end);
291     subset.refcount = nullptr;
292     subset.data.inlined.length = static_cast<uint8_t>(end - begin);
293     memcpy(subset.data.inlined.bytes, source.data.inlined.bytes + begin,
294            end - begin);
295   }
296   return subset;
297 }
298 
grpc_slice_sub(grpc_slice source,size_t begin,size_t end)299 grpc_slice grpc_slice_sub(grpc_slice source, size_t begin, size_t end) {
300   grpc_slice subset;
301 
302   if (end - begin <= sizeof(subset.data.inlined.bytes)) {
303     subset.refcount = nullptr;
304     subset.data.inlined.length = static_cast<uint8_t>(end - begin);
305     memcpy(subset.data.inlined.bytes, GRPC_SLICE_START_PTR(source) + begin,
306            end - begin);
307   } else {
308     subset = grpc_slice_sub_no_ref(source, begin, end);
309     /* Bump the refcount */
310     subset.refcount->vtable->ref(subset.refcount);
311   }
312   return subset;
313 }
314 
grpc_slice_split_tail_maybe_ref(grpc_slice * source,size_t split,grpc_slice_ref_whom ref_whom)315 grpc_slice grpc_slice_split_tail_maybe_ref(grpc_slice* source, size_t split,
316                                            grpc_slice_ref_whom ref_whom) {
317   grpc_slice tail;
318 
319   if (source->refcount == nullptr) {
320     /* inlined data, copy it out */
321     GPR_ASSERT(source->data.inlined.length >= split);
322     tail.refcount = nullptr;
323     tail.data.inlined.length =
324         static_cast<uint8_t>(source->data.inlined.length - split);
325     memcpy(tail.data.inlined.bytes, source->data.inlined.bytes + split,
326            tail.data.inlined.length);
327     source->data.inlined.length = static_cast<uint8_t>(split);
328   } else {
329     size_t tail_length = source->data.refcounted.length - split;
330     GPR_ASSERT(source->data.refcounted.length >= split);
331     if (tail_length < sizeof(tail.data.inlined.bytes) &&
332         ref_whom != GRPC_SLICE_REF_TAIL) {
333       /* Copy out the bytes - it'll be cheaper than refcounting */
334       tail.refcount = nullptr;
335       tail.data.inlined.length = static_cast<uint8_t>(tail_length);
336       memcpy(tail.data.inlined.bytes, source->data.refcounted.bytes + split,
337              tail_length);
338       source->refcount = source->refcount->sub_refcount;
339     } else {
340       /* Build the result */
341       switch (ref_whom) {
342         case GRPC_SLICE_REF_TAIL:
343           tail.refcount = source->refcount->sub_refcount;
344           source->refcount = &noop_refcount;
345           break;
346         case GRPC_SLICE_REF_HEAD:
347           tail.refcount = &noop_refcount;
348           source->refcount = source->refcount->sub_refcount;
349           break;
350         case GRPC_SLICE_REF_BOTH:
351           tail.refcount = source->refcount->sub_refcount;
352           source->refcount = source->refcount->sub_refcount;
353           /* Bump the refcount */
354           tail.refcount->vtable->ref(tail.refcount);
355           break;
356       }
357       /* Point into the source array */
358       tail.data.refcounted.bytes = source->data.refcounted.bytes + split;
359       tail.data.refcounted.length = tail_length;
360     }
361     source->data.refcounted.length = split;
362   }
363 
364   return tail;
365 }
366 
grpc_slice_split_tail(grpc_slice * source,size_t split)367 grpc_slice grpc_slice_split_tail(grpc_slice* source, size_t split) {
368   return grpc_slice_split_tail_maybe_ref(source, split, GRPC_SLICE_REF_BOTH);
369 }
370 
grpc_slice_split_head(grpc_slice * source,size_t split)371 grpc_slice grpc_slice_split_head(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 (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->refcount = source->refcount->sub_refcount;
391     source->data.refcounted.bytes += split;
392     source->data.refcounted.length -= split;
393   } else {
394     GPR_ASSERT(source->data.refcounted.length >= split);
395 
396     /* Build the result */
397     head.refcount = source->refcount->sub_refcount;
398     /* Bump the refcount */
399     head.refcount->vtable->ref(head.refcount);
400     /* Point into the source array */
401     head.data.refcounted.bytes = source->data.refcounted.bytes;
402     head.data.refcounted.length = split;
403     source->refcount = source->refcount->sub_refcount;
404     source->data.refcounted.bytes += split;
405     source->data.refcounted.length -= split;
406   }
407 
408   return head;
409 }
410 
grpc_slice_default_eq_impl(grpc_slice a,grpc_slice b)411 int grpc_slice_default_eq_impl(grpc_slice a, grpc_slice b) {
412   if (GRPC_SLICE_LENGTH(a) != GRPC_SLICE_LENGTH(b)) return false;
413   if (GRPC_SLICE_LENGTH(a) == 0) return true;
414   return 0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
415                      GRPC_SLICE_LENGTH(a));
416 }
417 
grpc_slice_eq(grpc_slice a,grpc_slice b)418 int grpc_slice_eq(grpc_slice a, grpc_slice b) {
419   if (a.refcount && b.refcount && a.refcount->vtable == b.refcount->vtable) {
420     return a.refcount->vtable->eq(a, b);
421   }
422   return grpc_slice_default_eq_impl(a, b);
423 }
424 
grpc_slice_cmp(grpc_slice a,grpc_slice b)425 int grpc_slice_cmp(grpc_slice a, grpc_slice b) {
426   int d = static_cast<int>(GRPC_SLICE_LENGTH(a) - GRPC_SLICE_LENGTH(b));
427   if (d != 0) return d;
428   return memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
429                 GRPC_SLICE_LENGTH(a));
430 }
431 
grpc_slice_str_cmp(grpc_slice a,const char * b)432 int grpc_slice_str_cmp(grpc_slice a, const char* b) {
433   size_t b_length = strlen(b);
434   int d = static_cast<int>(GRPC_SLICE_LENGTH(a) - b_length);
435   if (d != 0) return d;
436   return memcmp(GRPC_SLICE_START_PTR(a), b, b_length);
437 }
438 
grpc_slice_is_equivalent(grpc_slice a,grpc_slice b)439 int grpc_slice_is_equivalent(grpc_slice a, grpc_slice b) {
440   if (a.refcount == nullptr || b.refcount == nullptr) {
441     return grpc_slice_eq(a, b);
442   }
443   return a.data.refcounted.length == b.data.refcounted.length &&
444          a.data.refcounted.bytes == b.data.refcounted.bytes;
445 }
446 
grpc_slice_buf_start_eq(grpc_slice a,const void * b,size_t len)447 int grpc_slice_buf_start_eq(grpc_slice a, const void* b, size_t len) {
448   if (GRPC_SLICE_LENGTH(a) < len) return 0;
449   return 0 == memcmp(GRPC_SLICE_START_PTR(a), b, len);
450 }
451 
grpc_slice_rchr(grpc_slice s,char c)452 int grpc_slice_rchr(grpc_slice s, char c) {
453   const char* b = reinterpret_cast<const char*> GRPC_SLICE_START_PTR(s);
454   int i;
455   for (i = static_cast<int> GRPC_SLICE_LENGTH(s) - 1; i != -1 && b[i] != c; i--)
456     ;
457   return i;
458 }
459 
grpc_slice_chr(grpc_slice s,char c)460 int grpc_slice_chr(grpc_slice s, char c) {
461   const char* b = reinterpret_cast<const char*> GRPC_SLICE_START_PTR(s);
462   const char* p = static_cast<const char*>(memchr(b, c, GRPC_SLICE_LENGTH(s)));
463   return p == nullptr ? -1 : static_cast<int>(p - b);
464 }
465 
grpc_slice_slice(grpc_slice haystack,grpc_slice needle)466 int grpc_slice_slice(grpc_slice haystack, grpc_slice needle) {
467   size_t haystack_len = GRPC_SLICE_LENGTH(haystack);
468   const uint8_t* haystack_bytes = GRPC_SLICE_START_PTR(haystack);
469   size_t needle_len = GRPC_SLICE_LENGTH(needle);
470   const uint8_t* needle_bytes = GRPC_SLICE_START_PTR(needle);
471 
472   if (haystack_len == 0 || needle_len == 0) return -1;
473   if (haystack_len < needle_len) return -1;
474   if (haystack_len == needle_len)
475     return grpc_slice_eq(haystack, needle) ? 0 : -1;
476   if (needle_len == 1)
477     return grpc_slice_chr(haystack, static_cast<char>(*needle_bytes));
478 
479   const uint8_t* last = haystack_bytes + haystack_len - needle_len;
480   for (const uint8_t* cur = haystack_bytes; cur != last; ++cur) {
481     if (0 == memcmp(cur, needle_bytes, needle_len)) {
482       return static_cast<int>(cur - haystack_bytes);
483     }
484   }
485   return -1;
486 }
487 
grpc_slice_dup(grpc_slice a)488 grpc_slice grpc_slice_dup(grpc_slice a) {
489   grpc_slice copy = GRPC_SLICE_MALLOC(GRPC_SLICE_LENGTH(a));
490   memcpy(GRPC_SLICE_START_PTR(copy), GRPC_SLICE_START_PTR(a),
491          GRPC_SLICE_LENGTH(a));
492   return copy;
493 }
494