Debian Codegraph @

src/core/lib/slice/slice.cc

raw
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
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
42 grpc_slice grpc_empty_slice(void) {
43 return grpc_core::slice_detail::EmptySlice();
44 }
45
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:
59 NewSliceRefcount(void (*destroy)(void*), void* user_data)
60 : grpc_slice_refcount(Destroy),
61 user_destroy_(destroy),
62 user_data_(user_data) {}
63 ~NewSliceRefcount() { user_destroy_(user_data_); }
64
65 private:
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
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
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
89 grpc_slice grpc_slice_from_static_string(const char* s) {
90 return grpc_core::StaticSlice::FromStaticString(s).TakeCSlice();
91 }
92
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
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:
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) {}
119 ~NewWithLenSliceRefcount() { user_destroy_(user_data_, user_length_); }
120
121 private:
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:
134 explicit MovedStringSliceRefCount(UniquePtr<char>&& str)
135 : grpc_slice_refcount(Destroy), str_(std::move(str)) {}
136
137 private:
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:
148 explicit MovedCppStringSliceRefCount(std::string&& str)
149 : grpc_slice_refcount(Destroy), str_(std::move(str)) {}
150
151 uint8_t* data() {
152 return reinterpret_cast<uint8_t*>(const_cast<char*>(str_.data()));
153 }
154
155 size_t size() const { return str_.size(); }
156
157 private:
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
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
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
183 grpc_slice grpc_slice_from_copied_string(const char* source) {
184 return grpc_slice_from_copied_buffer(source, strlen(source));
185 }
186
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
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
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
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
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
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
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
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 grpc_slice grpc_slice_split_tail_maybe_ref(grpc_slice* source, size_t split,
293 grpc_slice_ref_whom ref_whom) {
294 grpc_slice tail;
295
296 if (source->refcount == nullptr) {
297 /* inlined data, copy it out */
298 GPR_ASSERT(source->data.inlined.length >= split);
299 tail.refcount = nullptr;
300 tail.data.inlined.length =
301 static_cast<uint8_t>(source->data.inlined.length - split);
302 memcpy(tail.data.inlined.bytes, source->data.inlined.bytes + split,
303 tail.data.inlined.length);
304 source->data.inlined.length = static_cast<uint8_t>(split);
305 } else if (source->refcount == grpc_slice_refcount::NoopRefcount()) {
306 /* refcount == NoopRefcount(), so we can just split in-place */
307 tail.refcount = grpc_slice_refcount::NoopRefcount();
308 tail.data.refcounted.bytes = source->data.refcounted.bytes + split;
309 tail.data.refcounted.length = source->data.refcounted.length - split;
310 source->data.refcounted.length = split;
311 } else {
312 size_t tail_length = source->data.refcounted.length - split;
313 GPR_ASSERT(source->data.refcounted.length >= split);
314 if (tail_length < sizeof(tail.data.inlined.bytes) &&
315 ref_whom != GRPC_SLICE_REF_TAIL) {
316 /* Copy out the bytes - it'll be cheaper than refcounting */
317 tail.refcount = nullptr;
318 tail.data.inlined.length = static_cast<uint8_t>(tail_length);
319 memcpy(tail.data.inlined.bytes, source->data.refcounted.bytes + split,
320 tail_length);
321 } else {
322 /* Build the result */
323 switch (ref_whom) {
324 case GRPC_SLICE_REF_TAIL:
325 tail.refcount = source->refcount;
326 source->refcount = grpc_slice_refcount::NoopRefcount();
327 break;
328 case GRPC_SLICE_REF_HEAD:
329 tail.refcount = grpc_slice_refcount::NoopRefcount();
330 break;
331 case GRPC_SLICE_REF_BOTH:
332 tail.refcount = source->refcount;
333 /* Bump the refcount */
334 if (tail.refcount != grpc_slice_refcount::NoopRefcount()) {
335 tail.refcount->Ref();
336 }
337 break;
338 }
339 /* Point into the source array */
340 tail.data.refcounted.bytes = source->data.refcounted.bytes + split;
341 tail.data.refcounted.length = tail_length;
342 }
343 source->data.refcounted.length = split;
344 }
345
346 return tail;
347 }
348
349 grpc_slice grpc_slice_split_tail(grpc_slice* source, size_t split) {
350 return grpc_slice_split_tail_maybe_ref(source, split, GRPC_SLICE_REF_BOTH);
351 }
352
353 grpc_slice grpc_slice_split_head(grpc_slice* source, size_t split) {
354 grpc_slice head;
355
356 if (source->refcount == nullptr) {
357 GPR_ASSERT(source->data.inlined.length >= split);
358
359 head.refcount = nullptr;
360 head.data.inlined.length = static_cast<uint8_t>(split);
361 memcpy(head.data.inlined.bytes, source->data.inlined.bytes, split);
362 source->data.inlined.length =
363 static_cast<uint8_t>(source->data.inlined.length - split);
364 memmove(source->data.inlined.bytes, source->data.inlined.bytes + split,
365 source->data.inlined.length);
366 } else if (split < sizeof(head.data.inlined.bytes)) {
367 GPR_ASSERT(source->data.refcounted.length >= split);
368
369 head.refcount = nullptr;
370 head.data.inlined.length = static_cast<uint8_t>(split);
371 memcpy(head.data.inlined.bytes, source->data.refcounted.bytes, split);
372 source->data.refcounted.bytes += split;
373 source->data.refcounted.length -= split;
374 } else {
375 GPR_ASSERT(source->data.refcounted.length >= split);
376
377 /* Build the result */
378 head.refcount = source->refcount;
379 /* Bump the refcount */
380 if (head.refcount != grpc_slice_refcount::NoopRefcount()) {
381 head.refcount->Ref();
382 }
383 /* Point into the source array */
384 head.data.refcounted.bytes = source->data.refcounted.bytes;
385 head.data.refcounted.length = split;
386 source->data.refcounted.bytes += split;
387 source->data.refcounted.length -= split;
388 }
389
390 return head;
391 }
392
393 int grpc_slice_eq(grpc_slice a, grpc_slice b) {
394 if (GRPC_SLICE_LENGTH(a) != GRPC_SLICE_LENGTH(b)) return false;
395 if (GRPC_SLICE_LENGTH(a) == 0) return true;
396 return 0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
397 GRPC_SLICE_LENGTH(a));
398 }
399
400 int grpc_slice_differs_refcounted(const grpc_slice& a,
401 const grpc_slice& b_not_inline) {
402 size_t a_len;
403 const uint8_t* a_ptr;
404 if (a.refcount) {
405 a_len = a.data.refcounted.length;
406 a_ptr = a.data.refcounted.bytes;
407 } else {
408 a_len = a.data.inlined.length;
409 a_ptr = &a.data.inlined.bytes[0];
410 }
411 if (a_len != b_not_inline.data.refcounted.length) {
412 return true;
413 }
414 if (a_len == 0) {
415 return false;
416 }
417 // This check *must* occur after the a_len == 0 check
418 // to retain compatibility with grpc_slice_eq.
419 if (a_ptr == nullptr) {
420 return true;
421 }
422 return memcmp(a_ptr, b_not_inline.data.refcounted.bytes, a_len);
423 }
424
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
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
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
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
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;
456 i--) {
457 }
458 return i;
459 }
460
461 int grpc_slice_chr(grpc_slice s, char c) {
462 const char* b = reinterpret_cast<const char*> GRPC_SLICE_START_PTR(s);
463 const char* p = static_cast<const char*>(memchr(b, c, GRPC_SLICE_LENGTH(s)));
464 return p == nullptr ? -1 : static_cast<int>(p - b);
465 }
466
467 int grpc_slice_slice(grpc_slice haystack, grpc_slice needle) {
468 size_t haystack_len = GRPC_SLICE_LENGTH(haystack);
469 const uint8_t* haystack_bytes = GRPC_SLICE_START_PTR(haystack);
470 size_t needle_len = GRPC_SLICE_LENGTH(needle);
471 const uint8_t* needle_bytes = GRPC_SLICE_START_PTR(needle);
472
473 if (haystack_len == 0 || needle_len == 0) return -1;
474 if (haystack_len < needle_len) return -1;
475 if (haystack_len == needle_len) {
476 return grpc_slice_eq(haystack, needle) ? 0 : -1;
477 }
478 if (needle_len == 1) {
479 return grpc_slice_chr(haystack, static_cast<char>(*needle_bytes));
480 }
481
482 const uint8_t* last = haystack_bytes + haystack_len - needle_len;
483 for (const uint8_t* cur = haystack_bytes; cur != last; ++cur) {
484 if (0 == memcmp(cur, needle_bytes, needle_len)) {
485 return static_cast<int>(cur - haystack_bytes);
486 }
487 }
488 return -1;
489 }
490
491 grpc_slice grpc_slice_dup(grpc_slice a) {
492 grpc_slice copy = GRPC_SLICE_MALLOC(GRPC_SLICE_LENGTH(a));
493 memcpy(GRPC_SLICE_START_PTR(copy), GRPC_SLICE_START_PTR(a),
494 GRPC_SLICE_LENGTH(a));
495 return copy;
496 }
497
498 grpc_slice grpc_slice_ref(grpc_slice slice) {
499 return grpc_core::CSliceRef(slice);
500 }
501
502 void grpc_slice_unref(grpc_slice slice) { grpc_core::CSliceUnref(slice); }